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个区间,问你被覆盖至少k次的区间(两端连续区间可以合并)最少有多少个,并输出

解 :先把线段按左端点由小到大排序,再把他们放入优先队列,以线段右端点小的在上面,如果队列里面有k个,那么计算这k个的重叠部分,大于k个的时候,把最上面的取出到只有k个,再计算 ,显然重叠部分的左端点为最后放进去的线段的左端点,因为最后放进去左端点值最大,重叠部分右端点为队列最上面的线段右端点值,如果左端点大于右端点说明没有重叠,有所有重叠部分后 ,排序合并输出

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<math.h>
#include<queue>
#include<map>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
struct node
{
    ll x,y;
    friend bool operator < (node n1,node n2)
     {
         return n2.y<n1.y;
     }
}e[1100000];
int cmp(node n1,node n2)
{
    return n1.x<n2.x;
}
struct no
{
    ll l,r;
}aa[100000],bb[100000];
int cmp1(no n1,no n2)
{
    if(n1.l==n2.l)
       return n1.r<n2.r;
    return n1.l<n2.l;
}
int n,k;
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)
      scanf("%lld%lld",&e[i].x,&e[i].y);
    sort(e,e+n,cmp);
    priority_queue<node>q;
    int tot=0,num=0;
    for(int i=0;i<n;i++)
    {
        ll l=e[i].x,r;
        q.push(e[i]);
        tot++;
        if(tot>k)
        {
            q.pop();
            tot--;
        }
        if(tot==k)
        {
           node u=q.top();
           r=u.y;
           if(r>=l)
           {
               aa[num].l=l;
               aa[num++].r=r;
           }
        }
    }
    sort(aa,aa+num,cmp1);
    if(num==0)
    {
        printf("0\n");
        return 0;
    }
    int numm=0;
    ll l=aa[0].l,r=aa[0].r;
    for(int i=1;i<num;i++)
    {
        if(aa[i].l<=r)
           r=aa[i].r;
        else
        {
          bb[numm].l=l;
          bb[numm++].r=r;
          l=aa[i].l;
          r=aa[i].r;
        }
    }
    bb[numm].l=l;
    bb[numm++].r=r;
    printf("%d\n",numm);
    for(int i=0;i<numm;i++)
        printf("%lld %lld\n",bb[i].l,bb[i].r);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值