1. 程式人生 > >BZOJ 1629 Usaco Cow Acrobats

BZOJ 1629 Usaco Cow Acrobats

col str sort 一道 const 題解 bsp truct algo

感覺就是一道貪心的題目,但是苦於沒法下手。
在瞎寫了幾組數據之後,猜了一個結論。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