无论如何都不应该放弃希望!codeforces976C(暴力排序过)

啊没错这篇博客是在比赛进行中写下的
题意:给n条线段,请找出a和b线段使得a包含b,即a的左顶点小于等于b的左顶点所在位置,a的右顶点大于等于b的右顶点所在位置。若无,输出-1 -1,若有,输出b和a
代码等比赛尘埃落定了再贴

分析:只需要从小到大将左节点排序,左节点大小一样时右节点降序排序,每次保留最大的右节点就能找到了
AC代码:

#include<bits/stdc++.h>
using namespace std;
struct line{
long long l,r;
int id;
}s[300000+7];
bool cmp(line a,line b){
    if(a.l==b.l) return a.r>b.r;
    else return a.l<b.l;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>s[i].l>>s[i].r;
        s[i].id=i;
    }
    sort(s+1,s+n+1,cmp);
    int q,w;
    q=1,w=2;
    while(q!=n){
        if(s[q].r>=s[w].r){
            cout<<s[w].id<<" "<<s[q].id<<endl;
            return 0;
        }
        else{
            q=w;
            w++;
        }
    }
    cout<<"-1 -1"<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhubozhen/article/details/80153486
今日推荐