【UVA-1595】Symmetry【紫书】

【UVA-1595】Symmetry【紫书】

问题链接:https://vjudge.net/problem/UVA-1595

Solution:

可以求出对称轴,之后判断一下每个点是否都有与它对称的点就好,仍然是set判重。

#include <iostream>
#include <set>
#include <cmath>

using namespace std;

struct Point {
    double x, y;
    bool operator < (const Point &p) const {
        if (x != p.x) return x < p.x;
        return y < p.y;
    }
};
set<Point> vis;

int main(void) {
    ios::sync_with_stdio(false);
    // cin.tie(NULL);
    int t, n;
    double mid;
    Point crt;
    cin >> t;
    while (t--) {
        cin >> n;
        mid = 0;
        vis.clear();
        for (int i = 0; i < n; i++) {
            cin >> crt.x >> crt.y;
            mid += crt.x;
            vis.insert(crt);
        }
        mid /= n;
        bool flag = true;
        for (auto &p : vis) {
            if (p.x == mid) continue;
            if (!vis.count({p.x + (p.x < mid ? 2 * (mid - p.x) : -2 * (p.x - mid)), p.y})) {
                cout << "NO\n";
                flag = false;
                break;
            }
        }
        if (flag) cout << "YES\n";
    }
    return 0;
}