A service engineer gets a list of jobs Ji with a serving time si and a deadline di. A job Ji needs time si, and if it is completed at time Ci, then the penalty of Ji is defined to be max{0, Ci - di}. For convenience, we assume that the time t when a job can be served is 0t < and si and di are given positive integers such that 0 < sidi. The goal is to find a schedule of jobs minimizing the sum of the penalties of the jobs with the two largest penalties.
For example, there are six jobs Ji with the pair (si, di) of the serving time si and the deadline di, i = 1,..., 6, where (s1, d1) = (1, 7), (s2, d2) = (4, 7), (s3, d3) = (2, 4), (s4, d4) = (2, 15), (s5, d5) = (3, 5), (s6, d6) = (3, 8). Then Figure 1 represents a schedule which minimizes the sum of the penalties of the jobs with the two largest penalties. The sum of the two largest penalties of an optimal schedule is that of the penalties of J2 and J6, namely 6 and 1, respectively, which is equal to 7 in this example.
Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given on the first line of the input. The first line of each test case contains an integer n ( 1n500), the number of the given jobs. In the next n lines of each test case, the i-th line contains two integer numbers si and di, representing the serving time and the deadline of the job Ji, respectively, where 1sidi10, 000.
Your program is to write to standard output. Print exactly one line for each test case. The line contains the sum of the penalties of the jobs with the two largest penalties.
The following shows sample input and ouput for three test cases.
3 6 1 7 4 7 2 4 2 15 3 5 3 8 7 2 17 2 11 3 4 3 20 1 20 4 7 5 14 10 2 5 2 9 5 10 3 11 3 4 4 21 1 7 2 9 2 11 2 23
7 0
14
只想到最大惩罚值用贪心,不知道两个怎么搞,也是看了别人的题解,分析问题的思维好欠缺啊。
首先还是贪心,然后分三种情况讨论分析
1.所有惩罚值为0.答案就是0
2.有一个有惩罚值,答案就是该惩罚值。
3有两个以上的惩罚值,我们假设最大为max1,次大为max2,就会有两个下标为xb1,xb2.
会把任务划分成三段
我们试着移动任务:
(1)从1找一个任务移动到2执行,则xb2所对应的惩罚值不变,xb1所对应的惩罚值增加。
(2)从2找一个任务移动到3执行,则xb1所对应的惩罚值不变,xb2所对应的惩罚值增加。
(3)从1找一个任务移动到3执行,虽然最大惩罚值会增大,但是则xb2所对应的惩罚值和xb1所对应的惩罚值减小。也就说最大惩罚值增大,次大惩罚值有可能减小。总和也就有可能减小。分析可以发现,最好也就是移动任务到xb2后面第一个。
所以我们只要枚举(3)情况就可以了。
#include
#include
#include
#include
#include
#include