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

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

Rocky Mountain 2004

枚举两个点计算另外俩个,二分查找。。。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

struct node
{
    int x,y;
}p[1100],p1,p2;

int n,cnt;

bool cmp(node a,node b)
{
    if(a.x!=b.x) return a.x<b.x;
    return a.y<b.y;
}

bool findp(node a)
{
    int s=0,t=n-1,m;
    int X=a.x,Y=a.y;
    while(s<=t)
    {
        m=(s+t)/2;
        if(p.x==X&&p.y==Y) return true;
        if(p.x<X||(p.x==X&&p.y<Y)) s=m+1;
        else t=m-1;
    }
    return false;
}

int main()
{
    while(scanf("%d",&n)!=EOF&&n)
    {
        cnt=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&p.x,&p.y);
        }
        sort(p,p+n,cmp);
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                p1.x=p.x+p[j].y-p.y; p1.y=p.y+p.x-p[j].x;
                p2.x=p[j].x+p[j].y-p.y; p2.y=p[j].y+p.x-p[j].x;
                if(!findp(p1)) continue;
                if(findp(p2)) cnt++;
            }
        }
        printf("%d\n",cnt/2);
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

POJ 2002 Squares的更多相关文章

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

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

  2. POJ 2002 Squares 哈希

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

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

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

  4. POJ 2002 Squares 数学 + 必须hash

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

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

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

  6. poj 2002 Squares 几何二分 || 哈希

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

  7. POJ 2002 Squares [hash]

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

  8. Squares - poj 2002(hash)

    枚举两个点作为一条边,求出正方形的另外两个点,利用hash查找另外两个点. #include<stdio.h> #include<string.h> #include<s ...

  9. POJ 2002 统计正方形 HASH

    题目链接:http://poj.org/problem?id=2002 题意:给定n个点,问有多少种方法可以组成正方形. 思路:我们可以根据两个点求出对应正方形[有2个一个在两点左边,一个在两点右边] ...

随机推荐

  1. javascript模块化详解

    模块化:每个模块只完成一个独立的功能,然后提供该功能的接口.模块间通过接口访问.模块中的(过程和数据)对于其它模块来说是私有的(不能访问修改) 原始人写法: function m1(){ //... ...

  2. ORA-00911: 无效字符

    思路:遇到这样问题首先第一步:将有误sql粘至数据库运行一下,如果报错,说明sql存在问题. 第二步:数据库没问题.那么就要想你的书写方式是否正确,是否是ibatasi里的写法,或许是多了个 :  或 ...

  3. 架构师速成-如何高效编程 for java

    引子 赵云大喝一声,挺枪骤马杀入重围,左冲右突,如入无人之境.那枪浑身上下,若舞梨花:遍体纷纷,如飘瑞雪. 赵云是所有历史人物中我最喜欢的一个,如果放到现代,他走了it的道路,一定可以成为一个编程高手 ...

  4. Bootstrap系列 -- 15. 下拉选择框select

    Bootstrap框架中的下拉选择框使用和原始的一致,多行选择设置multiple属性的值为multiple.Bootstrap框架会为这些元素提供统一的样式风格 <form role=&quo ...

  5. RockWare RockWorks的Ollydbg调试过程及注册机(破解)思路

    最近拿到了RockWorks15的安装包,可惜没有破解,试用也只能用14天.用PEiD工具察看了一下,Delphi编写的程序,竟然没加壳.本想用OllyDBG调试进去爆破一下,不意发现注册码很简单,如 ...

  6. Sql Server 附加没有日志文件的数据库(.mdf)文件方法

    附加数据库,附加的时候会提醒找不到log文件 针对以上现象有两个写法的语句能解决: 写法一: USE MASTER; EXEC sp_detach_db @dbname = 'TestDB'; EXE ...

  7. [USACO2002][poj1947]Rebuilding Roads(树形dp)

    Rebuilding RoadsTime Limit: 1000MS Memory Limit: 30000KTotal Submissions: 8589 Accepted: 3854Descrip ...

  8. JavaScript基础---作用域,匿名函数和闭包

    匿名函数就是没有名字的函数,闭包是可访问一个函数作用域里变量的函数. 一.匿名函数 //普通函数 function box() { //函数名是 box return 'TT'; } //匿名函数 f ...

  9. HTML5——多次定位请求

    多次定位请求及点击一次 就不断的触发请求事件,和单次定位请求写法差不多,只需要将 navigator.geolocation.getCurrentPosition改为navigator.geoloca ...

  10. Java数组的--遍历

    (I)标准for循环遍历数组 例如代码片段: int [] nums = new int [] {0,1,2,3,4,5,6,7,8,9}; for(int i=0;i<11;i++){ Sys ...