[Explanations] P1842 cows acrobatic play

P1842 cows acrobatic play

SOL:

Constructed adjacent two cows, see if I can greedy, let me see what conditions need to be met when they could greedy.
And an adjoining two cows a, b, to discuss what the conditions are met in the case of a b placed on top of a better place than b above:

Located on a, b above the total weight of bovine W

i: a b placed above
a flattening values: W-Sa
flattened value of b: W + Wa-Sb

ii: b on a top
a flattening values: W is Wb-Sa +
B flattening values: W-Sb

For a ratio b in b above in a more preferred above,
the max (W-Sa, W + Wa-Sb,) <max (W + Wb-Sa, W-Sb);
easy to prove that W-Sa < W + Wb-Sa, W- Sb <W + Wa-Sb;

那就转化NaruRyoshi Tasu Wa-W Sb <W Tasu Wb-Sa;
-> Wa Tasu Sa <Wb Tasu Sb

In summary, when this condition is satisfied, a ratio b in b above in a more preferred above;

So we have to Wi + Si is a keyword from small to large, (this time has established the position of each cow), then we use the prefix from top to bottom and sweep over the minimum squash value thousand million.

int n,m;
const int N=5e4+10;
struct nainiu{
    int s,w;
    bool operator < (const nainiu &rhs)const{
        return s+w<rhs.s+rhs.w; //贪心 
    }
}cow[N];

#undef int
int main(){
#define int long long
    #ifdef WIN32
    freopen("test.txt","r",stdin);
    #endif
    rd(n);
    rep(i,1,n){
        rd(cow[i].w),rd(cow[i].s);
    }
    sort(cow+1,cow+n+1);
    int sum=0,ans=INT_MIN;
    rep(i,1,n){
        ans=max(ans,sum-cow[i].s);
        sum+=cow[i].w;
    }
    printf("%d",ans);
    return 0; 
}

Guess you like

Origin www.cnblogs.com/sjsjsj-minus-Si/p/11655917.html