星星

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
//題意:給定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;
}

發佈了268 篇原創文章 · 獲贊 12 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章