Intervals ZOJ - 3953 (区间贪心)
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 aintersects 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 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 ≤ n, li ≠ 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 (区间贪心)的更多相关文章
- 力扣leetcode 435. 无重叠区间 - 贪心
非常经典的区间贪心思想 -- 详见博文: 贪心思想之区间贪心 本题给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] ...
- HDU 1936 区间贪心
/* *区间贪心.前几天刚做了POJ 1328 ...思路完全相同... *最多有100个表情,100行文字.遍历寻找每个表情的所在区间.时间复杂度大约在10^5 ~ 10^6 可以接受. *然后对每 ...
- TZOJ 4007 The Siruseri Sports Stadium(区间贪心)
描述 The bustling town of Siruseri has just one sports stadium. There are a number of schools, college ...
- HDU 2037 今年暑假不AC (区间贪心)
题意:又是中文题... 析:先说一下区间贪心的一个定理,选择不相交的区间:数轴上有n个开区间(ai, bi).选择尽量多的区间,使得这些区间两两不相交,贪心策略是,一定是选bi小的.(想一下为什么). ...
- UVA-11134 Fabled Rooks 贪心问题(区间贪心)
题目链接:https://cn.vjudge.net/problem/UVA-11134 题意 在 n*n 的棋盘上,放上 n 个车(ju).使得这 n 个车互相不攻击,即任意两个车不在同一行.同一列 ...
- 【题解】P1712 [NOI2016]区间(贪心+线段树)
[题解]P1712 [NOI2016]区间(贪心+线段树) 一个observe是,对于一个合法的方案,将其线段长度按照从大到小排序后,他极差的来源是第一个和最后一个.或者说,读入的线段按照长度分类后, ...
- 贪心思想之区间贪心 关联洛谷P1803
力扣上也有一道类似的题 几乎是一样 输出不同 → 力扣leetcode 435. 无重叠区间 区间贪心是比较经典的 就拿洛谷P1803来举例 题目大意 n个比赛 [开始时间,结束时间] 问一个人最多能 ...
- ZOJ - 3953 Intervals 【贪心】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3953 题意 给出N个区间,求去掉某些区间,使得剩下的区间中,任何 ...
- 扫描线(线段树)+贪心 ZOJ 3953
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5572 Intervals Time Limit: 1 Second ...
随机推荐
- 垃圾收集器与内存分配策略(深入理解Java虚拟机)
3.1 概述 垃圾收集器要解决哪些问题? 哪些内存需要回收 什么时候回收 如何回收 引用计数算法:当有一个地方引用,+1,引用失效,-1. 缺点:对象之间相互循环引用的问题. 可达性分析算法: ...
- [Clr via C#读书笔记]Cp18 定制Attribute
Cp18 定制Attribute 意义 利用Attribute,可以声明性的给自己的代码结构创建注解,从而实现一些特殊的功能:最终在元数据中生成,这种可扩展的元数据信息可以在运行时的时候查询,从而动态 ...
- ThreadLocal 线程的私有内存
话说在<操作系统原理>这门课里面,我们学到了很多概念:进程.线程.锁.PV操作.读写者问题等等,大家还记得么?(估计有些概念早已忘记了吧,哈哈哈~) 其中关于进程.线程和锁的东西是我们平时 ...
- [转载]CENTOS 6.0 iptables 开放端口80 3306 22端口
原文地址:6.0 iptables 开放端口80 3306 22端口">CENTOS 6.0 iptables 开放端口80 3306 22端口作者:云淡风轻 #/sbin/iptab ...
- 【转载】2015Android 面试题 01
1.如何避免ANR? 答:ANR:Application Not Responding,五秒在Android中,活动管理器和窗口管理器这两个系统服务负责监视应用程序的响应. 当出现下列情况时,Andr ...
- Kali信息收集工具-dimtry
帮助文档 -s和-e参数需要用到google搜索 1.获取whois主机ip信息 2.扫描端口,根据banner信息判断服务
- 小程序之web-view打开外部链接
小程序之web-view - 传送门 web-view 组件是一个可以用来承载网页的容器,会自动铺满整个小程序页面.个人类型与海外类型的小程序暂不支持使用. 一:小程序使用web-view打开链接的前 ...
- Python练习—函数
1.编写函数f(n),实现输入n的值,求出n的阶乘.然后调用此函数计算1! +2!+3!+……10!的结果,输出到屏幕上. def f(n): count = 1; for i in range(1, ...
- TCP系列39—拥塞控制—2、拥塞相关算法及基础知识
一.拥塞控制的相关算法 早期的TCP协议只有基于窗口的流控(flow control)机制而没有拥塞控制机制,因而易导致网络拥塞.1988年Jacobson针对TCP在网络拥塞控制方面的不足,提出了& ...
- shell练习题讲解
写一个脚本,计算100以内所有的奇数的和以及所有偶数的和,分别显示出来#! /bin/bashsum1=0for i in `seq 1 2 100`do sum1=$[$sum1+$i]doneec ...