星星

来源:互联网 发布:txt编辑软件 编辑:程序博客网 时间:2024/04/27 15:56
Problem DescriptionLucy 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.InputThe 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.OutputFor each test case, output the number of squares in a single line.Sample Input211 240 01 01 10 1Sample Output01//题意:给定N个点,求有多少个正方形。组成正方形的条件是,四条边或与X轴平行或与Y轴平行。//关键字: 二分.//标程:#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>using namespace std;struct node{    int x, y;}p[1010];bool cmp(node s1,node s2){    if(s1.x != s2.x)        return s1.x < s2.x;    return s1.y < s2.y;}int findx(int i, int j, int x,int y){    int l = i, mid, r = j;    while(l <= r)    {        mid = (l + r) / 2;        if(p[mid].x == x && p[mid].y == y)            return 1;        if(p[mid].x < x)            l = mid + 1;        else if(p[mid].x > x)            r = mid - 1;        else if(p[mid].y < y)            l = mid + 1;        else if(p[mid].y > y)            r = mid - 1;    }    return 0;}int main(){//    freopen("a.txt","r",stdin);    int t, i, j, n;    cin >> t;    while(t --)    {        cin >> n;        for(i = 0; i < n; ++ i)            cin >> p[i].x >> p[i].y;        sort(p,p+n,cmp);        int  cnt = 0;        for(i = 0; i < n; ++ i)            for(j = i + 1; j < n; ++ j)                if(abs(p[j].x-p[i].x) == abs(p[j].y - p[i].y))                    if(findx(i,j,p[i].x,p[j].y) == 1 && findx(i,j,p[j].x,p[i].y) == 1)                         cnt ++;        cout << cnt << endl;    }    return 0;}

0 0
原创粉丝点击