Rolling boulder - thinking greedy

H "soil" rolling stone

Meaning of the questions: soil ball has a stable value m, when m <= 0 immediately disintegrated. Each barrier has damaging a, b two recovery properties when it hits an obstacle requires a> m, the loss of stability of m = ma, then the stability of recovery after the striker m = m + b. M and given all the obstacles a, b, determines whether there is a disorder of all obstacles can undergo disintegration order of appearance.
Solution: greed. First go bump ba> obstacles 0 hit because these will increase m, m to maximum, and then run into the obstacle ba <0's. ba> 0: Press a small to large, so you can ensure over as many obstacles. ab: This needs a little proof, the conclusion is: according to descending b. A simple proof: If the answer is "yes", the last m must be determined, consider the reverse process, is to restore a stable value of spit stable value plus the loss, reverse (positive process look like?) necessary to ensure that m> 0, and -b + a> 0 (forward and front ba> 0 the same process), it is in accordance with descending b, b is ascending it forward.

#include<bits/stdc++.h>
using namespace std;
struct ac{
    long long a,b,get;
}arr[500010];
bool cmp(ac a1,ac a2){
    if(a1.get>0&&a2.get>0) return a1.a<a2.a;
    else if(a1.get==0&&a2.get==0) return a1.a<a2.a;
    else if(a1.get<0&&a2.get<0) {
            return a1.b>a2.b;
    }
    else
        return a1.get>a2.get;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        long long n,m;
        scanf("%lld%lld",&n,&m);
        for(int i=1;i<=n;++i){
            scanf("%lld%lld",&arr[i].a,&arr[i].b);
            arr[i].get=arr[i].b-arr[i].a;
        }
        sort(arr+1,arr+n+1,cmp);
        int cnt=0;
        int f=1;
        for(int i=1;i<=n;++i){
            if(arr[i].a<m){
                m+=arr[i].get;
            }
            else {
                f=0;
                cout<<"No"<<endl;
                break;
            }
        }
        if(f==0) continue;
        cout<<"Yes"<<endl;
    }
    return 0;
}
Published 96 original articles · won praise 11 · views 2278

Guess you like

Origin blog.csdn.net/weixin_43769146/article/details/103330181