Codeforces 750A New Year and Hurry 新年與匆忙

題目說明

難易度 : 800

英文原文如下

Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5·i minutes to solve the i-th problem.

Limak’s friends organize a New Year’s Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first.

How many problems can Limak solve if he wants to make it to the party?

中文翻譯

Limak在2016年的最後一天將參加一個比賽。比賽將於晚上20:00開始,持續四個小時,準確來說就是到午夜。共有n個問題,按困難程度排序,意思是問題1是最容易的,問題n是最難的。Limak知道他解決第i個問題需要5·i分鐘。

Limak的朋友們組織了一個跨年派對,Limak希望在午夜或之前到達那裡。他需要k分鐘從他的家去那裡,但他首先會先在家參加比賽。

如果Limak想參加派對,他可以解決多少問題?

輸入

The only line of the input contains two integers n and k (1 ≤ n ≤ 10, 1 ≤ k ≤ 240) — the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house.

唯一一行輸入包含了兩個整數 n 跟 k (1 ≤ n ≤ 10, 1 ≤ k ≤ 240) — 也就是比賽有多少個問題以及 Limak 從他家到達派對所需的分鐘數。

輸出

Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier.

印出一個整數,表示 Limak 在準時或提早到派對的前提下可以解完的最多題數。

範例

輸入3 222
輸出2
輸入4 190
輸出4
輸入7 1
輸出7

筆記

In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn’t have enough time to solve 3 problems so the answer is 2.

In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight.

In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems.

在一個範例中,有3個問題以及 Limak 需要222分鐘去抵達派對。3個問題分別需要 5、10、15分鐘。Limak可以花費5+10=15分鐘來解決前兩個問題,接下來20:15 他可以離開他的房子並在23:57分抵達派對(222分鐘之後)。在這一次的場景中,Limak會解決兩個問題,他沒有足夠的時間來處理第三個問題,所以答案是2。

在第二個問題中,Limak 可以用 5 + 10 + 15 + 20 = 50分鐘解決所有共4個問題,然後在20:50離開房子,他會剛好在午夜時抵達派對。

在第三個例子中,Limak只需要一秒就可以抵達派對,他有足夠的時間解決全部共7個問題。


解題思路

在我們這次的解法中,我們首先要先知道 Limak 剩多少時間可以解題,也就是 4個小時 = 240分鐘 再扣掉抵達派對的 k 分鐘 ➡ 可以解題的時間共240-k分鐘。

再來,題目中表示了最多就10題,所以我們可以用一個簡單的for 迴圈來計算做完該題剩餘時間就好。

要是這次的迴圈計算剩餘時間 < 0,代表Limak 的時間是不夠解決這一題的,故會回傳上一題的題號(從1開始計算)。

C#解決方案

方案1

string[] arr = Console.ReadLine().Split(" ");
int n = int.Parse(arr[0]);
int k = int.Parse(arr[1]);


int left = 240-k;
int cnt = 0;
bool found = false;

for(int i=0; i<n; i++){
    left -= (i+1)*5;
    
    if(left<0){
        cnt=i;
        found = true;
        break;
    }
}

Console.WriteLine(found ? cnt : n);

結論

要是是我的話,我在準備要去跨年派對前肯定是一題都不會去解的!!!

🧡如果文章有幫上你的忙,那是我的榮幸

🧡收藏文章或幫我點個廣告,那都是對我的支持

✅如有任何疑問,歡迎透過留言或messenger讓我知道 !

題目連結 : Problem – 750A – Codeforces

一些其他的Codeforces文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *