您的位置:首页 > 其它

突击战(Commando War,UVa 11729)

2017-12-27 21:42 393 查看

题目链接

https://vjudge.net/problem/UVA-11729

题解

贪心题,将J值大的任务先交代是最优的。

所以,只需将任务存放在结构体中,对结构体按J值从大到小排序即可。

代码

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxn=1e3+100;
struct node
{
int b,j;
bool operator<(const node &x)const
{
return j>x.j;
}
}a[maxn];

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