#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
const ll INF = 1e18;
int N, Q;
vi A, AA;
ll ans;
vi P;
vi answerPermutation;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> Q;
AA.resize(N);
for (int i = 0; i < N; ++i)
cin >> AA[i];
while (Q--)
{
{
int l, r;
cin >> l >> r;
--l;
A = vi(AA.begin() + l, AA.begin() + r);
}
ll n = sz(A);
vi heatMap(*max_element(all(A)) + 1, 0);
for (int i = 0; i < sz(A); ++i)
++heatMap[A[i]];
map<int, int> cnts;
for (int i = 0; i < sz(heatMap); ++i)
if (heatMap[i])
++cnts[heatMap[i]];
ll pref = 0;
ll num = sz(cnts);
vi dp[2];
for (int foo = 0; foo < 2; ++foo)
dp[foo].reserve(N + 1), dp[foo].resize(N + 1);
fill(all(dp[0]), INF);
dp[0][0] = n * (n + 1) * (n + 1) / 2 - (2 * n + 1) * (n + 1) * n / 6;
auto it = cnts.begin();
for (int j = 0; j < num; ++j)
{
int ii = j & 1;
int ni = ii ^ 1;
fill(all(dp[ni]), INF);
ll x = it->first; // how much
ll y = it->second; // how often
ll subsure = 0;
// either with left or right endpoint in set
subsure += (n - x) * x * (x - 1) / 2;
// if both are in set
subsure += x * x * (x - 1) / 2 - (2 * x - 1) * x * (x - 1) / 6;
// for every such set
subsure *= y;
for (ll left = 0; left < sz(dp[ii]); ++left)
{
ll tmp = dp[ii][left];
if (tmp == INF)
continue;
tmp -= subsure;
vi L(y + 1, 0), R(y + 1, 0);
ll l = left, r = pref - left;
for (int i = 1; i <= y; ++i)
{
L[i] = L[i - 1] + (x - 1) * l * (n - l - x);
R[i] = R[i - 1] + (x - 1) * r * (n - r - x);
l += x, r -= x;
}
for (int i = 0; i <= y; ++i)
dp[ni][left + x * i] = min(dp[ni][left + x * i], tmp - L[i] - R[y - i]);
}
++it;
pref += x * y;
}
ans = *min_element(all(dp[num & 1]));
cout << ans << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
7 ms |
3656 KB |
Output is correct |
5 |
Correct |
14 ms |
6860 KB |
Output is correct |
6 |
Correct |
24 ms |
10060 KB |
Output is correct |
7 |
Correct |
23 ms |
10068 KB |
Output is correct |
8 |
Correct |
23 ms |
10088 KB |
Output is correct |
9 |
Correct |
23 ms |
10060 KB |
Output is correct |
10 |
Correct |
25 ms |
10024 KB |
Output is correct |
11 |
Correct |
24 ms |
10068 KB |
Output is correct |
12 |
Correct |
25 ms |
10108 KB |
Output is correct |
13 |
Correct |
24 ms |
10088 KB |
Output is correct |
14 |
Correct |
25 ms |
10108 KB |
Output is correct |
15 |
Correct |
26 ms |
10096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
7 ms |
3656 KB |
Output is correct |
5 |
Correct |
14 ms |
6860 KB |
Output is correct |
6 |
Correct |
24 ms |
10060 KB |
Output is correct |
7 |
Correct |
23 ms |
10068 KB |
Output is correct |
8 |
Correct |
23 ms |
10088 KB |
Output is correct |
9 |
Correct |
23 ms |
10060 KB |
Output is correct |
10 |
Correct |
25 ms |
10024 KB |
Output is correct |
11 |
Correct |
24 ms |
10068 KB |
Output is correct |
12 |
Correct |
25 ms |
10108 KB |
Output is correct |
13 |
Correct |
24 ms |
10088 KB |
Output is correct |
14 |
Correct |
25 ms |
10108 KB |
Output is correct |
15 |
Correct |
26 ms |
10096 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
17 ms |
3668 KB |
Output is correct |
20 |
Correct |
35 ms |
6964 KB |
Output is correct |
21 |
Correct |
56 ms |
10012 KB |
Output is correct |
22 |
Correct |
56 ms |
9980 KB |
Output is correct |
23 |
Correct |
55 ms |
10088 KB |
Output is correct |
24 |
Correct |
64 ms |
10088 KB |
Output is correct |
25 |
Correct |
59 ms |
10068 KB |
Output is correct |
26 |
Correct |
65 ms |
10024 KB |
Output is correct |
27 |
Correct |
59 ms |
10000 KB |
Output is correct |
28 |
Correct |
54 ms |
10092 KB |
Output is correct |
29 |
Correct |
57 ms |
10012 KB |
Output is correct |
30 |
Correct |
58 ms |
9964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
7 ms |
3656 KB |
Output is correct |
5 |
Correct |
14 ms |
6860 KB |
Output is correct |
6 |
Correct |
24 ms |
10060 KB |
Output is correct |
7 |
Correct |
23 ms |
10068 KB |
Output is correct |
8 |
Correct |
23 ms |
10088 KB |
Output is correct |
9 |
Correct |
23 ms |
10060 KB |
Output is correct |
10 |
Correct |
25 ms |
10024 KB |
Output is correct |
11 |
Correct |
24 ms |
10068 KB |
Output is correct |
12 |
Correct |
25 ms |
10108 KB |
Output is correct |
13 |
Correct |
24 ms |
10088 KB |
Output is correct |
14 |
Correct |
25 ms |
10108 KB |
Output is correct |
15 |
Correct |
26 ms |
10096 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
17 ms |
3668 KB |
Output is correct |
20 |
Correct |
35 ms |
6964 KB |
Output is correct |
21 |
Correct |
56 ms |
10012 KB |
Output is correct |
22 |
Correct |
56 ms |
9980 KB |
Output is correct |
23 |
Correct |
55 ms |
10088 KB |
Output is correct |
24 |
Correct |
64 ms |
10088 KB |
Output is correct |
25 |
Correct |
59 ms |
10068 KB |
Output is correct |
26 |
Correct |
65 ms |
10024 KB |
Output is correct |
27 |
Correct |
59 ms |
10000 KB |
Output is correct |
28 |
Correct |
54 ms |
10092 KB |
Output is correct |
29 |
Correct |
57 ms |
10012 KB |
Output is correct |
30 |
Correct |
58 ms |
9964 KB |
Output is correct |
31 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
7 ms |
3656 KB |
Output is correct |
15 |
Correct |
14 ms |
6860 KB |
Output is correct |
16 |
Correct |
24 ms |
10060 KB |
Output is correct |
17 |
Correct |
23 ms |
10068 KB |
Output is correct |
18 |
Correct |
23 ms |
10088 KB |
Output is correct |
19 |
Correct |
23 ms |
10060 KB |
Output is correct |
20 |
Correct |
25 ms |
10024 KB |
Output is correct |
21 |
Correct |
24 ms |
10068 KB |
Output is correct |
22 |
Correct |
25 ms |
10108 KB |
Output is correct |
23 |
Correct |
24 ms |
10088 KB |
Output is correct |
24 |
Correct |
25 ms |
10108 KB |
Output is correct |
25 |
Correct |
26 ms |
10096 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
17 ms |
3668 KB |
Output is correct |
30 |
Correct |
35 ms |
6964 KB |
Output is correct |
31 |
Correct |
56 ms |
10012 KB |
Output is correct |
32 |
Correct |
56 ms |
9980 KB |
Output is correct |
33 |
Correct |
55 ms |
10088 KB |
Output is correct |
34 |
Correct |
64 ms |
10088 KB |
Output is correct |
35 |
Correct |
59 ms |
10068 KB |
Output is correct |
36 |
Correct |
65 ms |
10024 KB |
Output is correct |
37 |
Correct |
59 ms |
10000 KB |
Output is correct |
38 |
Correct |
54 ms |
10092 KB |
Output is correct |
39 |
Correct |
57 ms |
10012 KB |
Output is correct |
40 |
Correct |
58 ms |
9964 KB |
Output is correct |
41 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
42 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
7 ms |
3656 KB |
Output is correct |
15 |
Correct |
14 ms |
6860 KB |
Output is correct |
16 |
Correct |
24 ms |
10060 KB |
Output is correct |
17 |
Correct |
23 ms |
10068 KB |
Output is correct |
18 |
Correct |
23 ms |
10088 KB |
Output is correct |
19 |
Correct |
23 ms |
10060 KB |
Output is correct |
20 |
Correct |
25 ms |
10024 KB |
Output is correct |
21 |
Correct |
24 ms |
10068 KB |
Output is correct |
22 |
Correct |
25 ms |
10108 KB |
Output is correct |
23 |
Correct |
24 ms |
10088 KB |
Output is correct |
24 |
Correct |
25 ms |
10108 KB |
Output is correct |
25 |
Correct |
26 ms |
10096 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
17 ms |
3668 KB |
Output is correct |
30 |
Correct |
35 ms |
6964 KB |
Output is correct |
31 |
Correct |
56 ms |
10012 KB |
Output is correct |
32 |
Correct |
56 ms |
9980 KB |
Output is correct |
33 |
Correct |
55 ms |
10088 KB |
Output is correct |
34 |
Correct |
64 ms |
10088 KB |
Output is correct |
35 |
Correct |
59 ms |
10068 KB |
Output is correct |
36 |
Correct |
65 ms |
10024 KB |
Output is correct |
37 |
Correct |
59 ms |
10000 KB |
Output is correct |
38 |
Correct |
54 ms |
10092 KB |
Output is correct |
39 |
Correct |
57 ms |
10012 KB |
Output is correct |
40 |
Correct |
58 ms |
9964 KB |
Output is correct |
41 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
42 |
Halted |
0 ms |
0 KB |
- |