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
- ZOJ3953 Intervals 【贪心 双线程活动分配问题】
- zoj3953贪心
- 贪心算法之活动分配问题
- zoj3953 贪心 training 3
- 【贪心】活动安排问题
- 活动选择问题 贪心
- 贪心-活动安排问题
- 活动安排问题 贪心
- 贪心:活动选择问题
- 活动选择问题(贪心)
- 【ZOJ3953 The 17th Zhejiang University Programming Contest F】【贪心 or 费用流】Intervals 删最少线段使得每点最多2次覆盖
- 活动安排问题 贪心算法
- 贪心算法活动选择问题
- 贪心算法---活动选择问题
- 贪心法 活动选择问题
- 活动安排问题(贪心算法)
- 贪心算法-活动选择问题
- 2073 活动选择问题【贪心】
- 2017 携程 笔试编程题 1
- 机器学习的两类参数
- leetcode [Happy Number]//待整理多种解法
- 九度OJ题目1101:计算表达式
- Faster R-cnn和Cudnn5.0冲突解决过程记录
- ZOJ3953 Intervals 【贪心 双线程活动分配问题】
- 用 <button> , <input type="button"> , <input type="submit"> 提交表单的异同
- 当年第一个Java作品,Java坦克大战,写于2016年2月9日,放着给初学者~
- mysql首次登陆随机密码
- Perfect Number
- 减治法之插入排序
- woj-Divide by Six
- 用java代码对ACID的实现
- Rancher体系下容器日志采集