描述
http://poj.org/problem?id=3045
分析
贪心.
易证:w+s越大应在越下面.所以排序,扫一遍即可.
注意:
1.ans的初始值应为-INF而非0,因为很可能大家的力气都很大,但都很轻!
1 #include<cstdio> 2 #include<algorithm> 3 using std :: sort; 4 using std :: max; 5 6 const int maxn=50005,INF=0x7fffffff; 7 int n; 8 9 struct point 10 { 11 int w,s,sum; 12 }c[maxn]; 13 14 bool comp(point x,point y) { return x.sum<y.sum; } 15 16 void solve() 17 { 18 sort(c+1,c+n+1,comp); 19 int ans=-INF,sum=0; 20 for(int i=1;i<=n;i++) 21 { 22 ans=max(ans,sum-c[i].s); 23 sum+=c[i].w; 24 } 25 printf("%d\n",ans); 26 } 27 28 void init() 29 { 30 scanf("%d",&n); 31 for(int i=1;i<=n;i++) 32 { 33 scanf("%d%d",&c[i].w,&c[i].s); 34 c[i].sum=c[i].w+c[i].s; 35 } 36 } 37 38 int main() 39 { 40 freopen("cow.in","r",stdin); 41 freopen("cow.out","w",stdout); 42 init(); 43 solve(); 44 fclose(stdin); 45 fclose(stdout); 46 return 0; 47 }
时间: 2024-10-14 23:33:00