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
题意:在二维平面上,给你n个点的坐标,求任取四点构成正方形的个数
题解:好吧,暴力O(n^4)又t了……
那么折半枚举吧
枚举两个点连成一条线,然后根据这条线可以得到正方形的另外两个点,根据hash可以快速确定点是否在n个点中,即可以计算出正方形的个数
代码如下:
#include<map>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std; struct node
{
int x,y;
} a[]; vector<long long> g[];
int n; int cmp(node x,node y)
{
if(x.x==y.x)
{
return x.y<y.y;
}
return x.x<y.x;
} int main()
{ while(scanf("%d",&n)==&&n)
{
int ans=;
vector<long long> g[];
for(int i=; i<=n; i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
sort(a+,a+n+,cmp);
for(int i=; i<=n; i++)
{
long long key=(a[i].x*a[i].x+a[i].y*a[i].y)%;
g[key].push_back(i);
}
for(int i=; i<=n; i++)
{
for(int j=i+; j<=n; j++)
{
int x1,y1,x2,y2;
int dx=(a[j].x-a[i].x),dy=(a[j].y-a[i].y);
// if(dx<0||dy<0)
// {
// continue;
// }
x1=a[i].x-dy;
y1=a[i].y+dx;
x2=a[j].x-dy;
y2=a[j].y+dx;
int flag=;
long long key=(x1*x1+y1*y1)%;
for(int k=; k<g[key].size(); k++)
{
if(a[g[key][k]].x==x1&&a[g[key][k]].y==y1)
{
flag+=;
}
}
key=(x2*x2+y2*y2)%;
for(int k=; k<g[key].size(); k++)
{
if(a[g[key][k]].x==x2&&a[g[key][k]].y==y2)
{
flag+=;
}
}
if(flag==)
{
// if(a[i].x==x1&&a[i].y==y1||a[j].x==x2&&a[j].y==y2||a[i].x==x2&&a[i].y==y2||a[j].x==x1&&a[j].y==y1)
// {
// continue;
// }
// printf("%d %d\n",dx,dy);
// printf("%d %d %d %d %d %d %d %d\n",a[i].x,a[i].y,a[j].x,a[j].y,x1,y1,x2,y2);
ans++;
}
}
}
printf("%d\n",ans/);
} }


poj2002 Squares(hash+折半枚举)的更多相关文章

  1. poj1840 Eqs(hash+折半枚举)

    Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...

  2. 折半枚举+Hash(HDU1496升级版)

    题目链接:N - 方程的解 给定一个四元二次方程: Ax1^2+Bx2^2+Cx3^2+Dx4^2=0 试求−1000≤x1,x2,x3,x4≤1000非零整数解的个数. −10000≤A,B,C,D ...

  3. Load Balancing 折半枚举大法好啊

    Load Balancing 给出每个学生的学分.   将学生按学分分成四组,使得sigma (sumi-n/4)最小.         算法:   折半枚举 #include <iostrea ...

  4. CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。

    1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] ...

  5. NYOJ 1091 超大01背包(折半枚举)

    这道题乍一看是普通的01背包,最最基础的,但是仔细一看数据,发现普通的根本没法做,仔细观察数组发现n比较小,利用这个特点将它划分为前半部分和后半部分这样就好了,当时在网上找题解,找不到,后来在挑战程序 ...

  6. Codeforces 888E - Maximum Subsequence(折半枚举(meet-in-the-middle))

    888E - Maximum Subsequence 思路:折半枚举. 代码: #include<bits/stdc++.h> using namespace std; #define l ...

  7. Codeforces 912 E.Prime Gift (折半枚举、二分)

    题目链接:Prime Gift 题意: 给出了n(1<=n<=16)个互不相同的质数pi(2<=pi<=100),现在要求第k大个约数全在所给质数集的数.(保证这个数不超过1e ...

  8. poj_3977 折半枚举

    题目大意 给定N(N<=35)个数字,每个数字都<= 2^15. 其中一个或多个数字加和可以得到s,求出s的绝对值的最小值,并给出当s取绝对值最小值时,需要加和的数字的个数. 题目分析 需 ...

  9. POJ 3977 Subset(折半枚举+二分)

    SubsetTime Limit: 30000MS        Memory Limit: 65536KTotal Submissions: 6754        Accepted: 1277 D ...

随机推荐

  1. dxjk中 支付宝二维码支付 git 存疑

    线上的vendor/latrell/alipay 文件拉取不了至本地,失去了git监控 要想本地使用 1.注释掉config/app.php 'providers' 下的Latrell模块 2.下载线 ...

  2. Erlang基础 -- 介绍 -- Erlang特点

    前言 Erlang是具有多重范型的编程语言,具有很多特点,主要的特点有以下几个: 函数式 并发性 分布式 健壮性 软实时 热更新 递增式代码加载 动态类型 解释型 函数式 Erlang是函数式编程语言 ...

  3. 我的HibernateSearch笔记

    话不多说,直接上代码: 实体类: package com.smt.pojo; import java.io.Serializable; import javax.persistence.Column; ...

  4. (转)pipe row的用法, Oracle split 函数写法.

    本文转载自:http://www.cnblogs.com/newsea/archive/2010/12/14/1905482.html 关于 pipe row的用法2009/12/30 14:53 = ...

  5. oracle里的统计信息

    1 oracle里的统计信息 Oracle的统计信息是这样的一组数据,存储在数据字典,从多个维度描述了oracle数据库对象的详细信息,有6种类型 表的统计信息:记录数.表块的数量.平均行长度等 索引 ...

  6. zabbix短信监控

    [ ] zabbix-短信报警(参考http://hanyun.blog.51cto.com/1060170/1604918 ) [ ] zabbix-电话报警(参考http://dl528888.b ...

  7. C++对Lua中table进行读取、修改和创建

    C++代码: // LuaAndC.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #i ...

  8. mysql数据增删查授权

    一 介绍 MySQL数据操作: DML ======================================================== 在MySQL管理软件中,可以通过SQL语句中的 ...

  9. DPtoLP/LPtoDP 和 ScreenToClient/ClientToScreen

    设备坐标(Device Coordinate)又称为物理坐标(Physical Coordinate),是指输出设备上的坐标.通常将屏幕上的设备坐标称为屏幕坐标.设备坐标用对象距离窗口左上角的水平距离 ...

  10. c++builder Active Form

    新增的属性.方法刷新一下才可以生成方法的实现.保存按钮不生成,刷新就好了. Refresh Implemention