贪心 UVA 11729 Commando War
1 /*
2 贪心:按照执行时间长的优先来排序
3 */
4 #include <cstdio>
5 #include <algorithm>
6 #include <iostream>
7 #include <cstring>
8 #include <string>
9 #include <cmath>
10 using namespace std;
11
12 const int MAXN = 1e3 + 10;
13 const int INF = 0x3f3f3f3f;
14 struct Node {
15 int b, j;
16 bool operator < (const Node &r) const {
17 return j > r.j;
18 }
19 }node[MAXN];
20
21 int main(void) //UVA 11729 Commando War
22 {
23 //freopen ("UVA_11729.in", "r", stdin);
24
25 int n; int cas = 0;
26 while (scanf ("%d", &n) == 1 && n)
27 {
28 for (int i=1; i<=n; ++i)
29 {
30 scanf ("%d%d", &node[i].b, &node[i].j);
31 }
32
33 sort (node+1, node+1+n);
34
35 int ans = 0; int mx = -1;
36 for (int i=1; i<=n; ++i)
37 {
38 ans += node[i].b;
39 mx = max (mx, ans + node[i].j);
40 }
41
42 printf ("Case %d: %d\n", ++cas, mx);
43 }
44
45 return 0;
46 }
47
48
49 /*
50 Case 1: 8
51 Case 2: 15
52 */
编译人生,运行世界!