1. 程式人生 > >洛谷P1842 奶牛玩雜技

洛谷P1842 奶牛玩雜技

cmp 經典 += fine truct inf cst tchar style

洛谷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 奶牛玩雜技