UOJ222 【NOI2016】区间
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000 
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
Description
Input
Output
Sample Input
3 5
1 2
3 4
2 2
1 5
1 4
Sample Output
正解:离散化+线段树维护+决策单调性
解题报告:
首先我们考虑暴力怎么做,按长度排序之后,我们容易发现,如果枚举一个区间作为左端点,一个区间作为右端点,那么我们就是求只在这个区间中选取的答案。
我们把这一段的所有区间的对应的一段的经过次数都加一,最后只需$check$一下这一段中是否出现了一个被经过$m$次的点,一旦存在就说明,我们一定可以找到其中的$m$个区间满足题目的要求,所以我们就可以确保在这个区间中能够选取$m$个区间并一定合法,就可以用右端点的那个区间长度-左端点的那个区间长度来更新答案。(并不关心具体选了哪些区间)
上述做法的复杂度可以用线段树维护来做到$ n^2 logn $。深入思考可以发现,其实右端点肯定是不降的,所以我们没必要再枚举一个右端点,只要用单调指针一直往后扫即可。总复杂度为:$O(nlogn)$。
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
using namespace std;
typedef long long LL;
const int MAXN = 1000011;
const int inf = (1<<30);
int n,m,top,c[MAXN*2],cnt;
int ql,qr,type,ans;
struct interval{ int l,r,len; }a[MAXN];//存储区间 struct node{ int maxl,tag; }s[MAXN*3];//线段树节点
inline bool cmp(interval q,interval qq){ return q.len<qq.len; } inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void pushdown(int root,int l,int r){
if(s[root].tag==0) return ; if(l==r) { s[root].tag=0; return ; }
int lc=root*2,rc=lc+1;
s[lc].tag+=s[root].tag; s[rc].tag+=s[root].tag;
s[lc].maxl+=s[root].tag; s[rc].maxl+=s[root].tag;
s[root].tag=0;
} inline void add(int root,int l,int r){
pushdown(root,l,r);
if(ql<=l && r<=qr) {
s[root].tag+=type;
s[root].maxl+=type;
return ;
}
int mid=(l+r)>>1; int lc=root*2,rc=lc+1;
if(ql<=mid) add(lc,l,mid); if(qr>mid) add(rc,mid+1,r);
s[root].maxl=max(s[lc].maxl,s[rc].maxl);
} inline void work(){
n=getint(); m=getint();
for(int i=1;i<=n;i++) {
a[i].l=getint(),a[i].r=getint();
a[i].len=a[i].r-a[i].l;
c[++cnt]=a[i].l; c[++cnt]=a[i].r;
}
top=0; ans=inf; sort(c+1,c+cnt+1); cnt=unique(c+1,c+cnt+1)-c-1;
for(int i=1;i<=n;i++) a[i].l=lower_bound(c+1,c+cnt+1,a[i].l)-c,a[i].r=lower_bound(c+1,c+cnt+1,a[i].r)-c;
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++) {
//强制选择第i个区间,那么根据单调性需要往后选择若干个区间,线段树维护,直到发现某个点被经过了m次则说明合法
while(s[1].maxl<m && top<n) {
top++;
ql=a[top].l; qr=a[top].r; type=1;
add(1,1,cnt);
}
if(s[1].maxl==m) ans=min(ans,a[top].len-a[i].len);
ql=a[i].l; qr=a[i].r; type=-1;
add(1,1,cnt);
}
if(ans==inf) printf("-1");
else printf("%d",ans);
} int main()
{
work();
return 0;
}
UOJ222 【NOI2016】区间的更多相关文章
- UOJ222 NOI2016 区间 线段树+FIFO队列
		首先将区间按长度排序后离散化端点(这里的“长度”指的是离散化之前区间的实际长度) 然后模拟一个队列,区间按排好的顺序依次进入,直到某个点被覆盖了M次.之后依次出队,直到所有点都被覆盖小于M次 修改和询 ... 
- BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针
		BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间, ... 
- [Noi2016]区间[离散化+线段树维护+决策单调性]
		4653: [Noi2016]区间 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 621 Solved: 329[Submit][Status][D ... 
- [NOI2016]区间 线段树
		[NOI2016]区间 LG传送门 考虑到这题的代价是最长边减最短边,可以先把边按长度排个序,双指针维护一个尺取的过程,如果存在包含某个点的区间数\(\ge m\),就更新答案并把左指针右移,这样做的 ... 
- [BZOJ4653][NOI2016]区间 贪心+线段树
		4653: [Noi2016]区间 Time Limit: 60 Sec Memory Limit: 256 MB Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],. ... 
- 【BZOJ4653】[Noi2016]区间 双指针法+线段树
		[BZOJ4653][Noi2016]区间 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间,使得这 m个区间共同包含 ... 
- [NOI2016]区间 题解(决策单调性+线段树优化)
		4653: [Noi2016]区间 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1593 Solved: 869[Submit][Status][ ... 
- Luogu P1712 [NOI2016]区间(线段树)
		P1712 [NOI2016]区间 题意 题目描述 在数轴上有 \(N\) 个闭区间 \([l_1,r_1],[l_2,r_2],...,[l_n,r_n]\) .现在要从中选出 \(M\) 个区间, ... 
- 【题解】P1712 [NOI2016]区间(贪心+线段树)
		[题解]P1712 [NOI2016]区间(贪心+线段树) 一个observe是,对于一个合法的方案,将其线段长度按照从大到小排序后,他极差的来源是第一个和最后一个.或者说,读入的线段按照长度分类后, ... 
- 洛谷P1712 [NOI2016]区间 尺取法+线段树+离散化
		洛谷P1712 [NOI2016]区间 noi2016第一题(大概是签到题吧,可我还是不会) 链接在这里 题面可以看链接: 先看题意 这么大的l,r,先来个离散化 很容易,我们可以想到一个结论 假设一 ... 
随机推荐
- XSL-FO Page Layout
			Simple Layout Let's take a look at the simple page layout that we saw earlier in the course. The sim ... 
- mysql练手
			1.根据图创建下列表格 没有外键的表先创建,创建顺序为teacher,class,course,student CREATE TABLE class ( cid INT NOT NULL auto_i ... 
- Laravel 出现"RuntimeException inEncrypter.php line 43: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths."问题的解决办法
			如果输入命令:php artisan key:generate 还是报错 那就要从别的项目里复制一个key到.env中,然后再运行命令:composer update和php artisan key: ... 
- 登录案例locustfile.py
			# 保存为locustfile.py # coding=utf-8 from locust import HttpLocust, TaskSet, task ''' 实现场景:先登录(只登录一次),然 ... 
- Python获取位数
			import platform platform.architecture() 
- hbase shell编码显示中文
			最近测试hbase shell,碰到个中文显示编码问题,最后通过Python解决了问题,具体操作如下: hbase(main):015:0* scan 'fr_test_hbase:test_log1 ... 
- 《Python数据分析》笔记1 ——Numpy
			Numpy数组 1.Numpy数组对象 Numpy中的多维数组称为ndarray,他有两个组成部分. 1.数据本身 2.描述数据的元数据 2.Numpy的数值类型 bool: 布尔型 inti:其长度 ... 
- 常用模块之hashlib,configparser,logging模块
			常用模块二 hashlib模块 hashlib提供了常见的摘要算法,如md5和sha1等等. 那么什么是摘要算法呢?摘要算法又称为哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定 ... 
- C#:连接本地SQL Server语句
			一.Windows身份验证方式 SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source ... 
- 02_虚拟机的安装和SecureCRT、FileZilla、Xmanage、UltraEdit工具的介绍
			上述几个工具连接不成功的情况,很多时候是因为ssh服务没有安装,CentOS默认安装,不会出现问题,Ubuntu桌面版默认没有安装,需要手动安装,安装部分参考下文SecureCRT部分 一.安装Cen ... 
