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成为划分后 ...
随机推荐
- leecode刷题(17)-- 实现StrStr
leecode刷题(17)-- 实现StrStr 实现StrStr 描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串 ...
- Code Chef February Challenge 2019题解
传送门 \(HMAPPY2\) 咕 话说这题居然卡\(scanf\)的么??? int T;cin>>T; while(T--){ cin>>n>>a>> ...
- html实现时间输入框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Github 升级到 Rails 5.2.1 了
简评:之前用的可是 3.2,早就该升级了啊. Github 的 Rails 升级花了大约一年半的时间,这是有原因的,首先,Rails 本身的升级并不总是平滑的,有些版本有重大改变(breaking c ...
- C++与C的区别二
1. new,delete的局部重载: #include <iostream> using namespace std; ; class myclass { public: myclass ...
- iOS学习笔记(7)——解析json中的中文
NSURL *url = [NSURL URLWithString:@"http://nycode.sinaapp.com/d.php"]; NSError *error = ni ...
- QuantLib 金融计算——随机过程之 Heston 过程
目录 QuantLib 金融计算--随机过程之 Heston 过程 Heston 过程 参考文献 如果未做特别说明,文中的程序都是 Python3 代码. QuantLib 金融计算--随机过程之 H ...
- (六)Audio子系统之AudioRecord.release
在上一篇文章<(五)Audio子系统之AudioRecord.stop>中已经介绍了AudioRecord如何暂停录制,接下来,继续分析AudioRecord方法中的release的实 ...
- 没事用html5 canvas画一个仪表盘自用,自适应的哦
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- java常量类的实现方式_枚举类_项目实践
前言 众所周知,系统里有很多比如订单状态.审核状态:性别.结算方式.交易类型等属性,这些属性只有几个值,一般用0.1.2.3等的数字标识存入数据库,每次对这些属性所属对象的增删改操作,都会在代码里给状 ...