HDOJ 4415 Assassin’s Creed
#include<iostream> #include<stdio.h> #include<algorithm> #include<cstring> using namespace std; struct point { int A,B; bool vis; } node[100005]; bool cmp( point a,point b ) { return a.A < b.A; } int main( ) { int i,j,k,t,T,cas,n,m,ans,count,sum1,sum2,b1,b2; scanf("%d",&T); for( cas = 1; cas <= T; cas++ ) { scanf("%d%d",&n,&m); for( i = 1; i <= n; i++ ) { scanf("%d%d",&node[i].A,&node[i].B); node[i].vis = false; } sort( &node[1],&node[1] + n,cmp); sum1 = 0; b1 = m; sum2 = 0; b2 = 0; ans = 0, count = n; for( i = 1; i <= n; i++ ) if( node[i].A <= m && node[i].B > 0 ) { sum1++; count--; node[i].vis = 1; b1 -= node[i].A; break; } for( i = 1; i <= n; i++ ) if( node[i].B > 0 ) ans += node[i].B; if( sum1 ) { for( i = 1; i <= n; i++ ) { if( count <= ans ) { ans = 0; sum1 += count; break; } if( !node[i].vis && b1 >= node[i].A ) { b1 -= node[i].A; sum1++; count--; } } sum1 += ans; } for( i = 1; i <= n; i++ ) if( b2 >= node[i].A && node[i].B == 0 ) { b2-= node[i].A; sum2++; } printf("Case %d: ",cas); if( sum1 > sum2 ) { printf("%d %d\n",sum1,m-b1); continue; } if( sum2 > sum1 ) { printf("%d %d\n",sum2,m-b2); continue; } if( b1 >= b2 ) printf("%d %d\n",sum1,m-b1); else printf("%d %d\n",sum1,m-b2); } system("pause"); return 0; } /* 40 4 3 1 1 1 1 2 0 2 0 5 4 1 1 1 1 1 1 2 0 2 0 5 3 1 1 1 1 1 1 2 0 2 0 6 3 1 1 1 1 1 1 1 1 2 0 2 0 5 3 1 1 2 0 2 0 2 0 2 0 5 3 1 1 1 1 2 0 2 0 2 0 5 5 1 1 1 2 2 1 3 0 2 0 3 5 4 1 5 1 7 7 2 1 2 2 4 0 5 5 10 1 4 1 5 2 110 0 1 0 3 5 1 0 2 0 3 0 6 20 10 2 10 2 100 0 100 0 100 0 100 0 5 7 7 1 7 1 1 0 1 0 1 0 4 5 4 1 1 0 2 0 1 0 Case 1: 4 2 Case 2: 5 2 Case 3: 5 2 Case 4: 6 2 Case 5: 3 3 Case 6: 4 2 Case 7: 5 1 Case 8: 3 4 Case 9: 0 0 Case 10: 5 4 Case 11: 2 3 Case 12: 6 20 Case 13: 3 3 Case 14: 3 4 */