Codeforces Round 97B 点分治
2 seconds
256 megabytes
standard input
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.
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.
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.
2
1 1
2 2
3
1 1
2 2
1 2
题意:给定n个点,请添加一些点,使任意两点满足:
①在同一条水平线或竖直线上
②或构成一个矩形框住其他点。
输出添加最少点后,满足条件的点集。
思路:将点集按x从小到大排序,取中间的点m的x坐标做一条直线l,取其他的点在其上的投影,将这些点加入点集,此时点m与其他所有点之间都已满足条件且在l两端的任意两点也互相满足条件,之后将区间二分递归操作,即可得到最后的点集。
代码:
#include"bits/stdc++.h"
#include"cstdio"
#include"map"
#include"set"
#include"cmath"
#include"queue"
#include"vector"
#include"string"
#include"cstring"
#include"ctime"
#include"iostream"
#include"cstdlib"
#include"algorithm"
#define db double
#define ll long long
#define vec vector<ll>
#define mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
//#define rep(i, x, y) for(int i=x;i<=y;i++)
#define rep(i, n) for(int i=0;i<n;i++)
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const int inf = 0x3f3f3f3f;
const db PI = acos(-1.0);
const db eps = 1e-;
using namespace std;
typedef pair<int,int> P;
P a[];
set <P> s;
void dfs(int l,int r)
{
int mid=(l+r)>>;
int x=a[mid].first;
for(int i=l;i<=r;i++){
s.emplace(P(x,a[i].second));//去重+不改变顺序
}
if(l<mid){
dfs(l,mid-);
}
if(r>mid){
dfs(mid+,r);
} }
int main()
{
int n;
ci(n);
for(int i=;i<n;i++){
ci(a[i].first),ci(a[i].second);
}
sort(a,a+n);
for(int i=;i<n;i++) s.insert(a[i]);
dfs(,n-);
pi(s.size());
for(auto &it: s){//遍历
printf("%d %d\n",it.first,it.second);
}
}
Codeforces Round 97B 点分治的更多相关文章
- Educational Codeforces Round 41 967 E. Tufurama (CDQ分治 求 二维点数)
Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama (CDQ分治 求 二维点数) time limit per test 2 ...
- Codeforces Round #372 (Div. 2)
Codeforces Round #372 (Div. 2) C. Plus and Square Root 题意 一个游戏中,有一个数字\(x\),当前游戏等级为\(k\),有两种操作: '+'按钮 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Educational Codeforces Round 64 部分题解
Educational Codeforces Round 64 部分题解 不更了不更了 CF1156D 0-1-Tree 有一棵树,边权都是0或1.定义点对\(x,y(x\neq y)\)合法当且仅当 ...
- CFEducational Codeforces Round 66题解报告
CFEducational Codeforces Round 66题解报告 感觉丧失了唯一一次能在CF上超过wqy的机会QAQ A 不管 B 不能直接累计乘法打\(tag\),要直接跳 C 考虑二分第 ...
- Educational Codeforces Round 64部分题解
Educational Codeforces Round 64部分题解 A 题目大意:给定三角形(高等于低的等腰),正方形,圆,在满足其高,边长,半径最大(保证在上一个图形的内部)的前提下. 判断交点 ...
- Codeforces Round #790 (Div. 4) A-H
Codeforces Round #790 (Div. 4) A-H A 题目 https://codeforces.com/contest/1676/problem/A 题解 思路 知识点:模拟. ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
随机推荐
- intellijidea课程 intellijidea神器使用技巧 3-4 alter+enter
alter enter ==> 创建函数 fi() ==> alter enter
- Java中的各种锁
.共享锁和排它锁的区别
- CSS的框模型(div)与边距(margin、padding)
所谓框模型,例如div标签,你就可以直接把它理解成一个相框. 这个相框里面的相片有高度和宽度,框本身也有一定的宽度.相框和别的相框之间,还有一定的边距. div设置常见属性 border:边框 pad ...
- python基础:冒泡和选择排序算法实现
冒泡排序和选择排序 首先引用一下百度百科对于冒泡算法的定义: 冒泡排序算法的原理如下: 比较相邻的元素.如果第一个比第二个大,就交换他们两个. 对每一对相邻元素做同样的工作,从开始第一对到结尾 ...
- jQuery实现焦点图[兼容ie7+]
HTML: <div class="freehand" id="freehand"> <h1>宠物手绘</h1> <d ...
- 一道Java集合框架题
问题:某班30个学生的学号为20070301-20070330,全部选修了Java程序设计课程,给出所有同学的成绩(可用随机数产生,范围60-100),请编写程序将本班各位同学的成绩按照从低到高排序打 ...
- Java Knowledge series 1
Programming language evolves always along with Compiler's evolvement JVM as Additional Indirection I ...
- 使用C#实现计划任务(corn job)
维基百科上是这样描述计划任务的: “Cron is a time-based job scheduler in Unix-like computer operating systems. Cron i ...
- matlab练习程序(粒子群优化PSO)
算法没有和图像处理直接相关,不过对于图像分类中的模式识别相关算法,也许会用到这个优化算法. 算法步骤: 1.首先确定粒子个数与迭代次数. 2.对每个粒子随机初始化位置与速度. 3.采用如下公式更新每个 ...
- Python3爬虫04(其他例子,如处理获取网页的内容)
#!/usr/bin/env python# -*- coding:utf-8 -*- import osimport reimport requestsfrom bs4 import Navigab ...