Intervals ZOJ

Chiaki has n intervals and the i-th of them is [liri]. She wants to delete some intervals so that there does not exist three intervals ab and c such that aintersects with bb intersects with c and c intersects with a.

Chiaki is interested in the minimum number of intervals which need to be deleted.

Note that interval a intersects with interval b if there exists a real number x such that la ≤ x ≤ ra and lb ≤ x ≤ rb.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains an integer n (1 ≤ n ≤ 50000) -- the number of intervals.

Each of the following n lines contains two integers li and ri (1 ≤ li < ri ≤ 109) denoting the i-th interval. Note that for every 1 ≤ i < j ≤ nli ≠ lj or ri ≠ rj.

It is guaranteed that the sum of all n does not exceed 500000.

<h4< dd="">Output

For each test case, output an integer m denoting the minimum number of deletions. Then in the next line, output m integers in increasing order denoting the index of the intervals to be deleted. If m equals to 0, you should output an empty line in the second line.

<h4< dd="">Sample Input

1
11
2 5
4 7
3 9
6 11
1 12
10 15
8 17
13 18
16 20
14 21
19 22

<h4< dd="">Sample Output

4
3 5 7 10

删除最少的区间使得不存在 3个区间两两重合

#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define  pi acos(-1.0)
#define  eps 1e-6
#define  fi first
#define  se second
#define  lson l,m,rt<<1
#define  rson m+1,r,rt<<1|1
#define  bug         printf("******
")
#define  mem(a,b)    memset(a,b,sizeof(a))
#define  fuck(x)     cout<<"["<<x<<"]"<<endl
#define  f(a)        a*a
#define  sf(n)       scanf("%d", &n)
#define  sff(a,b)    scanf("%d %d", &a, &b)
#define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define  pf          printf
#define  FRE(i,a,b)  for(i = a; i <= b; i++)
#define  FREE(i,a,b) for(i = a; i >= b; i--)
#define  FRL(i,a,b)  for(i = a; i < b; i++)
#define  FRLL(i,a,b) for(i = a; i > b; i--)
#define  FIN         freopen("DATA.txt","r",stdin)
#define  gcd(a,b)    __gcd(a,b)
#define  lowbit(x)   x&-x
#pragma  comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long  LL;
typedef unsigned long long ULL;
const int INF = 0x7fffffff;
const int mod = 1e9 + 7;
const int maxn = 5e5 + 10;
int t, n;
struct node {
    int x, y, id;
} p[maxn];
int cmp1(node a, node b) {
    if (a.x == b.x) return a.y < b.y;
    return a.x < b.x;
}
int cmp2(node a, node b) {
    if (a.y == b.y) return a.x < b.x;
    return a.y > b.y;
}
int check(node a, node b, node c) {
    int cnt1 = (b.x <= a.y);
    int cnt2 = (c.x <= b.y && c.x <= a.y);
    return (cnt1 && cnt2);
}
int ans[maxn];
int main() {
    sf(t);
    while(t--) {
        sf(n);
        for(int i = 1 ; i <= n ; i++) {
            sff(p[i].x, p[i].y);
            p[i].id = i;
        }
        sort(p + 1, p + 1 + n, cmp1);
        node x[5];
        x[0] = p[1], x[1] = p[2];
        int num = 0;
        for (int i = 3 ; i <= n ; i++) {
            x[2] = p[i];
            sort(x, x + 3, cmp1);
            int flag = check(x[0], x[1], x[2]);
            sort(x, x + 3, cmp2);
            if (flag) {
                ans[num++] = x[0].id;
                swap(x[0], x[2]);
            }
        }
        printf("%d
", num);
        if (!num) {
            printf(" 
");
            continue;
        }
        sort(ans, ans + num);
        if (!num) {
            printf(" 
");
            continue;
        }
        for (int i = 0 ; i < num ; i++) printf("%d ", ans[i]);
        printf("
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/qldabiaoge/p/9544785.html