【codefores 612】 The Union of k-Segments 【思维+ 线段处理】

D. The Union of k-Segments
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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

写这道题 之前是先写了 这道题 C 题

都是对 线段问题的处理 ,这种处理方式可以 记录一下。

处理完之后,找一下规律就知道,cnt表示以当前点为起点之后的点被线段的重叠个数。 之后枚举所有端点,一个左端点cnt++,一个右端点cnt–。 同时记录一下 最后答案的左点和右点就行。
代码

#include<bits/stdc++.h>
using namespace std;
#define _int64 LL

const int MAXN = 1e6+10;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

struct Node{
    int p,t;
    Node(){}
    Node (int a,int b)  {  p=a; t=b; }
    bool operator<(Node b){
        if(b.p==p) return t<b.t;
        else return p<b.p;
    }
}P[MAXN*2];

vector < pair<int,int> > ve ;
int main(){
    int n,k;scanf("%d%d",&n,&k);
    int a,b,c;int tot=0;
    for(int i=1;i<=n;i++){
        scanf("%d%d",&a,&b);
        P[tot++]={a,-1};
        P[tot++]={b,1}; 
    }
    sort(P,P+tot);
    int l,r;int cnt=0;
    for(int i=0;i<tot;i++){
        if(P[i].t==-1) {
            cnt++;
            if(cnt==k) l=P[i].p;
        }else {
            cnt--;
          if(cnt==k-1) {
                r=P[i].p;
                ve.push_back({l,r});
            }
        }
    }
    printf("%d\n",ve.size());
    for(int i=0;i<ve.size();i++) 
        printf("%d %d\n",ve[i].first,ve[i].second);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值