• 百练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)
    ", p[i].x, p[i].y, p[j].x, p[j].y, a.x, a.y, b.x, b.y);
    //                        printf("%d == %d
    ", mp[a], mp[b]);
                        }
                    }
                }
            cout << ans/4 << endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    leetcode 890. 查找和替换模式 Python
    TensorFlow-GPU+cuda8+cudnn6+anaconda安装遇到的版本错误
    leetcode 921. 使括号有效的最少添加(Python)
    BFC概念详解及应用
    做一个网页阅读百分比指示器
    margin-bottom和vertical-align的区别
    MD5算法
    Array.prototype.slice.call()方法详解
    String stringbuffer StringBuilder
    价值观
  • 原文地址:https://www.cnblogs.com/galaxies/p/bailian2805.html
Copyright © 2020-2023  润新知