跳到正文

题解

AT_abc418_e [ABC418E] Trapezium 题解

Snowflake_Fairy约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年8月17日
收录
2026年4月8日
更新
2026年11月12日
篇幅
约 3 分钟

版本

  1. v1当前

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

解题思路

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

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

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

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

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

CODE:

C++
#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;
}
原文
存档评论4 条抓取于