题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4221
2 2 3 4 2 2 4 3 6 2 7 4 5 3 9
Case 1: 1 Case 2: 3
转()
题目链接:Click here~~
题意:
给n个活动,每个活动需要一段时间C来完成,并且有一个截止时间D,当完成时间t大于截止时间完成时,会扣除t-D分,让你找出如何使自己所扣分的最大值最小。
解题思路:
贪心策略:每次先安排截止时间小的活动。
对于两个活动1、2,假设D1<D2,以起始时间为0为例。
如果先安排活动1,则扣分最大值为max(C1-D1,C1+C2-D2)。
如果先安排活动2,则扣分最大值为max(C2-D2,C1+C2-D1)。
显然第二个式子中的C1+C2-D1既大于C1-D1,又大于C1+C2-D2,则选择活动1是明智的。
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
struct node
{
int c, d;
} a[100017];
bool cmp(node a, node b)
{
return a.d < b.d;
}
int main()
{
int t;
int n;
__int64 cas = 0;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i = 0; i < n; i++)
{
scanf("%d%d",&a[i].c,&a[i].d);
}
sort(a,a+n,cmp);
__int64 maxx = 0, sum = 0;
for(int i = 0; i < n; i++)
{
sum+=a[i].c;
if(maxx < sum-a[i].d)
{
maxx = sum-a[i].d;
}
}
printf("Case %I64d: %I64d\n",++cas,maxx);
}
return 0;
}原文地址:http://blog.csdn.net/u012860063/article/details/45797271