您的位置:首页 > 其它

Uva 11729 Commando War - 水题 - 贪心

2013-12-04 19:07 495 查看
题目描述:lrj厚白书第一章第二道例题

题目分析:因为任务是可以并行的执行,所以直觉上是花费时间长的任务优先去部署。但是这到题目还给你交待任务的时间,所以容易让人想多了。

不管有没有交待任务的时间,对于任务x和y,只可能有两种情况。x在y之前结束,和x在y之后结束。这里讨论x在y之前完成。



未交换x和y的位置时,完成时间为:B[x] + B[y] + J[y]

交换h和y位置之后,完成时间为:B[y] + B[x] + J[x]

如果J[y] 大于J[x],那么交换后,时间变短了,所以应该将y放在x之前执行。另一种情况也类似。

这样证明以后就可以大胆的用贪心策略了。

下面是代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int maxn = 1005;
struct node {
int b;
int j;
} S[maxn];
int B[maxn];
int J[maxn];

bool cmp(node a,node b)
{
return a.j > b.j;
}
int main()
{
int n;
int cas = 0;
while(scanf("%d",&n) && n != 0) {
cas++;
for(int i = 0; i < n; i++) {
int b,j;
scanf("%d%d",&b,&j);
S[i].b = b;
S[i].j = j;
}
sort(S,S+n,cmp);
int tot = 0;
int t1= 0;
for(int i = 0; i < n; i++) {
t1 += S[i].b;
tot = max(tot,S[i].j + t1);
}
printf("Case %d: %d\n",cas,tot);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  贪心