poj 2002 Squares 几何二分 || 哈希
| Time Limit: 3500MS | Memory Limit: 65536K | |
| Total Submissions: 15137 | Accepted: 5749 |
Description
So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x and y coordinates.
Input
Output
Sample Input
4
1 0
0 1
1 1
0 0
9
0 0
1 0
2 0
0 2
1 2
2 2
0 1
1 1
2 1
4
-2 5
3 7
0 0
5 2
0
Sample Output
1
6
1
Source
/*
题意:给你1000个点的坐标(x,y),让你找出能
构成正方形的个数。
思路:由于是1000,则枚举两个点,求出相应的另外
两个点的坐标。然后用二分判断是否两个点都存在。 就个人而言,关键在 "求出相应的另外两个点的坐标"
设两个点a1,a2;
由a1为中心,逆时针旋转求出
a3.x=a1.y-a2.y+a1.x;
a3.y=a2.x-a1.x+a1.y;
由a2为中心,顺时针旋转求出
a4.x=a1.y-a2.y+a2.x;
a4.y=a2.x-a1.x+a2.y;
由于被计算两次,所以除2
*/
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std; typedef struct
{
int x,y;
}node;
node a[];
bool cmp(node n1,node n2)
{
if( n1.x!=n2.x )
return n1.x<n2.x;
else return n1.y<n2.y;
}
bool query(int l,int r,node cur)
{
int mid;
while(l<=r)
{
mid=(l+r)/;
if( a[mid].x<cur.x || (a[mid].x==cur.x&&a[mid].y<cur.y))
l=mid+;
else if( a[mid].x>cur.x || ( a[mid].x==cur.x&&a[mid].y>cur.y))
r=mid-;
if( a[mid].x==cur.x && a[mid].y==cur.y) return true;
}
return false;
}
int main()
{
int n,i,j,num;
node a1,a2,a3,a4;
while(scanf("%d",&n)>)
{
if(n==)break;
for(i=;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
sort(a+,a++n,cmp); for(i=,num=;i<n;i++)
{
a1=a[i];
for(j=i+;j<=n;j++)
{
a2=a[j];
a3.x=a1.y-a2.y+a1.x;
a3.y=a2.x-a1.x+a1.y;
if( !query(,n,a3)) continue;
a4.x=a1.y-a2.y+a2.x;
a4.y=a2.x-a1.x+a2.y;
if( query(,n,a4)) num++; }
}
printf("%d\n",num/);
}
return ;
}
哈希做法:
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std; const int INF = ;
typedef struct
{
int x,y;
}node;
struct hash
{
int x;
int y;
struct hash *next;
};
struct hash hash_table[];
node a[INF+]; bool cmp(node n1,node n2)
{
if( n1.x!=n2.x )
return n1.x<n2.x;
else return n1.y<n2.y;
}
void Insert(int x,int y)
{
unsigned k=(x*x+y*y)%INF;
struct hash *new_hash;
new_hash=(struct hash *)malloc(sizeof(struct hash));
new_hash->x=x;
new_hash->y=y;//build new_hash->next=hash_table[k].next;
hash_table[k].next=new_hash;
}
bool found(int x,int y)
{
unsigned k=(x*x+y*y)%INF;
struct hash *new_hash;
new_hash=hash_table[k].next;
while(new_hash!=NULL)
{
if(new_hash->x==x && new_hash->y==y)break;
else new_hash=new_hash->next;
}
if(new_hash==NULL)return false;
else return true;
} int main()
{
int n,i,j,num;
node a1,a2,a3,a4;
while(scanf("%d",&n)>)
{
if(n==)break;
memset(hash_table,,sizeof(hash_table));
for(i=;i<=n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
Insert(a[i].x,a[i].y);
}
sort(a+,a++n,cmp); for(i=,num=;i<n;i++)
{
a1=a[i];
for(j=i+;j<=n;j++)
{
a2=a[j];
a3.x=a1.y-a2.y+a1.x;
a3.y=a2.x-a1.x+a1.y;
if(!found(a3.x,a3.y))continue;
a4.x=a1.y-a2.y+a2.x;
a4.y=a2.x-a1.x+a2.y;
if(found(a4.x,a4.y)==true)
num++;
}
}
printf("%d\n",num/);
}
return ;
}
poj 2002 Squares 几何二分 || 哈希的更多相关文章
- POJ 2002 Squares 几何, 水题 难度: 0
题目 http://poj.org/problem?id=2002 题意 已知平面内有1000个点,所有点的坐标量级小于20000,求这些点能组成多少个不同的正方形. 思路 如图,将坐标按照升序排列后 ...
- POJ 2002 Squares【值得摸索的一道二分+点旋转】
id=2002">Squares 很好的一道二分,事实上本来我是没有思路的,看了基神的题解之后才似乎明确了点. 题意:给出最多有1000个点,问这些点能够组成多少个正方形 分析:先想想 ...
- POJ 2774 后缀数组 || 二分+哈希
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 35607 Accepted: 14 ...
- POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)
经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...
- POJ 2002 Squares 哈希
题目链接: http://poj.org/problem?id=2002 #include <stdio.h> #include <string.h> ; struct Has ...
- POJ 2002 Squares 数学 + 必须hash
http://poj.org/problem?id=2002 只能说hash比二分快很多.随便一个hash函数都可以完爆二分. 判断是否存在正方形思路如下: 1.枚举任意两个点,作为正方形的一条边,那 ...
- POJ 2002 Squares
二分.... Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 14530 Accepted: 5488 Descr ...
- POJ 2002 Squares [hash]
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 16631 Accepted: 6328 Descript ...
- 2016vijos 1-1 兔子的字符串(后缀数组 + 二分 + 哈希)
题意: 给出一个字符串,至多将其划分为n部分,每一部分取出字典序最大的子串ci,最小化 最大的ci 先看一个简化版的问题: 给一个串s,再给一个s的子串t,问能否通过将串划分为k个部分,使t成为划分后 ...
随机推荐
- 如何在WS系统的DOS命令台打印JAVA_HOME变量
echo %JAVA_HOME% 查看环境变量 path 新增临时环境变量 path D:\test;%path% 注意是反斜杆 cls 清空 F7 显示历史CMD指令
- 脚本:定时释放 Linux/CentOS 缓存【转载自:杭州山不高】
定时释放Linux/CentOS缓存的脚本(yl_dropcaches)如下: #!/bin/bash used=`free -m | awk 'NR==2' | awk '{print $3}'` ...
- 现在就启用 HTTPS,免费的!
现在就启用 HTTPS,免费的! 现在,你应该能在访问https://konklone.com的时候,在地址栏里看到一个漂亮的小绿锁了,因为我把这个网站换成了HTTPS协议.一分钱没花就搞定了. 为什 ...
- Hibernate 查询数据库中的数据
1.Criteria介绍 Criteria与Session绑定,其生命周期跟随着Session结束而结束,使用Criteria时进行查询时,每次都要于执行时期动态建立物件,并加入各种查询条件,随着Se ...
- [Alpha]Scrum Meeting#5
github 本次会议项目由PM召开,时间为4月7日晚上10点30分 时长10分钟 任务表格 人员 昨日工作 下一步工作 木鬼 撰写博客目录 整理清明开会记录 SiMrua 模型再训练(issue#1 ...
- Service层异常处理
1.在service方法里面如果对异常进行了捕获的话,该事务是不会进行回滚的 * 默认spring事务只在发生未被捕获的 runtime excetpion()时才回滚. * * spring aop ...
- 开源项目-Aiguille
项目地址: https://github.com/wwkai555/Aiguille 这个项目主要使用Android L新特性 - 最新的widget以及一些值得推荐和使用的开源库比如butt ...
- 本地jar包 安装到本地仓库中的命令
maven 项目 本地jar包 安装到本地仓库中去: 首先进入到该文件所在文件夹内 若不在直接绝对路径就可以.注意命令中的空格 mvn install:install-file -Dfile=文件名 ...
- 【Kafka】Kafka集群搭建
一.准备工作 服务器:最好是多台,大于等于2 已经搭建好的zookeeper集群 下载软件kafka_2.11-0.10.0.1.tgz 二.创建目录 #创建目录 cd /opt/ mkdir kaf ...
- 关于禁止html缓存
在现代的浏览器里,为了增强用户体验,浏览器一般都会把网页上所需的静态文件缓存到本地,再次刷新的时候则无需再重新加载,但是我们有时候就是不需要浏览器缓存这些文件,而是每次都从服务器端读取数据,可以用以下 ...