首页 > 代码库 > 百练2805:正方形

百练2805:正方形

传送门:http://bailian.openjudge.cn/practice/2805/

【题解】

n不大,枚举两个端点,推算出剩下两个,拿map或哈希看看有多少即可。

技术分享
# include <map>
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + 10;
const int mod = 1e9+7;

# define RG register
# define ST static

int n;
struct point {
    int x, y;
    point() {}
    point(int x, int y) : x(x), y(y) {}
    friend bool operator == (point a, point b) {
        return a.x == b.x && a.y == b.y;
    }
    friend bool operator != (point a, point b) {
        return !(a==b);
    }
    friend bool operator < (point a, point b) {
        return a.x < b.x || (a.x == b.x && a.y < b.y);
    }
    friend bool operator > (point a, point b) {
        return a.x > b.x || (a.x == b.x && a.y > b.y);
    }
}p[M];

map < point, int > mp;

inline void getpoint(point a, point b, point &c, point &d) {
    // a->b, right a->d
    int dy = b.y - a.y, dx = a.x - b.x;
    d = point(a.x + dy, a.y + dx);
    c = point(b.x + dy, b.y + dx);
}

int main() {
    while(cin >> n) {
        if(!n) break;
        ll ans = 0;
        mp.clear();
        for (int i=1; i<=n; ++i) {
            scanf("%d%d", &p[i].x, &p[i].y);
            mp[p[i]] = mp[p[i]] + 1;
        }
        for (int i=1; i<=n; ++i)
            for (int j=1; j<=n; ++j) {
                if(i == j || p[i] == p[j]) continue;
                point a, b;
                getpoint(p[i], p[j], a, b);
                if(a != p[i] && a != p[j] && b != p[i] && b != p[j]) {
                    if(mp.find(a) != mp.end() && mp.find(b) != mp.end()) {
                        ans += 1ll * mp[a] * mp[b];
//                        printf("(%d,%d), (%d,%d), (%d,%d), (%d,%d)\n", p[i].x, p[i].y, p[j].x, p[j].y, a.x, a.y, b.x, b.y);
//                        printf("%d == %d\n", mp[a], mp[b]);
                    }
                }
            }
        cout << ans/4 << endl;
    }
    return 0;
}
View Code

 

百练2805:正方形