uva11729 Commando War<贪心>
链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2829
View Code
1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <cstring> 5 #include <cmath> 6 #include <vector> 7 #include <algorithm> 8 using namespace std; 9 int N; 10 11 class Node 12 { 13 public: 14 int s, e; 15 bool operator <( const Node x ) const{ 16 return e>x.e; 17 } 18 }; 19 20 int main( ) 21 { 22 int Case=1, s, e; 23 while( scanf( "%d", &N )!= EOF, N ){ 24 cout<< "Case "<<Case++<<": "; 25 vector<Node>v; 26 for( int i=0; i<N; ++ i ){ 27 cin>>s>>e; 28 v.push_back((Node){s, e} ); 29 } 30 sort( v.begin(), v.end() ); 31 int ans=0, s=0; 32 for( int i=0; i<N; ++ i ){ 33 s+=v[i].s; 34 ans=max( ans, s+v[i].e ); 35 } 36 cout<<ans<<endl; 37 } 38 return 0; 39 }
View Code
1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <cstring> 5 #include <cmath> 6 #include <vector> 7 #include <algorithm> 8 using namespace std; 9 struct Node 10 { 11 int a, b; 12 }p[100000]; 13 bool cmp( const Node &x, const Node &y ) 14 { 15 return y.b<x.b; 16 } 17 int main( ) 18 { 19 int N, Case=1; 20 while( scanf("%d", &N )==1, N ){ 21 printf( "Case %d: ", Case++ ); 22 for( int i=0; i<N; ++ i ){ 23 scanf( "%d%d", &p[i].a, &p[i].b ); 24 } 25 sort( p, p+N, cmp ); 26 int ans=0, s=0; 27 for( int i=0; i<N; ++ i ){ 28 s+=p[i].a; 29 ans=max( ans, s+p[i].b ); 30 } 31 printf("%d\n", ans ); 32 } 33 }