POJ 2002 Squares(二分)

Squares

Time Limit: 3500MS   Memory Limit: 65536K
Total Submissions: 17423   Accepted: 6614

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

给一个平面散点集,判断能够构成多少个正方形。虽然有3.5秒,但四层暴力循环的话肯定会超时循环。所以有这样一种思路:先把点排序,双层循环枚举前(n-2)个点,为了防止重复判断,第二层循环里的j要从i+1开始,二分查找后(n-j)个点中是否存在能与s[i],s[j]构成正方形的点,所以第二层循环结束的条件是j<=n-2,剩下2个点用来查找,二分查找的范围是[j+1,n]。

已知2个点,写出能与这2个点构成正方形的坐标的计算方法如下图(计算坐标的时候不要用double,否则很容易TLE或者WA)

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=1e5+20;
int n;
long long ans;
struct star
{
    int x,y;
    star(){}
    star(int x,int y)
    {
        this->x=x;
        this->y=y;
    }
    bool operator<(const star& n)const
    {
        if(this->x==n.x)
            return this->y<n.y;
        return this->x<n.x;
    }
}s[MAXN];

int searchh(int l,int r,star n)
{
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(s[mid].x==n.x&&s[mid].y==n.y)
            return 1;
        if(s[mid]<n)
            l=mid+1;
        else
            r=mid-1;
    }
    return 0;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
    while(scanf("%d",&n)!=EOF&&n)
    {
        ans=0;
        for(int i=1;i<=n;i++)
            scanf("%d%d",&s[i].x,&s[i].y);
        sort(s+1,s+n+1);
        for(int i=1;i<=n-3;i++)
        {
            for(int j=i+1;j<=n-2;j++)
            {
                int xxx=s[j].x-s[i].x;
                int yyy=s[j].y-s[i].y;
                int sx1=s[i].x+yyy;
                int sy1=s[i].y-xxx;
                int sx2=s[j].x+yyy;
                int sy2=s[j].y-xxx;
                if(searchh(j+1,n,star(sx1,sy1))&&searchh(j+1,n,star(sx2,sy2)))
                    ans++;
                int sx3=s[i].x-yyy;
                int sy3=s[i].y+xxx;
                int sx4=s[j].x-yyy;
                int sy4=s[j].y+xxx;
                if(searchh(j+1,n,star(sx3,sy3))&&searchh(j+1,n,star(sx4,sy4)))
                    ans++;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-12 16:55:10

POJ 2002 Squares(二分)的相关文章

poj 2002 Squares,hash

poj 2002 Squares 给出n个点,问能组成多少个正方形? 题解: 先把每个点hash 然后枚举两点(即枚举正方形的一条边),然后通过三角形全等,可以推出正方形的另外两点,在hash表里查找这两点看是存在,存在则 Cnt +1. 最后 answer = Cnt/4 //因为同一正方形都统计了4次. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const

poj 2002 Squares

题目链接:http://poj.org/problem?id=2002 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 onl

POJ 2002 Squares 数学 + 必须hash

http://poj.org/problem?id=2002 只能说hash比二分快很多.随便一个hash函数都可以完爆二分. 判断是否存在正方形思路如下: 1.枚举任意两个点,作为正方形的一条边,那么,整个正方形就确定了,有两个方向. 因为, 设枚举的坐标为(x1, y1) & (x2, y2),所求的坐标是和x1,y1这个点相连,那么有方程如下. 1.垂直,向量积是0 2.边长相等,然后距离公式化简. 即可解出剩下的两个点. 然后要注意两个点要在正方形的同一侧,不然变了平行四边形了. 唤醒了

POJ 2002 Squares (哈希)

题意:给定 n 个点,问有能组成多少个正方形. 析:通过直接桥梁两个顶点,然后再算另外两个,再通过哈希进行查找另外两个,这里我先是用的map,竟然卡过了3400ms多,后来改成哗哈希,900ms,哈希我也是用STL中的容器来写的,list,先枚举的那两个点是相邻的,然后再通过旋转90度,去计算另外两个. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #inclu

POJ 2002 Squares 计算集合 点的hash

题目大意:给出平面上的n个点,问能组成多少个正方形. 思路:一开始看时间3秒半,就想用set水过,然而失败了.没办法手写hash吧.观察坐标的范围,<20000,样例中还有负的,我们读进来的时候就将点的坐标+20000,这样避免负数,方便hash.我的哈希很弱,就是把xy坐标加起来作为哈希值,想卡的话应该很轻松.但还是过得很快. CODE: #include <cstdio> #include <cstring> #include <iostream> #incl

POJ 2002 点的hash

Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 15489   Accepted: 5864 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 abou

POJ 3484 Showstopper 二分

 Showstopper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1218   Accepted: 356 Description Data-mining huge data sets can be a painful and long lasting process if we are not aware of tiny patterns existing within those data sets. On

poj 1469 COURSES (二分匹配)

COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16877   Accepted: 6627 Description Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is poss

POJ 2398 计算几何+二分+排序

Toy Storage Time Limit: 1000MS  Memory Limit: 65536K Total Submissions: 3953  Accepted: 2334 Description Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box t