uva 11729 Commando War

解题思路:按照 J 从大到小的顺序给各个任务排序,然后依次交代

 1 ///////////////////////////////////////////////////////////////////////////
 2 //problem_id: uva 11729
 3 //user_id: SCNU20102200088
 4 ///////////////////////////////////////////////////////////////////////////
 5 
 6 #include <algorithm>
 7 #include <iostream>
 8 #include <iterator>
 9 #include <iomanip>
10 #include <cstring>
11 #include <cstdlib>
12 #include <string>
13 #include <vector>
14 #include <cstdio>
15 #include <cctype>
16 #include <cmath>
17 #include <queue>
18 #include <stack>
19 #include <list>
20 #include <set>
21 #include <map>
22 using namespace std;
23 
24 ///////////////////////////////////////////////////////////////////////////
25 #pragma comment(linker,"/STACK:1024000000,1024000000")
26 
27 #define lson l,m,rt<<1
28 #define rson m+1,r,rt<<1|1
29 ///////////////////////////////////////////////////////////////////////////
30 
31 ///////////////////////////////////////////////////////////////////////////
32 const double EPS=1e-8;
33 const double PI=acos(-1.0);
34 
35 const int x4[]={-1,0,1,0};
36 const int y4[]={0,1,0,-1};
37 const int x8[]={-1,-1,0,1,1,1,0,-1};
38 const int y8[]={0,1,1,1,0,-1,-1,-1};
39 ///////////////////////////////////////////////////////////////////////////
40 
41 ///////////////////////////////////////////////////////////////////////////
42 typedef long long LL;
43 
44 typedef int T;
45 T max(T a,T b){ return a>b? a:b; }
46 T min(T a,T b){ return a<b? a:b; }
47 T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
48 T lcm(T a,T b){ return a/gcd(a,b)*b; }
49 ///////////////////////////////////////////////////////////////////////////
50 
51 ///////////////////////////////////////////////////////////////////////////
52 //Add Code:
53 struct Node{
54     int B,J;
55     bool operator <(const Node &a) const{
56         return J>a.J;
57     }
58 }t[1005];
59 ///////////////////////////////////////////////////////////////////////////
60 
61 int main(){
62     ///////////////////////////////////////////////////////////////////////
63     //Add Code:
64     int n,i,Case=1;
65     while(scanf("%d",&n)!=EOF){
66         if(n==0) break;
67         for(i=0;i<n;i++) scanf("%d%d",&t[i].B,&t[i].J);
68         sort(t,t+n);
69         int Max=0,sum=0;
70         for(i=0;i<n;i++){
71             sum+=t[i].B;
72             if(sum+t[i].J>Max) Max=sum+t[i].J;
73         }
74         printf("Case %d: %d
",Case++,Max);
75     }
76     ///////////////////////////////////////////////////////////////////////
77     return 0;
78 }
79 
80 ///////////////////////////////////////////////////////////////////////////
81 /*
82 Testcase:
83 Input:
84 3
85 2 5
86 3 2
87 2 1
88 3
89 3 3
90 4 4
91 5 5
92 0
93 Output:
94 Case 1: 8
95 Case 2: 15
96 */
97 ///////////////////////////////////////////////////////////////////////////
原文地址:https://www.cnblogs.com/linqiuwei/p/3313565.html