星星

来源:互联网 发布:手机如何装修淘宝店铺 编辑:程序博客网 时间:2024/04/27 13:22
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;}



0 0
原创粉丝点击