原题链接:http://poj.org/problem?id=2002
博主的中文题面:
https://www.luogu.org/problemnew/show/T23926

Squares

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个点可以构成n^2/2条边,对于每一条边,都可以查找对应的可以构成正方形边,这个过程可以二分查找,也可以用哈希,因为每个正方形会被两组对边统计两次,所以需要除以二。

代码
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int M=1005;
struct pt{int x,y;};
bool operator == (const pt &a,const pt &b){return a.x==b.x&&a.y==b.y;}
bool operator < (const pt &a,const pt &b){return a.x!=b.x?a.x<b.x:a.y<b.y;}
pt p[M];
int n;
void in()
{for(int i=1;i<=n;++i)scanf("%d%d",&p[i].x,&p[i].y);
}
int find(int le,int ri,pt x)
{int mid=(le+ri)>>1;if(p[mid]==x) return mid;if(le==ri) return -1;if(p[mid]<x) return find(mid+1,ri,x);return find(le,mid,x);
}
void ac()
{long long ans=0;sort(p+1,p+1+n);pt f;for(int i=1;i<=n;++i)for(int j=i+1;j<=n;++j){f.x=p[i].x-p[j].y+p[i].y;f.y=p[i].y+p[j].x-p[i].x;if(find(1,n,f)<0) continue;f.x=p[j].x-p[j].y+p[i].y;f.y=p[j].y+p[j].x-p[i].x;if(find(1,n,f)<0) continue;ans++;}printf("%lld\n",ans/2);
}
void reset()
{memset(p,0,sizeof(p));
}
int main()
{while(scanf("%d",&n)&&n){reset();in();ac();}return 0;
}

POJ2002 Squares相关推荐

  1. cjson构建_[置顶] cJSON库(构建json与解析json字符串)-c语言

    一.c语言获取json中的数据. 1.先要有cJOSN库,两个文件分别是cJSON.c和cJSON.h. 2.感性认识 char * json = "{ \"json\" ...

  2. R语言层次聚类:通过内平方和(Within Sum of Squares, WSS)选择最优的聚类K值、以内平方和(WSS)和K的关系并通过弯头法(elbow method)获得最优的聚类个数

    通过内平方和(Within Sum of Squares, WSS)选择最佳的聚类K值.以内平方和(WSS)和K的关系并通过弯头法(elbow method)获得最佳的聚类个数 目录

  3. R构建加权最小二乘回归模型(Weighted Least Squares Regression)

    R构建加权最小二乘回归模型(Weighted Least Squares Regression) 目录 R构建加权最小二乘回归模型(Weighted Least Squares Regression)

  4. 数学知识--Methods for Non-Linear Least Squares Problems(第三章)

    Methods for Non-Linear Least Squares Problems 非线性最小二乘问题的方法 2nd Edition, April 2004 K. Madsen, H.B. N ...

  5. 数学知识--Methods for Non-Linear Least Squares Problems(第二章)

    Methods for Non-Linear Least Squares Problems 非线性最小二乘问题的方法 2nd Edition, April 2004 K. Madsen, H.B. N ...

  6. 数学知识--Methods for Non-Linear Least Squares Problems(第一章)

    Methods for Non-Linear Least Squares Problems 非线性最小二乘问题的方法 2nd Edition, April 2004 K. Madsen, H.B. N ...

  7. poj2002 poj3432 正方形个数 (hash,二分)

    给出一堆点,求其中正方形的个数. 题目很简单,如果枚举,复杂度为O(n^4),肯定超时,我们利用hash,或者二分来降低复杂度,枚举其中的两个点,然后利用正方形的性质求出其余的两个点,然后判断这两个点 ...

  8. [LeetCode]Perfect Squares

    题目链接:Perfect Squares 题目内容: Given a positive integer n, find the least number of perfect square numbe ...

  9. 4kyu Sums of Perfect Squares

    4kyu Sums of Perfect Squares 题目背景: The task is simply stated. Given an integer n (3 < n < 109) ...

  10. POJ - 2002 Squares 数正方形【二分】【哈希表】

    Squares POJ - 2002 题意 平面上有N个点,任取4个点,求能组成正方形的不同组合方式有多少种:相同的四个点,不同顺序构成的正方形视为同一正方形 分析 做法1 先枚举两个点,通过计算得到 ...

最新文章

  1. xuniji查看php.ini,虚拟机 phpstorm xdebug配置
  2. java 0-9所有排列_java实现:键盘输入从0~9中任意5个数,排列组合出所有不重复的组合,打印出来...
  3. 详解C#自动注册Dll(OCX)控件的两种方法(转)
  4. linux 锐捷客户端登录密码,Linux使用经验_使用锐捷客户端登录校园网
  5. Java 删除文件夹
  6. 自制狂拍灰太狼小游戏(HTML+CSS+JavaScript)
  7. 关于Eclipse安装插件出现An error occurred while collecting items to be installed解决办法
  8. AutoCAD2010激活闪退
  9. Error opening device
  10. Spring Security OAuth2 开发指南
  11. 【转】艺术设计、数字媒体、环艺、影视动画、摄影、广编专业…等…视频、教程、资讯、图库、作品汇总大全
  12. MFI认证与PPID
  13. 徐磊英语 4 5 时态,被动,时态的错误定义
  14. 一个简单有效的兼容IE7浏览器的办法
  15. 深度解析dubbo源码系列
  16. 超级计算机在天文学的应用,破世界记录:交大π2.0超算系统实现天文学N体模拟粒子数新突破...
  17. python3中 operator模块用法介绍
  18. 基于 DataOps 的运营商大数据平台探索
  19. 开源超融合私有云神器proxmox VE
  20. python身体指数BMI

热门文章

  1. servlet后端连接 微信小程序与_微信小程序授权登录
  2. 2020牛客多校训练1 I 1or2(拆点拆边带花树一般图最大匹配)
  3. cas 4.0 mysql 验证_在Ja-sig CAS中使用MySQL数据库进行身份验证
  4. java read bytes 阻塞_InputStream中read()与read(byte[] b)java InputStream读取数据问题 | 学步园...
  5. 查询字符串中子字符串所有出现位置
  6. 在Silverlight中使用HierarchicalDataTemplate为TreeView实现递归树状结构
  7. 动态生成WebService的客户端
  8. 在SQL server中查找特定类别的列
  9. asp.net身份验证
  10. mysql数据库索引使用总结和对比