Submission #858007

#TimeUsernameProblemLanguageResultExecution timeMemory
858007danikoynovDiversity (CEOI21_diversity)C++14
64 / 100
76 ms16060 KiB
#include<bits/stdc++.h> #define endl '\n' using namespace std; typedef long long ll; void speed() { ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL); } const int maxn = 3e5 + 10; int n, q, a[maxn], cnt[maxn], m; ll bef[maxn], aft[maxn], pref[maxn]; const ll inf = 1e18; vector < ll > groups; ll val[maxn]; void solve() { cin >> n >> q; for (int i = 1; i <= n; i ++) cin >> a[i]; int l, r; cin >> l >> r; for (int i = 1; i <= n; i ++) cnt[a[i]] ++; for (int i = 1; i <= n; i ++) if (cnt[i] > 0) groups.push_back((ll)(cnt[i])); m = groups.size(); sort(groups.begin(), groups.end()); reverse(groups.begin(), groups.end()); l = (m + 1) / 2; r = (m + 1) / 2; val[l] = groups[0]; for (int i = 1; i < m; i ++) { if (l == 1) { val[++ r] = groups[i]; } else if (r == n) { val[-- l] = groups[i]; } else { if (i % 2 == 1) { val[++ r] = groups[i]; } else { val[-- l] = groups[i]; } } } ll tot = 0; ll bf = 0; for (int i = 1; i <= m; i ++) { bef[i] = bf; bf += val[i]; } ll af = 0; for (int i = m ; i > 0; i --) { aft[i] = af; af += val[i]; } for (ll i = 1; i <= m; i ++) { tot = tot + i * val[i] * (bef[i] - aft[i]); } ll sf = 0; for (int i = 1; i <= m; i ++) { ll cur = val[i]; tot = tot + cur * (cur + 1) / 2; tot = tot + val[i] * sf; sf += val[i]; } cout << tot << endl; } int main() { solve(); return 0; } /** 4 1 1 1 1 1 1 2 2 4 4 1 2 1 3 2 1 4 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...