PTA Review of Programming Contest Rules

題目重現

The ACM ICPC’s rule of scoring is as the following:

A problem is solved when it is accepted by the judges. Teams are ranked according to the most problems solved. For the purposes of awards, or in determining qualifier(s) for the World Finals, teams who solve the same number of problems are ranked by least total time. The total time is the sum of the time consumed for each problem solved. The time consumed for a solved problem is the time elapsed from the beginning of the contest to the submittal of the accepted run plus 20 penalty minutes for every rejected run for that problem regardless of submittal time. There is no time consumed for a problem that is not solved.

For example, if one has the following contest record:

  • At the 100th minute submitted problem A, rejected;
  • At the 140th minute submitted problem B, rejected;
  • At the 150th minute submitted problem B, accepted;
  • At the 160th minute submitted problem C, accepted.

Then his total time = (150+20) + 160 = 330. Notice that it took him only 10 minutes to finish problem C. Assume that he spent 40 minutes on reading all the problems through, then if he could do problem C first and problem A last, his contest record might look like:

  • At the 50th minute submitted problem C, accepted;
  • At the 90th minute submitted problem B, rejected;
  • At the 100th minute submitted problem B, accepted;
  • At the 160th minute submitted problem A, rejected.

Then his total time would be 50 + (100+20) = 170. Hence the order of solving the problems will affect one’s final rank.

Now suppose that you are in a one-person team. It will take you t0 ​​ minutes to read all the problems through. Judging by your experience, you may estimate the minutes ti ​​ that you will take to solve problem i . And more, the earlier you get one problem done, the more unlikely that you will make mistakes on programming. Assume that if a solution is first time submitted in the first hour (60 minutes inclusive), it will get accepted at once; if it is first time submitted in the second hour, it will be rejected once; if it is first time submitted in the third hour, it will be rejected twice; etc. You may also estimate the minutes di for debugging problem i every time it is rejected. For the sake of simplicity, let us assume that di is fixed for each problem i , and once you start working on a problem, you will keep submitting until it is accepted.

Your task is to write a program to find yourself the order of solving the problems which gives you the best chance of winning the contest.

Input Specification

Each input file contains several test cases. For each test case, the 1st line contains 3 integers: the total number of contest hours H (0<H5 ), the total number of problems N (0<N9 ), and the time t0 (minutes) for you to read through all the problems at the beginning of the contest. The following N lines each contains a problem’s name and a pair of positive integers ti and di . A problem’s name is a string of no more than 20 characters without any space. A negative H signals the end of input.

Output Specification

For each test case, you are supposed to print to standard output the total time in a line in the format shown by the sample. Then print the names of the problems you will solve in the correct order, each name in a line. If there are several solutions, you must output the smallest sequence. A sequence of problems {i1,i2,...in} is smaller than another sequence {j1,j2,...jn} means that there exists an integer K between 1 and n such that for every 1k<K , we have ik=jk ​​ , and iK<jK .

Sample Input

2 3 40
A 60 10
B 40 10
C 10 20
2 3 40
Try_This_One 20 5
And_This_One 20 5
Then_This_One 20 5
-1

Sample Output

Total Time = 170
C
B
Total Time = 295
Try_This_One
And_This_One
Then_This_One

題目大意

按照ACM/ICPC的比賽計分規則(解題數+20分鐘罰時規則),給定比賽時間、總題數。

假設某個人是這樣做題的:
1. 用一定時間通讀所有的題,計算出解出每個題目所需的時間,以及如果錯了,調試一次所需的時間。
2. 他一旦開始做某題,不做出來就不換題(一不做二不休)。
3. 對於每道題,他第一次提交的時間決定了他需要調試的次數。

具體地說是這樣的:

  • 開始1小時(包括1小時整)內提交一次AC;
  • 第二個小時內提交需要調試一次才能AC;
  • ……
  • 第N個小時內提交需要提交N次才能AC;

問題是要幫他確定一個可以獲得儘量高排名的做題順序:在解題量儘量高的前提下讓罰時儘量小。

題目解法

對於N 道題規模的暴力搜索的時間複雜度是O(NN!)

然而題目的規模不大,N9 ,可取。

完全模擬真實比賽進程就可以了,由此可以得到兩個基本的搜索出口:比賽時間到、題目全部解出(AK)。

每當到達搜索出口時,要將當前解與當前最優解比較並試圖更新(最優解合併),這裏需要O(N) 的時間。

這個問題呢,僅以N爲因子則時間複雜度不可能降低,最優算法即O(NN!) ,原因是你總是需要生成解題順序的全排列來計算最優的方案。比如在比賽時長足夠長(無限)的情況下,這也是一個有後效性的問題,不存在動態規劃解。

代碼實現

PTA Review of Programming Contest Rules 暴力搜索

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章