贪心:zoj3953 Intervals
Description
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 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.
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的更多相关文章
- zoj3953 Intervals 最大不重叠区间加强版 zoj排名第一~
Intervals Time Limit: 1 Second Memory Limit:65536 KB Special Judge Chiaki has n intervals ...
- ZOJ3953 Intervals
题意 有n个区间,要求删除一些区间使得不存在三个相交的区间.找出删除的最少区间. 分析 是个比较显然的贪心吧. 先按照区间的左起点进行排序,然后从左往右扫,当有三个区间相交的时候,删除那个右端点最远的 ...
- ZOJ-3953 Intervals,t
Intervals 题意:给出n个区间,求最少删除多少个区间使得任意三个区间都不相交. 思路:按区间左端点排序,每次选取r最大的两个与当前比较,如果能放更新r,否则删除r最大的.关键就在怎么删除r最大 ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- Integer Intervals(贪心)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12123 Accepted: 5129 Description An i ...
- poj 1716 Integer Intervals (差分约束 或 贪心)
Integer Intervals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12192 Accepted: 514 ...
- POJ 1716 Integer Intervals#贪心
(- ̄▽ ̄)-* //求一个集合,这个集合与任意一个区间的交集,需至少有两个数字 //贪心过程:按n个区间的最右值从小到大对区间进行排列, //集合首先取第一个区间的最右两个数字, //到第二个区间, ...
- E - Intervals 贪心
Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some intervals so that ...
- Intervals ZOJ - 3953 (区间贪心)
Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some intervals so that ...
随机推荐
- python学习笔记(十一)之序列
之前学习的列表,元组,字符串都是序列类型,有很多共同特点: 通过索引得到每一个元素,索引从0开始 通过分片的方法得到一个范围的元素的集合 很多通用的操作符(重复操作符,拼接操作符,成员关系操作符) 序 ...
- 避免无用的渲染绘制(Avoiding Unnecessary Paints)
本文翻译自html5rock上的文章,文章英文原版地址在最后给出. 文中的Paints我翻译成渲染绘制,我自己是这么理解. 开始 绘制(渲染)一个网站或者一个应用的元素对浏览器来说开销是很大的,它会对 ...
- 安装node-sass的正确姿势【转】
安装 node-sass 的时候总是会各种不成功,今天我琢磨了一会儿总算知道要怎么解决了. 首先要知道的是,安装 node-sass 时在 node scripts/install 阶段会从 gith ...
- 【IDEA】IDEA中配置tomcat虚拟路径的两种方法
首先要确保使用的是本地的tomcat服务器,而不是maven插件. -------------------------第一种:使用IDEA工具自动配置(推荐这种)------------------- ...
- flask插件系列之flask_session会话机制
flask_session是flask框架实现session功能的一个插件,用来替代flask自带的session实现机制. 配置参数详解 SESSION_COOKIE_NAME 设置返回给客户端的c ...
- caffe Python API 之Model训练
# 训练设置 # 使用GPU caffe.set_device(gpu_id) # 若不设置,默认为0 caffe.set_mode_gpu() # 使用CPU caffe.set_mode_cpu( ...
- Android仿新浪新闻SlidingMenu界面的实现 .
先看看原图: 如图所示,这种侧滑效果以另一种方式替代了原先tab导航的那种用户体验方式 给人耳目一新的感觉,现已被广大知名应用所效仿,如新浪新闻,网易新闻,人人网等 那么这种效果该如何实现呢?那就需要 ...
- tp 框架 利用反射实现对象调用方法
<?php class Person{ public $name="xiaoming"; function say(){ echo "i am ".$th ...
- oracle只要第一条数据SQL
select * from ( select * from COMMON_BIZREL_WF where sponsor is not null order by serialid ) where r ...
- TreeSet基本用法
TreeSet的基础方法: public class TreeSetTest { public static void main(String[] args) { TreeSet nums = new ...