Description
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 of the fields on our hands and our knees
And all that I ever, is 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 had a commando squad at your disposal, and planning an ambush in an important enemy camp located nearby. You haveNSoldiers in your squad. In your Master-plan, every single soldier have a unique responsibility and you don ' t want any of your soldier to know the P LAN for other soldiers so, everyone can focus on he task only. In order to enforce this, you brief every individual soldier about he tasks separately and just before sending him to the Battlefield. You know this every single soldier needs a certain amount of time-to-execute his job. You also know very clearly what much time you need to brief every single soldier. Being anxious to finish the total operation as soon as possible, you need to find a order of briefing your soldiers that Would minimize the time necessary for all the soldiers to complete their tasks. To assume that, no soldier have 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'll is 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 the integers B (1<=b<=10000) & J (1<=j< =10000). B seconds is needed to brief the soldier and completing his job needs J seconds. The end of input would be denoted by a case with N =0. This case is should not being processed.
Output
For each test case, print a line in the format, ' Case X:y ', where X is the case number & Y are the total number of SEC Onds 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 7 · 4 4 5 5 0 |
Case 1:8 Case 2:15
|
Nature is a greedy algorithm of the topic, first there are two sets of data, natural to judge the sort. After the experiment, we can know that the execution time is sorted (from large to small), and then accumulated, and the maximum value is calculated. Note 21 91 2 this situation.
1#include <stdio.h>2 intH=1;3 inta[10010],b[10010];4 voidPaixu (intB[],intA[],intN)5 {6 intI=0, j=n-1, t=b[0],m=a[0];7 if(n>1)8 {9 while(i<j)Ten { One for(; i<j; j--) A if(b[j]>t) - { -b[i]=B[j]; thea[i]=A[j]; -i++; - Break; - } + for(; i<j; i++) - { + if(b[i]<t) A { atb[j]=B[i]; -a[j]=A[i]; -j--; - Break; - } - } in } -b[i]=T; toa[i]=m; + Paixu (b,a,i); -Paixu (b+i+1, a+i+1, n-i-1); the } * } $ voidShuchu (intA[],intB[],intN)Panax Notoginseng { - inti,m=a[0],t,max=a[0]+b[0]; the for(i=1; i<n; i++) + { Am=m+A[i]; thet=m+B[i]; + if(T>max) max=T; - } $printf"Case %d:%d\n", h++, max); $ } - intMain () - { the intn,i; - while(SCANF ("%d", &n) &&N)Wuyi { the - for(i=0; i<n; i++) Wuscanf"%d%d",&a[i],&b[i]); - Paixu (b,a,n); About Shuchu (a,b,n); $ - } - return 0; -}
B-commando War