UVA-11729-Commando War

题目:UVA-11729-Commando War

贪心,按执行时间从大到小排序。

 

 1 #include <iostream>
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 #include<algorithm>
 6 
 7 using namespace std;
 8 
 9 struct job{
10 int b,j;
11 };
12 
13 bool cmp(job x,job y)
14 {
15     return x.j>y.j;
16 }
17 
18 int main()
19 {
20     job a[1010];
21     int n,cnt=0,i,ans,sum;
22     while(scanf("%d",&n)!=EOF && n)
23     {
24         ++cnt;
25         ans=0;
26         sum=0;
27         for(i=0;i<n;i++)
28             scanf("%d%d",&a[i].b,&a[i].j);
29         sort(a,a+n,cmp);
30         for(i=0;i<n;i++)
31             {
32                 sum+=a[i].b;
33                 ans=max(ans,sum+a[i].j);//#
34             }
35         printf("Case %d: %d\n",cnt,ans);
36     }
37     return 0;
38 }

 

posted @ 2015-01-22 15:41  alohagin  阅读(127)  评论(0编辑  收藏  举报