Commando war
Input:Standard Input
Output:Standard output
"Waiting for orders we held in the wood, word from thefront never came
By evening the sound of the gunfire was miles away
Ah softly We moved through the shadows, slip awaythrough the trees
Crossing their lines in the mists in the fields on ourhands and our knees
And all that I ever, was able to see
The fire in the air, glowing red, silhouetting thesmoke on the breeze"
Thereis a war and it doesn't look very promising for your country. now it's time toact. you have a commando squad at your disposal and planning an ambush on animportant enemy camp located nearby. you haveN
Soldiers in your squad. in your master-plan, every single soldier has a unique responsibility and youdon't want any of your soldier to know the plan for other soldiers so thateveryone can focus on his task only. in order to enforce this, you brief everyindividual
Soldier about his tasks separately and just before sending him tothe battlefield. you know that every single soldier needs a certain amount oftime to execute his job. you also know very clearly how much time you need tobrief every single soldier. being anxious
To finish the total operation as soonas possible, you need to find an order of briefing your soldiers that willminimize the time necessary for all the soldiers to complete their tasks. youmay assume that, no soldier has a plan that depends on the tasks
Hisfellows. In other words, once a soldier begins a task, he can finish it without the necessity of pausing inbetween.
Input
Therewill be multiple test cases in the input file. Every test case starts with anintegerN (1 <= n <= 1000), Denoting the number of soldiers. Each ofthe following n lines describe a soldier with two integersB (1 <= B <= 10000)
&J (1 <= j <= 10000).BSecondsare needed to brief the soldier while completing his job needs
JSeconds. The end of input will be denoted by a case
N = 0.This case shouldnot be processed.
Output
Foreach test case, print a line in the format, "Case X: Y ", where X is the casenumber & Y is the total number of seconds counted from the start of yourfirst briefing till the completion of all jobs.
Sample Input
3
2 5
3 2
2 1
3
3 3
4
5 5
0
Output for sample input
Case 1: 8
Case 2: 15
Solution:
1. First, give the task to the soldiers with the longest time.
2. task completion time = Waiting Time + account time + task completion time.
3. The total time is the total time spent by soldiers at the latest end time.
# Include <cstdio> # include <algorithm> using namespace STD; struct data {int wait; int do1;} time [1002]; bool CMP (struct data a, struct data B) {if (. do1 = B. do1) return. wait <B. wait; return. do1> B. do1 ;}int main () {int N; int I, j, k = 1; int t; int Max; while (scanf ("% d", & N) & N) {T = 0; max = 0; // remember to initialize the data for each group of test cases. Here we will find the data for (I = 0; I <n; I ++) scanf ("% d", & time [I]. wait, & time [I]. do1); sort (time, time + N, CMP); ( I = 0; I <n; I ++) {T + = time [I]. wait; // wait time (wait time + explain time) if (time [I]. do1 + T> MAX) // task completion time = wait time + explain time + done time. The total time is the last time the soldiers use the end time. max = time [I]. do1 + T;} printf ("case % d: % d \ n", K ++, max);} return 0 ;}