题意:给定几组区间 [l, r],让你找到一对区间为包含关系,若有多组随意输出一组,若无则输出-1, -1
算法:排序
题解:使用结构体储存左边界l,右边界r,序号idx。按照 l递增,r递减的顺序排序。分析可知只要前面的r比后面的大,则前面的区间一定包含后面的区间,使用结构体 t存储遍历过的 r最大的区间。
代码:时间复杂度O(n)
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <iterator>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <deque>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define endl "\n"
#define IOS ios::sync_with_stdio(0);
#define debug(x) cout<<"**"<<x<<"**"<<endl;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int mod = 998244353;
const int N = 3e5 + 100;
int n;
struct node
{
int l, r, idx;
bool operator < (const node &b) const
{
if(l != b.l) return l < b.l;
return r > b.r;
}
}s[N];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
#endif
IOS;
cin>>n;
for(int i = 1; i <= n; i++)
{
cin>>s[i].l>>s[i].r;
s[i].idx = i;
}
sort(s+1, s+n+1);
node t = s[1];
for(int i = 2; i <= n; i++)
{
if(t.r >= s[i].r)
{
cout<<s[i].idx<<" "<<t.idx;
return 0;
}
else t = s[i];
}
cout<<-1<<" "<<-1;
return 0;
}