洛谷P1842 奶牛玩杂技

洛谷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 }

 

转载于:https://www.cnblogs.com/third2333/p/7625506.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值