bzoj 4653: [Noi2016]区间

Description

在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn]。现在要从中选出 m 个区间,使得这 m个区间共同包含至少一个位置。换句话说,就是使得存在一个 x,使得对于每一个被选中的区间 [li,ri],都有 li≤x≤ri。
 
对于一个合法的选取方案,它的花费为被选中的最长区间长度减去被选中的最短区间长度。区间 [li,ri] 的长度定义为 ri−li,即等于它的右端点的值减去左端点的值。
 
求所有合法方案中最小的花费。如果不存在合法的方案,输出 −1。

Input

第一行包含两个正整数 n,m用空格隔开,意义如上文所述。保证 1≤m≤n
 
接下来 n行,每行表示一个区间,包含用空格隔开的两个整数 li 和 ri 为该区间的左右端点。
N<=500000,M<=200000,0≤li≤ri≤10^9

Output

只有一行,包含一个正整数,即最小花费。

Sample Input

6 3
3 5
1 2
3 4
2 2
1 5
1 4

Sample Output

2

HINT

 

Source

这道题和极差最小生成树有点像,就是枚举一个区间作为长度最短的区间,求得满足覆盖>=m次的最大的区间的长度最小;

暴力的做法是把长度sort一边,从前往后枚举最小边,对于每一个枚举的最小长度区间,一直往后加,直到覆盖m次后break;

至于如何判断是否覆盖了>=m次,这就是线段树区间加法了;

然后我们发现左端点是单调的,因为是在同样有点满足覆盖>=m次的条件下,后面的肯定比前面优,所以就可用单调队列扫一遍即可

坐标离散化一下。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ls (x<<1)
#define rs (x<<1|1)
using namespace std;
const int N=1000000;
const int Inf=2147483647;
struct data{
   int l,r,len;
}q[N];
int tr[N*4],lazy[N*4],hsh[N],num[N],tot,n,m;
bool cmp(const data &a,const data &b){
	return a.len<b.len;
}
void pushdown(int x){
	tr[ls]+=lazy[x],tr[rs]+=lazy[x];
	lazy[ls]+=lazy[x];lazy[rs]+=lazy[x];lazy[x]=0;
} 
void pushup(int x){
	tr[x]=max(tr[ls],tr[rs]);
}
void update(int x,int l,int r,int xl,int xr,int v){
	if(xl<=l&&r<=xr){
		lazy[x]+=v;tr[x]+=v;return;
	}
	if(lazy[x]) pushdown(x);
	int mid=(l+r)>>1;
	if(xr<=mid) update(ls,l,mid,xl,xr,v);
	else if(xl>mid) update(rs,mid+1,r,xl,xr,v);
	else update(ls,l,mid,xl,mid,v),update(rs,mid+1,r,mid+1,xr,v);
	pushup(x);
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&q[i].l,&q[i].r);
		hsh[++tot]=q[i].l;hsh[++tot]=q[i].r; 
	}
	sort(hsh+1,hsh+tot+1),tot=unique(hsh+1,hsh+tot+1)-hsh-1;
	for(int i=1;i<=n;i++){
		q[i].l=lower_bound(hsh+1,hsh+1+tot,q[i].l)-hsh;
		q[i].r=lower_bound(hsh+1,hsh+1+tot,q[i].r)-hsh;
		q[i].len=hsh[q[i].r]-hsh[q[i].l];
	}
	int head=1,ans=Inf;sort(q+1,q+1+n,cmp);
	for(int i=1;i<=n;i++){
		update(1,1,tot,q[i].l,q[i].r,1);
		while(tr[1]>=m){
			ans=min(q[i].len-q[head].len,ans);
			update(1,1,tot,q[head].l,q[head].r,-1);
			head++;
		}
	}
	if(ans==Inf) puts("-1");
	else printf("%d\n",ans); 
} 

  

posted @ 2017-05-13 17:57  qt666  阅读(170)  评论(0编辑  收藏  举报