题意

有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的更多相关文章

  1. zoj3953 Intervals 最大不重叠区间加强版 zoj排名第一~

    Intervals Time Limit: 1 Second      Memory Limit:65536 KB      Special Judge Chiaki has n intervals ...

  2. 贪心:zoj3953 Intervals

    Description Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some interv ...

  3. ZOJ-3953 Intervals,t

    Intervals 题意:给出n个区间,求最少删除多少个区间使得任意三个区间都不相交. 思路:按区间左端点排序,每次选取r最大的两个与当前比较,如果能放更新r,否则删除r最大的.关键就在怎么删除r最大 ...

  4. [LeetCode] Non-overlapping Intervals 非重叠区间

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

  5. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  6. [LeetCode] Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

  7. POJ1201 Intervals[差分约束系统]

    Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26028   Accepted: 9952 Descri ...

  8. Understanding Binomial Confidence Intervals 二项分布的置信区间

    Source: Sigma Zone, by Philip Mayfield The Binomial Distribution is commonly used in statistics in a ...

  9. Leetcode Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

随机推荐

  1. Wordpress在主题或者插件中自定义存储附件的方法

    1.前端使用form表单 //单文件上传,我的业务需求中限制了必须上传图片 <input type="file" name="singlename" ac ...

  2. cmd 操作WinService

    1.运行--〉cmd:打开cmd命令框 2.在命令行里定位到InstallUtil.exe所在的位置 InstallUtil.exe 默认的安装位置是在C:\Windows\Microsoft.NET ...

  3. LeetCode — (1)

    摘要: Nim Game.WordPattern.Move zeros.First Bad version.Ugly Number五个算法的python实现. 一个月多没更新,大概是因为状态一直不太好 ...

  4. 项目中使用的ajax代码_:觉得还好

    POST>> submitHandler:function(form){ var username = $('#user_name').val(); var password = $('# ...

  5. ACM ICPC 2018 青岛赛区 部分金牌题题解(K,L,I,G)

     目录: K Airdrop I Soldier Game L Sub-cycle Graph G Repair the Artwork ———————————————————— ps:楼主脑残有点严 ...

  6. MySql必知必会实战练习(四)主键、外键、sql约束、联结表

    本博将对主键.外键.MySql数据库约束和联结表的相关特性进行总结和实战 1. 主键 表中的每一行都应该具有可以唯一标识自己的一列(或一组列),而这个承担标识作用的列称为主键 如果没有主键,数据的管理 ...

  7. C# 浅拷贝与深拷贝(复制)

    在有些时候,我们需要从数据库读取数据填充对象或从硬盘读取文件填充对象,但是这样做相对耗时.这时候我们就想到了对象的拷贝.本文即以实例形式解析了C#浅拷贝和深拷贝的用法. C#中有两种类型变量,一种 是 ...

  8. 转载论文关于fir滤波器的fpga实现

    摘 要 本文讨论的FIR滤波器因其具有严格的线性相位特性而得到广泛的应用.在工程实践中,往往要求信号处理具有实时性和灵活性,本论文研究FIR的FPGA解决方案正体现了电子系统的微型化和单片化. 本论文 ...

  9. 【spring源码学习】spring的AOP面向切面编程的实现解析

    一:Advice(通知)(1)定义在连接点做什么,为切面增强提供织入接口.在spring aop中主要描述围绕方法调用而注入的切面行为.(2)spring定义了几个时刻织入增强行为的接口  => ...

  10. break、continue与return的区别

    1. break break语句的使用场合主要是switch语句和循环结构.在循环结构中使用break语句,如果执行了break语句,那么就退出循环,接着执行循环结构下面的第一条语句.如果在多重嵌套循 ...