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成为划分后 ...
随机推荐
- BruteXSS(汉化版)
BruteXSS是一个非常强大和快速的跨站点脚本暴力注入.它用于暴力注入一个参数.该BruteXSS从指定的词库加载多种有效载荷进行注入并且使用指定的载荷和扫描检查这些参数很容易受到XSS漏洞.得益于 ...
- ElasticSearch速学 - IK中文分词器远程字典设置
前面已经对”IK中文分词器“有了简单的了解: 但是可以发现不是对所有的词都能很好的区分,比如: 逼格这个词就没有分出来. 词库 实际上IK分词器也是根据一些词库来进行分词的,我们可以丰富这个词库. ...
- 在 iOS 中使用 iconfont
如何使用自定义字体 在讲icon font之前,首先先来看看普通自定义字体是如何在ios中使用的,两个原理是一样的.这里以KaushanScript-Regular为例: Step 1: 导入字体 ...
- QuantLib 金融计算——基本组件之 InterestRate 类
目录 QuantLib 金融计算--基本组件之 InterestRate 类 InterestRate 对象的构造 一些常用的成员函数 如果未做特别说明,文中的程序都是 Python3 代码. Qua ...
- python学习,day3:函数式编程,递归和高阶函数
# coding=utf-8 # Author: RyAn Bi def calc(n): #递归 print(n) if int(n/2) > 0: #设置条件,否则会循环999 次,报错, ...
- 算法图解学习笔记01:二分查找&大O表示法
二分查找 二分查找又称折半查找,其输入的必须是有序的元素列表.二分查找的基本思想是将n个元素分成大致相等的两部分,取a[n/2]与x做比较,如果x=a[n/2],则找到x,算法中止:如果x<a[ ...
- jackson构建复杂Map的序列化
1.序列化复杂的Map时,可能需要下面的方式 import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson. ...
- Linux网络编程服务器模型选择之并发服务器(下)
前面两篇文章(参见)分别介绍了循环服务器和简单的并发服务器网络模型,我们已经知道循环服务器模型效率较低,同一时刻只能为一个客户端提供服务,而且对于TCP模型来说,还存在单客户端长久独占与服务器的连接, ...
- Window10安装Django,并创建第一个Django项目
1.在cmd中输入pip install Django==1.11.7,安装的版本为:1.11.7. 2.安装完成后输入: >>> import django >>> ...
- 枚举类型与Switch
1.枚举类型,就是一个集合,集合内所有的元素都是枚举类型的, 主要是应用在可预计的集合中,(你知道它的值就只有那么几种情况,这时就可以使用枚举类型) 如: //结果一般只有两种,成功与失败 publi ...