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