[Codeforces 863E]Turn Off The TV

本文介绍了一种通过排序和比较相邻区间的方法来寻找可以安全移除的冗余电视播放区间的算法。该方法避免了使用复杂的数据结构,如线段树,并通过简单的逻辑解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

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 of integer 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.

Sample Input

3
1 3
4 6
1 7

Sample Output

1

题解

给的标签又是数据结构...本来想打一个线段树...发现值域太大...

我们考虑这样一个问题:什么情况下才会导致一个区间被另外的区间完全覆盖呢?

一定只有两种情况:一种是一个区间只被一个完整的区间完全包括,另一种是被两个区间拼在一起包括。

我们按左端点为主关键词,右端点为次关键词,将所有区间排序。

那么发现以上的两种情况只会变成:

1.第$i$个区间被第$i-1$个区间完全包括;

2.第$i$个区间被第$i-1$个和第$i+1$个区间包括。

第一种情况很好考虑,只要按顺序遍历,找到一个$a[i].r<=a[i-1].r$的就是满足条件的。

我们看第二种情况,假设我们在考虑第$i$个区间时判断$i-1$满不满足,如果$i-2$的右端点$+1$大于$i$的左端点,那么$i-1$就可以被覆盖。

 1 //It is made by Awson on 2017.10.1
 2 #include <set>
 3 #include <map>
 4 #include <cmath>
 5 #include <ctime>
 6 #include <queue>
 7 #include <stack>
 8 #include <vector>
 9 #include <cstdio>
10 #include <string>
11 #include <cstdlib>
12 #include <cstring>
13 #include <iostream>
14 #include <algorithm>
15 #define LL long long
16 #define Min(a, b) ((a) < (b) ? (a) : (b))
17 #define Max(a, b) ((a) > (b) ? (a) : (b))
18 using namespace std;
19 const int N = 2e5;
20 void read(int &x) {
21   char ch; bool flag = 0;
22   for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
23   for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
24   x *= 1-2*flag;
25 }
26 
27 int n;
28 struct tt {
29   int l, r, id;
30   bool operator < (const tt &b) const{
31     return l == b.l ? r < b.r : l < b.l;
32   }
33 }a[N+5];
34 
35 void work() {
36   read(n);
37   for (int i = 1; i <= n; i++)
38     read(a[i].l), read(a[i].r), a[i].id = i;
39   sort(a+1, a+1+n);
40   for (int i = 2; i <= n; i++) {
41     if (a[i].r <= a[i-1].r) {
42       printf("%d\n", a[i].id);
43       return;
44     }
45     else if (a[i].l <= a[i-1].l) {
46       printf("%d\n", a[i-1].id);
47       return;
48     }
49     a[i].l = Max(a[i-1].r+1, a[i].l);
50   }
51   printf("-1\n");
52 }
53 int main() {
54   work();
55   return 0;
56 }

 

转载于:https://www.cnblogs.com/NaVi-Awson/p/7617191.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值