这个要注重二分的查找,,。先是二分查找的方法,

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<set>
#include<cstring>using namespace std;#define N 1001struct my
{int x,y;bool operator<(my b){if (b.x!=x)return x<b.x;else return y<b.y;}bool operator==(my b){if (b.x==x&&b.y==y)return true;elsereturn false;}void put(){cout<<x<<' '<<y<<"    ";}}
p[N];
int n;bool cmp(my a,my b)
{if (a.y!=b.y)return a.y<b.y;return a.x<b.x;
}bool find (my a)
{int i,j,k;int s=0;int m;int e=n-1;while (s<=e){m=(e+s)/2;if (p[m]==a){return true;}else if (p[m].x>a.x||(p[m].x==a.x&p[m].y>a.y)){e=m-1;}elses=m+1;}return false;
}int main()
{freopen("in.txt","r",stdin);int i,j,k;while (cin>>n,n){for (i=0;i<n;i++){cin>>p[i].x>>p[i].y;}sort(p,p+n);int ans=0;my a,b;for (i=0;i<n;i++){for (j=i+1;j<n;j++){a.x=p[i].y-p[j].y+p[i].x;a.y=p[j].x-p[i].x+p[i].y;b.x=p[i].y-p[j].y+p[j].x;b.y=p[j].x-p[i].x+p[j].y;if (find(a)&&find(b))ans++;}}cout<<ans/2<<endl;}return 0;
}
Squares
Time Limit: 3500MS   Memory Limit: 65536K
Total Submissions: 11803   Accepted: 4319

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

(哈希)Squares (p2002)相关推荐

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

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

  2. [LeetCode]202. Happy Number(平衡二叉树 哈希表)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  3. 《神经网络与深度学习》邱希鹏 学习笔记(3)

    <神经网络与深度学习>邱希鹏 学习笔记(3) 完成进度 第二章 机器学习概述 线性回归 参数学习 偏差-方差分解 自我理解 概率 似然 先验 后验 参考文献 完成进度 - 第二章 (1) ...

  4. [USACO3.2]魔板 Magic Squares

    [USACO3.2]魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题 ...

  5. 数据结构与算法 学习笔记(8):字典、集合、哈希表

    数据结构与算法 学习笔记(8):字典.集合.哈希表 本次文章记录的是和字典.集合.哈希表等数据结构相关的LeetCode算法题(题号与LeetCode对应),包括其构造和使用,针对每一题或一类题给出了 ...

  6. 通过python利用哈希值实现比较两个文件的一致性

    背景 近来学习到python的内置函数hash(),深入发现通过python的哈希值可以做很多的事情,最典型的可能就是文件加密了,在我们现实生活中大约有如下一些用途: 加密网站注册用户的密码. 网站用 ...

  7. 【C#】集合_哈希表_字典_泛型_文件

    数组能做到:存放同种类型数据,且数据个数确定 object类型的数组能满足:放各种类型的数据,确定放多少个,但是随意插入元素,数组做不到 集合能做到:存放各种数据类型,且不确定存放多少个,能做到随意插 ...

  8. ELFhash - 优秀的字符串哈希算法

    原 ELFhash - 优秀的字符串哈希算法 分类:算法杂论算法精讲数据结构 (1424)  (2) 1.字符串哈希: 我们先从字符串哈希说起 在很多的情况下,我们有可能会获得大量的字符串,每个字符串 ...

  9. 一致 先验分布 后验分布_浅谈Loki分布式架构中的一致性哈希

    云原生小白 看到上面蓝色字了么?关注下吧! Loki在分布式部署的模式下,保存Ingester服务的状态主要有3个渠道,分别是etcd.consul和基于gossip协议的memberlist.不管L ...

最新文章

  1. Python,OpenCV中的图像修复——cv2.inpaint()
  2. Windows10下SSH远程拷贝文件
  3. 4.1.3 OS之文件目录目录结构(单级-两级-多级-无环图)、索引节点FCB瘦身
  4. BZOJXXXX: [IOI2000]邮局——四边形不等式优化初探
  5. 怎么判断当前的os类型,手机类型
  6. 不下载APP就不能看全文?工信部出手了...
  7. 系统服务器ping偶尔超时,云服务器ping超时原因
  8. 【openjudge 计算概论(A)】[函数递归练习(3)]
  9. iOS UI自动化测试详解
  10. 线性调频信号的时频域分析
  11. CS224N WINTER 2022(三)RNN、语言模型、梯度消失与梯度爆炸(附Assignment3答案)
  12. raw格式转换jpg软件都有什么?这里有你想要的答案
  13. Java学习方法——类的构造方法
  14. Hbase的数据切分
  15. 庄懂21课 塞博小人
  16. rabbitmq和erlang版本对应关系
  17. 监控相机IQ调试策略
  18. 面向对象和面向过程~~编程思想
  19. 360网页html,360综艺频道-更新更全更受欢迎的影视网站-在线观看
  20. 2021高考成绩查询衢州,2021浙江衢州中考成绩查询时间 衢州中考成绩查询入口

热门文章

  1. java.lang.IllegalStateException: Fragment already added
  2. echarts3 地图应用 给背景地图上色(2)附:世界各大城市经纬度
  3. drcom宽带认证登录超时_DrCOM客户端常见问题解决方法
  4. 简述新图像文件格式——SVG
  5. STM32使用FreeRTOS CLI
  6. 蓝牙简单配对(Simple Pairing)协议及代码流程简述
  7. 微生物qPCR定量检测
  8. 抢票软件之——py12306使用指南
  9. docer启动一个容器时的过程
  10. 紫光云这匹黑马,了解一下