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 thatlaxra andlbxrb.

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

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排名第一~的更多相关文章

  1. [LeetCode] Non-overlapping Intervals 非重叠区间

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

  2. [LeetCode] 435. Non-overlapping Intervals 非重叠区间

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

  3. Leetcode 435.无重叠区间

    无重叠区间 给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] 和 [2,3] 的边界相互"接触" ...

  4. 【LeetCode】435-无重叠区间

    题目描述 给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] 和 [2,3] 的边界相互"接触", ...

  5. Java实现 LeetCode 435 无重叠区间

    435. 无重叠区间 给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] 和 [2,3] 的边界相互"接触& ...

  6. 力扣leetcode 435. 无重叠区间 - 贪心

    非常经典的区间贪心思想 -- 详见博文: 贪心思想之区间贪心 本题给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] ...

  7. ZOJ 2770 Burn the Linked Camp 差分约束 ZOJ排名第一~

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1770 题目大意: 陆逊为了火烧连营七百里,派出了间谍刺探敌情,得之刘备的军营以 ...

  8. [Swift]LeetCode435. 无重叠区间 | Non-overlapping Intervals

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

  9. 435 Non-overlapping Intervals 无重叠区间

    给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠.注意:    可以认为区间的终点总是大于它的起点.    区间 [1,2] 和 [2,3] 的边界相互“接触”,但没有相互重叠.示例 ...

随机推荐

  1. Stylus-NodeJS下构建更富表现力/动态/健壮的CSS

    --------------------------本文来自张鑫旭大神博客------------------------------ 一.为什么我会讲Stylus,而不是SASS和LESS? SAS ...

  2. SourceTree使用方法介绍

    SourceTree比命令行更容易操作,能更直观看到发生了什么.但是没有哪一家git图形化软件能完成git的所有操作,封装后的使用也隐藏了git的一些细节,在图形化工具出现一些非常罕见的情况时,还是需 ...

  3. MIT6.828课程JOS在macOS下的环境配置

    本文将介绍如何在macOS下配置MIT6.828 JOS实验的环境. 写JOS之前,在网上搜寻JOS的开发环境,很多博客和文章都提到"不是32位linux就不好配置,会浪费大量时间在配置环境 ...

  4. Android学习记录:线程

    在Java中,线程的建立方法如下. 新建一个类,接口Runnable,重载 run方法 import javax.swing.JTextField; public class test impleme ...

  5. 转: 【Java并发编程】之五:volatile变量修饰符—意料之外的问题(含代码)

    转载请注明出处:     volatile用处说明     在JDK1.2之前,Java的内存模型实现总是从主存(即共享内存)读取变量,是不需要进行特别的注意的.而随着JVM的成熟和优化,现在在多线程 ...

  6. 团队作业7——Alpha冲刺之事后诸葛亮

    Deadline: 2017-5-15 22:00PM,以博客发表日期为准 评分基准: 按时交 - 有分,检查的项目内容为事后诸葛亮分析报告 晚交 - 0分 迟交一周以上 - 倒扣本次作业分数 抄袭 ...

  7. C++学习笔记——STL(标准模板库)

    1.首先.需要学习C++ 模板的概念 2.C++ STL(标准模板库)是一套功能强大的 C++ 模板类,提供了通用的模板类和函数,这些模板类和函数可以实现多种流行和常用的算法和数据结构,如向量.链表. ...

  8. 201521123017 《Java程序设计》第11周学习总结

    1. 本周学习总结 2. 书面作业 Q1.互斥访问与同步访问 完成题集4-4(互斥访问)与4-5(同步访问) 1.1 除了使用synchronized修饰方法实现互斥同步访问,还有什么办法实现互斥同步 ...

  9. 201521123071 《JAVA程序设计》第十周学习总结

    第十周-异常与多线程 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业:本次PTA作业题集异常.多线程 1. finally:题目4-2 1. ...

  10. 201521123121 《Java程序设计》第11周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 进程:每个进程都有独立的代码和数据空间,进程间的切换会有较大的开销,一个进程包含1--n个线程. 线程:同一类线程 ...