当前位置: 首页 > 工具软件 > Commando > 使用案例 >

突击战(Commando War,UVa 11729)

夏振国
2023-12-01

题目链接

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;
}
 类似资料: