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] 的边界相互“接触”,但没有相互重叠.示例 ...
随机推荐
- 说说 DWRUtil
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp27 说说 DWRUtil 比如我们从服务器端获得了一个citylist的数 ...
- 浅谈java中==与equals的区别
今天做了一个业务模块,需要简单的遍历比较值,所以习惯性的用了 "==" ,但是结果没有达到预想的结果是什么鬼? 看到这里,有人一定会指出这俩货不是基本变量! "关系操作符 ...
- socket和抓包工具wireshark
socket和抓包工具wireshark 最近在学习Python代码中的socket和抓包工具wireshark,故又将socket等概念又学习了一遍,温故而知新: Python代码如下: serve ...
- (2)ES6解构赋值-数组篇
1.解构赋值-数组篇 //Destrcturing(解构) //ES5 /* var a = 1; var b = 2; var c = 3; */ //ES6 var [a,b,c] = [1,2, ...
- 【Alpha】 第七次Daily Scrum Meeting
一.本次会议为第七次meeting会议 二.时间:9:37AM-9:50AM 地点:禹州三楼 三.会议站立式照片 四.今日任务 成员 昨日任务 今日任务 林清青 学习并了解微信程序相关方面知识,为小组 ...
- 201521123075 《Java程序设计》第7周学习总结
1. 本周学习总结 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码为 public boolean contains(Object o) ...
- 201521123104 《JAVA程序设计》第二周学习总结
1. 本周学习总结 认识了JAVA编程中一些类型与变量,了解了一些基本运算符的使用 变量在命名时,不可以使用数字或一些特殊字符作为开头 不可以声明局部变量后未指定任何值给它之前就使用变量 在程序中写下 ...
- 读Zepto源码之Selector模块
Selector 模块是对 Zepto 选择器的扩展,使得 Zepto 选择器也可以支持部分 CSS3 选择器和 eq 等 Zepto 定义的选择器. 在阅读本篇文章之前,最好先阅读<读Zept ...
- java:接口特性 接口与抽象类/普通类的区别
接口 书面定义: Java接口是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为(功能). 在ja ...
- DAOFactory复用代码
工厂设计模式 public class DaoFactory { private static final DaoFactory factory = new DaoFactory(); private ...