UVA 11729 Commando War

Source: Internet
Author: User


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 has N soldiers 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. Assume that, no soldier have a plan that depends on the tasks ofHis 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
2 5
2
1
3 3
4
5
Case 1:8
Case 2:15

Problem Setter:mohammad Mahmudur Rahman, special Thanks:manzurur Rahman Khan

Analysis: According to test instructions, the task can not be parallel, then the task must be one after another. But because the tasks are parallel, we should allow as many tasks as possible to be executed in parallel in order to shorten the time. The way to achieve this goal is to take a long-time task first.

#include <iostream>#include<map>using namespacestd;intMainintargcChar Const*argv[]) {    intTest, CaseN =0;  while(CIN >> Test && Test! =0) {Multimap<int,int>Soldierandfinishtime; intB, J; intm =0, sum =0;  for(inti =0; I! = test; ++i) {cin>> B >>J; Soldierandfinishtime.insert (Pair<int,int> (1-J, B));//automatic sorting of maps, sorted by job from big to small        }         for(Auto &i:soldierandfinishtime) {            /** mi: Assignment of the time of the I task, si: The execution time of the first task * then the completion time of the task of I is M1 + m2 + ... + mi + si, or * The time of the previous task is completed, this is the maximum time for the previous task to be longer than the current task is completed **/m+ = I.second;//the start time of the current tasksum = max (sum, M + (1-I.first));//sum indicates the maximum time to be executed so far} cout<<" Case"<< ++casen <<": "<< sum <<Endl; }    return 0;}

UVA 11729 Commando War

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.