AT_abc418_e [ABC418E] Trapezium 题解

· · 题解

好久没写过简洁的题解了。

解题思路

不仅上过小学二年级的同学就知道平行四边形满足对边平行。

而且上过中学二年级的同学就知道 a\overset{/\!/}{=\!\!=} c 就可以推出四边形 abcd 为平行四边形。

然而上过小学二年级的同学就知道梯形只需满足 a \parallel c,那么平行四边形 \subset 梯形。

因此做过 AT_abc418_e 的同学就知道如果要统计梯形数量,需要去掉平行四边形的数量。

于是有经验的同学就知道枚举平行四边形对角线中点,如果有另一条线段的中点也是这个点,那么这四个点就会构成平行四边形,因为题目说了不会有三点共线,所以其他情况不用考虑。

CODE:

#include <bits/stdc++.h>
using namespace std;
#define int long long
int x[2010], y[2010];
unordered_map<double, int> f;
map<pair<double, int>, int> f2;
signed main() {
    ios::sync_with_stdio(false);
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> x[i] >> y[i];
    }
    map<pair<int, int>, int> m;
    for (int i = 1; i <= n; i++) {
        for (int j = i + 1; j <= n; j++) {
            m[ {x[i] + x[j], y[i] + y[j]}]++;
        }
    }
    int res = 0;
    for (auto it : m) {
        int t = it.second;
        res += t * (t - 1) / 2;
    }
    for (int i = 1; i <= n; i++) {
        for (int j = i + 1; j <= n; j++) {
            double t;
            if (x[j] == x[i]) {
                t = -998244353;
            } else {
                t = (y[j] - y[i]);
                t /= (x[j] - x[i]);
            }
//          cout << t << ' ' << i << ',' << j << endl;
            f[t]++;
        }
    }
    int ans = 0;
    for (auto u : f) {
        ans += u.second * (u.second - 1) / 2;
    }
    cout << ans - res;
    return 0;
}