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

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. vue项目base64转img

    最近一个vue项目中,有个需求是, 发送消息是base64时,转换成图片预览发送出去. 输入框: <el-input class="input-box" ref=" ...

  2. mac上安装mamp集成环境

    深知mac配置环境是个坑,本人之前用的是xampp因为近期需要mongodb扩展,死活装不明白,索性就换了一个集成环境,在网上找了好多,最后选择了mamp 因为正版的要收费,所以在下载了N个以后终于找 ...

  3. AcWing 203. 同余方程 (线性同余方程)打卡

    求关于x的同余方程 ax ≡ 1(mod b) 的最小正整数解. 输入格式输入只有一行,包含两个正整数a,b,用一个空格隔开. 输出格式输出只有一行,包含一个正整数x,表示最小正整数解. 输入数据保证 ...

  4. Mysql LOAD DATA读取客户端任意文件漏洞复现(原理分析)

    环境搭建 怎么设置Mysql支持外联? use mysql; grant all privileges on *.* to root@'%' identified by '密码'; //授权语句 fl ...

  5. inode缓存与dentry缓存

    1. inode缓存 1: struct inode { 2: /* RCU path lookup touches following: */ 3: umode_t i_mode; 4: uid_t ...

  6. 拾遗:使用 systemd-journald 管理 Docker 容器日志

    在 docker.service 文件中的 ExecStart 字段中,添加(或:docker run --log-driver=journald): --log-driver=journald \ ...

  7. PhotoShop的10大误区

    一.使用错误的图像模式 操作菜单变成灰色?颜色看起来不对劲?当你遇到这些问题的时候,应该停下来,不要再编辑.这些可 能就是使用错误的图像模式而引起的问题.在主菜单中选择“图像>模式”,在这里确保 ...

  8. 1-vim-简介

    vi(visual interface)是Linux最经典的文本编辑器 vi的核心设计思想-让程序员的手指始终保持在键盘的核心区域,就能完成所有的编辑操作. vi的特点 没有图形界面 只能编辑文本内容 ...

  9. 13、如何拆分含有多种分隔符的字符串 14、如何判断字符串a是否以字符串b开头或结尾 15、如何调整字符串中文本的格式 16、如何将多个小字符串拼接成一个大的字符串

    13.如何拆分含有多种分隔符的字符串 import re s = "23:41:2314\1234#sdf\23;" print(re.split(r'[#:\;]+',s))   ...

  10. 要素选择变化事件 IActiveViewEvents_SelectionChanged

    void IDockableWindowDef.OnCreate(object hook) { m_application = hook as IApplication; m_hookHelper = ...