(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦

Catalog

Problem:Portal传送门

 原题目描述在最下面。

 给你n个区间,问最少删除多少个区间,使得任意三个区间不两两相交。

Solution:

 先把所有区间按先左端点再右端点从小到大排序,每次选出前面相邻的三个区间:

 如果两两相交则一定要删除右端点最大的那个区间,因为这个区间的能影响的范围更大!

 若不两两相交就放掉右端点最小的,用下一个区间代替它,因为左端点是递增的,任选后面两个区间肯定不会和这个右端点最小的区间两两相交!重复这个过程。

 还有就是最开始区间一定要先按左端点再按右端点排序。

 我提供一组样例:

4
2 5
3 7
1 15
9 12

####AC_Code:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<vector>
#include<cmath>
#include<bitset>
#include<cassert>
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define mme(a,b) memset((a),(b),sizeof((a)))
#define fuck(x) cout<<"* "<<x<<"\n"
#define iis std::ios::sync_with_stdio(false)
using namespace std;
typedef pair<int,int> pii;
typedef long long LL;
const int MXN = 60000 + 7;
const int MXE = 1e6 + 7;
const int mod = 998244353;
const int INF = 0x3f3f3f3f; int n, m;
struct lp{
int l, r, id;
}cw[MXN],op[3];
bool cmp1(lp &a,lp &b){
if(a.r!=b.r)return a.r < b.r;
return a.l < b.l;
}
bool cmp2(lp &a,lp &b){
if(a.l!=b.l)return a.l < b.l;
return a.r < b.r;
}
int main(){
int tim;
scanf("%d", &tim);
while(tim--){
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d%d", &cw[i].l, &cw[i].r);cw[i].id = i+1;
}
if(n<2){
printf("0\n\n");
continue;
}
sort(cw,cw+n,cmp2);
op[0] = cw[0]; op[1] = cw[1];
int cnt = 0;
std::vector<int> ans;
for(int t = 2; t < n; ++t){
op[2] = cw[t];
sort(op,op+3,cmp1);
int flag = 1;
for(int i = 0; i < 3; ++i){
for(int j = i + 1; j < 3; ++j){
if(op[j].l>op[i].r)flag = 0;
}
}
if(flag == 0){
op[0] = op[1];
op[1] = op[2];
}else{
cnt++;
ans.push_back(op[2].id);
}
}
printf("%d\n", cnt);
if(cnt == 0)printf("\n");
else {
sort(ans.begin(),ans.end());
for(int i = 0; i < cnt; ++i){
printf("%d%c", ans[i], " \n"[i==cnt-1]);
}
}
}
return 0;
}

####Problem Description:

ZOJ3953-Intervals-贪心的更多相关文章

  1. 贪心:zoj3953 Intervals

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

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

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

  3. POJ 1716 Integer Intervals#贪心

    (- ̄▽ ̄)-* //求一个集合,这个集合与任意一个区间的交集,需至少有两个数字 //贪心过程:按n个区间的最右值从小到大对区间进行排列, //集合首先取第一个区间的最右两个数字, //到第二个区间, ...

  4. E - Intervals 贪心

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

  5. ZOJ3953 Intervals

    题意 有n个区间,要求删除一些区间使得不存在三个相交的区间.找出删除的最少区间. 分析 是个比较显然的贪心吧. 先按照区间的左起点进行排序,然后从左往右扫,当有三个区间相交的时候,删除那个右端点最远的 ...

  6. ZOJ-3953 Intervals,t

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

  7. {POJ}{动态规划}{题目列表}

    动态规划与贪心相关: {HDU}{4739}{Zhuge Liang's Mines}{压缩DP} 题意:给定20个点坐标,求最多有多少个不相交(点也不相交)的正方形 思路:背包问题,求出所有的正方形 ...

  8. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  9. Integer Intervals(贪心)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12123   Accepted: 5129 Description An i ...

  10. poj 1716 Integer Intervals (差分约束 或 贪心)

    Integer Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12192   Accepted: 514 ...

随机推荐

  1. 生产者消费者模式-->线程

    #_author:来童星#date:2019/12/17#生产者消费者模式-->线程from queue import Queueimport random,time,threading#生产者 ...

  2. 深入理解Magento – 第三章 – 布局,块和模板

    深入理解Magento 作者:Alan Storm 翻译:Hailong Zhang 第三章 – 布局,块和模板 我们接着研究Magento.根据我们第二章讲的Magento MVC的架构,我们接下来 ...

  3. ajax验证用户名是否存在,手机号是不是匹配

    <label class="col-sm-2 control-label font">用户名</label> <div class="col ...

  4. Spring Cloud Alibaba 从孵化到 "挂牌" 之旅

    背景 2014 年,Spring Boot 1.0 发布.Spring Boot 的发布绝对是 Pivotal 历史上具有里程碑意义的事件,它让我们能够非常简便地开发 Spring 应用,屏蔽了各种配 ...

  5. [20190725NOIP模拟测试8]题解

    Orz T1 大水题,考场上看到题目中什么前几位相同末尾加字母莫名慌的一批 后来发现直接无脑哈希就能$O(n)$ KMP同样可切 仔细读题,数组别开小 #include<cstdio> # ...

  6. 使用Objective-C的+(void)initialize初始化static变量

    在<Objective C类方法load和initialize的区别>一文中,我介绍了Objective-C对待+(void)initialize和+(void)load两个方法在编译和执 ...

  7. python 获取手机设备号

    上代码 ##获取设备多台设备号列表 def get_deviceid(): str_init=' ' all_info= os.popen('adb devices').readlines() pri ...

  8. 词表征 1:WordNet、0-1表征、共现矩阵、SVD

    原文地址:https://www.jianshu.com/p/c1e4f42b78d7 一.基于知识的表征 参见图1.1,WordNet中包含同义词集(synonym sets)和上位词(hypern ...

  9. python 实现异常退出

    https://blog.csdn.net/u013385362/article/details/81206822 有时当一个条件成立的情况下,需要终止程序,可以使用sys.exit()退出程序.sy ...

  10. Django框架(一)—— 安装使用Django

    目录 Django入门 一.web应用 二.C/S 和B/S 架构 三.python中的web框架 四.http协议 五.URL简介 六.Django的安装和使用 Django入门 一.web应用 W ...