UVA11729 Commando War

Source: Internet
Author: User

Problem Link: UVA11729 Commando War.

Problem Summary: There are n subordinates need to complete a task, to the first subordinate to the task needs bi time, the task requires Ji time, request to complete the task as soon as possible, please output the final task required to complete the minimum total time.

This problem is a typical greedy law problem, the shortest time to complete the task. It is convenient to use C + + programming.

In the program, each task is represented by a class object, and the program is more convenient to handle, so a simple class job is implemented.

When sorting, Task J executes from large to small in time, and can get the shortest time to complete the task. However, if the task is of the same time, the task with the shorter task time should be given priority.

The following C + + language programs are provided by AC:

/* UVA11729 Commando War */#include <iostream> #include <vector> #include <algorithm> #include < cstdio>using namespace Std;const int maxn = 10000;class Job {public:    int B, J;    Job (int b, int j): B (b), J (j) {}    bool operator < (const job& r) Const {        return (j = = R.J)? b < R.b:j &G T R.J;    }}; vector<job> Myjob;int Main () {    int n, b, J, caseno=0;    while (scanf ("%d", &n)! = EOF && n! = 0) {        myjob.clear ();        for (int i=0; i<n; i++) {            scanf ("%d%d", &b, &j);            Myjob.push_back (Job (b, J));        }        Sort (Myjob.begin (), Myjob.end ());        int sum = 0, ans = 0;        for (int i=0; i<n; i++) {            sum + = myjob[i].b;            ans = max (ans, sum + myjob[i].j);        }        printf ("Case%d:%d\n", ++caseno, ans);    }    return 0;}


UVA11729 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.