ACM: uva 1467 - Installations

来源:互联网 发布:ios 网络请求第三方 编辑:程序博客网 时间:2024/05/16 00:28

Installations

In the morning, service engineers in a telecomcompany receive a list of jobs which they must serve today. Theyinstall telephones, internet, ipTVs, etc and repair troubles withestablished facilities. A client requires a deadline when therequested job must be completed. But the engineers may not completesome jobs within their deadlines because of job overload. For eachjob, we consider, as a penalty of the engineer, the differencebetween the deadline and the completion time. It measures how longthe job proceeds after its deadline. The problem is to find aschedule minimizing the sum of the penalties of the jobs with thetwo largest penalties.

A service engineer gets a list ofjobs Ji with aservingtime si and adeadline di. Ajob Ji needstime si, and if it is completedat time Ci, then the penaltyof Ji isdefined tobe max{0, Ci di}.For convenience, we assume that thetime t when a job can beserved is 0t  and si and di aregiven positive integers such that 0< sidi. The goal is to find a schedule of jobsminimizing the sum of the penalties of the jobs with the twolargest penalties.

For example, there are sixjobs Ji withthepair (sidiofthe servingtime si and thedeadline di,i =1,..., 6,where (s1d1)= (1,7), (s2d2)= (4,7), (s3d3)= (2,4), (s4d4)= (2,15), (s5,d5) =(3,5), (s6d6)= (3, 8). Then Figure 1 represents a schedule which minimizes thesum of the penalties of the jobs with the two largest penalties.The sum of the two largest penalties of an optimal schedule is thatof the penaltiesof J2 and J6,namely 6 and 1, respectively, which is equal to 7 in thisexample.

ACM: <wbr>uva <wbr>1467 <wbr>- <wbr>Installations

 

Figure 1. The optimal schedule of the example

Input 

Your program is to read from standard input. Theinput consists of T testcases. The number of testcases T is given on thefirst line of the input. The first line of each test case containsan integern 1 n500), the number of the given jobs. In thenext n lines of each testcase, the i-th line contains two integernumbers si and di,representing the serving time and the deadline of thejob Ji, respectively,where 1 si di10, 000.

Output 

Your program is to write to standard output. Printexactly one line for each test case. The line contains the sum ofthe penalties of the jobs with the two largest penalties.

The following shows sample input and ouput forthree test cases.

SampleInput 

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

SampleOutput 

7

0

14

 

题意: 有n个的任务要完成, 给出你它完成需要的时间和deadline, 惩罚值 = max(完成的时刻-deadline,0);

     需要你计算出最大的惩罚值和次大的惩罚值之和.(每个任务都有一个超时惩罚值)

 

解题思路:

     1. 工作任务分配问题, 很容易想到将deadline小的任务先执行, 任务根据deadline排序.

     2. 如果按照排序后从小到大执行一遍, 可以得出超时任务的最大和次大的惩罚值和它们的编号. 很容易

     想到, 把超时的任务往前掉, 最后就是尝试把超时任务插入哪个位置使得最大和次大的惩罚值之和最小.

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 505

struct node
{
 int s, d;
 bool operator <(const node&x) const
 {
  return d <x.d;
 }
}a[MAX];

int n;

inline int max(int a, int b)
{
 return a > b ? a : b;
}

inline int min(int a, int b)
{
 return a < b ? a : b;
}

int solve()
{
 sort(a, a+n);
 int i, j;
 int c1 = 0, c2 = 0, pos = 0;
 int ans, cur = 0;
 for(i = 0; i < n; ++i)
 {
  cur += a[i].s;
  int temp = max(cur-a[i].d,0);
  if(temp >c1)
  {
   c2 =c1;
   c1 =temp;
   pos =i;
  }
  else if(temp >c2)
  {
   c2 =temp;
   pos =i;
  }
 }

 ans = c1+c2;
 for(i = 0; i < pos; ++i)
 {
  c1 = c2 = cur = 0;
  for(j = 0; j <n; ++j)
  {
   if(i == j)continue;
   cur +=a[j].s;
   int temp =max(cur-a[j].d, 0);
   if(temp> c1)
   {
    c2= c1;
    c1= temp;
   }
   else if(temp> c2) c2 = temp;

   if(j ==pos)
   {
    cur+= a[i].s;
    inttemp = max(cur-a[i].d, 0);
    if(temp> c1)
    {
     c2= c1;
     c1= temp;
    }
    elseif(temp > c2) c2 = temp;
   }
  }
  ans = min(ans, c1+c2);
 }
 return ans;
}

int main()
{
// freopen("input.txt", "r", stdin);
 int caseNum;
 scanf("%d", &caseNum);
 while(caseNum--)
 {
  scanf("%d",&n);
  for(int i = 0; i< n; ++i)
   scanf("%d%d", &a[i].s, &a[i].d);

  printf("%d\n",solve());
 }

 return 0;
}

0 0