zoj3953 Intervals 最大不重叠区间加强版 zoj排名第一~
Intervals
Time Limit: 1 Second Memory Limit:65536 KB Special Judge
Chiaki has n intervals and thei-th of them is [li,ri]. She wants to delete some intervals so that there does not exist three intervalsa,b andc such thata intersects withb,b intersects withc andc intersects witha.
Chiaki is interested in the minimum number of intervals which need to be deleted.
Note that interval a intersects with intervalb if there exists a real numberx such thatla ≤x ≤ra andlb ≤x ≤rb.
Input
There are multiple test cases. The first line of input contains an integerT, 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 integersli andri (1 ≤li <ri ≤ 109) denoting thei-th interval. Note that for every 1 ≤i <j ≤n,li ≠lj orri ≠rj.
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, outputm integers in increasing order denoting the index of the intervals to be deleted. Ifm 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
Author: LIN, Xi
Source: The 17th Zhejiang University Programming Contest Sponsored by TuSimple
1,根据数据应该猜到是O(N)或者O(Nlgn)的复杂度解决为题。即区间扫描类型。
2,很容易想到贪心模型中的工作选择问题(最大不重叠区间数)。
3,排序后判断,具体有两种方法。
(1)排序加+线段树(或者BIT)(每加入一个区间前只需要判断L到R的最大值是否‘合格’)。
具体的:按照R从小到大排序之后逐个检查,如果L,R最大值不超过2,那么就把这个区间放进去,区间+1,否则不能放进去。
然后,区间线段树区间处理可以用lazy加以标记。当然也可以离散的树状数组,虽然BIT写起来简单一些,不过加离散化也麻烦,所以将就用线段树。)
(2)排序+贪心,思想和最大不重叠区间数类似。
(1)方法比较好想,不想写代码。点击打开链接
(2)时间和代码上上更加优化。
//本人代码,禁止不思考就引用或者提交。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
int N,ans,fin[50030];
struct in{
int L,R,num;
} s[50030]; bool cmp(in a,in b){
return a.R<b.R;
}
void _in()
{
scanf("%d",&N);
for(int i=1;i<=N;i++){
scanf("%d%d",&s[i].L,&s[i].R);
s[i].num=i;
}
}
void _solve()
{
int Fro=0,Now=1,i;
sort(s+1,s+N+1,cmp);
for(i=2;i<=N;i++){
if(s[i].L>s[Now].R) Now=i;
else if(s[i].L>s[Fro].R) {Fro=Now;Now=i;}
else fin[++ans]=s[i].num;
}
printf("%d\n",ans);
sort(fin+1,fin+ans+1);//答案需要排序,我tm找了半天错。。。
for(int j=1;j<=ans;j++)
printf("%d ",fin[j]);
printf("\n");
}
int main()
{
int t,T;
cin>>T;
while(T--){
ans=0;//有计数器,所以不必update
_in();
_solve();
}
return 0;
}
zoj3953 Intervals 最大不重叠区间加强版 zoj排名第一~的更多相关文章
- [LeetCode] Non-overlapping Intervals 非重叠区间
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
- [LeetCode] 435. Non-overlapping Intervals 非重叠区间
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
- Leetcode 435.无重叠区间
无重叠区间 给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] 和 [2,3] 的边界相互"接触" ...
- 【LeetCode】435-无重叠区间
题目描述 给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] 和 [2,3] 的边界相互"接触", ...
- Java实现 LeetCode 435 无重叠区间
435. 无重叠区间 给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] 和 [2,3] 的边界相互"接触& ...
- 力扣leetcode 435. 无重叠区间 - 贪心
非常经典的区间贪心思想 -- 详见博文: 贪心思想之区间贪心 本题给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] ...
- ZOJ 2770 Burn the Linked Camp 差分约束 ZOJ排名第一~
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1770 题目大意: 陆逊为了火烧连营七百里,派出了间谍刺探敌情,得之刘备的军营以 ...
- [Swift]LeetCode435. 无重叠区间 | Non-overlapping Intervals
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
- 435 Non-overlapping Intervals 无重叠区间
给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠.注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] 和 [2,3] 的边界相互“接触”,但没有相互重叠.示例 ...
随机推荐
- copy和mutableCopy都是浅拷贝!!!------你被骗了很多年
所有系统容器类的copy或mutableCopy方法,都是浅拷贝!!! (ps:什么是容器?比如NSArray,NSMutableArray,NSDictionary,NSMutableDiction ...
- Java面试系列之HashMap大扫盲汇总
PS:整理的稍微有点急,不足之处,望各路道友指正,List相关可以查看前一篇随笔! HashMap的工作原理是近年来常见的Java面试题,几乎每个Java程序员都知道HashMap,都知道哪里要用Ha ...
- Java 反射之动态代理
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt205 利用Java反射机制你可以在运行期动态的创建接口的实现.java.la ...
- SQL 软解析和硬解析详解
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt329 当客户端进程,将SQL语句通过监听器发送到Oracle时, 会触发一个 ...
- grunt之filerev、usemin
窃以为这两个插件是比较有用的,filerev是给js.css进行编码重命名,usemin修改html中被重命名的js.css文件的引用.另外说明下之前将concat.cssmin.uglify放在一篇 ...
- 深度剖析Java变量栈&对象堆
Java把内存分成两种,一种叫做栈内存,一种叫做堆内存 在函数中定义的一些基本类型的变量和对象的引用变量都是在函数的栈内存中分配.当在一段代码块中定义一个变量时,java就在栈中为这个变量分配内存空间 ...
- 大家一起来找茬(BUG)
大家一起来找茬(BUG) ----------目录---------- 一.上手体验 1.主界面 2.功能 二.程序的 BUG 三.必应词典的 BUG 1."每日一句"里的句子不能 ...
- 201521123122 《java程序设计》第八周实验总结
201521123122 <java程序设计>第八周实验总结 1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 List中指定元素的删除(题目4- ...
- Java课程设计—象棋
1. 团队名称.团队成员介绍 团队名称:WY 团队成员: 吴慧婷[组长] 201521123094 网络1514 姚佳希 201521123042 网络1512 2 项目git地址 Java课程设计 ...
- Java课程设计——GUI密码生成器201521123035
1.团队课程设计博客链接 (http://www.cnblogs.com/wuling15/p/7061857.html) 2.个人负责模块或任务说明 (1)确定课题并进行任务分工 (2)编写随机数产 ...