CF-976 C. Nested Segments

题意:给定几组区间 [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;
}


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值