星星
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 8 Accepted Submission(s) : 4
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Lucy loves stars very much. There are N (1 <= N <= 1000) stars in the sky. Assume the sky is a flat plane. All of the stars lie on it with a location (x, y), -10000 <= x, y <= 10000. Now, Lucy wants you to tell her how many squares with each of four vertices formed by these stars, and the edges of the squares should parallel to the coordinate axes.
Input
The first line of input is the number of test case.
The first line of each test case contains an integer N. The following N lines each contains two integers x, y, meaning the coordinates of the stars. All the coordinates are different.
The first line of each test case contains an integer N. The following N lines each contains two integers x, y, meaning the coordinates of the stars. All the coordinates are different.
Output
For each test case, output the number of squares in a single line.
Sample Input
2 1 1 2 4 0 0 1 0 1 1 0 1
Sample Output
0 1
//题目意思是求在天空(平面)中的星星能围成多少个与坐标轴平行的正方形。
//首先对输入坐标排序,然后枚举对角线上两点,在进行二分查找另外两个点如果找得到就累加。因为正方形平行坐标轴,则已知两个点p1(x1,y1), p2(x2,y2) 则 p3(x1,y1),p4(x1,y2) 。
#include<cstdio>
#include<algorithm>
using namespace std;
struct point
{
int x,y;
}p[1005];
bool cmp(point a,point b) //为了进行二分查找 必须排序,
{
if(a.x!=b.x) return a.x<b.x;
else return a.y<b.y;
}
bool solve(int l,int r,int x,int y) //二分查找一个点是否存在
{
int left,right,mid;
left=l;right=r;
while(left<=right)
{
mid=(left+right)>>1;
if(p[mid].x==x&&p[mid].y==y)
return 1;
if(p[mid].x<x) left=mid+1;
else if(p[mid].x>x) right=mid-1;
else if(p[mid].y<y) left=mid+1;
else if(p[mid].y>y) right=mid-1;
}
return 0;
}
int main()
{
int t,n,i,j,ans;
scanf("%d",&t);
while(t--)
{
ans=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
}
sort(p,p+n,cmp);
for(i=0;i<n;i++) //两重循环枚举对角线上两点
{
for(j=i+1;j<n;j++)
{
if((p[j].x-p[i].x)==(p[j].y-p[i].y)) //算出两条边是否相等
{ //因为j比i大 则查找另外两个点时范围肯定在i和j间查找是否存在这个点,
if(solve(i,j,p[i].x,p[j].y)&&solve(i,j,p[j].x,p[i].y))//同时满足这个条件的点是唯一的
ans++;
}
}
}
printf("%d\n",ans);
}
return 0;
}