/*

分治法,第一次做不是很懂,借鉴了神犇代码,但实操之后感觉像二分,,可能做得少了或者就是。。。。

*/

题目大意:

一个集合里有若干点,要求你添加某些点后保证这个集合里的任意两点满足以下三个条件中至少一个:

1.在一个水平线上 2.在一个竖直线上 3.两点组成的矩形之间有点.

解题思路:

神犇所讲,将点排序,在中间点处建一条竖直线,令其余点在其上投影,二分。

ps:不是很明白错误是什么,,结构体里重载<运算符可以,但是写个cmp函数就报错,不是很懂,贴上错误代码,如果神犇们知道什么错误,请评论告知,多谢;

错误代码

#include<iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <cstdio>
#include <iterator>
#include <sstream>
#include <cmath>
#include <deque>
using namespace std; struct node
{
int x;
int y;
// bool operator <(const node b)const
// {
// if(x==b.x) return y<b.y;
// else return x<b.x;
// }
};
int n;
node p1[];
set<node > p;
bool cmp(const node a,const node b)
{
return a.x<b.x;
}
void dfs(int l,int h)
{
if (l==h) return ;
int mid;
mid=(l+h)/;
for (int i=l; i<=h; i++)
{
node c;
c.x=p1[mid].x;
c.y=p1[i].y;
p.insert(c);
}
dfs(l,mid);
dfs(mid+,h);
}
int main()
{
cin>>n;
for (int i=; i<n; i++)
{
cin>>p1[i].x>>p1[i].y;
p.insert(p1[i]);
}
sort(p1,p1+n,cmp);
dfs(,n-);
cout<<p.size()<<endl;
set<node >::iterator it;
for (it=p.begin(); it!=p.end(); it++)
{
cout<<(*it).x<<" "<<(*it).y<<endl;
}
}

AC代码:

#include<iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <cstdio>
#include <iterator>
#include <sstream>
#include <cmath>
#include <deque>
using namespace std; struct node
{
int x;
int y;
bool operator <(const node b)const
{
if(x==b.x) return y<b.y;
else return x<b.x;
}
};
int n;
node p1[];
set<node > p;
bool cmp(const node a,const node b)
{
return a.x<b.x;
}
void dfs(int l,int h)
{
if (l==h) return ;
int mid;
mid=(l+h)/;
for (int i=l; i<=h; i++)
{
node c;
c.x=p1[mid].x;
c.y=p1[i].y;
p.insert(c);
}
dfs(l,mid);
dfs(mid+,h);
}
int main()
{
cin>>n;
for (int i=; i<n; i++)
{
cin>>p1[i].x>>p1[i].y;
p.insert(p1[i]);
}
sort(p1,p1+n);
dfs(,n-);
cout<<p.size()<<endl;
set<node >::iterator it;
for (it=p.begin(); it!=p.end(); it++)
{
cout<<(*it).x<<" "<<(*it).y<<endl;
}
}

A - Superset CodeForces - 97B(人生第一个分治法,感觉,像二分啊。。)的更多相关文章

  1. Codeforces 448C Painting Fence(分治法)

    题目链接:http://codeforces.com/contest/448/problem/C 题目大意:n个1* a [ i ] 的木板,把他们立起来,变成每个木板宽为1长为 a [ i ] 的栅 ...

  2. 分治法(一)(zt)

    这篇文章将讨论: 1) 分治策略的思想和理论 2) 几个分治策略的例子:合并排序,快速排序,折半查找,二叉遍历树及其相关特性. 说明:这几个例子在前面都写过了,这里又拿出来,从算法设计的策略的角度把它 ...

  3. C语言实现快速排序法(分治法)

    title: 快速排序法(quick sort) tags: 分治法(divide and conquer method) grammar_cjkRuby: true --- 算法原理 分治法的基本思 ...

  4. p1257 平面上最接近点对---(分治法)

    首先就是一维最接近点的情况... #include<iostream> #include<cstdio> #include<cstring> #include< ...

  5. 分治法——快速排序(quicksort)

    先上代码 #include <iostream> using namespace std; int partition(int a[],int low, int high) { int p ...

  6. python 实现分治法的几个例子

    分治法所能解决的问题一般具有以下几个特征: 1) 该问题的规模缩小到一定的程度就可以容易地解决 2) 该问题可以分解为若干个规模较小的相同问题,即该问题具有最优子结构性质. 3) 利用该问题分解出的子 ...

  7. Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)

    1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...

  8. 分治法及其python实现例子

    在前面的排序算法学习中,归并排序和快速排序就是用的分治法,分治法作为三大算法之一的,有非常多的应用例子. 分治法概念 将一个复杂的问题分成两个或更多的相同或相似的子问题,再把子问题分成更小的子问题-- ...

  9. Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays 二分

    D. Professor GukiZ and Two Arrays 题目连接: http://www.codeforces.com/contest/620/problem/D Description ...

随机推荐

  1. 并查集:POJ No1703 Find them, Catch them

    题目链接:http://poj.org/problem?id=1703 题意:两个坏蛋属于不同的组织,给出两个坏蛋判定是否一个组织. 题解:已知每次输入的两个帮派人员 x, y; 合并 (x, y + ...

  2. Linux下压缩文件-1

    tar负责打包,gzip负责压缩 tar-c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的 ...

  3. Flash数据的采集方法-搜房房价走势采集

    一般来说flash中的数据是不能被现有技术很容易采集到的,但是也不能谈flash色变,要具体问题具体分析,有些flash是可以通过一些分析发现背后的数据.然后采集就变得很容易了. 具体案例:搜房房价走 ...

  4. [php]mysqli操作流程

    <?php class SqlTools{ private $con; private $trans; public function __construct($host, $user, $ps ...

  5. 安装Win8引起Ubuntu启动项丢失的恢复过程

    画电路图的时候手痒,于是将之前做好的Win8PE拿出来装着玩儿.至于Win8的pE很好做,用UltraISO将Win8 的镜像用制作硬盘镜像的方法烧进U盘就行了. Win8的安装过程也很简单.安装前为 ...

  6. apache服务器yii2报The fileinfo PHP extension is not installed解决思路

    这个问题整整困扰了我两天,今天终于搞定了.记录一下. 背景是这样的,我呢,在centos服务器上安装了lamp环境,其中php是5.3.3,在用composer安装yii2的时候,出现了某些yii2插 ...

  7. c++ ACM常用函数

    1 保留小数点后两位 #include <iomanip> cout << setiosflags(ios::fixed) << setprecision(2)&l ...

  8. 2016.5.18——leetcode:Majority Element

    Majority Element 本题收获: 1.初步了解hash,nth_element的用法 2.题目的常规思路 题目: Given an array of size n, find the ma ...

  9. 4 - django-orm基本使用

    目录 1 数据库与ORM 2 orm的配置 2.1 引擎和配置 2.2 mysql驱动程序 3 orm 表模型 3.1 创建表对象 3.2 Django字段类型 3.3 常用字段参数说明 3.4 特殊 ...

  10. device tree --- #address-cells and #size-cells property【转】

    转自:http://www.cnblogs.com/youchihwang/p/7050846.html device tree source Example1 / { #address-cells ...