码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2002 Squares(二分)

时间:2015-07-25 09:35:53      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:acm   算法   二分   

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;
}



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

POJ 2002 Squares(二分)

标签:acm   算法   二分   

原文地址:http://blog.csdn.net/noooooorth/article/details/47051061

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!