区间不相交&区间覆盖
//code by virtualtan 2018 寒7
区间不相交
#include <cstdio>
#include <algorithm>
#define MAXN 1111
using namespace std;
struct node {
int l, r;
}a[MAXN];
int cmp(node x, node xx) {
if(x.r != xx.r) return x.r < xx.r; //从小到大排序就用 ‘<’
return x.l > xx.l;//优先选择右端点小的 若右端点相等 优先左端点大的
}
int n, ans;
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d%d", &a[i].l, &a[i].r);
sort(a + 1, a + 1 + n, cmp);
// for(int i = 1; i <= n; i++) printf("> %d %d\n", a[i].l, a[i].r);
int R = -2033333333;
// 2147483647
for(int i = 1; i <= n; i++) {
if(R <= a[i].l) {
ans++;
R = a[i].r;//更新 限制区间左端点 的数
}
}
printf("%d", ans);
return 0;
}
区间覆盖
#include <cstdio>
#include <algorithm>
#define MAXN 1111
using namespace std;
struct node {
int l, r;
}a[MAXN];
int cmp(node x, node xx) {
return x.l < xx.l;
}
int n, ans;
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d%d", &a[i].l, &a[i].r);
sort(a + 1, a + 1 + n, cmp);
int R = 1;
int rr = 0;
for(int i = 1; i <= n; i++) {0
if(a[i].l <= R) rr = max(rr, a[i].r);
if(R > n) break;
if(a[i + 1].l > R || i == n) {
R = rr + 1;//rr 是 已成立的 区间中 最大区间 的右端点
//R是:需要:覆盖区间的左端点 又因为rr 已被覆盖 所以不能写 R == rr
ans++;
}
printf("%d %d %d\n", i, ans, R);
}
printf("%d", ans);
return 0;
}
区间不相交&区间覆盖的更多相关文章
- UVaLive 6609 Meeting Room Arrangement (贪心,区间不相交)
题意:给定 n 个区间,让你选出最多的区间,使得每个区间不相交. 析:贪心题,贪心策略是按右端点排序,然后按着选即可. 代码如下: #pragma comment(linker, "/STA ...
- HDU 2037 - 今年暑假不AC - [经典 选择不相交区间 问题]
是一道很经典的选择不相交区间的问题. 关于选择不相交区间,可以参考刘汝佳.也可以参考:http://blog.csdn.net/dgq8211/article/details/7534488 以及模板 ...
- Java实现 LeetCode 352 将数据流变为多个不相交区间
352. 将数据流变为多个不相交区间 给定一个非负整数的数据流输入 a1,a2,-,an,-,将到目前为止看到的数字总结为不相交的区间列表. 例如,假设数据流中的整数为 1,3,7,2,6,-,每次的 ...
- LeetCode352 将数据流变为多个不相交区间
LeetCode352 将数据流变为多个不相交区间 1 题目 给你一个由非负整数 a1, a2, ..., an 组成的数据流输入,请你将到目前为止看到的数字总结为不相交的区间列表. 实现 Summa ...
- 整数区间及区间集合(C#实现)
/// <summary> /// 整数区间类 /// </summary> private class Interval { , _end = ; public int St ...
- [用CDQ分治解决区间加&区间求和]【习作】
[前言] 作为一个什么数据结构都不会只会CDQ分治和分块的蒟蒻,面对区间加&区间求和这么难的问题,怎么可能会写线段树呢 于是,用CDQ分治解决区间加&区间求和这篇习作应运而生 [Par ...
- 线段树 区间开方区间求和 & 区间赋值、加、查询
本文同步发表于 https://www.zybuluo.com/Gary-Ying/note/1288518 线段树的小应用 -- 维护区间开方区间求和 题目传送门 约定: sum(i,j) 表示区间 ...
- 树状数组区间加法&区间求和操作
树状数组区间加法&区间求和操作 一般的树状数组解决区间加&单点询问并不复杂 但是要解决区间求和... 我们假设原数组是\(\{a_i\}\),差分数组\(\{d_i=a_i-a_{i- ...
- pandas处理时间序列(2):DatetimeIndex、索引和选择、含有重复索引的时间序列、日期范围与频率和移位、时间区间和区间算术
一.时间序列基础 1. 时间戳索引DatetimeIndex 生成20个DatetimeIndex from datetime import datetime dates = pd.date_rang ...
随机推荐
- UESTC--1262--Memory(dfs)
Memory Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & %llu SubmitStatus De ...
- EOJ 3213 向右看齐
题目描述 N 头奶牛被紧急动员起来了,它们排成了一条长列.从左向右看,排在第 i 个位置的奶牛身高为 Hi.约翰一声令下,所有奶牛向右看齐.假设每头奶牛只能看到比自己高的牛.请问它们各自看到的最近的一 ...
- .NET平台开源JSON序列化
转载: http://blog.csdn.net/ddgweb/article/details/39643747 一个简单示例: String str = "{’name’:’cyf’,’i ...
- BZOJ 3238 后缀数组+单调栈
单调栈跑两遍求出来 ht[i]为最小值的那段区间 //By SiriusRen #include <cstdio> #include <cstring> #include &l ...
- js-apply call bind 浅析
call 1.第一个参数指定了this,第二个参数传给this,也就是call前面的函数,作为他的参数第三个参数也一样 指定了this,就是执行环境,greet的this在i里面找 function ...
- mysql安装出现 conflicts with mysql*的解决办法
rpm -ivh Percona-Server-client-56-5.6.16-rel64.0.el6.x86_64.rpm --nodeps --force error: Failed depen ...
- GRpc-Proto3语法
syntax = "proto3"; 文件的第一行指定了你使用的是proto3的语法:如果你不指定,protocol buffer 编译器就会认为你使用的是proto2的语 ...
- AVL树,红黑树,B树,B+树,Trie树都分别应用在哪些现实场景中?
AVL树: 最早的平衡二叉树之一.应用相对其他数据结构比较少.windows对进程地址空间的管理用到了AVL树. 红黑树: 平衡二叉树,广泛用在C++的STL中.如map和set都是用红黑树实现的. ...
- runloop源代码
https://github.com/zzf073/runloopDemo /** * 调度例程 * 当将输入源安装到run loop后,调用这个协调调度例程,将源注册到客户端(可以理解为其他线程 ...
- 在centos6.5上升级php-libxml版本到2.9.0
当前系统,软件版本说明: php libxml glibc 2.12 zlib xz-libs 需求: 应开发的需求,线上环境,php-libxml版本升级到2.8以上. 升级步骤:1.安装工具集 y ...