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

BZOJ 1629 Usaco Cow Acrobats

时间:2017-09-08 20:39:33      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:algo   bat   blog   log   scanf   int   ret   amp   顺序   

感觉就是一道贪心的题目,但是苦于没法下手。
在瞎写了几组数据之后,猜了一个结论。A1-B1<A2-B2
在看了看题解之后,发现自己好菜啊。

首先由两个前提条件
1. 两头牛调换顺序,并不会影响后面牛的计算。
2. 两头牛的顺序会影响答案.

所以 我们设 A,B为两个相邻的牛
需要满足 Ax-By < Bx-Ay 的关系 才会使答案变小

#include <cstdio>
#include <algorithm>
 
struct node{
    long long  x,y;
}now[50005];
 
int n;
bool CMP(const node &a,const node &b){
    return a.x-b.y<b.x-a.y;
}
 
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%lld%lld",&now[i].x,&now[i].y);
    std::sort(now+1,now+1+n,CMP);
    long long tot = 0,Max=-23333333333;
    for(int i=1;i<=n;i++){
        Max = std::max(Max,tot-now[i].y);
        tot+=now[i].x;
    }   
    printf("%lld\n",Max);
    return 0;
}

 

BZOJ 1629 Usaco Cow Acrobats

标签:algo   bat   blog   log   scanf   int   ret   amp   顺序   

原文地址:http://www.cnblogs.com/OIerLYF/p/7496096.html

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