Description

Chiaki has n intervals and the i-th of them is [liri]. She wants to delete some intervals so that there does not exist three intervals ab and c such that a intersects with bb 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.

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 < j ≤ nli≠ lj or ri ≠ 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, 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 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

题意:

  给你n个区间的左右端点,让你取走一些区间,使得任何三个区间都不两两相交。

思路:

  贪心,首先将所有区间以左端点从小到大排序,然后每次判断当前的三个区间是否两两相交。

  如果两两相交则删除右端点值最大的那个区间,因为这样对后面的影响会最小。
  如果不相交则删除最左侧的区间然后继续添加区间,每三个判断一次。

代码:

#include <bits/stdc++.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set> #define IO ios::sync_with_stdio(false);\
cin.tie();\
cout.tie();
const long long INF = 0x3f3f3f3f;
const long long mod = 1e9+;
const double PI = acos(-1.0);
const double wyth=(sqrt()+)/2.0;
const char week[][]= {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
const char month[][]= {"Janurary","February","March","April","May","June","July",
"August","September","October","November","December"
};
const int daym[][] = {{, , , , , , , , , , , , },
{, , , , , , , , , , , , }
};
const int dir4[][] = {{, }, {, }, {-, }, {, -}};
const int dir8[][] = {{, }, {, }, {-, }, {, -}, {, }, {-, -}, {, -}, {-, }};
using namespace std;
const int maxn = ;
int ans[maxn],cnt;
struct node
{
int l,r;
int id;
} a[maxn], tmp[];
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)//按终点从大到小排列
{
return a.r > b.r;
}
bool f(node x, node y, node z)//判断是否相交
{
return y.l <= x.r && z.l <= y.r && z.l <= x.r;
}
int main()
{
int T;
cin>>T;
while(T--)
{
cnt = ;
int n;
cin>>n;
for(int i = ; i <= n; i++)
{
cin>>a[i].l>>a[i].r;
a[i].id = i;
}
sort(a+, a+n+, cmp1);
tmp[] = a[];
tmp[] = a[];
for(int i = ; i <= n; i++)
{
tmp[] = a[i];
sort(tmp+, tmp+, cmp1);
//如果两两相交,记录下来,然后将最右侧的区间交换到tmp[3]
if(f(tmp[], tmp[], tmp[]))
{
sort(tmp+, tmp+, cmp2);
ans[cnt++] = tmp[].id;
swap(tmp[], tmp[]);
}
//如果不相交,那么将最左侧的区间交换到tmp[3];
else
sort(tmp+, tmp+, cmp2);
}
sort(ans, ans+cnt);
cout<<cnt<<endl;
for(int i = ; i < cnt; i++)
cout<<ans[i]<<" ";
cout<<endl;
}
return ;
}

贪心:zoj3953 Intervals的更多相关文章

  1. zoj3953 Intervals 最大不重叠区间加强版 zoj排名第一~

    Intervals Time Limit: 1 Second      Memory Limit:65536 KB      Special Judge Chiaki has n intervals ...

  2. ZOJ3953 Intervals

    题意 有n个区间,要求删除一些区间使得不存在三个相交的区间.找出删除的最少区间. 分析 是个比较显然的贪心吧. 先按照区间的左起点进行排序,然后从左往右扫,当有三个区间相交的时候,删除那个右端点最远的 ...

  3. ZOJ-3953 Intervals,t

    Intervals 题意:给出n个区间,求最少删除多少个区间使得任意三个区间都不相交. 思路:按区间左端点排序,每次选取r最大的两个与当前比较,如果能放更新r,否则删除r最大的.关键就在怎么删除r最大 ...

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

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

  5. Integer Intervals(贪心)

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

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

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

  7. POJ 1716 Integer Intervals#贪心

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

  8. E - Intervals 贪心

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

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

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

随机推荐

  1. GD库imagecopyresampled()方法详解~

    整理了一下GD库这个缩放,拉伸复制的方法 因为这个函数参数太多了~ imagecopyresampled()   /* //拷贝部分图像并调整大小 bool imagecopyresampled ( ...

  2. 2017ACM暑期多校联合训练 - Team 3 1003 HDU 6058 Kanade's sum (模拟)

    题目链接 Problem Description Give you an array A[1..n]of length n. Let f(l,r,k) be the k-th largest elem ...

  3. window10_使用技巧

    1.系统关机文件 @echo offshutdown -s -t 0 2.终端常用命令 notepad 3.解决浏览器跨域 --disable-web-security --user-data-dir ...

  4. tf.name_scope tf.variable_scope学习

    1. 首先看看比较简单的 tf.name_scope(‘scope_name’). tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理. ''' Signatu ...

  5. Mac 下安装 ruby 环境解决 brew 安装 yarn 问题

    在brew安装yarn提示 ruby的版本过低.在网上搜了一下发现 1. mac下自带的ruby 在 system 目录下 2. 其实可以用brew安装一个ruby brew install ruby ...

  6. 145.Binary Tree Postorder Traversal---二叉树后序非递归遍历

    题目链接 题目大意:后序遍历二叉树. 法一:普通递归,只是这里需要传入一个list来存储遍历结果.代码如下(耗时1ms): public List<Integer> postorderTr ...

  7. 构建基于TCP的应用层通信模型

    各层的关系如下图,表述的是两个应用或CS间通信的过程:   通常使用TCP构建应用时,需要考虑传输层的通信协议,以便应用层能够正确识别消息请求.比如,一个请求的内容很长(如传文件),那肯定要分多次发送 ...

  8. fedroa20 没法开启ntpd服务器

    1现象:ntpd老是没法开启,ntpd -d显示有个进程占用123端口. [root@vd13crmtb01 ~]# systemctl enable ntpd.service         //开 ...

  9. shell视频

    本帖最后由 Shell_HAT 于 2014-04-18 16:51 编辑 尚观全套RHCE视频http://pan.baidu.com/s/1pJvzVR1 马哥网络班-中级视频内容http://p ...

  10. TCP可靠传输和拥塞控制

    1.TCP的可靠传输 tcp的可靠传输主要靠 来自接收方的确认报文 和 超时重传. 发出报文,计时器开始计时,在规定超时时间内未收到确认报文则重新发送. 注意:发送报文都留一个副本,如果收到确认报文就 ...