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 ...
随机推荐
- SEH:结构化异常处理 学习
SEH:结构化异常处理 结构化异常处理机制提供了一个操作系统,用于优化结构的方案,为客户提供更强大的程序执行环境.试想一下,你写程序不用考虑内存访问错误,那里是空指针错误,一直在按照程序的逻辑结构来写 ...
- python tricks —— datetime 删除日期中的前导 0
>> from datetime import datetime >> now = datetime.now() >> now.strftime('%Y-%m-%d ...
- 使用Audition录制自己的歌曲
Audition专为在照相室.广播设备和后期制作设备方面工作的音频和视频专业人员设计,可提供先进的音频混合.编辑.控制和效果处理功能.最多混合 128 个声道,可编辑单个音频文件,创建回路并可使用 4 ...
- 重温CLR(七 ) 属性和事件
无参属性 许多类型都定义了能被获取或更高的状态信息.这种状态信息一般作为类型的字段成员实现.例如一下类型包含两个字段: public sealed class Employee{ public str ...
- 《DSP using MATLAB》示例Example7.20
代码: M = 51; alpha = (M-1)/2; Dw = 2*pi/M; l = 0:M-1; wl = Dw*l; T1 = j*0.39; k1 = 0:floor((M-1)/2); ...
- C语言词法分析:C#源码
今天继续研究代码解析的算法 这个是算法流程图 有图解可能更直观一点: 以下是c#源码: 1using System; 2using System.IO; 3using System.Tex ...
- 6kzz整合ueditor
用过6kzz免费版的朋友都知道,它的后台编辑器是kindeditor,并且是两三年前的版本,有很多功能都没有,现在的kindeditor也不错,但感觉还是没有百度的ueditor强大(据说微信后台的编 ...
- What’s that ALUA exactly?
What’s that ALUA exactly? 29 September, 20098 Comments Of course by now we have all read the excelle ...
- iSCSI存储的3种连接方式
我们分析了iSCSI存储的系统结构,下面来看iSCSI是如何与服务器.工作站等主机设备来连接的,也就是我们如何建立一个iSCSI网络存储系统. iSCSI设备的主机接口一般默认都是IP接口,可以直接与 ...
- (转)Oracle执行字符串
declare v_out ); begin execute immediate 'select p_guid from c_itcomp where rownum = 1 ' into v_out; ...