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

  1. 4
  2. 1 0
  3. 0 1
  4. 1 1
  5. 0 0
  6. 9
  7. 0 0
  8. 1 0
  9. 2 0
  10. 0 2
  11. 1 2
  12. 2 2
  13. 0 1
  14. 1 1
  15. 2 1
  16. 4
  17. -2 5
  18. 3 7
  19. 0 0
  20. 5 2
  21. 0

Sample Output

  1. 1
  2. 6
  3. 1
    题意:在二维平面上,给你n个点的坐标,求任取四点构成正方形的个数
    题解:好吧,暴力O(n^4)又t了……
    那么折半枚举吧
    枚举两个点连成一条线,然后根据这条线可以得到正方形的另外两个点,根据hash可以快速确定点是否在n个点中,即可以计算出正方形的个数
    代码如下:
  1. #include<map>
  2. #include<vector>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<iostream>
  6. #include<algorithm>
  7. #define inf 0x3f3f3f3f
  8. using namespace std;
  9. struct node
  10. {
  11. int x,y;
  12. } a[];
  13. vector<long long> g[];
  14. int n;
  15. int cmp(node x,node y)
  16. {
  17. if(x.x==y.x)
  18. {
  19. return x.y<y.y;
  20. }
  21. return x.x<y.x;
  22. }
  23. int main()
  24. {
  25. while(scanf("%d",&n)==&&n)
  26. {
  27. int ans=;
  28. vector<long long> g[];
  29. for(int i=; i<=n; i++)
  30. {
  31. scanf("%d%d",&a[i].x,&a[i].y);
  32. }
  33. sort(a+,a+n+,cmp);
  34. for(int i=; i<=n; i++)
  35. {
  36. long long key=(a[i].x*a[i].x+a[i].y*a[i].y)%;
  37. g[key].push_back(i);
  38. }
  39. for(int i=; i<=n; i++)
  40. {
  41. for(int j=i+; j<=n; j++)
  42. {
  43. int x1,y1,x2,y2;
  44. int dx=(a[j].x-a[i].x),dy=(a[j].y-a[i].y);
  45. // if(dx<0||dy<0)
  46. // {
  47. // continue;
  48. // }
  49. x1=a[i].x-dy;
  50. y1=a[i].y+dx;
  51. x2=a[j].x-dy;
  52. y2=a[j].y+dx;
  53. int flag=;
  54. long long key=(x1*x1+y1*y1)%;
  55. for(int k=; k<g[key].size(); k++)
  56. {
  57. if(a[g[key][k]].x==x1&&a[g[key][k]].y==y1)
  58. {
  59. flag+=;
  60. }
  61. }
  62. key=(x2*x2+y2*y2)%;
  63. for(int k=; k<g[key].size(); k++)
  64. {
  65. if(a[g[key][k]].x==x2&&a[g[key][k]].y==y2)
  66. {
  67. flag+=;
  68. }
  69. }
  70. if(flag==)
  71. {
  72. // 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)
  73. // {
  74. // continue;
  75. // }
  76. // printf("%d %d\n",dx,dy);
  77. // 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);
  78. ans++;
  79. }
  80. }
  81. }
  82. printf("%d\n",ans/);
  83. }
  84. }

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. Terracotta设计原理分析--(部分内容来自官方描述)

    因为工作中历史产品采用了terracotta作为分布式缓存线性扩展平台,因此不得不提前对其原理做了相关了解,当然其中很多的设计思想和oracle.memcached的设计相似,但也有自己的亮点,那就是 ...

  2. apache通过.htaccess(rewrite)判断手机电脑跳转-手机用户重定向到手机版

    自动判断.重定向的办法也有几种: 使用网站构建的程序(例如PHP)来判断.重定向:使用服务器上的Web服务(例如Apache)来判断.重定向. 在Apache中设置重定向有两个办法: 在网站的http ...

  3. C# VS Java

    摘要:C#的语言规范由Microsoft的Anders Hejlsberg与Scott Wiltamuth编写.在当前Microsoft天花乱坠的宣传中,对C#和C++.Java作一番比较总是很有趣的 ...

  4. 数据科学:numpy.where() 的用法

    原文出处:numpy.where() 用法讲解 原创作者:massquantity numpy.where() 有两种用法: 1. np.where(condition, x, y) 满足条件(con ...

  5. 【转】Jmeter项目测试

    Jmeter的录制回放功能是现将你对要测试的项目进行访问的历史记录进行录制,然后虚拟出多个用户对历史记录进行回放,从而达到压力测试的目的. 录制是通过代理服务器进行录制. 一.下载地址 http:// ...

  6. phantomjs 安装和试用

    准备学习casperjs, 发现官网上说  it’s an extremely useful companion to PhantomJS, 所以决定下把它下来试试.下载安装(win7)没什么可说的, ...

  7. 字符编码py2,py3操作,SecureCRT的会话编码的设置

    对之前的字符串类型和二进制类型(bytes类型),可以这样关联记忆,把字符串类型当作是Unicode,把bytes类型当作是GBK或者UTF-8或者是日文编码.这样字符串要转成二进制,那么就需要编码e ...

  8. 40个Java多线程问题总结【转】

    1.多线程有什么用? 一个可能在很多人看来很扯淡的一个问题:我会用多线程就好了,还管它有什么用?在我看来,这个回答更扯淡.所谓”知其然知其所以然”,”会用”只是”知其然”,”为什么用”才是”知其所以然 ...

  9. oracle删除多个分区表

    declare v_date date; v_part_name varchar(); begin v_date := date'2015-2-4'; while v_date >= date' ...

  10. 安全测试回顾(一)补充:burp 的基本操作

    浏览器设置; 拦截请求: Spider 伪造请求: 对这个url 伪造请求 拦截一个页面后,进入攻击模块 sniper  两个参数值 保证一个不变  另一个 进行枚举 battering ram 两个 ...