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

<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 删除最少的区间使得不存在 3个区间两两重合
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x7fffffff;
const int mod = 1e9 + ;
const int maxn = 5e5 + ;
int t, n;
struct node {
int x, y, id;
} p[maxn];
int cmp1(node a, node b) {
if (a.x == b.x) return a.y < b.y;
return a.x < b.x;
}
int cmp2(node a, node b) {
if (a.y == b.y) return a.x < b.x;
return a.y > b.y;
}
int check(node a, node b, node c) {
int cnt1 = (b.x <= a.y);
int cnt2 = (c.x <= b.y && c.x <= a.y);
return (cnt1 && cnt2);
}
int ans[maxn];
int main() {
sf(t);
while(t--) {
sf(n);
for(int i = ; i <= n ; i++) {
sff(p[i].x, p[i].y);
p[i].id = i;
}
sort(p + , p + + n, cmp1);
node x[];
x[] = p[], x[] = p[];
int num = ;
for (int i = ; i <= n ; i++) {
x[] = p[i];
sort(x, x + , cmp1);
int flag = check(x[], x[], x[]);
sort(x, x + , cmp2);
if (flag) {
ans[num++] = x[].id;
swap(x[], x[]);
}
}
printf("%d\n", num);
if (!num) {
printf(" \n");
continue;
}
sort(ans, ans + num);
if (!num) {
printf(" \n");
continue;
}
for (int i = ; i < num ; i++) printf("%d ", ans[i]);
printf("\n");
}
return ;
}

Intervals ZOJ - 3953 (区间贪心)的更多相关文章

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

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

  2. HDU 1936 区间贪心

    /* *区间贪心.前几天刚做了POJ 1328 ...思路完全相同... *最多有100个表情,100行文字.遍历寻找每个表情的所在区间.时间复杂度大约在10^5 ~ 10^6 可以接受. *然后对每 ...

  3. TZOJ 4007 The Siruseri Sports Stadium(区间贪心)

    描述 The bustling town of Siruseri has just one sports stadium. There are a number of schools, college ...

  4. HDU 2037 今年暑假不AC (区间贪心)

    题意:又是中文题... 析:先说一下区间贪心的一个定理,选择不相交的区间:数轴上有n个开区间(ai, bi).选择尽量多的区间,使得这些区间两两不相交,贪心策略是,一定是选bi小的.(想一下为什么). ...

  5. UVA-11134 Fabled Rooks 贪心问题(区间贪心)

    题目链接:https://cn.vjudge.net/problem/UVA-11134 题意 在 n*n 的棋盘上,放上 n 个车(ju).使得这 n 个车互相不攻击,即任意两个车不在同一行.同一列 ...

  6. 【题解】P1712 [NOI2016]区间(贪心+线段树)

    [题解]P1712 [NOI2016]区间(贪心+线段树) 一个observe是,对于一个合法的方案,将其线段长度按照从大到小排序后,他极差的来源是第一个和最后一个.或者说,读入的线段按照长度分类后, ...

  7. 贪心思想之区间贪心 关联洛谷P1803

    力扣上也有一道类似的题 几乎是一样 输出不同 → 力扣leetcode 435. 无重叠区间 区间贪心是比较经典的 就拿洛谷P1803来举例 题目大意 n个比赛 [开始时间,结束时间] 问一个人最多能 ...

  8. ZOJ - 3953 Intervals 【贪心】

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

  9. 扫描线(线段树)+贪心 ZOJ 3953

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5572 Intervals Time Limit: 1 Second       ...

随机推荐

  1. 389. Valid Sudoku【LintCode java】

    Description Determine whether a Sudoku is valid. The Sudoku board could be partially filled, where e ...

  2. lr 常用操作

    lr脚本编写语法: web_add_cookie();:服务器注入cookies lr_save_string("网址或其他","参数2");:一个保存函数,它 ...

  3. kubernetes相关

    1.获取client , api-server 加token 或in-cluster方式 2.所有对象均有list update get 等方法 3.对象属性源码追踪,yaml与源码一一对应 4.一些 ...

  4. 开源自动驾驶仿真平台 AirSim (1) - Unreal Engine

    AirSim 官方Github: https://github.com/Microsoft/AirSim AirSim 是微软的开源自动驾驶仿真平台(其实它还能做很多事情,这里主要用于自动驾驶仿真研究 ...

  5. Matlab 图象操作函数讲解

    h = imrect;pos = getPosition(h); 这个函数用来获取图象上特定区域的坐标,其中pos的返回值中有四个参数[xmin,ymin,width,height],特定区域的左上角 ...

  6. mysql 导入 大sql文件

    任务:第一次用mysql,需要将一个1G左右的sql文件导入: 步骤:1:安装mysql-installer-community-5.7.20.0.msi 64位安装包 2:命令行登录:  mysql ...

  7. 基于angular+bower+glup的webapp

    一:bower介绍 1:全局安装安装bower cnpm i -g bower bower常用指令: bower init //初始化文件 bower install bower uninstall ...

  8. Bacon's Cipher(培根密码)

    Description Bacon's cipher or the Baconian cipher is a method of steganography (a method of hiding a ...

  9. SDUST OJ 时间类的加、减法赋值运算

    Problem F: 时间类的加.减法赋值运算 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 3801  Solved: 2210[Submit][St ...

  10. Swift-闭包理解

    /* 闭包(Closures) * 闭包是自包含的功能代码块,可以在代码中使用或者用来作为参数传值. * 在Swift中的闭包与C.OC中的blocks和其它编程语言(如Python)中的lambda ...