bibibi724

波澜不起,宠辱不惊

Intervals ZOJ - 3953(贪心就完事了)【浙大17th校赛】

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.

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 laxra and lbxrb.


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 < jn, lilj or rirj.

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

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

Code:

#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
const int maxn=100000+5;
struct line{
    int l,r;
    int count;
    int number;
    line(){}
    line(int l,int r):l(l),r(r){}
}L[maxn];
int isCol(line a,line b){
    return b.l<=a.r;
}
bool cmp(line a, line b)
{
    if (a.l != b.l) return a.l < b.l;
    else return a.r < b.r;
}
bool cmp2(line a, line b)
{
    if (a.r != b.r) return a.r > b.r;
    else return a.l < b.l;
}
int main(){
    int t,n;
    line temp1,temp2;
    cin>>t;
    while(t--){
        //memset(del, 0, sizeof(del));
        set<int > cnt;
        cin>>n;
        for(int i=1;i<=n;i++){
            scanf("%d %d",&L[i].l,&L[i].r);
            L[i].number=i;
        }
        sort(L+1,L+n+1,cmp);
        if(n<3){
            printf("0\n\n");
        }
        else{
            int ans=0;
            int cnt[maxn];
            line x[4];
            x[0]=L[1];x[1]=L[2];
            for(int i=3;i<=n;i++){
                
                x[2]=L[i];//printf("%d %d %d\n",x[0].number,x[1].number,x[2].number);
                sort(x,x+3,cmp);
                int f=(isCol(x[0], x[1]) && isCol(x[1], x[2]) && isCol(x[0], x[2]));
                sort(x,x+3,cmp2);
                if(f){
                    cnt[ans++]=x[0].number;
                    swap(x[0],x[2]);
                }
            }
            cout<<ans<<endl;
            sort(cnt,cnt+ans);
            for(int i=0;i<ans;i++){
                printf("%d",cnt[i]);
                if(i<ans-1) printf(" ");
            }
            if(ans==0) cout<<endl;
            cout<<endl;
        }
    }
    return 0;
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39665840/article/details/79952944
个人分类: ACM 贪心
上一篇Partitioning by Palindromes UVA - 11584(最小回文串分解DP)
下一篇51Nod 1013 3的幂的和 经典模板题+思路详细讲解【等比数列+快速幂+逆元】
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭