UVA 11729 Commando War

\Waiting for orders we held in the wood, word from the front never cameBy evening the sound of the gun re was miles away
Ah softly we moved through the shadows, slip away through the treesCrossing their lines in the mists in the elds on our hands and our kneesAnd all that I ever, was able to seeThe re in the air, glowing red, silhouetting the smoke on the breeze”There is a war and it doesn’t look very promising for your country. Now it’s time to act. You have a commando squad at your disposal and planning an ambush on an important enemy camp located nearby. You have N soldiers in your squad. In your master-plan, every single soldier has a unique responsibility and you don’t want any of your soldier to know the plan for other soldiers so that everyone can focus on his task only. In order to enforce this, you brief every individual soldier about his tasks separately and just before sending him to the battle eld. You know that every single soldier needs a certain amount of time to execute his job. You also know very clearly how much time you need to brief every single soldier. Being anxious to nish the total operation as soon as possible, you need to nd an order of brie ng your soldiers that will minimize the time necessary for all the soldiers to complete their tasks. You may assume that, no soldier has a plan that depends on the tasks of his fellows. In other words, once a soldier begins a task, he can nish it without the necessity of pausing in between.


【题目分析】
还是贪心的策略,执行时间长的任务先交代。用邻项交换法很容易得到。


【代码】

#include <cstdio>
#include <algorithm>
#include <vector> 
using namespace std;
struct node{
    int a,b;
    bool operator < (const node& y) const{
        return a>y.a;
    }
}x;
int main()
{
    int kase=1,n;
    while (scanf("%d",&n)==1&&n)
    {
        vector<node> v;
        for (int i=0;i<n;++i) scanf("%d%d",&x.b,&x.a),v.push_back(x);
        sort(v.begin(),v.end());
        int s=0,ans=0;
        for (int i=0;i<n;++i)s+=v[i].b,ans=max(ans,s+v[i].a);
        printf("Case %d: %d\n",kase++,ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值