Educational Codeforces Round 4 D 扫描线思想



链接:戳这里


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


题意:

给定n个线段[l,r],问哪些区间正好被线段覆盖了k次。输出这样的区间个数,以及区间


思路:

对于每一条线段,l即为进入,r即为出去。如果当前l进入了之后,使得区间覆盖值==k,那么这个l肯定是可以匹配相应的一个r的。对于r出去以后,相应的之前肯定存在一个l与之匹配


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
vector<pair<ll,int> > V;
vector<ll> anw;
int n,k;
int main(){
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        ll l,r;
        scanf("%I64d%I64d",&l,&r);
        V.push_back(make_pair(l,-1));
        V.push_back(make_pair(r,1));
    }
    sort(V.begin(),V.end());
    int num=0;
    for(int i=0;i<V.size();i++){
        if(V[i].second==-1){
            num++;
            if(num==k) anw.push_back(V[i].first);
        } else {
            if(num==k) anw.push_back(V[i].first);
            num--;
        }
    }
    printf("%d\n",anw.size()/2);
    for(int i=0;i<anw.size();i+=2) printf("%I64d %I64d\n",anw[i],anw[i+1]);
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值