SCUT Training 20170920 Problem J

来源:互联网 发布:桃源网络硬盘登录APP 编辑:程序博客网 时间:2024/06/05 11:16

原题:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1091


思路:

原本我犯懒打算暴力,结果最后一个测试点TLE了。贪心其实是最好的


源代码:

代码是某个大佬的源码,这种红黑树的操作我还不太会用,反正比我的方法好很多

#include <cstdio> #include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 50010;typedef long long ll;struct Seg{    ll l, r;    Seg() {}    Seg(ll t_l,ll t_r):l(t_l),r(t_r){}    bool operator<(const Seg &s) const    {        if(l == s.l)        {            return r > s.r;        }        return l < s.l;    }}segs[maxn];inline ll max(ll a,ll b){    return a>b?a:b;}inline ll min(ll a,ll b){    return a<b?a:b;}inline void swap(ll &a,ll &b) {    ll t=a;    a=b;    b=t;    return;}int main() {    int n;    int ans = 0;    Seg s;    scanf("%d", &n);    for (int i = 1; i <= n; ++i)     {        scanf("%lld %lld",&segs[i].l,&segs[i].r);        if (segs[i].l>segs[i].r)         {            swap(segs[i].l,segs[i].r);        }    }    sort(segs+1,segs+n+1);    s.l=segs[1].l,s.r=segs[1].r;    for (int i=2;i<=n;++i)     {        ans=max(ans,min(s.r,segs[i].r)-max(s.l,segs[i].l));        if(s.r<segs[i].r)         {            s=segs[i];        }    }    cout<<ans<<endl;    return 0;}

原创粉丝点击