码迷,mamicode.com
首页 > 其他好文 > 详细

POJ_3045_Cow_Acrobats

时间:2016-04-23 08:57:42      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

描述


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 }
View Code

 

POJ_3045_Cow_Acrobats

标签:

原文地址:http://www.cnblogs.com/Sunnie69/p/5423827.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!