星星

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.
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

#include<stdio.h>
#include<cmath>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct point
{
	int x,y;
}p[1003];
bool cmp(point p1,point p2)
{
	if(p1.x==p2.x) return p1.y<p2.y;
	else return p1.x<p2.x;
}
bool search(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 true;
		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 false;
}
int main()
{
	//freopen("b.txt","r",stdin);
	int cases,n,i,cnt=0,j;
	scanf("%d",&cases);
	while(cases--)
	{
		scanf("%d",&n);
		for(i=0;i<n;i++)
			scanf("%d %d",&p[i].x,&p[i].y);
		sort(p,p+n,cmp);
		cnt=0;
		for(i=0;i<n-1;i++)
			for(j=i+1;j<n;j++)
			{
				if(abs(p[j].x-p[i].x)==abs(p[j].y-p[i].y))
				{
					//printf("%d %d %d %d\n",p[i].x,p[j].y,p[j].x,p[i].y);
					if(search(i,j,p[i].x,p[j].y)&&search(i,j,p[j].x,p[i].y))
			
					cnt++;
				}
			}
			printf("%d\n",cnt);
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值