AtCoder 🌈 AWC0096A Closing Time of the Reception Window
🔗 AWC0096A Closing Time of the Reception Window
Problem Statement
題目簡述
有 位訪客依序來到只有一個服務窗口的辦公室。第 位訪客在時間 到達,服務需要 單位時間。窗口從時間 開始可用,且必須按照訪客編號順序服務。
每位訪客會在「窗口空閒」且「自己已到達」後立刻開始服務。求最後一位訪客服務結束的時間。
Constraints
約束條件
- 輸入皆為整數。
思路:依序維護窗口空閒時間
由於窗口一次只能處理一位訪客,且服務順序固定,所以沒有排程選擇,也不用排序;唯一需要維護的是「窗口目前最早什麼時候能接下一位」,即上一位訪客服務結束的時間。
令 為第 位訪客服務結束的時間,則對於下一位訪客,開始服務的時間由兩個條件共同限制:
- 如果訪客還沒到,就必須等到他的到達時間。
- 如果窗口還在忙,就必須等到上一位服務結束。
因此開始時間就是這兩個時間的較大值;接著再加上該訪客的服務時間,就能得到新的窗口空閒時間,有以下遞迴式:
複雜度分析
- 時間複雜度:
- 空間複雜度:,只需維護窗口下一次空閒的時間。
Code
1 | def solve(): |
寫在最後
Cover Image Credit
The cover image was created by @Qurami. All rights belong to the original artist.
It is used here only as a non-commercial cover illustration for this note. I do not claim ownership of the artwork.
If you are the copyright holder and believe this usage is inappropriate, please contact me by email or leave a comment. I will remove the image promptly.
本部落格所有文章除特別聲明外,均採用CC BY-NC-SA 4.0 授權協議。轉載請註明來源 十六個天亮!
評論
WalineGiscus





