Chiaki 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 laxra and lbxrb.

Input

There 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 < jn, lilj or rirj.

It is guaranteed that the sum of all n does not exceed 500000.

<h4< dd="">Output

For 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.

<h4< dd="">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

<h4< dd="">Sample Output

4
3 5 7 10
E - Intervals
题目大意:
就是给你n组数据,在这n组数据里,删除一部分,使得任意三个组数据不相交。
题目思路:
先对它开始的位置进行排序,小的在前,然后取三组数据,进行判断是否两两相交,如果是就先要对它进行排序。
这个排序比较重要,是以结束的数据为标准,数据越大排在越前面。
如果两两相交就删去之后排序在最前面的那组数据,否则就更新一个为最后那组数据。
其实比较想清楚就比较简单了,分成两组数据进行处理,第一组以l进行排序,第二组以r进行排序,如果要删除,就删除r最大的
那组数据,再更新一个数据。值得学习的是,这种处理方法,这样子可以很好的处理表示删除的数据。
 
不过这里要注意一下,对是不是两两相交判断的标准,就是先选两个例如x,y,先满足x与y相交,再加入z,z与x,z与y相交
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=50050;
struct node
{
int l,r,id;
}ex[maxn];
int ans[maxn]; bool cmp1(node a,node b)
{
if(a.l==b.l) return a.r<b.r;
return a.l<b.l;
}
bool cmp2(node a,node b)
{
if(a.r==b.r) return a.l<b.l;
return a.r>b.r;
}
int isinterval(node x,node y,node z)
{
int f1=0,f2=0;
if(x.r>=y.l) f1=1;//x&y
if(x.r>=z.l&&y.r>=z.l) f2=1;//z&x,z&y
if(f1&&f2) return 1;
return 0;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,pos=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d",&ex[i].l,&ex[i].r);
ex[i].id=i+1;
}
sort(ex,ex+n,cmp1);
node x[5];
x[0]=ex[0];
x[1]=ex[1];
for(int i=2;i<n;i++)
{
x[2]=ex[i];
// sort(x,x+3,cmp1);
int f=isinterval(x[0],x[1],x[2]);
sort(x,x+3,cmp2);
if(f)
{
ans[pos++]=x[0].id;
swap(x[0],x[2]);
}
}
sort(ans,ans+pos);
printf("%d\n",pos);
if(pos==0) printf("\n");
else
{
for(int i=0;i<pos-1;i++) printf("%d ",ans[i]);
printf("%d\n",ans[pos-1]);
}
}
return 0;
}

  

 
 

E - Intervals 贪心的更多相关文章

  1. POJ 1716 Integer Intervals#贪心

    (- ̄▽ ̄)-* //求一个集合,这个集合与任意一个区间的交集,需至少有两个数字 //贪心过程:按n个区间的最右值从小到大对区间进行排列, //集合首先取第一个区间的最右两个数字, //到第二个区间, ...

  2. {POJ}{动态规划}{题目列表}

    动态规划与贪心相关: {HDU}{4739}{Zhuge Liang's Mines}{压缩DP} 题意:给定20个点坐标,求最多有多少个不相交(点也不相交)的正方形 思路:背包问题,求出所有的正方形 ...

  3. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  4. Integer Intervals(贪心)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12123   Accepted: 5129 Description An i ...

  5. 贪心:zoj3953 Intervals

    Description Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some interv ...

  6. poj 1716 Integer Intervals (差分约束 或 贪心)

    Integer Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12192   Accepted: 514 ...

  7. Intervals ZOJ - 3953 (区间贪心)

    Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some intervals so that ...

  8. POJ 1089 Intervals【合并n个区间/贪心】

    There is given the series of n closed intervals [ai; bi], where i=1,2,...,n. The sum of those interv ...

  9. ZOJ - 3953 Intervals 【贪心】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3953 题意 给出N个区间,求去掉某些区间,使得剩下的区间中,任何 ...

随机推荐

  1. JPA、Hibernate、Spring data jpa之间的关系,终于明白了

    什么么是JPA? 全称Java Persistence API,可以通过注解或者XML描述[对象-关系表]之间的映射关系,并将实体对象持久化到数据库中. 为我们提供了: 1)ORM映射元数据:JPA支 ...

  2. EF(EntityFramework)与mysql使用,序列化问题[System.ObjectDisposedException]

    在EF 中使用mysql关联取数据时,如果当前实体中包含另一个实体的list成员,而这个成员为空的话,在json序列化的时候就会报错: '((System.Data.Entity.DynamicPro ...

  3. [PHP] 算法-字符串的全排列的PHP实现

    输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 思路: 1.利用递归形成 ...

  4. openjudge------ 日期的种类题目

    描述TXT is a vegetable chicken,so 出题什么的完全不会啊! 干脆直接从网络上copy一题下来吧. 小明正在整理一批历史文献.这些历史文献中出现了很多日期.小明知道这些日期都 ...

  5. python中的协程

    目录 协程是啥 协程和线程差异 简单实现协程 greenlet 安装方式 gevent 安装 1. gevent的使用 2. gevent切换执行 3. 给程序打补丁 进程.线程.协程对比 请仔细理解 ...

  6. JavaWeb学习日记----XML基础

    1.XML基础: XML全称为eXtensible Markup Language;即可扩展标记型语言,同HTML一样使用标签来操作.它的可扩展性体现在标签可以由自己定义,可以是中文标签. XML用途 ...

  7. 《JavaScript高级程序设计》笔记:变量、作用域和内存问题(四)

    基本类型和引用类型的值 ECMAScript变量可能包含两种不同数据类型的值:基本类型值和引用类型值.基本类型值指的是简单的数据段,而引用类型的值指那些可能有多个值构成的对象. 动态的属性 var p ...

  8. 2017-11-09 中文代码示例之Vuejs入门教程(一)

    "中文编程"知乎专栏原链 为了检验中文命名在主流框架中的支持程度, 在vuejs官方入门教程第一部分的示例代码中尽量使用了中文命名. 所有演示都在本地测试通过, 源码在这里. 下面 ...

  9. CentOS7 离线安装MySQL

    1.删除原有的mariadb 不然安装报错 rpm -qa|grep mariadb rpm -e --nodeps mariadb-libs 2. 下载RPM安装包 在https://dev.mys ...

  10. ios12怎么投屏电脑 苹果手机怎么投

    Ios12系统发布成功之后,是不是给我们带来更大的惊喜呢.我们只需要利用手机上的屏幕镜像就可以轻松将手机画面投屏至电脑上,那么ios12怎么投屏电脑?下面便是今天所要分享的手机投屏的方法. 使用工具: ...