lqdoj
      • 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Solution Giao lưu Tin học trẻ Mở rộng 2023 Lần 1 - Bảng B ## Em trang trí **Fact:** toàn bộ học sinh lớp 9A, 9B đều không thể AC bài này. ## Đong dầu ### Đề bài Cho bể dầu lớn vô hạn. Cần đong $n$ lít dầu bằng một trong hai loại thao tác: - Thêm $2\ell$ hoặc $3 \ell$ dầu - Bớt $2\ell$ hoặc $3 \ell$ dầu ### Trâu Gọi số can $2\ell$ dầu cần dùng là $x$, số can $3\ell$ cần dùng sẽ là $\dfrac{n-2x}{3}$. Nếu số can là nguyên dương, ta tìm tổng số can nhỏ nhất. ```cpp= int answer = 1e9; for (int x = 0; x * 2 <= n; x++){ int remain = n - 2 * x; if (remain % 3 == 0){ int y = remain / 3; answer = min(answer, x + y); } } ``` ### Thuật chuẩn Dùng càng nhiều can $3\ell$ nhất có thể. $\Rightarrow$ không dư, hoặc dư $1\ell$, hoặc dư $2\ell$ - Nếu còn dư $2\ell$: dùng thêm 1 can $2\ell$ - Nếu còn dư $1\ell$: bớt một can $3\ell$ và thay bằng 2 can $2\ell$. **Trường hợp đặc biệt:** $n = 1$ $\Rightarrow$ không thể bớt can nào được $\Rightarrow$ $3-2 = 1$, tức múc vào 3$\ell$ và bỏ ra $2\ell$, tức đáp án là 2 thao tác. Nếu không xử lý trường hợp này thì chỉ ăn được $24.75/25$ điểm ## Biến đổi ### Đề bài Cho hai số $a$ và $b$. Mỗi lần biến đổi, ta lấy số lớn trừ bớt cho số bé. Đếm số lần biến đổi tối thiểu để đưa một trong hai số về $0$. ### Trâu Đề bảo gì làm nấy ```cpp= int answer = 0; while (a > 0 and b > 0){ answer++; if (a > b) a -= b; else b -= a; } cout << answer; ``` ### Thuật chuẩn Nhận thấy, thuật toán được nhắc trong đề chính là thuật toán Euclid. Trong lúc thực hiện thuật toán, sẽ có trường hợp số $a$ cực lớn, và $b$ cực nhỏ, khi trừ nhiều lần sẽ rất tốn thời gian. Vì vậy, ta gom các phép trừ lại thành một phép chia duy nhất. $a \div b = q$ dư $r$ $\Rightarrow$ $a = bq + r$ $\Rightarrow$ diễn ra $q$ phép trừ, và sau khi trừ thì $a = r$. ```cpp= int answer = 0; while (a > 0 and b > 0){ if (a < b) swap(a, b); // số nào là a, số nào là b không quan trọng answer += a / b; a = a % b; } cout << answer; ``` ## Avatar ### Subtask 1: $a[i] = b[j] = 1$ Mỗi dũng sĩ giết được một con. Đáp án là `min(m, n)`. ### Subtask 2: $b[1] = b[2] = \dots = b[m]$ Đặt $x = b[1]$. Các dũng sĩ chỉ giết được robot có sức mạnh $\le x$. Vì thế, ta đếm số lượng robot có sức mạnh nhỏ hơn hoặc bằng $x$, và nếu có quá $m$ robot thỏa mãn thì ta chỉ giết $m$ con. ```cpp= int x = b[1]; int answer = 0; for (int i = 1; i <= n; i++) answer += a[i] <= x; answer = min(answer, m); cout << answer; ``` ### Subtask 3 Sắp xếp $a, b$ theo thứ tự tăng dần. **Nhận xét:** Khi đánh, để diệt nhiều con nhất, ta ưu tiên bốc những con robot yếu nhất, và dùng những chiến binh mạnh nhất. #### Cách 1 Nếu người Na-vi thứ $j$ đánh được robot thứ $i$, tức $b[j] \ge a[i]$ Thì người Na-vi thứ $j + 1$ cũng đánh được robot thứ $i$, vì $b[j+1] \ge b[j] \ge a[i]$ Đồng thời người Na-vi thứ $j$ cũng đánh bại được robot thứ $i-1$, vì $b[j] \ge a[i] \ge a[i - 1]$. Vì vậy, với mỗi người Na-vi thứ $j$, ta tìm robot $i$ lớn nhất sao cho $b[j] \ge a[i]$. $\Rightarrow$ chỉ những người từ $j \rightarrow n$ đánh được robot $1 \rightarrow i$ $\Rightarrow$ giết được $\min(n-j+1, i)$ con robot. Điều này có thể tính được bằng chặt nhị phân, hoặc hai con trỏ (vì $j$ tăng thì $i$ cũng tăng). ```cpp= // sort mảng a và b int i = 0; int answer = 0; for (int j = 1; j <= m; j++){ while (i != n and a[i + 1] <= b[j]) i++; // nếu đánh được thêm 1 robot thì tăng i. // ta sẽ thử cho người j->n đánh i robot đầu int outcome = min(n - j + 1, i ); // số người robot answer = max(answer, outcome); } ``` #### Cách 2 **Hệ quả của nhận xét:** nếu đáp án là $x$, thì ta cứ lấy $x$ người Na-vi mạnh nhất chọi với $x$ con robot yếu nhất. $\Rightarrow$ ta sẽ xử lý $a[1\rightarrow x]$ và $b[m-x+1\rightarrow m]$, so sánh lần lượt từng phần tử tương ứng. Hiển nhiên, nếu diệt được $x$ con thì diệt được $x-1$ con. Vì vậy, ta chặt nhị phân đáp án. ```cpp= bool check(int x){ for (int i = 1; i <= x; i++) if (a[i] > b[m - i + 1]) return false; return true; } int lo = 1, hi = min(m, n), ans = 0; while (lo <= hi){ int mid = (lo + hi) / 2; if (check(mid)) ans = mid, lo = mid + 1; else hi = mid - 1; } cout << ans; ``` ## Chọn dãy ### Subtask 2: $n = k$ Ta suy ra chỉ được chọn một phần tử $\Rightarrow$ chọn phần tử lớn nhất mảng $A$. ### Subtask 3: $A[1] = A[2] = \dots = A[n]$ Ta sẽ tìm cách bốc nhiều phần tử nhất có thể. Số phần tử bốc được sẽ là $\dfrac{n}{k}$, cộng thêm $1$ nếu $n$ chia $k$ có dư. ### Subtask 1: $n, k \le 20$ Ta đệ quy quay lui để thử toàn bộ các chọn $n$ phần tử. Để thuận tiện cho việc xử lý các subtask sau, ta gọi hàm `dqql(i, sum)` là việc thử xử lý các phần tử từ $1\rightarrow i$, và các phần từ được bốc trước đó (nằm trong khoảng từ $i+1\rightarrow n$ đã qua) thì có tổng là $sum$. ```cpp= int answer = 0; void dqql(int i, int sum){ if (i < 1){ // không bốc được nữa answer = max(answer, sum); return; } // có bốc i // => tổng tăng lên a[i], thằng tiếp theo được bốc <= i-k dqql(i - k, sum + a[i]); // không bốc i // thằng tiếp theo bốc có thể là từ ngay i-1 rồi, tổng không đổi. dqql(i - 1, sum); } ``` ### Subtask 5 Nhìn vào code của sub2, ta dễ dàng suy ra được công thức quy hoạch động: - Nếu chỉ gồm phần tử $a[i]$, đáp án là $a[i]$ - Nếu không chọn phần tử $a[i]$, ta xem đáp án của $i-1$ thằng còn lại tốt nhất là bao nhiêu, tức $f[i - 1]$ - Nếu $i\le k$, và ta chọn phần tử $i$, thì bỏ qua rất nhiều phần tử khoảng cách không đủ xa, chỉ quan tâm $1 \rightarrow i-k$ $\Rightarrow$ $f[i - k] + a[i]$ - $f[i] = \max(a[i], f[i - 1], f[i - k] + a[i])$ (nếu $i \ge k$) ### Subtask 4 Có thể một số bạn đi theo hướng này: $f[i]$ là cách chọn tốt nhất trong $i$ phần tử đầu và có chọn $i$. $\Rightarrow f[i] = a[i] + \max\limits_{1 \le j \le i - k}f[j]$ Ta cũng có thể đưa về sub5 một cách dễ dàng bằng cách đặt $g[j] = \max\limits_{1 \le j \le i - k}f[j] = \max(g[j - 1], f[j])$

    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