UVa 11729-commando War (Greed)

Source: Internet
Author: User

"Waiting for orders we held in the wood, word from the front never came
By evening the sound of the gunfire is miles away
Ah softly we moved through the shadows, slip away through the trees
Crossing their lines in the mists, fields on we hands and our knees
And all I ever, is able to
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 a ambush on a important enemy camp
Located nearby. You are have N soldiers in your squad. In your Master-plan, every single soldier has a
Unique responsibility and don ' t want any of your soldier to know the
Everyone can focus in 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 this every single soldier
Needs a certain amount to execute the his job. You also know very clearly
Need to brief every a single soldier. Being anxious to finish the total operation as soon as possible
Need to find a order of briefing your soldiers that'll minimize the time necessary for all the soldiers
To complete their tasks. You may assume that, no soldier has a and depends on the tasks of
Fellows. In other words, once a soldier begins a task, he can finish it without the necessity of pausing
In between.

Input

There is multiple test cases in the input file. Every test case starts and 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). B seconds are needed to brief the soldier while
Completing his job needs J seconds. The end of input is denoted by a case with N = 0. This case
Should is not 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 A-briefing till the completion of all jobs.

Sample Input
3
2 5
3 2
2 1
3
3 3
4 4
5 5
0

Sample Output
Case 1:8
Case 2:15

Meaning
You have n subordinates, and each of your subordinates needs to do a task. The first one needs you to spend B (i) minutes to account for the task,
Then he will immediately complete the task in a separate, uninterrupted execution of J (i) minutes.
You need to choose the order in which tasks are assigned, so that all tasks are executed as soon as possible (i.e. the last completed task should be terminated as soon as possible).
Note: It is not possible to give 2 or more subordinates a task at the same time, but the subordinates can perform their respective tasks at the same time.

Enter the first act N,n as the number of subordinates.
The next n behavior B and J.
When n is 0 o'clock, the input ends.

Analysis:
Tasks that take longer to execute need to be accounted for first,
So, we can think of such a greedy algorithm:
According to J from small to large order for each task sorted, and then confessed.

Package cn.hncu.UVa;
Import Java.util.Arrays;
Import Java.util.Comparator;

Import Java.util.Scanner; /** * * * * * @author Chen Haoxiang * * * 2016-4-25/public class P11729 {public static void main (string[] args) {Sca
        Nner sc = new Scanner (system.in);
        int k=0;
            while (Sc.hasnext ()) {k++;
            int n =sc.nextint ();
            if (n==0) {break;
            Bj bj[] = new Bj[n];
                for (int i=0;i<n;i++) {bj[i]=new bj ();
                bj[i].b = Sc.nextint ();
            BJ[I].J = Sc.nextint ();
            Sort (BJ);
            int s=0;
            int ans = 0; for (int i=0;i<n;i++) {s = s+bj[i].b;//the start execution time of the current task, that is, after the time of command ans = math.max (ans, s+bj[i
        ].J)//Update the latest time when a user task finishes executing System.out.println ("Case" +k+ ":" +ans); } private static void sort (bj[] bj) {arrays.sort (BJ, New comparator<Bj> () {@Override public int compare (BJ O1, bj O2) {if (O1.J&LT;O2.J) {
                return 1;
            } return-1;
    }
        });
    class bj{public int b;
public int J;
 }

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.