贪心: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 ...
随机推荐
- 【Atcoder】ARC088 D - Wide Flip
[题目]D - Wide Flip [题意]给定n个数字的01序列,要求每次翻转>=k个数字使得全0,求最大的k.n<=10^5 [算法]数学 [题解]有两个角度可以得到等价的结论: 1. ...
- js常见易错点
原文:http://www.jianshu.com/p/1c77853d4f01 前言 本文是我学习JavaScript过程中收集与整理的一些易错知识点,将分别从变量作用域,类型比较,this指向,函 ...
- c语言学习笔记.指针.
指针: 一个变量,其值为另一个变量的地址,即,内存位置的直接地址. 声明: int *ptr; /* 一个整型的指针,指针指向的类型是整型 */ double *ptr; /* 一个 double 型 ...
- hdu 5326 Work(杭电多校赛第三场)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5326 Work Time Limit: 2000/1000 MS (Java/Others) M ...
- py,pyc,pyw文件的区别和使用
熟悉python编程的都知道,python三种最常见的py文件格式,.py,.pyc,.pyw,下面说一说它们各自的使用. py文件 python最常见的文件,是python项目的源码: 文件执行时l ...
- defer用途
package main /* defer :程序退出时执行,先进后执行 defer庸碌: 1.关闭文件句柄 2.锁资源释放 3.数据库连接释放 */ import ( "fmt" ...
- 64_s2
sipwitch-1.9.15-3.fc26.x86_64.rpm 13-Feb-2017 09:19 162822 sipwitch-cgi-1.9.15-3.fc26.x86_64.rpm 13- ...
- spring中的任务调度Quartz
Spring 整合 Quartz 任务调度 主要有两种方式. Quartz的官网:http://www.quartz-scheduler.org/ 这两种只是一些配置文件简单配置就OK了,但是根本无法 ...
- spring data jpa条件分组查询及分页
原book对象 package com.shaying.domain; import javax.persistence.Column; import javax.persistence.Entity ...
- 利用Google API生成二维码
什么是二维码:二维码是二维条形码的一种,可以将网址.文字.照片等信息通过相应的编码算法编译成为一个方块形条码图案,手机用户可以通过摄像头和解码软件将相关信息重新解码并查看内容.读取方式:利用30万画素 ...