CodeForces 612D——The Union of k-Segments

You are given n segments on the coordinate axis Ox and the number k. The point is satisfied if it belongs to at least k segments. Find the smallest (by the number of segments) set of segments on the coordinate axis Ox which contains all satisfied points and no others.

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 106) — the number of segments and the value of k.

The next n lines contain two integers li, ri ( - 109 ≤ li ≤ ri ≤ 109) each — the endpoints of the i-th segment. The segments can degenerate and intersect each other. The segments are given in arbitrary order.

Output

First line contains integer m — the smallest number of segments.

Next m lines contain two integers aj, bj (aj ≤ bj) — the ends of j-th segment in the answer. The segments should be listed in the order from left to right.

Examples

Input

3 2
0 5
-3 2
3 8

Output

2
0 2
3 5

Input

3 2
0 5
-3 3
3 8

Output

1
0 5

题意:

本题中的n是有几个段区间,m是段区间重叠多少层。输出重叠层数大于等于k层的区间个数和区间。

根据第一个样例来说,(0,5) 与(-3,2)重叠区间(0,2);(0,5)与(3,8)重叠区间(3,5),他们重叠的层数都为2,所以满足题目的要求(大于等于k)。最后我们输出满足条件的区间个数,可以知道它为2,并且将满足条件的区间也输出。


思路:

1.我们可以用结构体来记录,定义一个结构体meng,l代表了区间的边界点数,r代表了这个点是该区间的开始还是结束。 

const int maxn=1e6+9;
struct meng{
	int l,r;
}fan[maxn*2];


for(int i=1;i<=n;i++){
	scanf("%d%d",&fan[len].l,&fan[len+1].l);
	fan[len++].r=1;//开端是1
	fan[len++].r=0;//末端是0
}

2.我们讲这些区间从左到右依次排列,以方便我们后续的操作。(若两区间开端点相同,那么我们将末端大的那一区间排在前面。

bool cmp(meng a,meng b){
	if(a.l==b.l)
	return a.r>b.r;
	return a.l<b.l;
}


sort(fan,fan+len,cmp);

3.由上述操作我们可以得到,一个排好序的结构体数组,它的长度是区间个数*2。我们用for循环遍历这些边界点,如果是开端的话就用sum++记录一下,如果这个sum满足等于条件中给的k那么我们就用一个数组shu将这个点存储起来以便于后续的输出。如果是末端如果满足等于k同样存储,不同的是在末端操作完之后要sum--,这是代表一个区间的结束(减少一层重叠)。

int ans=0,sum=0;
for(int i=0;i<len;i++){
	if(fan[i].r){
		//遇到开始端 
		sum++;
		if(sum==k){
			shu[ans++]=fan[i].l;
		}
	}
	else{
		//结束端
		 if(sum==k){
		 	shu[ans++]=fan[i].l;
		 }
		sum--;//判断完,再减减 
		//因为结束了一个区间 
	} 
} 

4.由于我们在满足条件的数组中存储的是开端和末端两个点,所以最后输出有多少个区间的时候我们要除以2,代表区间的个数。并且遍历存储两点的数组将他们输出。

    printf("%d\n",ans/2);
    for(int i=0;i<ans;i+=2)
        printf("%d %d\n",shu[i],shu[i+1]);

全部代码如下:

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e6+9;
struct meng{
	int l,r;
}fan[maxn*2];
int shu[maxn*2];
bool cmp(meng a,meng b){
	if(a.l==b.l)
	return a.r>b.r;
	return a.l<b.l;
}
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	int len=0;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&fan[len].l,&fan[len+1].l);
	    cout<<len<<" ";
		fan[len++].r=1;//开端是1
		cout<<len<<" ";
		fan[len++].r=0;//末端是0
		cout<<len<<" ";
	}
	sort(fan,fan+len,cmp);
	int ans=0,sum=0;
	for(int i=0;i<len;i++){
		if(fan[i].r){
			//遇到开始端 
			sum++;
			if(sum==k){
				shu[ans++]=fan[i].l;
			}
		}
		else{
			//结束端
			 if(sum==k){
			 	shu[ans++]=fan[i].l;
			 }
			sum--;//判断完,再减减 
			//因为结束了一个区间 
		} 
	} 
    printf("%d\n",ans/2);
    for(int i=0;i<ans;i+=2)
        printf("%d %d\n",shu[i],shu[i+1]);
	return 0;
}

值得注意的是该题后台时间有限,卡了cin,cout,开始我用cin,cout提交显示时间超限,更改为scanf和printf之后才正确。 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值