• [Codeforces 863E]Turn Off The TV


    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
    ", a[i].id);
    43       return;
    44     }
    45     else if (a[i].l <= a[i-1].l) {
    46       printf("%d
    ", a[i-1].id);
    47       return;
    48     }
    49     a[i].l = Max(a[i-1].r+1, a[i].l);
    50   }
    51   printf("-1
    ");
    52 }
    53 int main() {
    54   work();
    55   return 0;
    56 }
  • 相关阅读:
    高程5.4 RegExp类型
    高程5.3 Date类型
    高程5.2.9归并方法
    20151119js上课总结
    从0~100之间随机取出不重复的10个数
    高程5.2.8迭代方法
    HTML常用标签
    20151118小问题
    20151117小问题
    《QT Creator快速入门》
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/7617191.html
Copyright © 2020-2023  润新知