wiwiho
    • Create new note
    • Create a note from template
      • 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
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • 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
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync 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
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
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
--- title: 2021 師大附中資訊校隊培訓 模擬競賽 題解 tags: 2021 師大附中資訊校隊培訓 slideOptions: transition: fade --- ### 2021 師大附中資訊校隊培訓 模擬競賽 題解 [程式碼看我](https://gist.github.com/wiwiho/6e263471274404b2985cf2f40201a547) --- ## pE 校慶紀念品 (Souvenir) 「欸我今天 vir 到一個酷題」 - joy <!-- .element: class="fragment" data-fragment-index="1" --> ---- 白話文: 對於 $1 \leq i \leq K$ 你想知道每個物品都只能選 0 個或 i 個時的最大總價值 ---- ### subtask 3 $K=1$ 只要算 $i=1$ 的狀況 等同 0/1 背包 ---- ### subtask 4 $w_i=1$ 當要算 $k$ 個人的狀況時 送給一個人的紀念品不能超過 $N/k$ 個 直接選最受歡迎的 $N/k$ 個就好 ---- ### subtask 5 選一組紀念品,總價錢是 $w$,總受歡迎度是 $v$ 送給 $k$ 個人,總價錢是 $kw$、總受歡迎度是 $kv$ $kw \leq M \implies w \leq M/k$ 找到在只有 $M/k$ 元時,每種紀念品最多買一個的最大受歡迎度 乘上 $k$ 後就是有 $k$ 個人的答案 <!-- .element: class="fragment" data-fragment-index="1" --> 做一次 0/1 背包就能算完,時間 $O(NM+K)$ <!-- .element: class="fragment" data-fragment-index="2" --> --- ## pA 圓錐 (Cone) 題源:台大 ADA 作業 圓角錐到底有角還是沒角? <!-- .element: class="fragment" data-fragment-index="1" --> ---- ### subtask 2 $N \leq 10$ 用遞迴暴搜要先偷哪些圓錐 $O(N!)$ ---- ### subtask 3 $A_i$ 是質數,且每種數字都會出現恰 2 或 3 次 因為不同的數都會互質,所以能一起偷的只有同一個數而已 而且數量一定是 2 或 3,代表只能全部一起偷 答案是固定的,問題只在判斷有沒有解 無解:3 2 3 2 3 有解:3 2 2 3 3 <!-- .element: class="fragment" data-fragment-index="1" --> 把同一種數字看成一對括號 掃描線 $O(n)$ <!-- .element: class="fragment" data-fragment-index="2" --> ---- ### subtask 4,5 $dp[i][j]=$ 把 $[i,j]$ 偷光後的最大滿足度 偷法有一些 case 1. $[i,k]$、$[k+1,j]$ 分開偷 2. 先偷 $i$ 和 $j$,再偷 $[i+1][j-1]$ 3. 先偷 $i$、$j$ 和 $k$($i < k < j$),再偷 $[i+1,k-1]$ 和 $[k+1,j-1]$ <!-- .element: class="fragment" data-fragment-index="1" --> ---- 如果每次 $\gcd$ 都重算會花 $O(n^3)$ 的時間 預處理 <!-- .element: class="fragment" data-fragment-index="1" --> --- ## pD 足球場 (Soccer) 「我覺得在校內賽放計幾不太健康」 - joy <!-- .element: class="fragment" data-fragment-index="1" --> ---- 怎麼判斷四個點能不能構成矩形? 四個角要是直角 外積判斷 ---- ## subtask 2 $N \leq 80$ 枚舉四個點 判斷是不是矩形 $O(N^4)$ ---- ## subtask 3 $N \leq 200$ 枚舉其中三個點 可以發現到第四個點的位置是唯一的 判斷是否存在那個點就好 $O(N^3 \log N)$ ---- ## subtask 4 一直把注意力放在頂點上好像有點麻煩 改把注意力放到對角線呢? 對角線有什麼性質? ---- ![](https://i.imgur.com/0WhTejK.png) ---- 長方形的兩條對角線 - 相交在它們的中點 - 長度一樣 對於兩兩點對的連線 如果兩條線滿足長度一樣、相交在中點 這兩條線就可以當矩形的對角線 ---- 只要是中點在同一個位置 且長度一樣的兩個點的連線 就能當同一個矩形的對角線 把每一條兩個點的連線 按照中點位置和長度分類 同一類的可以一起當對角線 ---- 有一類有 $c$ 條線 那這一類可以做出 $\frac{c(c-1)}{2}$ 個矩形 <!-- .element: class="fragment" data-fragment-index="1" --> 避免浮點數: 中點座標可以不用除以 2 長度可以不用開根號 $O(N^2 \log N)$ --- ## 拿破崙改 (Napoleon) 題源:[CF 103186B](https://codeforces.com/gym/103186/problem/B) 「我是秘書我是秘書我是秘書」 - ZCK <!-- .element: class="fragment" data-fragment-index="1" --> ---- ### subtask 3 $C=1$ 如果已經選好了哪一組要選 $c$ 可以先假裝其他都要選 $a$ 如果有一組要改選 $b$ 那麼總和會改變 $b-a$ 取最大的 $B$ 個 $b_i-a_i$ <!-- .element: class="fragment" data-fragment-index="1" --> ---- 可以直接把每一組按照 $b_i-a_i$ 大到小排序 當要選 $c_i$ 的那組編號 $\leq B$ 時 選到 $b_i$ 的就是第 1 到 $B+1$ 組除了他 否則就是第 1 到 B 組 $O(N)$ ---- ### subtask 4 $N \leq 500$ $dp[i][j][k]=$ 前 $i$ 組中,有 $j$ 個選 $a$、$k$ 個選 $b$ 的最大總和 選 $c$ 的數量是 $i-j-k$ $O(N^3)$ ---- ### subtask 5 $N \leq 5000$ 把前兩個子題的想法混合 先照 $b_i-a_i$ 大到小排序 $dp[i][j]=$ 前 $i$ 組裡選 $c$ 的恰有 $j$ 個時的最大總和 ---- 如果第 $i$ 個要選 $c$ 那就是 $dp[i-1][j-1]+c_i$ 不然,$i$ 如果是扣掉選 $c$ 的人之後的前 $B$ 個 就要選 $b_i$,否則選 $a_i$ 條件:$i-j \leq B$ <!-- .element: class="fragment" data-fragment-index="1" --> $O(N^2)$ ---- ### subtask 6 可以很直覺地猜到這應該可以 Greedy ---- ### 一種 Greedy 的方法 在做到第 $i$ 組的時候 如果 $i \leq A$,就前 $i$ 組全部選 $a$ 如果 $i \leq A+B$,找前 $i$ 組選恰 $A$ 個 $a$ 的最大總和 否則,我想知道前 $i$ 組選恰 $A$ 個 $a$、恰 $B$ 個 $b$ 的最大總和 ---- 前兩種很好算 重點是第三種 如果第 $i$ 組要選 $c_i$ 那麼顯然我們前 $i-1$ 組就要有恰 $A$ 個 $a$、恰 $B$ 個 $b$ 可以直接計算 ---- 假設第 $i$ 組想要選 $a$ 那前 $i-1$ 組就要有恰 $A-1$ 個 $a$、恰 $B$ 個 $b$ 怎麼從「前 $i-1$ 組有恰 $A$ 個 $a$、恰 $B$ 個 $b$」推出它? ---- 亂想: 把某個本來是選 $a$ 的換成選 $c$ 或者把某個本來是選 $a$ 的換成選 $b$、某個本來是選 $b$ 的換成選 $c$ ---- 為什麼這樣是對的? 把「前 $i-1$ 組有恰 $A$ 個 $a$、恰 $B$ 個 $b$」的最好選法 和有恰 $A-1$ 個 $a$ 的最好選法 視為一個只包含 $a,b,c$ 的字串 $S_1$ 跟 $S_2$ 想像一個三個節點的圖,節點分別對應到 $a,b,c$ 如果某個位置在 $S_1$ 上和 $S_2$ 上分別是 $t_1,t_2$ 就連一條邊 $t_1 \to t_2$ ---- 邊的意思就是我們把那個位置換掉 權重是換掉後總和會改變多少 這個圖上如果有環的話 那這個環正著走和倒著走一圈(倒著走權重要變負的) 要嘛都是 0,要嘛有一個是正的 ---- 如果正著走是正的 就代表可以把 $S_1$ 上的那些邊對到的位置換掉 每種花色的數量不會改變,而總和會變大 和他是最好選法矛盾 反著走同理 只是變成把 $S_2$ 的換掉 如果都是 0,那做了根本不會變好 可以把那些邊對到的位置 $S_2$ 變得和 $S_1$ 一樣 它還會是最佳選法 ---- 得出這張圖不會有環 而且 $a$ 的出度要比入度多 1 $c$ 的入度要比出度少 1 $b$ 的出入度相等 滿足這樣的圖只有 - 只有一條 $a \to b$ - 只有一條 $a \to b$ 和 $b \to c$ ---- 所以只考慮這兩種 case 會是好的 用 set 維護我想找一個特定花色改成另一個,最好的選擇是誰 $O(n \log n)$ 也有別種 Greedy 法但應該差不多都是這樣換來換去的 --- ## 長頸鹿園 (Giraffe) 有長頸鹿那有沒有短頸鹿 <!-- .element: class="fragment" data-fragment-index="1" --> ---- 白話文: 把 $N$ 個節點依序加到樹上 每次加完後求 現在樹上有幾條直徑 ---- ### subtask 2 $N \leq 500$ 每次加完後,花 $O(N^2)$ 的時間看過樹上所有路徑的長度 $O(N^3)$ ---- ### subtask 3 這棵樹是星狀圖 把 1 拆掉後會變成一些鏈 直徑肯定是兩條鏈接起來 (或者一條,如果只有一條的話) 維護長度最長的那些鏈 (如果長度最長只有一條,還要找次長的) 然後算一下 $O(N)$ ---- ### subtask 4 $N \leq 5000$ 有一個性質是所有直徑都會經過樹圓心 然後我們看一下怎麼用樹圓心找直徑數量 ---- 若樹直徑的邊數是 $d$ 定義「樹半徑」是 $\lfloor \frac{d}{2} \rfloor$ ---- 當樹直徑上有奇數個點的時候 圓心只有一個 樹直徑會是從它的某個子樹裡的葉節點 到另一個子樹裡的葉節點 而且那兩個葉節點的深度是樹半徑 數有幾對葉節點,滿足它們在不同子樹且深度都是樹半徑 <!-- .element: class="fragment" data-fragment-index="1" --> ---- ![](https://i.imgur.com/Q5l0iij.png) ---- 當樹直徑上有偶數個點 圓心會有兩個 樹直徑會是跨越它們之間那條邊 以兩側某個葉節點為端點的路徑 這兩個葉節點到中間那條邊的距離都是樹半徑 數兩側有多少節點在那一側的子樹深度是樹半徑 然後乘起來 <!-- .element: class="fragment" data-fragment-index="1" --> ---- ![](https://i.imgur.com/RAMTvmS.png) ---- ### subtask 5 想辦法動態維護那些東西 ---- 首先要會動態維護圓心 如果新加的點到比較近的圓心的距離超過樹半徑 那樹直徑就會變長 而且樹圓心會往它的方向移動 ---- 如果本來只有一個圓心,那舊圓心還是圓心 然後往新加的點的方向走一步的那個會成為第二個圓心 如果本來有兩個圓心 那只有比較靠近新加的點的那個會繼續是圓心 ---- 會維護圓心了! 我們還要維護那些子樹裡有幾個深度是樹半徑的點的資訊 可以從本來圓心的資訊得出新圓心的資訊 $O(n \log n)$ ---- ![](https://i.imgur.com/7NXKOv7.png) ---- ![](https://i.imgur.com/p4RfQxc.png)

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