UVa 11729

【题目翻译】:

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

不管有没有交待任务的时间,对于任务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<Cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct People
{
int b,j;
} p[1005];
int cmp(People a,People b)
{
return a.j>b.j;
}
int main()
{
int n,cas=0;
while(scanf("%d",&n)!=EOF&&n)
{
cas++;
for(int i=0; i<n; i++)
scanf("%d %d",&p[i].b,&p[i].j);
sort(p,p+n,cmp);
int sum=0,ans=0;
for(int i=0; i<n; i++)
{
sum+=p[i].b;
ans=max(ans,sum+p[i].j);
}
printf("Case %d: %d ",cas,ans);
}
return 0;

}

原文地址:https://www.cnblogs.com/tsw123/p/4320502.html