A - Superset CodeForces - 97B(人生第一个分治法,感觉,像二分啊。。)
/*
分治法,第一次做不是很懂,借鉴了神犇代码,但实操之后感觉像二分,,可能做得少了或者就是。。。。
*/
题目大意:
一个集合里有若干点,要求你添加某些点后保证这个集合里的任意两点满足以下三个条件中至少一个:
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(人生第一个分治法,感觉,像二分啊。。)的更多相关文章
- Codeforces 448C Painting Fence(分治法)
题目链接:http://codeforces.com/contest/448/problem/C 题目大意:n个1* a [ i ] 的木板,把他们立起来,变成每个木板宽为1长为 a [ i ] 的栅 ...
- 分治法(一)(zt)
这篇文章将讨论: 1) 分治策略的思想和理论 2) 几个分治策略的例子:合并排序,快速排序,折半查找,二叉遍历树及其相关特性. 说明:这几个例子在前面都写过了,这里又拿出来,从算法设计的策略的角度把它 ...
- C语言实现快速排序法(分治法)
title: 快速排序法(quick sort) tags: 分治法(divide and conquer method) grammar_cjkRuby: true --- 算法原理 分治法的基本思 ...
- p1257 平面上最接近点对---(分治法)
首先就是一维最接近点的情况... #include<iostream> #include<cstdio> #include<cstring> #include< ...
- 分治法——快速排序(quicksort)
先上代码 #include <iostream> using namespace std; int partition(int a[],int low, int high) { int p ...
- python 实现分治法的几个例子
分治法所能解决的问题一般具有以下几个特征: 1) 该问题的规模缩小到一定的程度就可以容易地解决 2) 该问题可以分解为若干个规模较小的相同问题,即该问题具有最优子结构性质. 3) 利用该问题分解出的子 ...
- Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)
1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...
- 分治法及其python实现例子
在前面的排序算法学习中,归并排序和快速排序就是用的分治法,分治法作为三大算法之一的,有非常多的应用例子. 分治法概念 将一个复杂的问题分成两个或更多的相同或相似的子问题,再把子问题分成更小的子问题-- ...
- 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 ...
随机推荐
- Bootstrap简单入门
Bootstrap简单入门 BootStrap基本模板 <!DOCTYPE html> <html> <head> <meta charset="U ...
- 高性能流媒体服务器EasyDarwin
标准RTSP拉模式直播(EasyRelayModule):适合内部监控 分布式部署(EasyCMSModule):负载均衡主要是用Reids作为负载
- Composer 自动加载(autoload)机制
自动加载的类型 总体来说 composer 提供了几种自动加载类型 classmap psr-0 psr-4 files 这几种自动加载都会用到,理论上来说,项目代码用 psr-4 自动加载, hel ...
- swiper隐藏再显示出现点击不了情况
//初始化swiper var swiper = new Swiper('.swiper-container', { pagination: '.swiper-pagination', nextBut ...
- wait&waitpid状态值
[wait&waitpid状态值] 1. python 中 os.system 的返回值的format与wait的返回值status一致: On Unix, the return value ...
- 《设计模式》-原则四:接口隔离原则(ISP)
啊!天气很热啊,回来洗个澡,做个饭吃完后 又出了一身汗,真后悔先洗澡. 加油坚持学习,今天要学的是“接口隔离原则” 意思是说:在设计的时候使用多个专门的接口比使用一个总的接口好很多.一个类对另一个类的 ...
- urllib2模块初体验———豆瓣读书页面下载小爬虫
我也是根据:http://blog.csdn.net/pleasecallmewhy/article/details/8927832 ,来写出豆瓣读书的爬虫,废话不说直接上代码: #!/usr/bin ...
- HDU1693 Eat the Trees(zerojudge a228)
传送门: https://zerojudge.tw/ShowProblem?problemid=a228 http://acm.hdu.edu.cn/showproblem.php?pid=1693 ...
- hdu 1495 非常可乐 (广搜)
题目链接 Problem Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶 ...
- 20165227朱越 预备作业3 Linux安装及学习
预备作业3 Linux安装及学习 Linux的安装 虚拟机的安装远没有想象中的那样容易,下载还没有出现什么问题,当我安装的时候,第一个问题出现在创建虚拟机时选择安装的虚拟机版本和类型的时候的错误 当时 ...