ZOJ3953 Intervals 【贪心 双线程活动分配问题】

来源:互联网 发布:linux服务器绑定域名 编辑:程序博客网 时间:2024/05/18 11:37
IntervalsTime Limit: 1 Second      Memory Limit: 65536 KB      Special JudgeChiaki 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 la ≤ x ≤ ra and lb ≤ x ≤ rb.InputThere 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 ≤ n, li ≠ lj or ri ≠ rj.It is guaranteed that the sum of all n does not exceed 500000.OutputFor 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 Input1112 54 73 96 111 1210 158 1713 1816 2014 2119 22Sample Output43 5 7 10Author: LIN, XiSource: The 17th Zhejiang University Programming Contest Sponsored by TuSimple

明显,问题就是求:删除至少多少个区间,使得不存在任意一点x被>=3个区间覆盖
先简化一下问题
假如问题改为,不存在x被>=2个区间覆盖
那直接贪心,求一次活动分配问题就行了,标记哪个区间被用掉了,没被用的全部是被删掉的

在已经按以上的方法 ,选择了一部分区间后
显然 ,再选择任意一个还没被选择的区间,必然会让某一点x,被2个区间覆盖
那再对剩余的区间求一次活动分配
这样 如果再选择剩余的任意区间,都会使得某一点x被3个区间覆盖
然而 如果遇到
[1,2] [1,3] [4,100] [3,101] 这样的情况
经典的活动分配问题会选出
[1,2] [4,100]
[1,3]
最后删掉[3,101]

而显然
[1,2] [3,101]
[1,3] [4,100]更优

那就修改一下算法
同时进行2个线程的活动分配
假如线程1的最晚活动结束时间为t1
线程2的最晚结束时间为t2

对一个待选择的活动,优先添加到t更大的线程后面(前提是该活动开始时间>t)

#include<iostream>#include<cstdlib>#include<cstdio>#include<string>#include<vector>#include<deque>#include<queue>#include<algorithm>#include<set>#include<map>#include<stack>#include<ctime>#include<string.h>#include<math.h>#include<list>using namespace std;#define ll long long#define pii pair<int,int>const int inf = 1e9 + 7;const int N = 50000 + 5;struct S{    int l,r;    int idx;}a[N];bool used[N];bool cmp(const S&a,const S&b){    if(a.r!=b.r){        return a.r<b.r;//结束时间升序    }    return a.l>b.l;//开始时间递减序}vector<int>ans;void slove(int n){    sort(a,a+n,cmp);    fill(used,used+n+1,0);    int end1=0,end2=0;//线程1,2的最晚结束时间    for(int i=0;i<n;++i){        if(used[i]==0){            int*enda=&end1;            int*endb=&end2;            if(end2>end1){                swap(enda,endb);            }            if(*enda<a[i].l){                *enda=a[i].r;                used[i]=1;            }            else{                if(*endb<a[i].l){                    *endb=a[i].r;                    used[i]=1;                }            }        }    }    ans.clear();    for(int i=0;i<n;++i){//被删除的点        if(used[i]==0){            ans.push_back(a[i].idx);        }    }    sort(ans.begin(),ans.end());    printf("%d\n",ans.size());    if(ans.empty()){        putchar('\n');    }    else{        printf("%d",ans[0]);        for(int i=1;i<ans.size();++i){            printf(" %d",ans[i]);        }        putchar('\n');    }}int main(){    //freopen("/home/lu/Documents/r.txt","r",stdin);    //freopen("/home/lu/Documents/w.txt","w",stdout);    int T;    scanf("%d",&T);    while(T--){        int n;        scanf("%d",&n);        for(int i=0;i<n;++i){            scanf("%d%d",&a[i].l,&a[i].r);            a[i].idx=i+1;        }        slove(n);    }    return 0;}
1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孩子一年级数学学不好怎么办 小学一年级数学学不好怎么办 孩子一年级数学成绩不好怎么办 成绩差的孩子该怎么办 面对成绩差的孩子该怎么办 六年级数学成绩差该怎么办 孩子数学物理成绩差该怎么办 高三成绩很差该怎么办 孩子学习成绩差家长该怎么办 孩子一年级语文成绩不好怎么办 孩子上网成瘾不听父母话怎么办 10岁数学不开窍怎么办 孩子五年级数学不好怎么办 一岁宝宝难断奶怎么办 2岁宝宝断不了奶怎么办 快2岁宝宝不听话怎么办 2岁半的宝宝不听话怎么办 3岁宝宝哭闹不止怎么办 2岁宝宝爱打人怎么办 两周岁宝宝吃东西就吐怎么办 两周岁宝宝不爱吃饭怎么办 两周岁宝宝反复发烧怎么办 两周岁宝宝咳嗽厉害怎么办 2岁宝宝体内有火怎么办 4岁宝宝数都不会怎么办 两岁宝宝太撅怎么办 儿童晚上发烧白天不发烧怎么办 宝宝晚上睡觉认人怎么办 两岁宝宝尿裤子怎么办 分手了想和好怎么办说 2岁半宝宝胆小怎么办 1岁半宝宝胆小怎么办 分手了还是想他怎么办 两岁宝宝夜惊怎么办 孩子误吃了牙膏怎么办 孩子跳舞脸上的妆卸不掉怎么办 4周岁还不会说话怎么办 宝贝2岁多还不会说话怎么办 孩子20个月离婚怎么办 两个月宝宝闹夜怎么办 两个月宝宝闹瞌睡怎么办