zoukankan      html  css  js  c++  java
  • codeforces 612D The Union of k-Segments (线段排序)

    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.

    Sample test(s)
    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

     题意:问这些线段覆盖的不小于k次的线段有哪些,注意有可能是一个点;

    思路:把起点和终点标记后排序,用一个计数器当等于k事说明符合条件线段的起点或终点,加入容器中,注意排序的时候如果坐标相同一定是起点在终点前,不然点的情况会漏掉,最后还要把可以合并的区间合并;

    AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    const int N=1e6+5;
    int a,b;
    struct node
    {
        int fi,se;
    };
    node po[2*N];
    int cmp(node x,node y)
    {
        if(x.fi==y.fi)return x.se<y.se;
        return x.fi<y.fi;
    }
    vector < int >ve;
    int ans[2*N];
    int main()
    {
        int n,k;
        scanf("%d%d",&n,&k);
        for(int i =0;i<2*n;i+=2)
        {
            scanf("%d%d",&po[i].fi,&po[i+1].fi);
            po[i].se=-1;
            po[i+1].se=1;
        }
        sort(po,po+2*n,cmp);
        int num=0;
        for(int i=0;i<2*n;i++)
        {
            if(po[i].se==-1)
            {
                num++;
                if(num==k)
                {
                    ve.push_back(po[i].fi);
                }
            }
            else
            {
                if(num==k)
                {
                    ve.push_back(po[i].fi);
                }
                num--;
            }
        }
        int cnt=0;
        int len=ve.size();
        if(len>0){
        ans[cnt++]=ve[0];
        for(int i=1;i<len-1;i+=2)
        {
            if(ve[i]==ve[i+1])
            {
                continue;
            }
            else
            {
                ans[cnt++]=ve[i];
                ans[cnt++]=ve[i+1];
            }
        }
        ans[cnt++]=ve[len-1];
        }
        printf("%d
    ",cnt/2);
        for(int i=0;i<cnt;i+=2)
        {
            printf("%d %d
    ",ans[i],ans[i+1]);
        }
        return 0;
    }
  • 相关阅读:
    c++ bitset 10进制转二进制
    PIVOT
    西渡
    check all tables rows

    View Triggers Function Procedure
    ORA-01400: cannot insert NULL into
    中东
    力的合成
    正弦、余弦和正切
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5164586.html
Copyright © 2011-2022 走看看