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

洛谷P1842 奶牛玩杂技

时间:2017-10-04 14:23:00      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:logs   algorithm   +=   tchar   inline   node   比较   奶牛   经典   

洛谷P1842 奶牛玩杂技

贪心

做法很经典 比较相邻两个 而且这个式子更简单 Sx-wy>=Sy-Wx (x放下面更优)

移项之后排序直接弄就好

 1 #include <cstdio> 
 2 #include <algorithm>
 3 #define For(i,j,k) for(int i=j;i<=k;i++) 
 4 #define Dow(i,j,k) for(int i=j;i>=k;i--) 
 5 using namespace std ; 
 6 
 7 const int N = 50011,inf = 1e8 ; 
 8 struct node{
 9     int w,s,sum ; 
10 }a[N] ;
11 int n ; 
12 
13 inline int read() 
14 {
15     int x = 0 , f = 1 ; 
16     char ch = getchar() ; 
17     while(ch<0||ch>9) { if(ch==-) f = -1 ; ch = getchar(); } 
18     while(ch>=0&&ch<=9) { x = x * 10+ch-48 ; ch = getchar(); } 
19     return x * f ; 
20 }
21 inline bool cmp(node a,node b) 
22 {
23     return a.sum < b.sum ; 
24 }
25 
26 int main() 
27 {
28     n = read() ; 
29     For(i,1,n) 
30         a[i].w=read(),a[i].s=read(),a[i].sum=a[i].s+a[i].w ; 
31     sort(a+1,a+n+1,cmp) ; 
32     int ans = -inf,tot = 0 ; 
33     For(i,1,n) {
34         ans=max(ans,tot-a[i].s) ; 
35         tot+=a[i].w ; 
36     }
37     printf("%d\n",ans) ; 
38     return 0 ; 
39 }

 

洛谷P1842 奶牛玩杂技

标签:logs   algorithm   +=   tchar   inline   node   比较   奶牛   经典   

原文地址:http://www.cnblogs.com/third2333/p/7625506.html

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