Educational Codeforces Round 29-E-Turn Off The TV(想法题)

E. Turn Off The TV
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Luba needs your help again! Luba has n TV sets. She knows that i-th TV set will be working from moment of time li till moment ri, inclusive.

Luba wants to switch off one of TV sets in order to free the socket. Let's call some TV set redundant if after switching it off the number ofinteger moments of time when at least one of TV sets is working won't decrease. Luba will be very upset if she has to switch off a non-redundant TV set.

Help Luba by telling her the index of some redundant TV set. If there is no any, print -1.

Input

The first line contains one integer number n (1 ≤ n ≤ 2·105) — the number of TV sets.

Then n lines follow, each of them containing two integer numbers li, ri (0 ≤ li ≤ ri ≤ 109) denoting the working time of i-th TV set.

Output

If there is no any redundant TV set, print -1. Otherwise print the index of any redundant TV set (TV sets are indexed from 1 to n).

If there are multiple answers, print any of them.

Examples
input
Copy
3
1 3
4 6
1 7
output
1
input
Copy
2
0 10
0 10
output
1
input
Copy
3
1 2
3 4
6 8
output
-1
input
Copy
3
1 2
2 3
3 4
output
2
Note

Consider the first sample. Initially all integer moments of time such that at least one TV set is working are from the segment [1;7]. It's easy to see that this segment won't change if we switch off the first TV set (or the second one).

Note that in the fourth sample you can switch off the second TV set, since even without it all integer moments such that any of the TV sets is working denote the segment [1;4].

题意:直白的翻译就是给你n个区间,让你输出任意一个多余区间的编号(其中多余区间定义为去掉这个编号的区间,该区间可以被其他区间覆盖)

题解:一开始觉得就是覆盖问题,想着线段树维护区间最小值?但是后来感觉区间长度很大,不知怎么优化,后来看了一个大佬的写法感觉很有意思,我们可以将这n个区间拆分成2n个点,然后从左到右排序,对于当前点所在的区间若必须存在它一定有一个自己存在的时候,我们可以用一个数组或者set存一下,用set的话便于查找,然后从左到右扫一遍,并标记一下必须存在的区间即可。

#include<set>
#include<vector>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define maxn 200005
set<int>s;
vector<pair<int,int> >q;
int n,ans=-1,used[maxn];
int main(void)
{
	int l,r;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&l,&r);
		q.push_back(make_pair(l,i));
		q.push_back(make_pair(r+1,i));
	}
	sort(q.begin(),q.end());
	s.insert(q[0].second);
	for(int i=1;i<q.size();i++)
	{
		if(q[i-1].first!=q[i].first && s.size()==1)
			used[*s.begin()]=1;
		if(s.count(q[i].second))
			s.erase(q[i].second);
		else
			s.insert(q[i].second);
	}
	for(int i=1;i<=n;i++)
		if(used[i]==0)
			ans=i;
	printf("%d\n",ans);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值