ZOJ3953-Intervals-贪心
(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦
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-贪心的更多相关文章
- 贪心:zoj3953 Intervals
Description Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some interv ...
- zoj3953 Intervals 最大不重叠区间加强版 zoj排名第一~
Intervals Time Limit: 1 Second Memory Limit:65536 KB Special Judge Chiaki has n intervals ...
- POJ 1716 Integer Intervals#贪心
(- ̄▽ ̄)-* //求一个集合,这个集合与任意一个区间的交集,需至少有两个数字 //贪心过程:按n个区间的最右值从小到大对区间进行排列, //集合首先取第一个区间的最右两个数字, //到第二个区间, ...
- E - Intervals 贪心
Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some intervals so that ...
- ZOJ3953 Intervals
题意 有n个区间,要求删除一些区间使得不存在三个相交的区间.找出删除的最少区间. 分析 是个比较显然的贪心吧. 先按照区间的左起点进行排序,然后从左往右扫,当有三个区间相交的时候,删除那个右端点最远的 ...
- ZOJ-3953 Intervals,t
Intervals 题意:给出n个区间,求最少删除多少个区间使得任意三个区间都不相交. 思路:按区间左端点排序,每次选取r最大的两个与当前比较,如果能放更新r,否则删除r最大的.关键就在怎么删除r最大 ...
- {POJ}{动态规划}{题目列表}
动态规划与贪心相关: {HDU}{4739}{Zhuge Liang's Mines}{压缩DP} 题意:给定20个点坐标,求最多有多少个不相交(点也不相交)的正方形 思路:背包问题,求出所有的正方形 ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- Integer Intervals(贪心)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12123 Accepted: 5129 Description An i ...
- poj 1716 Integer Intervals (差分约束 或 贪心)
Integer Intervals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12192 Accepted: 514 ...
随机推荐
- js 读取本地文件(必须通过input控件才能实现) 及 下载文件
js 操作 文件的实现原理: 1.js是不能直接操作(读写)文件的,html的 input[type="file"] 控件是可以读取文件数据(获取文件数据流)的.js可以获取这个 ...
- Extjs各版本的下载链接,包含ext3.4源码示例
最近在维护一个老平台,用的是ext3.4,老东西在网上找示例发现既然还有人收钱,谷歌了一下总算找到了一位免费的发布的,感谢 yipanbo 分享 Extjs的版本繁多,本文收集了Extjs各个版本的下 ...
- 14、java实现poi操作excel,包括读和写日期格式,并且设置字体样式
1.首先大家来看导出的结果 下边就是导出的代码了 protected void testExcel() throws IOException{ String path=getServletContex ...
- 解决ajax跨域问题【5种解决方案】
什么是跨域问题?跨域问题来源于JavaScript的"同源策略",即只有 协议+主机名+端口号 (如存在)相同,则允许相互访问.也就是说JavaScript只能访问和操作自己域下的 ...
- 剑指offer第二版面试题6:重建二叉树(JAVA版)
题目:输入某二叉树的前序遍历和中序遍历的结果,请重新构造出该二叉树.假设输入的前序遍历和中序遍历的结果中不包含重复的数字.例如输入的前序遍历序列为{1,2,4,7,3,5,6,8}和中序遍历为{4,7 ...
- C++ 操作json文件
一.环境搭建: 参考文章:https://blog.csdn.net/fakine/article/details/79272090 二.创建实例: #include <stdio.h> ...
- MySQL在Win10与Ubuntu下的安装与配置
本文首发于cartoon的博客 转载请注明出处:https://cartoonyu.github.io/cartoon-blog 近段时间把自己电脑(win).虚拟机(Ubun ...
- 关于版本管理工具SVN
曾经使用过Git,但是目前使用的是相对简单的svn. 刚使用的时候,如果不出意外.会有同学配好环境与权限. 前端只需要将代码下载,修改,更新,与上传. 一 .下载 1.本地新建文件夹,作为本地仓库 命 ...
- 不在B中的A的子串数量 HDU - 4416 (后缀自动机模板题目)
题目: 给定一个字符串a,又给定一系列b字符串,求字符串a的子串不在b中出现的个数. 题解: 先将所有的查询串放入后缀自动机(每次将sam.last=1)(算出所有子串个数) 然后将母串放入后缀自动机 ...
- 如果手工启动chromedriver
使用selenium模拟登陆网站时,有些网站会识别chrome driver里的json信息,从而判断是不是爬虫程序,做到反爬效果.(比如知乎) 下面说明下怎么手动启动chromedriver 1). ...