貪心:zoj3953 Intervals

Description

Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some intervals so that there does not exist three intervals a, b and c such that a intersects with b, b intersects with c and c intersects with a.node

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

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

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:spa

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

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 ≤ n, li≠ lj or ri ≠ rj.code

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

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.排序

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

Sample Output

4
3 5 7 10

題意:

  給你n個區間的左右端點,讓你取走一些區間,使得任何三個區間都不兩兩相交。three

思路:

  貪心,首先將全部區間以左端點從小到大排序,而後每次判斷當前的三個區間是否兩兩相交。
  若是兩兩相交則刪除右端點值最大的那個區間,由於這樣對後面的影響會最小。
  若是不相交則刪除最左側的區間而後繼續添加區間,每三個判斷一次。

代碼:

#include <bits/stdc++.h> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> #include <iostream> #include <algorithm> #include <string> #include <queue> #include <stack> #include <map> #include <set>

#define IO ios::sync_with_stdio(false);\ cin.tie(0);\ cout.tie(0); const long long INF = 0x3f3f3f3f; const long long mod = 1e9+7; const double PI = acos(-1.0); const double wyth=(sqrt(5)+1)/2.0; const char week[7][10]= {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}; const char month[12][10]= {"Janurary","February","March","April","May","June","July", "August","September","October","November","December" }; const int daym[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} }; const int dir4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; const int dir8[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}; using namespace std; const int maxn = 50005; int ans[maxn],cnt; struct node { int l,r; int id; } a[maxn], tmp[5]; bool cmp1(node a, node b)//按起點從小到大排序
{ if(a.l == b.l) return a.r < b.r; return a.l < b.l; } bool cmp2(node a, node b)//按終點從大到小排列
{ return a.r > b.r; } bool f(node x, node y, node z)//判斷是否相交
{ return y.l <= x.r && z.l <= y.r && z.l <= x.r; } int main() { int T; cin>>T; while(T--) { cnt = 0; int n; cin>>n; for(int i = 1; i <= n; i++) { cin>>a[i].l>>a[i].r; a[i].id = i; } sort(a+1, a+n+1, cmp1); tmp[1] = a[1]; tmp[2] = a[2]; for(int i = 3; i <= n; i++) { tmp[3] = a[i]; sort(tmp+1, tmp+4, cmp1); //若是兩兩相交,記錄下來,而後將最右側的區間交換到tmp[3]
            if(f(tmp[1], tmp[2], tmp[3])) { sort(tmp+1, tmp+4, cmp2); ans[cnt++] = tmp[1].id; swap(tmp[1], tmp[3]); } //若是不相交,那麼將最左側的區間交換到tmp[3];
            else sort(tmp+1, tmp+4, cmp2); } sort(ans, ans+cnt); cout<<cnt<<endl; for(int i = 0; i < cnt; i++) cout<<ans[i]<<" "; cout<<endl; } return 0; }
相關文章
相關標籤/搜索