[codeforces 1287E] Delete a Segment 贪心

[codeforces 1287E] Delete a Segment 贪心

总目录详见https://blog.csdn.net/mrcrack/article/details/103564004

在线测评地址https://codeforces.com/contest/1285/problem/E

思路同https://www.cnblogs.com/Lanly/p/12181097.html

    #include <bits/stdc++.h>
    #define MIN(a,b) ((((a)<(b)?(a):(b))))
    #define MAX(a,b) ((((a)>(b)?(a):(b))))
    #define ABS(a) ((((a)>0?(a):-(a))))
    using namespace std;
    typedef long long LL;
    typedef vector<int> VI;
    typedef pair<int,int> PII;
    typedef vector<PII> VPII;
    typedef vector<LL> VL;
    typedef pair<LL,LL> PLL;
    typedef vector<PLL> VPLL;
     
    template <typename T>
    void read(T &x) {
        int s = 0, c = getchar();
        x = 0;
        while (isspace(c)) c = getchar();
        if (c == 45) s = 1, c = getchar();
        while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
        if (s) x = -x;
    }
     
    template <typename T>
    void write(T x, char c = ' ') {
        int b[40], l = 0;
        if (x < 0) putchar(45), x = -x;
        while (x > 0) b[l++] = x % 10, x /= 10;
        if (!l) putchar(48);
        while (l) putchar(b[--l] | 48);
        putchar(c);
    }
     
    int main(void) {
        //ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        int kase; read(kase);
        for (int i = 1; i <= kase; i++) {
            //printf("Case #%d: ", i);
            int n;
            vector<pair<int,int>> a;
            read(n);
            for(int l,r,i=0;i<n;++i){
                read(l);
                read(r);
                a.push_back(make_pair(l,r));
            }
            sort(a.begin(),a.end(),less<pair<int,int>>());
            int r=a[0].second,ans=0,ma=-2147483644,cnt=0,gap=0;
            for(int i=1;i<n;++i){
                if (a[i].first>r){//遇到间隙
                    ++gap;
                    r=a[i].second;
                    ma=-2147483644;
                    ans=max(ans,cnt);
                    cnt=0;
                }else if (ma==-2147483644){//间隙之后
                    ma=min(r,a[i].second);
                    r=max(r,a[i].second);
                }else{
                    if (a[i].first>ma) ++cnt;
                    if (a[i].second>=r){
                        ans=max(ans,cnt);
                        cnt=0;
                    }
                    ma=max(ma,min(r,a[i].second));
                    r=max(r,a[i].second);
                }
            }
            ans=max(ans,cnt);
            ans=ans+gap+1;
            if (gap+1==n) ans=n-1;
            printf("%d\n",ans);
        }
        return 0;
    }
发布了475 篇原创文章 · 获赞 509 · 访问量 42万+

猜你喜欢

转载自blog.csdn.net/mrcrack/article/details/103952067