codeforces976C(排序+贪心)

题目链接:http://codeforces.com/contest/976/problem/C
题意:给出一些区间,让你找出使得li>=lj&&ri<=rj的i j,有多组解输出任意一组即可,没有输出-1,-1
思路:按照l小到大排个序,相等r大到小排个序,挨着找就可以了。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+5;
typedef long long LL;
struct node
{
    int l;
    int r;
    int id;
}a[maxn];
bool cmp(node x,node y)
{
    if(x.l==y.l) return x.r>y.r;
    return x.l<y.l;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].l,&a[i].r);
            a[i].id=i;
        }
        sort(a+1,a+n+1,cmp);
        bool flag=false;
        for(int i=1;i<n;i++)
        {
            if(a[i].r>=a[i+1].r)
            {
                flag=true;
                printf("%d %d\n",a[i+1].id,a[i].id);
                break;
            }
        }
        if(!flag) printf("-1 -1\n");
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/dl962454/article/details/80217093