您的位置:首页 > 其它

(CodeForces - 612D)The Union of k-Segments

2017-09-28 21:21 399 查看

(CodeForces - 612D)The Union of k-Segments

time limit per test:4 seconds

memory limit per test:256 megabytes

input:standard input

output:standard output

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个线段问至少被覆盖k次的点的集合。

思路:将线段排序,用cnt从左到右计数,碰到左括号+1,碰到右括号-1,如果cnt等于k时输出左右括号的端点值就是所要的答案。(可以在草稿纸上划一下验证)

#include<cstdio>
#include<algorithm>
using namespace std;

const int maxn=1000005;
int ans[maxn<<1];

struct node
{
int left,x;
}a[maxn<<1];

bool cmp(node a,node b)
{
return ((a.x<b.x)||((a.x==b.x)&&a.left>b.left));
}

int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))
{
int tot=0;
for(int i=0;i<n;i++)
{
int l,r;
scanf("%d%d",&l,&r);
a[tot].x=l;
a[tot++].left=1;
a[tot].x=r;
a[tot++].left=0;
}
sort(a,a+tot,cmp);
int cnt=0,tot1=0;
for(int i=0;i<tot;i++)
{
if(a[i].left)
{
cnt++;
if(cnt==k) ans[tot1++]=a[i].x;
}
else
{
if(cnt==k) ans[tot1++]=a[i].x;
cnt--;
}
}
printf("%d\n",tot1/2);
for(int i=0;i<tot1;i+=2) printf("%d %d\n",ans[i],ans[i+1]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: