CF 527D

题目:

题目给出公式 xi-xj>=wi+wj -> xi-wi>=xj+wj 

现在有n<200000的线段,对每条线段输入两个数字a,b,线段左端是xi-wi,右端是xj+wj,求最多不重复覆盖的线段数量。

题解:

贪心裸题(我dp+线段树优化了很久最后放弃了还是贪心吧)

#include<iostream>
#include<algorithm>
using namespace std;

struct S{
    int lef,rig;
}s[201000];

bool cmp(S a,S b){
    return a.rig<b.rig;
}

int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        int a,b;
        cin>>a>>b;
        s[i].rig=a+b;
        s[i].lef=a-b;
    }
    sort(s,s+n,cmp);
    int ans=0;
    int maxx=-1e9-1;
    for(int i=0;i<n;i++){
        if(s[i].lef>=maxx){
            maxx=s[i].rig;
            ans++;
        }
    }
    cout<<ans<<endl;
}

猜你喜欢

转载自blog.csdn.net/mengwuyaaa/article/details/80343065