#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;
} ll p = 1;
for (auto &it : s) {
it.second = p++;
} for (int i = 1; i <= n; i++) {
a[i] = s[a[i]];
ans[a[i]]++;
}
while (q--) {
ll l, r; cin >> l >> r;
for (auto it : ans) res.push_back(it.second);
ll h = 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++;
} 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);
}
cout << h << '\n';
}
return 0;
}
Compilation message
diversity.cpp: In function 'int main()':
diversity.cpp:29:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for (int i = 0; i < res.size(); i++) {
| ~~^~~~~~~~~~~~
diversity.cpp:31:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (int j = i + 1; j < res.size(); j++) {
| ~~^~~~~~~~~~~~
diversity.cpp:37:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for (int i = 0; i < res.size(); i++) {
| ~~^~~~~~~~~~~~
diversity.cpp:39:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int j = i + 1; j < res.size(); j++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
324 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
324 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
324 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |