题目描述: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;
}
该博客详细分析了Uva 11729 Commando War问题,指出由于任务可以并行执行,采用贪心策略优先处理耗时长的任务。通过比较不同任务顺序的完成时间,证明了贪心策略的正确性,并提供了相关代码实现。

94

被折叠的 条评论
为什么被折叠?



