zoukankan      html  css  js  c++  java
  • Codeforces Round 97B 点分治

    B. Superset
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    A set of points on a plane is called good, if for any two points at least one of the three conditions is true:

    • those two points lie on same horizontal line;
    • those two points lie on same vertical line;
    • the rectangle, with corners in these two points, contains inside or on its borders at least one point of the set, other than these two. We mean here a rectangle with sides parallel to coordinates' axes, the so-called bounding box of the two points.

    You are given a set consisting of n points on a plane. Find any good superset of the given set whose size would not exceed 2·105 points.

    Input

    The first line contains an integer n (1 ≤ n ≤ 104) — the number of points in the initial set. Next n lines describe the set's points. Each line contains two integers xi and yi ( - 109 ≤ xi, yi ≤ 109) — a corresponding point's coordinates. It is guaranteed that all the points are different.

    Output

    Print on the first line the number of points m (n ≤ m ≤ 2·105) in a good superset, print on next m lines the points. The absolute value of the points' coordinates should not exceed 109. Note that you should not minimize m, it is enough to find any good superset of the given set, whose size does not exceed 2·105.

    All points in the superset should have integer coordinates.

    Examples
    input
    Copy
    2
    1 1
    2 2
    output
    Copy
    3
    1 1
    2 2
    1 2


    题意:给定n个点,请添加一些点,使任意两点满足:

    ①在同一条水平线或竖直线上

    ②或构成一个矩形框住其他点。

    输出添加最少点后,满足条件的点集。

    思路:将点集按x从小到大排序,取中间的点m的x坐标做一条直线l,取其他的点在其上的投影,将这些点加入点集,此时点m与其他所有点之间都已满足条件且在l两端的任意两点也互相满足条件,之后将区间二分递归操作,即可得到最后的点集。

    代码:

     1 #include"bits/stdc++.h"
     2 #include"cstdio"
     3 #include"map"
     4 #include"set"
     5 #include"cmath"
     6 #include"queue"
     7 #include"vector"
     8 #include"string"
     9 #include"cstring"
    10 #include"ctime"
    11 #include"iostream"
    12 #include"cstdlib"
    13 #include"algorithm"
    14 #define db double
    15 #define ll long long
    16 #define vec vector<ll>
    17 #define mt  vector<vec>
    18 #define ci(x) scanf("%d",&x)
    19 #define cd(x) scanf("%lf",&x)
    20 #define cl(x) scanf("%lld",&x)
    21 #define pi(x) printf("%d
    ",x)
    22 #define pd(x) printf("%f
    ",x)
    23 #define pl(x) printf("%lld
    ",x)
    24 //#define rep(i, x, y) for(int i=x;i<=y;i++)
    25 #define rep(i, n) for(int i=0;i<n;i++)
    26 const int N   = 1e6 + 5;
    27 const int mod = 1e9 + 7;
    28 const int MOD = mod - 1;
    29 const int inf = 0x3f3f3f3f;
    30 const db  PI  = acos(-1.0);
    31 const db  eps = 1e-10;
    32 using namespace std;
    33 typedef pair<int,int> P;
    34 P a[10005];
    35 set <P> s;
    36 void dfs(int l,int r)
    37 {
    38     int mid=(l+r)>>1;
    39     int x=a[mid].first;
    40     for(int i=l;i<=r;i++){
    41        s.emplace(P(x,a[i].second));//去重+不改变顺序
    42     }
    43     if(l<mid){
    44         dfs(l,mid-1);
    45     }
    46     if(r>mid){
    47         dfs(mid+1,r);
    48     }
    49 
    50 }
    51 int main()
    52 {
    53     int n;
    54     ci(n);
    55     for(int i=0;i<n;i++){
    56         ci(a[i].first),ci(a[i].second);
    57     }
    58     sort(a,a+n);
    59     for(int i=0;i<n;i++) s.insert(a[i]);
    60     dfs(0,n-1);
    61     pi(s.size());
    62     for(auto &it: s){//遍历
    63         printf("%d %d
    ",it.first,it.second);
    64     }
    65 }
     
  • 相关阅读:
    Shell使用
    从一道面试题谈linux下fork的运行机制
    老了,问题定位难了,xml编码解析
    javacc
    C++概述
    Notepad++中设置Windows、Unix、Mac三种行尾换行符格式间的转换
    玩转html5(一)-----盘点html5新增的那些酷酷的input类型和属性
    Java Drp项目实战—— 环境搭建
    cocos2d-x游戏开发 跑酷(八) 对象管理 碰撞检測
    电话拨号盘(带触摸振动反馈)
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/8995146.html
Copyright © 2011-2022 走看看