\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);
}
}