题目原文请移步下面的链接
数学
、容斥原理
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void best_coder() {
int n;
scanf("%d", &n);
unordered_map<int, LL> um;
vector<int> v(n);
for (int i = 0; i < n; ++i) {
scanf("%d", &v[i]);
++um[v[i]];
}
LL cnt = 0;
for (auto i : um) {
if (i.second > 1) {
cnt += i.second * (i.second - 1) / 2;
}
}
for (auto i : v) {
if (um[i] > 1) {
printf("%lld\n", cnt - (um[i] - 1));
} else {
printf("%lld\n", cnt);
}
}
}
void happy_coder() {
}
int main() {
// 提升cin、cout效率
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// 小码匠
best_coder();
// 最优解
// happy_coder();
// 返回
return 0;
}
END