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 ...
随机推荐
- Office VBA 参考
Office VBA 参考:https://docs.microsoft.com/zh-CN/office/vba/api/overview/
- flutter 使用keyboard_actions 关闭ios键盘
项目中登录 输入账号密码 弹出的键盘 关闭不了,从而 引来一些问题, 1,第一次关闭 项目是在 最外层包裹一层,点击的时候进行关闭, return Scaffold( resizeToAvoidBot ...
- JavaScript笔记(第一章,第二章)
JavaScript笔记(第一章,第二章) 第一章: <meta http-equiv="Content-Type" content="text/html; cha ...
- Go语言中接口组合的实现方法
在Go语言中,可以在接口A中组合其它的一个或多个接口(如接口B.C),这种方式等价于在接口A中添加接口B.C中声明的方法. 代码如下: //接口中可以组合其它接口,这种方式等效于在接口中添加其它接口的 ...
- 树莓派安装raspbian
需要准备 Win32DiskImager-v0.6 2016-09-23-raspbian-jessie.img 右键管理员打开Win32DiskImager 选择上面的img文件,选择存储卡盘符,点 ...
- gcc将c源文件中的宏展开
1: sudo gcc -P -I. -I../instrument/stubs -I../instrument/stubs -I../ -E ctrl_xfer32.cc -o preprocess ...
- HDU 6590 Code (判断凸包相交)
2019 杭电多校 1 1013 题目链接:HDU 6590 比赛链接:2019 Multi-University Training Contest 1 Problem Description Aft ...
- 剑指offer——570~n-1中缺失的数字
题目:0~n-1中缺失的数字. 一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个数字都在范围0~n-1之内. 在范围0~n-1内的n个数字中有且只有一个数字不在该数组中,请找出这个数字. ...
- 整合phoenix4.6.0-HBase-1.0到cdh5..4.7 编译phoenix4.6源码 RegionServer 宕机
Phoenix 集成HBase Phoenix 版本:phoenix-4.6.0-HBase-1.0 源码下载地址: http://apache.cs.uu.nl/phoenix/phoenix-4. ...
- jquery插件小集合
一.滑动轮播插件Swiper Swiper官网http://www.swiper.com.cn/, 这款插件移动端,pc端均试用 二.jquery-tmpl----让你从拼接字符串中解放出来 官方下载 ...