Codeforces Beta Round #98 (Div. 2) C题

题目

https://codeforces.com/contest/137/problem/C

问题重述

找到被覆盖的区间一共有多少个
\([3,4]\)\([1,5]\)覆盖但是不被\([3,5]\)覆盖

思路

先按第一个下标从小到大排序
双指针。后面的被前面的覆盖就加加答案,不然更新覆盖区间

代码

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include<vector>
     
    using namespace std;
    int n;
     
    int main()
    {
        cin >> n;
        vector<pair<int, int>>a(n);
        for (int i = 0; i < n; i ++ ) scanf("%d %d", &a[i].first, &a[i].second);
        sort(a.begin(), a.end());
        
        int ans = 0;
        int l = a[0].first, r = a[0].second;
        for(int i = 1; i < n; ++ i)
        {
            if(a[i].first > l && a[i].second < r) ans ++;
            else
            {
                l = a[i].first, r = a[i].second;
            }
        }
        cout << ans;
        return 0;
    }

通过链接
https://codeforces.com/contest/137/submission/137569901

posted @ 2021-12-01 13:04  birds_fly  阅读(45)  评论(0)    收藏  举报