#include <bits/stdc++.h>
#ifdef ONPC
#define cerr cout
#include "t_debug.cpp"
#else
#define debug(...) 42
#endif
#define sz(a) ((int)(a).size())
using namespace std;
using ll = long long;
const int INF = 1e9;
const ll INFL = 1e18;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rng(RANDOM);
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p) { return is >> p.first >> p.second; }
const int N = 3e5, LOGN = 17, MOD = 1e9+7;
ll brute(vector<int> arr) {
int n = sz(arr);
ll res = 0;
do {
ll cur = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
set<int>values;
for (int k = i; k <= j; k++) {
values.insert(arr[k]);
}
cur += sz(values);
}
}
if (cur < res) {
res = cur;
debug(cur, arr);
}
} while (next_permutation(arr.begin(), arr.end()));
return res;
}
int solve() {
int n, q;
if (!(cin >> n >> q)) {
return 1;
}
vector<int> arr(n);
for (auto& i : arr) {cin >> i;}
vector<int> count(N+1);
for (int i : arr) count[i]++;
auto comp = [&](int i, int j) {return count[i] < count[j];};
multiset<int, decltype(comp)> srr(arr.begin(), arr.end(), comp);
vector<int> pref, suff;
int last = 0;
for (int i = 0; i < n; i++) {
int cur = *srr.begin();
auto& arr = last ? suff : pref;
while (!srr.empty() && *srr.begin() == cur) {
arr.push_back(*srr.begin());
srr.erase(srr.begin());
}
last ^= 1;
}
while (!suff.empty()) {
pref.push_back(suff.back());
suff.pop_back();
}
ll res = 0;
for (int i = 0; i < n;i++) {
for (int j= i; j < n; j++) {
set<int> values;
for (int k = i; k <= j; k++) {
values.insert(pref[k]);
}
res += sz(values);
}
}
cout << res << '\n';
return 0;
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
#ifdef ONPC
t = 10000;
#endif
while (t-- && cin) {
if (solve()) break;
#ifdef ONPC
cout << "____________________" << endl;
#endif
}
return 0;
}
/*
█████ █████ ███ ████
▒▒███ ▒▒███ ▒▒▒ ▒▒███
███████ ████████ ███████ ████ ▒███ █████ ████ ██████ ████████
███▒▒███ ▒▒███▒▒███ ███▒▒███ ▒▒███ ▒███ ▒▒███ ▒███ ███▒▒███▒▒███▒▒███
▒███ ▒███ ▒███ ▒▒▒ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒▒▒
▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███
▒▒████████ █████ ▒▒████████ █████ █████ ▒▒███████ ▒▒██████ █████
▒▒▒▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒███ ▒▒▒▒▒▒ ▒▒▒▒▒
███ ▒███
▒▒██████
▒▒▒▒▒▒
*/
Compilation message
diversity.cpp: In function 'll brute(std::vector<int>)':
diversity.cpp:6:24: warning: statement has no effect [-Wunused-value]
6 | #define debug(...) 42
| ^~
diversity.cpp:34:13: note: in expansion of macro 'debug'
34 | debug(cur, arr);
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1476 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Incorrect |
1 ms |
1492 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1492 KB |
Output is correct |
2 |
Correct |
748 ms |
1492 KB |
Output is correct |
3 |
Execution timed out |
7033 ms |
2120 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1492 KB |
Output is correct |
2 |
Correct |
748 ms |
1492 KB |
Output is correct |
3 |
Execution timed out |
7033 ms |
2120 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1492 KB |
Output is correct |
2 |
Correct |
748 ms |
1492 KB |
Output is correct |
3 |
Execution timed out |
7033 ms |
2120 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1476 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Incorrect |
1 ms |
1492 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1476 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Incorrect |
1 ms |
1492 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |