困难。。。
cpp
class Solution {
public:
int countTrapezoids(vector<vector<int>>& points) {
unordered_map<double, vector<double>> groups; // 斜率 -> [截距]
unordered_map<int, vector<double>> groups2; // 中点 -> [斜率]
int n = points.size();
for (int i = 0; i < n; i++) {
int x = points[i][0], y = points[i][1];
for (int j = 0; j < i; j++) {
int x2 = points[j][0], y2 = points[j][1];
int dy = y - y2;
int dx = x - x2;
double k = dx ? 1.0 * dy / dx : DBL_MAX;
double b = dx ? 1.0 * (y * dx - x * dy) / dx : x;
groups[k].push_back(b);
int mid = (x + x2 + 2000) << 16 | (y + y2 + 2000); // 把二维坐标压缩成一个 int
groups2[mid].push_back(k);
}
}
int ans = 0;
for (auto& [_, g] : groups) {
if (g.size() == 1) {
continue;
}
// 对于本题的数据,map 比哈希表快
map<double, int> cnt;
for (double b : g) {
cnt[b]++;
}
int s = 0;
for (auto& [_, c] : cnt) {
ans += s * c;
s += c;
}
}
for (auto& [_, g] : groups2) {
if (g.size() == 1) {
continue;
}
map<double, int> cnt;
for (double k : g) {
cnt[k]++;
}
int s = 0;
for (auto& [_, c] : cnt) {
ans -= s * c; // 平行四边形会统计两次,减去多统计的一次
s += c;
}
}
return ans;
}
};