Editorial Slayers
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    3
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: VNOJ, THT, Greedy, Data Structure, Sorting, SPyofgame title: 🌱 Tin học trẻ 2021 - Vòng Khu vực - Bảng C - Thi đấu cầu lông author: Editorial-Slayers Team license: Public domain --- <style> .markdown-body { max-width: 2048px; } </style> $\Huge \text{🌱 Tin học trẻ 2021 - Vòng Khu vực - Bảng C - Thi đấu cầu lông}$ ----- ###### 🔗 Link: [https://oj.vnoi.info/problem/tht21_kvc_badmi](https://oj.vnoi.info/problem/tht21_kvc_badmi) ###### 📌 Tags: `Greedy`, `Data Structure`, `Sorting` ###### 👤 Writer: @SPyofgame ###### 👥 Contributor: [@Editorial Slayers Team](https://hackmd.io/@Editorial-Slayers) ###### 📋 Content: [TOC] ----- ## Hướng dẫn subtask 1-2-3 Với mỗi truy vấn, ta dựng đồ thị hai phía: - Các cạnh $u - v$ có ý nghĩa là vận động viên $(a_u, b_u)$ khi ghép với vận động viên $(x_v, y_v)$ sẽ mang tới bàn thằng - Chỉ có cạnh nối $u - v$ khi và chỉ khi $(a_u > x_v)$ và $(b_u > y_v)$, những trường hợp khác không có cạnh nối tới - Với mỗi giá trị $u$ của độ thị này, chỉ được bắt cặp với một giá trị $v$ duy nhất của đồ thị còn lại Kết quả bài toán là số cặp ghép tối đa có thể tạo được trên đồ thị hai phía kể trên Để tìm số cặp ghép cực đại trên đồ thị hai phía ta có thể có thể dùng thuật toán [Kuhn-Munkres](https://cp-algorithms.com/graph/kuhn_maximum_bipartite_matching.html) hoặc [Hopcroft-Karp-Karzanov](https://en.wikipedia.org/wiki/Hopcroft%E2%80%93Karp_algorithm) Tất nhiên ta có thể chuyển đồ thị hai phía về dạng luồng và sử dụng thuật toán [Ford-Fulkerson](https://cp-algorithms.com/graph/edmonds_karp.html), [Edmonds-Karp](https://cp-algorithms.com/graph/edmonds_karp.html), [Dinic](https://cp-algorithms.com/graph/dinic.html), [Push-relabel](https://cp-algorithms.com/graph/push-relabel-faster.html) ----- ## Hướng dẫn subtask 4 Với mỗi truy vấn, ta giải như sau: Không mất tính tổng quát ta sắp xếp các các vận động viên tăng dần - Các cặp $(a_u, b_u)$ sắp xếp theo $a_u$ tăng dần - Các cặp $(x_v, y_v)$ sắp xếp theo $x_v$ tăng dần Gọi tập ứng cử viên $S(u)$ là tập vào thời điểm $u$ có các giá trị $y_v$ mà tồn tại cặp $(x_v, y_v)$ thỏa mãn $(x_v < a_u)$, và chỉ những đỉnh $v$ này thì $u$ mới bắt cặp được - **Chứng minh:** Khi có tập $S$, điều kiện $x_v < a_u$ luôn thỏa nên ta chỉ xét các cạnh $y_v < b_u$ để bắt cặp - **Ứng dụng:** Khi mình bắt cặp $u - v$ thì mình cũng xóa phần tử đó ra khỏi tập $S$ và duyệt tới $u$ tiếp theo, cách chọn này sẽ không làm bị trùng giá trị với các cách chọn khác Từ định nghĩa của $S(u)$ thì với mọi $u \leq u'$ ta có $S(u) \subseteq S(u')$ - **Chứng minh:** Nếu tồn tại một phần tử $y_v$ sao cho tồn tại cặp $(x_v, y_v)$ thỏa mãn $x_v \geq a_{u_0}$ và $x_v < a_u$ thì vô lí vì $x_v < a_u \leq a_{u_0}$ (tính chất dãy sắp xếp) - **Ứng dụng:** Nếu chọn được một tập các giá trị $y_v$ lấy được từ $S(u)$ thì tồn tại cách thay thế giá trị $y_v$ với một giá trị nhỏ hơn trong $S(u)$ mà vẫn thỏa điều kiện ghép cặp Từ hai tính ứng dụng trên, nhận xét rằng khi có tập $T$ là các cạnh $u-v$ được bắt cặp, nếu ta luôn bắt cặp giá trị $y_v$ lớn nhất chưa được chọn cho mỗi $u$ thì luôn đảm bảo là cho ra kết quả không tệ hơn mọi cách bắt cặp khác - **Tính tối ưu:** $T$ luôn đảm bảo rằng sẽ không có tập $S$ thỏa mãn nào khác mà có thể thay đổi các giá trị được chọn thành một số nhỏ hơn để mà $S$ trở thành $T$ được cả - **Tính độc nhất:** Vì nếu tồn tại một cách bắt cặp tối ưu hơn hẳn tập $T$, thì cũng tồn tại cách để thay giá trị đó thành một giá trị nhỏ hơn sao cho các số được trọn trở thành $T$, nhưng điều này vì vô lí như đã nói ở trên ==Vậy, thuật toán tham lam của ta như sau:== - Sau khi sắp xếp, ta duyệt qua các phần tử $u$ đồng thời lấy thêm các phần tử $y_v < a_u$ chưa xét ở tập $S(u - 1)$ thêm vào tập $S(u)$ - Sắp xếp tập $S(u)$ và nếu tồn tại một giá trị $y_v < b_u$ trong $S(u)$ thì ta xóa $y_v$ khỏi $S(u)$ và tăng kết quả lên $1$ Để thêm bớt và tìm kiếm đủ nhanh cho một tập các giá trị **có thể trùng nhau** $S(u)$, ta có thể sài các cấu trúc dữ liệu dạng cây: - Như trong ngôn ngữ `C++` có `std::multiset` còn không bạn có thể dùng trie vì có hằng số nhỏ và code dễ - Các cấu trúc dữ liệu khác như Segment Tree với nén số, Dynamic Segment Tree, `std::map`, `C++ ordered_set` có vẻ như không đủ nhanh để qua `time limit` Độ phức tạp: - Thời gian: $O(n \log n)$ cho mỗi truy vấn, tổng cộng là $O(q \times n \log n)$ - Bộ nhớ: $O(n)$ cho nhập xuất và cây (nếu là cây trie thì khoảng $O(n \times \log_2 x)$) ----- ### Code > **General Time:** $O(q \times n \log n)$ > **Query Time:** $O(n \log n)$ > **Space:** $O(n)$ > **Algo:** Greedy, Data Structure, Sorting > [color=lightgreen] :::success :::spoiler SPyofgame multiset Implementation ```cpp= #include <algorithm> #include <iostream> #include <set> using namespace std; const int LIM = 1e5 + 15; int NumEarth, NumGamer, NumAlien, NumQuery; pair<int, int> Earth[LIM]; /// Earth player pair<int, int> Gamer[LIM]; /// All Z planet players pair<int, int> Alien[LIM]; /// Few Z planet players that in each query void input() { cin >> NumEarth >> NumGamer >> NumQuery; for (int idx = 1; idx <= NumEarth; ++idx) cin >> Earth[idx].first; for (int idx = 1; idx <= NumEarth; ++idx) cin >> Earth[idx].second; for (int idx = 1; idx <= NumGamer; ++idx) cin >> Gamer[idx].first; for (int idx = 1; idx <= NumGamer; ++idx) cin >> Gamer[idx].second; sort(Earth + 1, Earth + NumEarth + 1); /// Sort these players } int query() { int sft; cin >> sft; /// We find the set of player to play in this query NumAlien = NumEarth; for (int idx = 1; idx <= NumAlien; ++idx) Alien[idx] = Gamer[idx + sft - 1]; /// Let also sort it sort(Alien + 1, Alien + NumAlien + 1); int result = 0; multiset<int> Candidate; /// Out candidate list S(u) for (int idx = 1, pos = 1; idx <= NumEarth; ++idx) /// For each u { /// Such $y_v$ satisfied exist such pair $(x_v, y_v)$ that $x_v < a_u$ /// We only take such $y_v$ that not belong to $S(u - 1)$ for (; pos <= NumAlien && Alien[pos].first < Earth[idx].first; ++pos) Candidate.insert(Alien[pos].second); /// Add $y_v$ to $S(u)$ /// If there exist at least one solution if (Candidate.size() && *Candidate.begin() < Earth[idx].second) { /// Erase this $y_v$ out of $S(u)$ Candidate.erase(--Candidate.lower_bound(Earth[idx].second)); /// Increase the result ++result; } } return result; } int main() { // file("Test"); ios::sync_with_stdio(NULL); cin.tie(NULL); input(); while (NumQuery-->0) cout << query() << "\n"; return 0; } ``` ::: ----- ### Bonus > Bạn có thể giải truy vấn nhanh hơn $O(n)$ khi $q$ lớn hơn và $n$ đủ nhỏ không ?

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully