UOJ222 【NOI2016】区间

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

 

 

 

本文作者:ljh2000 
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

 

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

 

 

正解:离散化+线段树维护+决策单调性

解题报告:

  首先我们考虑暴力怎么做,按长度排序之后,我们容易发现,如果枚举一个区间作为左端点,一个区间作为右端点,那么我们就是求只在这个区间中选取的答案。

  我们把这一段的所有区间的对应的一段的经过次数都加一,最后只需$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;
}

  

posted @ 2016-08-02 19:28  ljh_2000  阅读(898)  评论(0编辑  收藏  举报