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 <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <set>
#warning That's the baby, that's not my baby
typedef long long ll;
const int NMAX = 3e5;
int a[NMAX + 1];
int f[100];
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
int n, q;
std::cin >> n >> q;
std::vector<int> norm;
for (int i = 1; i <= n; i++) {
std::cin >> a[i];
norm.push_back(a[i]);
}
std::sort(norm.begin(), norm.end());
norm.erase(std::unique(norm.begin(), norm.end()));
for (int i = 1; i <= n; i++) {
a[i] = std::lower_bound(norm.begin(), norm.end(), a[i]) - norm.begin();
}
/**
1 * (f[1] + f[2] + ... + f[k]) +
2 * (f[1] * f[2] + f[2] * f[3] + f[3] * f[4] + ... + f[k - 1] * f[k]) +
3 * (f[1] * f[3] + f[2] * f[4] + ...) +
...
**/
while (q--) {
int l, r;
std::cin >> l >> r;
std::vector<int> b;
memset(f, 0, sizeof(f));
for (int i = l; i <= r; i++) {
f[a[i]]++;
}
std::vector<int> fr;
for (int i = 0; i < 100; i++) {
if (f[i] != 0) {
fr.push_back(f[i]);
}
}
std::sort(fr.begin(), fr.end());
int answer = 1e9;
do {
int m = (int) fr.size();
int cur = 0;
for (int k = 1; k <= m; k++) {
for (int i = k - 1; i < m; i++) {
cur += k * (f[i - k + 1] * f[i]);
}
}
answer = std::min(answer, cur);
} while(std::next_permutation(fr.begin(), fr.end()));
// std::sort(fr.begin(), fr.end());
// do {
// int m = (int) fr.size();
// int cur = 0;
// for (int k = 1; k <= m; k++) {
// for (int i = k - 1; i < m; i++) {
// cur += k * (f[i - k + 1] * f[i]);
// }
// }
// if (cur == answer) {
// for (const auto &x : fr) {
// std::cout << x << ' ';
// }
// std::cout << '\n';
// }
// } while(std::next_permutation(fr.begin(), fr.end()));
std::cout << answer << '\n';
}
return 0;
}
Compilation message (stderr)
diversity.cpp:7:2: warning: #warning That's the baby, that's not my baby [-Wcpp]
7 | #warning That's the baby, that's not my baby
| ^~~~~~~
# | 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... |