ZOJ3953 Intervals
题意
有n个区间,要求删除一些区间使得不存在三个相交的区间。找出删除的最少区间。
分析
是个比较显然的贪心吧。
先按照区间的左起点进行排序,然后从左往右扫,当有三个区间相交的时候,删除那个右端点最远的那个区间。
这个想法显然是没错的,但是问题是n最大是50000,我们该怎么在时间复杂度以内边扫边找相交区间呢?
我们可以在从左到右扫的时候维护一个vector,里面是到目前为止,右端点最远的三个区间。我们判断相交是要从这里面判断就可以。当这里面的三个区间相交的时候,根据上面的规则删除,当不想交的时候,把新区间加进来,去掉右端点最近的一个(但是并不是删除,只是从这个vector中删除而已)。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <iostream>
#include <cmath> using namespace std;
typedef long long LL;
typedef unsigned long long ull;
const int INF=;
const int maxn=+; struct Node{
int l,r,id;
bool vis;
bool operator <(const Node &rhs)const{
return l<rhs.l;
}
}node[maxn];
int T,n;
int main(){
scanf("%d",&T);
for(int t=;t<=T;t++){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d",&node[i].l,&node[i].r);
node[i].id=i;
node[i].vis=;
}
sort(node+,node++n);
vector<Node>V;
int num;
vector<int>ans; for(int i=;i<=n;i++){
//cout<<node[i].l<<" "<<node[i].r<<endl;
V.push_back(node[i]);
if(V.size()<)continue;
sort(V.begin(),V.end()); if(V[].r>=V[].l&&V[].l<=V[].r&&V[].l<=V[].r){
int M=-;
M=max(M,max(max(V[].r,V[].r),V[].r));
vector<Node>::iterator it;
for( it=V.begin();it!=V.end();it++){
if(it->r==M){
ans.push_back(it->id);
V.erase(it);
break;
}
}
}
if(V.size()>=){
int M=INF;
for(int i=;i<;i++){
M=min(M,V[i].r);
}
vector<Node>::iterator it;
for(it=V.begin();it!=V.end();it++){
if(it->r==M){
V.erase(it);
break;
}
}
}
}
printf("%d\n",ans.size());
sort(ans.begin(),ans.end());
for(int i=;i<ans.size();i++){
if(i!=)printf(" ");
printf("%d",ans[i]);
}
if(ans.size()!=)
printf("\n");
}
return ;
}
ZOJ3953 Intervals的更多相关文章
- zoj3953 Intervals 最大不重叠区间加强版 zoj排名第一~
Intervals Time Limit: 1 Second Memory Limit:65536 KB Special Judge Chiaki has n intervals ...
- 贪心:zoj3953 Intervals
Description Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some interv ...
- ZOJ-3953 Intervals,t
Intervals 题意:给出n个区间,求最少删除多少个区间使得任意三个区间都不相交. 思路:按区间左端点排序,每次选取r最大的两个与当前比较,如果能放更新r,否则删除r最大的.关键就在怎么删除r最大 ...
- [LeetCode] Non-overlapping Intervals 非重叠区间
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
- [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- POJ1201 Intervals[差分约束系统]
Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26028 Accepted: 9952 Descri ...
- Understanding Binomial Confidence Intervals 二项分布的置信区间
Source: Sigma Zone, by Philip Mayfield The Binomial Distribution is commonly used in statistics in a ...
- Leetcode Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
随机推荐
- Leetcode 986. Interval List Intersections
暴搜.. class Solution(object): def intervalIntersection(self, A: List[Interval], B: List[Interval]) -& ...
- 基于Photon 的 PUN+ 如何自动实现RPC呼叫的.
基于Photon 的 PUN+ 如何自动实现RPC呼叫的. 简单说函数标记成 RPC 类型的. void Hello() { Debug.Log("Hello"); PhotonV ...
- HTTrack - 克隆任意网站
HTTrack可以克隆指定网站-把整个网站下载到本地. 可以用在离线浏览上,也可以用来收集信息(甚至有网站使用隐藏的密码文件). 一些仿真度极高的伪网站(为了骗取用户密码),也是使用类似工具做的. K ...
- PS基础教程[2]渐变工具的使用
PS中的渐变是一个很实用的工具,很多时候都会用到,我们在网页上看到的各种各样的颜色几乎都是一种渐变色,很少有一种颜色一层不变的.那么渐变如何使用呢?本次我们来介绍一下渐变的基本使用方法. 使用方法 1 ...
- junit学习之junit的基本介绍
Junit目前在一些大的公司或者相对规范的软件中使用的比较多,相当多的小公司并没有把单元测试看的太重要.在大点的公司开发人员每天上班后,第一件事情就是从svn上把自己负责的代码checkout下来,然 ...
- php中 curl模拟post发送json并接收json(转)
本地模拟请求服务器数据,请求数据格式为json,服务器返回数据也是json. 由于需求特殊性, 如同步客户端的批量数据至云端, 提交至服务器的数据可能是多维数组数据了. 这时需要将此数据以一定的数据 ...
- Delphi AES加密(转)
(**************************************************************) (* Advanced Encryption Standard (AE ...
- 关于bc 的scale .
linux下的bc命令可以设置结果的位数,通过 scale. 比如: $ echo "scale=4; 1.2323293128 / 1.1" | bc -l1.1202 但是sc ...
- 安装jenkins 的时候 记录默认密码文件为空的情况
1.把文件的权限改成 chmod 777 .jenkins/secrets/initialAdminPassword 然后再使用编辑器打开,密码就出来的 密码文件的地址 /var/root/.hud ...
- Python 列表笔记
列表解析 squares = []for i in range(1,11): square = i**2 squares.append(square)print squares ########## ...