LA 4850 Installations (贪心)

Description

Download as PDF In the morning, service engineers in a telecom company receive a list of jobs which they must serve today. They install telephones, internet, ipTVs, etc and repair troubles with established facilities. A client requires a deadline when the requested job must be completed. But the engineers may not complete some jobs within their deadlines because of job overload. For each job, we consider, as a penalty of the engineer, the difference between the deadline and the completion time. It measures how long the job proceeds after its deadline. The problem is to find a schedule minimizing the sum of the penalties of the jobs with the two largest penalties.

A service engineer gets a list of jobs Ji with a serving timesi and a deadlinedi. A jobJi needs timesi, and if it is completed at timeCi, then the penalty ofJi is defined to bemax{0,Ci -di}. For convenience, we assume that the timet when a job can be served is0t < andsi anddi are given positive integers such that0 <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 timesi and the deadlinedi,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 ofJ2 andJ6, namely 6 and 1, respectively, which is equal to 7 in this example.

Figure 1. The optimal schedule of the example

Input

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 integern (1n500), the number of the given jobs. In the next n lines of each test case, thei-th line contains two integer numberssi anddi, representing the serving time and the deadline of the jobJi, respectively, where1sidi10, 000.

Output

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.

Sample Input

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

Sample Output

7
0
14


题意:工程师要安装n个任务,其中第i个服务Ji需要si单位的安装时间,截止时间为di,如果在截止时间之前完成任务,不会有任何惩罚,否则惩罚值为任务完成时间与截止时间之差,你的任务是让惩罚值最大的两个任务的惩罚之和最小。


分析:完全没想到正解,但是很容易想到如果单单使惩罚值最大的最小的话按deadline排序就行了,但是这样做无法保证两个惩罚值最大的任务之和最小(样例就能看出来),

然后我想了好久终于想通了正解的做法,我们先按之前的思路按照DDL排序后,从后往前枚举一个任务作为最大惩罚值的任务,因为之前按照DDL排序的关系可以证明此时第

二大惩罚值一定是最小的。


#include 
#include  
#include 
using namespace std;
int n,T,maxn[505];
struct thing
{
    int s,d;
    friend bool operator <(const thing a,const thing b)
    {
        if(a.d == b.d) return a.s < b.s;
        return a.d < b.d;
    }
} job[501];
int main()
{

    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i = 1;i <= n;i++)
         scanf("%d %d",&job[i].s,&job[i].d);
        sort(job+1,job+1+n);
        int now = 0,MAX = 0,num = 0,ans = 0,pos = 0;
        for(int i = 1;i <= n;i++)
        {
            now += job[i].s;
            ans = max(ans,MAX + now - job[i].d);
            MAX = max(MAX,now - job[i].d);
            maxn[i] = max(maxn[i-1],MAX);
            if(now - job[i].d == MAX || now - job[i].d == ans - MAX) 
            {
                num = i;
                pos = now;
            }
        }
        for(int i = 1;i < num;i++) 
         ans = min(ans,max(maxn[i-1],max(0,MAX - job[i].s)) + max(0,pos - job[i].d));                  
        printf("%d\n",ans);
    }
}



你可能感兴趣的:(ACM,不会做,好题,贪心)