UVa 11729 Commando War



G

Commando War

Input: Standard Input

Output: Standard Output

 

 

“Waiting for orders we held in the wood, word from the front never came

By evening the sound of the gunfire was miles away

Ah softly we moved through the shadows, slip away through the trees

Crossing their lines in the mists in the fields on our hands and our knees

And all that I ever, was able to see

The fire 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 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 battlefield. 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 finish the total operation as soon as possible, you need to find an order of briefing 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 finish it without the necessity of pausing in between.

 

Input

 

There will be multiple test cases in the input file. Every test case starts with an integer N (1<=N<=1000), denoting the number of soldiers. Each of the following N lines describe a soldier with two integers B (1<=B<=10000) J (1<=J<=10000)seconds are needed to brief the soldier while completing his job needs seconds. The end of input will be denoted by a case with N =0 . This case should not be processed.

 

Output

 

For each test case, print a line in the format, “Case X: Y”, where X is the case number & Y is the total number of seconds counted from the start of your first briefing till the completion of all jobs.

 

                  Sample Input                        Output for Sample Input

3

2 5

3 2

2 1

3

3 3

4 4

5 5

0

Case 1: 8

Case 2: 15

 


Problem Setter: Mohammad Mahmudur Rahman, Special Thanks: Manzurur Rahman Khan

 

对于每个士兵来说,交代任务是不能同时进行的,但执行任务是可以同时进行的,因此最终的时间总和就等于所有士兵交代任务的时间总和再加上一部执行任务的时间。

因此对士兵的排序只需要考虑执行任务时间的长短即可,因为交代任务的时间不会影响最终结果(反正它的总和都要加上去),而由于任务是可以同时执行的,因此执行时间长的显然应该放在前面,所以就只要对所有的士兵按执行任务时间由长到短排序,然后计算时间即可

 

UVa 11729 Commando War
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 
 5 using namespace std;
 6 
 7 typedef struct
 8 {
 9     int b;
10     int j;
11 } SOLDIER;
12 
13 SOLDIER soldier[1050];
14 
15 bool cmp(SOLDIER a,SOLDIER b)
16 {
17     return a.j>b.j;
18 }
19 
20 int main()
21 {
22     int n,kase=0;
23 
24     while(scanf("%d",&n)==1&&n)
25     {
26         kase++;
27         for(int i=0;i<n;i++)
28             scanf("%d %d",&soldier[i].b,&soldier[i].j);
29         sort(soldier,soldier+n,cmp);
30         int time=0,tmp=0;
31         for(int i=0;i<n;i++)
32         {
33             tmp+=soldier[i].b;
34             if(tmp+soldier[i].j<=time)
35                 continue;
36             time=tmp+soldier[i].j;
37         }
38         printf("Case %d: %d\n",kase,time);
39     }
40 
41     return 0;
42 }
[C++]

UVa 11729 Commando War

上一篇:二叉树的旋转操作和平衡判断


下一篇:《观止-微软创建NT和未来的夺命狂奔》 语录摘抄