이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using i64 = long long;
#ifdef DEBUG
#include "/home/ahmetalp/Desktop/Workplace/debug.h"
#else
#define debug(...) void(23)
#endif
constexpr int max_A = int(3E5) + 5;
int cnt[max_A];
i64 calc(std::vector<i64> x) {
int n = int(x.size());
i64 ans = 0;
for (int i = 0; i < n; ++i) {
ans += x[i] * (x[i] + 1) / 2;
for (int j = i + 1; j < n; ++j) {
ans += x[i] * x[j] * (j - i + 1);
}
}
return ans;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, Q;
std::cin >> N >> Q;
std::vector<int> A(N);
for (int i = 0; i < N; ++i) {
std::cin >> A[i];
}
for (int i = 0; i < N; ++i) {
++cnt[A[i]];
}
std::vector<i64> x;
for (int i = 0; i < max_A; ++i) {
if (cnt[i] != 0) {
x.emplace_back(cnt[i]);
}
}
int n = int(x.size());
std::sort(x.begin(), x.end());
std::vector<i64> w(n);
int l = 0, r = n - 1;
for (int i = 0; i < n; ++i) {
if (~i & 1) w[i] = x[l++];
else w[i] = x[r--];
}
debug(w);
i64 ans = calc(w);
std::cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |