UVa11729 Commando War

  原题链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2829

  贪心。因为交代任务的时间Bi是不能减少的,所以首先交代完成时间Ji最长的部下。过程中更新最短所需时间。

View Code
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 #define INF 10000000
 5 #define N 1005
 6 
 7 struct node
 8 {
 9     int x, y;
10 }a[N];
11 
12 bool cmp(node u, node v){return u.y > v.y;}
13 inline int max(int x, int y){return x > y ? x : y;}
14 
15 int main()
16 {
17     int i, n, ans, cas = 1, m;
18     while(scanf("%d", &n) == 1 && n)
19     {
20         for(i = 0; i < n; i ++)
21             scanf("%d%d", &a[i].x, &a[i].y);
22         std::sort(a, a + n, cmp);
23         for(m = ans = i = 0; i < n; i ++)
24         {
25             ans += a[i].x;
26             m = max(m, ans + a[i].y);
27         }
28         printf("Case %d: %d\n", cas ++, m);
29     }
30     return 0;
31 }
原文地址:https://www.cnblogs.com/huangfeihome/p/2749530.html