Codeforces 976C

题意略。

思路:由于题中只要让我们找出嵌套的段就行了,那么我们只需要排序一下就好了。

排序方式:按左端由小到大排,左端一样的时候,右端小的排在前。

如果你担心1会因为2的阻隔而不能嵌套3的话,那么2可以保证嵌套3。

#include<bits/stdc++.h>
#define maxn 300005
using namespace std;

struct edge{
    int l,r,id;
    edge(int a = 0,int b = 0,int c = 0){
        l = a,r = b,id = c;
    }
    bool operator<(const edge& e) const{
        if(l != e.l) return l < e.l;
        return r > e.r;
    }
};

edge store[maxn];
int n;

int main(){
    scanf("%d",&n);
    for(int i = 1;i <= n;++i){
        scanf("%d%d",&store[i].l,&store[i].r);
        store[i].id = i;
    }
    sort(store + 1,store + 1 + n);
    int ans1 = -1,ans2 = -1;
    for(int i = 1;i < n;++i){
        if(store[i].l <= store[i + 1].l && store[i].r >= store[i + 1].r){
            ans1 = store[i + 1].id,ans2 = store[i].id;
            break; 
        }
    }
    printf("%d %d\n",ans1,ans2);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/tiberius/p/9032072.html