This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const long long N = 3e5 + 5;
long long a[N];
vector <long long> res;
long long cont (long long r) {
return (r + 1) * (r) / 2;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n, q; cin >> n >> q;
map <ll, ll> ans, s;
for (ll i = 1; i <= n; i++) {
cin >> a[i];
s[a[i]] = -1;
ans[a[i]]++;
}
while (q--) {
ll l, r; cin >> l >> r;
for (auto it : ans) res.push_back(it.second);
ll h = 0;
sort(res.begin(), res.end());
vector <int> rs(res.size());
bool ok = 0;
int lt = 0, rt = rs.size() - 1;
for (auto it : res) {
if (ok) rs[rt] = it, rt--, ok = 0;
else rs[lt] = it, lt++, ok = 1;
} for (int i = 0; i < rs.size(); i++) res[i] = rs[i];
for (int i = 0; i < res.size(); i++) {
ll k = 2, nwres = res[i];
for (int j = i + 1; j < res.size(); j++) {
nwres += k * res[j], k++;
} h += nwres * res[i] - cont(res[i] - 1);
}
// while (next_permutation(res.begin(), res.end())) {
// ll x = 0;
// for (int i = 0; i < res.size(); i++) {
// ll k = 2, nwres = res[i];
// for (int j = i + 1; j < res.size(); j++) {
// nwres += k * res[j], k++;
// } x += nwres * res[i] - cont(res[i] - 1);
// } h = min(h, x);
// if (x == 93) {
// for (auto it : res) cout << it << ' ';
// cout << '\n';
// }
// }
cout << h << '\n';
}
return 0;
}
Compilation message (stderr)
diversity.cpp: In function 'int main()':
diversity.cpp:31:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | } for (int i = 0; i < rs.size(); i++) res[i] = rs[i];
| ~~^~~~~~~~~~~
diversity.cpp:32:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int i = 0; i < res.size(); i++) {
| ~~^~~~~~~~~~~~
diversity.cpp:34:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for (int j = i + 1; j < res.size(); j++) {
| ~~^~~~~~~~~~~~
# | 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... |