Squares
Time Limit: 3500MS   Memory Limit: 65536K
Total Submissions: 15137   Accepted: 5749

Description

A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with the latter property, however, as a regular octagon also has this property.

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

The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers) of each point. You may assume that the points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.

Output

For each test case, print on a line the number of squares one can form from the given stars.

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 几何二分 || 哈希的更多相关文章

  1. POJ 2002 Squares 几何, 水题 难度: 0

    题目 http://poj.org/problem?id=2002 题意 已知平面内有1000个点,所有点的坐标量级小于20000,求这些点能组成多少个不同的正方形. 思路 如图,将坐标按照升序排列后 ...

  2. POJ 2002 Squares【值得摸索的一道二分+点旋转】

    id=2002">Squares 很好的一道二分,事实上本来我是没有思路的,看了基神的题解之后才似乎明确了点. 题意:给出最多有1000个点,问这些点能够组成多少个正方形 分析:先想想 ...

  3. POJ 2774 后缀数组 || 二分+哈希

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 35607   Accepted: 14 ...

  4. POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)

    经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...

  5. POJ 2002 Squares 哈希

    题目链接: http://poj.org/problem?id=2002 #include <stdio.h> #include <string.h> ; struct Has ...

  6. POJ 2002 Squares 数学 + 必须hash

    http://poj.org/problem?id=2002 只能说hash比二分快很多.随便一个hash函数都可以完爆二分. 判断是否存在正方形思路如下: 1.枚举任意两个点,作为正方形的一条边,那 ...

  7. POJ 2002 Squares

    二分.... Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 14530 Accepted: 5488 Descr ...

  8. POJ 2002 Squares [hash]

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 16631   Accepted: 6328 Descript ...

  9. 2016vijos 1-1 兔子的字符串(后缀数组 + 二分 + 哈希)

    题意: 给出一个字符串,至多将其划分为n部分,每一部分取出字典序最大的子串ci,最小化 最大的ci 先看一个简化版的问题: 给一个串s,再给一个s的子串t,问能否通过将串划分为k个部分,使t成为划分后 ...

随机推荐

  1. [转] 测试环境下将centos6.8升级到centos7的操作记录

    1)查看升级前的版本信息 lsb_release -a LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noa ...

  2. day01 --class --home

    # 1.简述变量命名规范# 2.name = input(“>>>”) name变量是什么数据类型?# 3.if条件语句的基本结构? # 4.用print打印出下面内容:# ⽂能提笔 ...

  3. python中的字符串和编码

    了解编码之前首先说下这几个词的概率: 位.字节.字符.字符串 1.位(bit)也称为比特 这个其实很简单,因为计算机都是二进制存储数据,也就是0和1,一个0或者1就表示一位.这是计算机存储的最小单位. ...

  4. C#-WebForm-AJAX阿贾克斯(二)★★★★★ajax的完整结构★★★★★

    ajax完整结构: $.ajax({ url:"",//服务器路径 data:{},//给服务端传递的参数,可以没有,也可以是多个 type:"post", / ...

  5. CUDA安装

    1.CUDA是什么? CUDA(Compute Unified Device Architecture),显卡厂商NVidia推出的运算平台. 随着显卡的发展,GPU越来越强大,而且GPU为显示图像做 ...

  6. QuantLib 金融计算——基本组件之 Schedule 类

    目录 QuantLib 金融计算--基本组件之 Schedule 类 Schedule 对象的构造 作为"容器"的 Schedule 对象 一些常用的成员函数 如果未做特别说明,文 ...

  7. 编程开发之--java多线程学习总结(5)

    4.对继承自Runnable的线程进行锁机制的使用 package com.lfy.ThreadsSynchronize; import java.util.concurrent.locks.Lock ...

  8. 性能测试 vs 负载测试 vs 压力测试

    在做一些软件测试工作时,常常会被提及性能测试.负载测试.压力测试,这也是在软件测试方面最容易混淆的三个概念.之前和一个测试大牛聊天,他和我说常常面试一些测试人员会问一些这样的问题,大多人认为负载测试等 ...

  9. Kettle 系列随笔

    1.Kettle导入数据到Hive 出现多余的几行全部是null值的情况 2.Kettle根据表输入的SQL脚本返回创建表的SQL脚本 3.Kettle 行列互换之——行转列(多列数据合并成一列变为多 ...

  10. mfix中统计气泡体积

    先转换为point data 提取空隙率在0.45-1.0之间的网格,为后面提取气泡内网格做准备 把free board部分去掉 然后积分 选择cell data后就得到气泡内所有网格的体积和,如果网 ...