zoukankan      html  css  js  c++  java
  • http://codeforces.com/contest/612/problem/D

    D. 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次的区间。

    题解:用线段扫描左端点(用-1标记)进入tmp++,右端点(用1标记)出来tmp--,当tmp为k时就是答案区间。

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<cstring>
    #include<algorithm> 
    #define pii pair<int,int> 
    #define pb push_back 
    #define mp make_pair 
    using namespace std;
    vector<int>ans;
    vector<pii>s; 
    int n,k; 
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin>>n>>k;
        for(int i=0;i<n;i++)
        {
            int a,b; 
            cin>>a>>b;
            s.pb(mp(a,-1));
            s.pb(mp(b,1)); 
        } 
        sort(s.begin(),s.end());
        int tmp=0; 
        for(int i=0;i<s.size();i++)
        {
            if(s[i].second==-1)
            {
                tmp++; 
                if(tmp==k)ans.pb(s[i].first); 
            }
            else
            {
                if(tmp==k)ans.pb(s[i].first); 
                tmp--;
            } 
        } 
        cout<<ans.size()/2<<endl; 
        for(int i=0;i<ans.size()/2;i++)
        {
            cout<<ans[2*i]<<" "<<ans[2*i+1]<<endl; 
        } 
    } 
  • 相关阅读:
    想学好H5的话,你一定要知道的9个网站!
    颜色拾取
    Linux ( Centos 7.3 x64) 安装 nginx (一)
    获取url链接 判断加上HTTPS
    使用PHPMailer发送邮件
    PHP 判断一维数组或者是二维数组
    谨以此纪念下今天开通了博客
    线上服务器运维问题记录
    使用requests遇到的坑
    一个java进程突然消失的问题
  • 原文地址:https://www.cnblogs.com/lhclqslove/p/7403848.html
Copyright © 2011-2022 走看看