二分。。。。
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. C#Winform使用扩展方法自定义富文本框(RichTextBox)字体颜色

    在利用C#开发Winform应用程序的时候,我们有可能使用RichTextBox来实现实时显示应用程序日志的功能,日志又分为:一般消息,警告提示 和错误等类别.为了更好地区分不同类型的日志,我们需要使 ...

  2. js验证身份证号

    /* * 身份证检测(格式.地区.生日.年龄范围) * code:检测字符串 rangeAge:年龄范围[格式为:25-55] * 返回值 0:为空 ,不为0则验证不通过 */ : : : : : : ...

  3. WCF 入门(15)

    前言 度过了一个阳光明媚的周末. 一个阴霾的周日夜晚. 第15集 WCF里面的异常处理(1)  Exception handling in WCF 大致看了一下,这个WCF视频里面总共有6集,这个是第 ...

  4. [设计模式] javascript 之 单件模式

    单件模式说明 1. 说明:单件模式,就是静态化的访问中已经实例化的对象,这个对象只能通过一个唯一的入口访问,已经实例或待实例化的对象:面向对象语言如Java, .Net C#这样的服务端动态语言里,能 ...

  5. Javascript基础系列之(六)循环语句(do while循环)

    do/while 循环是 while 循环的变体.该循环会执行一次代码块,在检查条件是否为真之前,然后如果条件为真的话,就会重复这个循环. 语法结构如下 do { statement } while ...

  6. 软工实践练习一——使用Git进行代码管理心得

    在github.com的操作 注册 创建Organization 将指定代码库fork到小组Organization下 在Organization下创建repository 这些操作在学校的机房已经完 ...

  7. 软工实践个人练习-使用github进行代码管理

    1.掌握使用Git进行代码版本,使用github进行代码托管. 2.创建小组Organization,并邀请组员进来. 3.将代码库https://github.com/sefzu2015/AutoC ...

  8. C语言中memset(void *s, char ch,unsigned n)用的用法

    将指针s所指的内存空间中前n为重置为字符c 程序例: #include <string.h> #include <stdio.h> #include <memory.h& ...

  9. Oracle查看锁表

     查看锁表进程SQL语句1: select sess.sid,     sess.serial#,     lo.oracle_username,     lo.os_user_name,     a ...

  10. 12.Android之Tabhost组件学习

    TabHost是整个Tab的容器,TabHost的实现有两种方式: 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab中的内容在布 ...