UVA 1467 - Installations(贪心)

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 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 0$ \le$t < $ \infty$ and si and di are given positive integers such that 0 < si$ \le$di. 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 (sidi) of the serving time si and the deadline dii = 1,..., 6, where (s1d1) = (1, 7)(s2d2) = (4, 7)(s3d3) = (2, 4)(s4d4) = (2, 15)(s5d5) = (3, 5)(s6d6) = (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.

\epsfbox{p4850.eps}

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 Tis given on the first line of the input. The first line of each test case contains an integer n ( 1$ \le$n$ \le$500), 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, where1$ \le$si$ \le$di$ \le$10, 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

题意:给定一些任务,有完成需要时间,和完成期限,不能同时做两个任务,任务超过期限会罚钱,罚钱数为超过时间,求一个做任务的顺序,使得最大和第二大罚钱数之和最小,输出这个最小值。

思路:贪心,按期限从小到大排序,这样的话去完成,最大的罚钱是最小的。但是这并不一定是答案,因为还有第二小的,这时就需要从第一个任务到最大罚钱任务,把每个任务都放到最大罚钱的任务后面计算一次,保留最小值。因为如果一个任务后移,那么第一大和第二大的任务的时间将同时减少,这样就会演变为最大罚钱增大,第二大罚钱减少,相加的话比较和之前的答案谁更小。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;

const int N = 505;
int t, n, pos, time, m1, m2;
struct Work {
	int s, d;
} w[N];

bool cmp(Work a, Work b) {
	return a.d < b.d;
}

void init() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		scanf("%d%d", &w[i].s, &w[i].d);
}

int cal(int i) {
	time = m1 = m2 = 0;
	for (int j = 0; j <= pos; j++) {
		if (i == j) continue;
		time +=w[j].s;
		int f = max(time - w[j].d, 0);
		if (m1 < f) {
			m2 = m1;
			m1 = f;
		}
		else if (m2 < f)
			m2 = f;
	}
	time += w[i].s;
	int f = max(time - w[i].d, 0);
		if (m1 < f) {
			m2 = m1;
			m1 = f;
		}
		else if (m2 < f)
			m2 = f;
	return m1 + m2;
}

int solve() {
	int ans; time = pos = m1 = m2 = 0;
	sort(w, w + n, cmp);
	for (int i = 0; i < n; i++) {
		time += w[i].s;
		int f = max(time - w[i].d, 0);
		if (m1 < f) {
			m2 = m1;
			m1 = f;
			pos = i;
		}
		else if (m2 < f) {
			m2 = f;
			pos = i;
		}
	}
	ans = m1 + m2;
	for (int j = 0; j < pos; j++)
		ans = min(cal(j), ans);
	return ans;
}

int main() {
	scanf("%d", &t);
	while (t--) {
		init();
		printf("%d\n", solve());
	}
	return 0;
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值