#include <bits/stdc++.h>
using namespace std;
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define sp " "
#define endl "\n"
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define N 300005
#define modulo 1000000007
#define int long long
#define M 20000005
const int INF = 2e18 + 7;
int arr[N], cnt[N], dp[M];
int f(int sz, int n, int mask, int ind)
{
if (ind > sz) return 0;
if (dp[mask] != -1) return dp[mask];
int ans = INF;
for (int i = 0; i < n; i++)
{
if (mask & (1<<i)) continue;
int fin = ind + cnt[i + 1] - 1;
int tmp = (sz * (sz + 1)) / 2 - (((ind - 1) * ind) / 2 + ((sz - fin) * (sz - fin + 1)) / 2);
ans = min(ans, tmp + f(sz, n, mask | (1<<i), fin + 1));
}
return dp[mask] = ans;
}
int32_t main()
{
fastio();
int n, q;
cin>>n>>q;
for (int i = 1; i <= n; i++)
cin>>arr[i];
while(q--)
{
memset(cnt, 0, sizeof(cnt));
memset(dp, -1, sizeof(dp));
int l, r;
cin>>l>>r;
map<int, int> val;
int counter = 1;
for (int i = l; i <= r; i++)
{
if (val[arr[i]] == 0)
{
val[arr[i]] = counter;
counter++;
}
cnt[val[arr[i]]]++;
}
int res = f(r - l + 1, counter - 1, 0, 1);
cout<<res<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
159216 KB |
Output is correct |
2 |
Correct |
90 ms |
159124 KB |
Output is correct |
3 |
Correct |
67 ms |
159192 KB |
Output is correct |
4 |
Correct |
77 ms |
159208 KB |
Output is correct |
5 |
Correct |
59 ms |
159156 KB |
Output is correct |
6 |
Correct |
70 ms |
159212 KB |
Output is correct |
7 |
Correct |
76 ms |
159132 KB |
Output is correct |
8 |
Correct |
69 ms |
159148 KB |
Output is correct |
9 |
Correct |
75 ms |
159176 KB |
Output is correct |
10 |
Correct |
73 ms |
159196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
159196 KB |
Output is correct |
2 |
Correct |
70 ms |
159168 KB |
Output is correct |
3 |
Correct |
82 ms |
159220 KB |
Output is correct |
4 |
Correct |
73 ms |
160104 KB |
Output is correct |
5 |
Correct |
88 ms |
161164 KB |
Output is correct |
6 |
Correct |
96 ms |
162032 KB |
Output is correct |
7 |
Correct |
85 ms |
162164 KB |
Output is correct |
8 |
Correct |
90 ms |
162156 KB |
Output is correct |
9 |
Correct |
91 ms |
162168 KB |
Output is correct |
10 |
Correct |
89 ms |
162152 KB |
Output is correct |
11 |
Correct |
84 ms |
162208 KB |
Output is correct |
12 |
Correct |
87 ms |
162200 KB |
Output is correct |
13 |
Correct |
95 ms |
162188 KB |
Output is correct |
14 |
Correct |
83 ms |
162312 KB |
Output is correct |
15 |
Correct |
95 ms |
162184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
159196 KB |
Output is correct |
2 |
Correct |
70 ms |
159168 KB |
Output is correct |
3 |
Correct |
82 ms |
159220 KB |
Output is correct |
4 |
Correct |
73 ms |
160104 KB |
Output is correct |
5 |
Correct |
88 ms |
161164 KB |
Output is correct |
6 |
Correct |
96 ms |
162032 KB |
Output is correct |
7 |
Correct |
85 ms |
162164 KB |
Output is correct |
8 |
Correct |
90 ms |
162156 KB |
Output is correct |
9 |
Correct |
91 ms |
162168 KB |
Output is correct |
10 |
Correct |
89 ms |
162152 KB |
Output is correct |
11 |
Correct |
84 ms |
162208 KB |
Output is correct |
12 |
Correct |
87 ms |
162200 KB |
Output is correct |
13 |
Correct |
95 ms |
162188 KB |
Output is correct |
14 |
Correct |
83 ms |
162312 KB |
Output is correct |
15 |
Correct |
95 ms |
162184 KB |
Output is correct |
16 |
Correct |
60 ms |
159192 KB |
Output is correct |
17 |
Correct |
68 ms |
159228 KB |
Output is correct |
18 |
Correct |
90 ms |
159200 KB |
Output is correct |
19 |
Correct |
308 ms |
160272 KB |
Output is correct |
20 |
Correct |
651 ms |
161324 KB |
Output is correct |
21 |
Correct |
2880 ms |
162372 KB |
Output is correct |
22 |
Correct |
2816 ms |
162340 KB |
Output is correct |
23 |
Correct |
2812 ms |
162356 KB |
Output is correct |
24 |
Correct |
2840 ms |
162340 KB |
Output is correct |
25 |
Correct |
3023 ms |
162332 KB |
Output is correct |
26 |
Correct |
3218 ms |
162316 KB |
Output is correct |
27 |
Correct |
3114 ms |
162332 KB |
Output is correct |
28 |
Correct |
3156 ms |
162284 KB |
Output is correct |
29 |
Correct |
2767 ms |
162300 KB |
Output is correct |
30 |
Correct |
2972 ms |
162360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
159196 KB |
Output is correct |
2 |
Correct |
70 ms |
159168 KB |
Output is correct |
3 |
Correct |
82 ms |
159220 KB |
Output is correct |
4 |
Correct |
73 ms |
160104 KB |
Output is correct |
5 |
Correct |
88 ms |
161164 KB |
Output is correct |
6 |
Correct |
96 ms |
162032 KB |
Output is correct |
7 |
Correct |
85 ms |
162164 KB |
Output is correct |
8 |
Correct |
90 ms |
162156 KB |
Output is correct |
9 |
Correct |
91 ms |
162168 KB |
Output is correct |
10 |
Correct |
89 ms |
162152 KB |
Output is correct |
11 |
Correct |
84 ms |
162208 KB |
Output is correct |
12 |
Correct |
87 ms |
162200 KB |
Output is correct |
13 |
Correct |
95 ms |
162188 KB |
Output is correct |
14 |
Correct |
83 ms |
162312 KB |
Output is correct |
15 |
Correct |
95 ms |
162184 KB |
Output is correct |
16 |
Correct |
60 ms |
159192 KB |
Output is correct |
17 |
Correct |
68 ms |
159228 KB |
Output is correct |
18 |
Correct |
90 ms |
159200 KB |
Output is correct |
19 |
Correct |
308 ms |
160272 KB |
Output is correct |
20 |
Correct |
651 ms |
161324 KB |
Output is correct |
21 |
Correct |
2880 ms |
162372 KB |
Output is correct |
22 |
Correct |
2816 ms |
162340 KB |
Output is correct |
23 |
Correct |
2812 ms |
162356 KB |
Output is correct |
24 |
Correct |
2840 ms |
162340 KB |
Output is correct |
25 |
Correct |
3023 ms |
162332 KB |
Output is correct |
26 |
Correct |
3218 ms |
162316 KB |
Output is correct |
27 |
Correct |
3114 ms |
162332 KB |
Output is correct |
28 |
Correct |
3156 ms |
162284 KB |
Output is correct |
29 |
Correct |
2767 ms |
162300 KB |
Output is correct |
30 |
Correct |
2972 ms |
162360 KB |
Output is correct |
31 |
Runtime error |
190 ms |
322656 KB |
Execution killed with signal 11 |
32 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
159216 KB |
Output is correct |
2 |
Correct |
90 ms |
159124 KB |
Output is correct |
3 |
Correct |
67 ms |
159192 KB |
Output is correct |
4 |
Correct |
77 ms |
159208 KB |
Output is correct |
5 |
Correct |
59 ms |
159156 KB |
Output is correct |
6 |
Correct |
70 ms |
159212 KB |
Output is correct |
7 |
Correct |
76 ms |
159132 KB |
Output is correct |
8 |
Correct |
69 ms |
159148 KB |
Output is correct |
9 |
Correct |
75 ms |
159176 KB |
Output is correct |
10 |
Correct |
73 ms |
159196 KB |
Output is correct |
11 |
Correct |
69 ms |
159196 KB |
Output is correct |
12 |
Correct |
70 ms |
159168 KB |
Output is correct |
13 |
Correct |
82 ms |
159220 KB |
Output is correct |
14 |
Correct |
73 ms |
160104 KB |
Output is correct |
15 |
Correct |
88 ms |
161164 KB |
Output is correct |
16 |
Correct |
96 ms |
162032 KB |
Output is correct |
17 |
Correct |
85 ms |
162164 KB |
Output is correct |
18 |
Correct |
90 ms |
162156 KB |
Output is correct |
19 |
Correct |
91 ms |
162168 KB |
Output is correct |
20 |
Correct |
89 ms |
162152 KB |
Output is correct |
21 |
Correct |
84 ms |
162208 KB |
Output is correct |
22 |
Correct |
87 ms |
162200 KB |
Output is correct |
23 |
Correct |
95 ms |
162188 KB |
Output is correct |
24 |
Correct |
83 ms |
162312 KB |
Output is correct |
25 |
Correct |
95 ms |
162184 KB |
Output is correct |
26 |
Correct |
60 ms |
159192 KB |
Output is correct |
27 |
Correct |
68 ms |
159228 KB |
Output is correct |
28 |
Correct |
90 ms |
159200 KB |
Output is correct |
29 |
Correct |
308 ms |
160272 KB |
Output is correct |
30 |
Correct |
651 ms |
161324 KB |
Output is correct |
31 |
Correct |
2880 ms |
162372 KB |
Output is correct |
32 |
Correct |
2816 ms |
162340 KB |
Output is correct |
33 |
Correct |
2812 ms |
162356 KB |
Output is correct |
34 |
Correct |
2840 ms |
162340 KB |
Output is correct |
35 |
Correct |
3023 ms |
162332 KB |
Output is correct |
36 |
Correct |
3218 ms |
162316 KB |
Output is correct |
37 |
Correct |
3114 ms |
162332 KB |
Output is correct |
38 |
Correct |
3156 ms |
162284 KB |
Output is correct |
39 |
Correct |
2767 ms |
162300 KB |
Output is correct |
40 |
Correct |
2972 ms |
162360 KB |
Output is correct |
41 |
Runtime error |
190 ms |
322656 KB |
Execution killed with signal 11 |
42 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
159216 KB |
Output is correct |
2 |
Correct |
90 ms |
159124 KB |
Output is correct |
3 |
Correct |
67 ms |
159192 KB |
Output is correct |
4 |
Correct |
77 ms |
159208 KB |
Output is correct |
5 |
Correct |
59 ms |
159156 KB |
Output is correct |
6 |
Correct |
70 ms |
159212 KB |
Output is correct |
7 |
Correct |
76 ms |
159132 KB |
Output is correct |
8 |
Correct |
69 ms |
159148 KB |
Output is correct |
9 |
Correct |
75 ms |
159176 KB |
Output is correct |
10 |
Correct |
73 ms |
159196 KB |
Output is correct |
11 |
Correct |
69 ms |
159196 KB |
Output is correct |
12 |
Correct |
70 ms |
159168 KB |
Output is correct |
13 |
Correct |
82 ms |
159220 KB |
Output is correct |
14 |
Correct |
73 ms |
160104 KB |
Output is correct |
15 |
Correct |
88 ms |
161164 KB |
Output is correct |
16 |
Correct |
96 ms |
162032 KB |
Output is correct |
17 |
Correct |
85 ms |
162164 KB |
Output is correct |
18 |
Correct |
90 ms |
162156 KB |
Output is correct |
19 |
Correct |
91 ms |
162168 KB |
Output is correct |
20 |
Correct |
89 ms |
162152 KB |
Output is correct |
21 |
Correct |
84 ms |
162208 KB |
Output is correct |
22 |
Correct |
87 ms |
162200 KB |
Output is correct |
23 |
Correct |
95 ms |
162188 KB |
Output is correct |
24 |
Correct |
83 ms |
162312 KB |
Output is correct |
25 |
Correct |
95 ms |
162184 KB |
Output is correct |
26 |
Correct |
60 ms |
159192 KB |
Output is correct |
27 |
Correct |
68 ms |
159228 KB |
Output is correct |
28 |
Correct |
90 ms |
159200 KB |
Output is correct |
29 |
Correct |
308 ms |
160272 KB |
Output is correct |
30 |
Correct |
651 ms |
161324 KB |
Output is correct |
31 |
Correct |
2880 ms |
162372 KB |
Output is correct |
32 |
Correct |
2816 ms |
162340 KB |
Output is correct |
33 |
Correct |
2812 ms |
162356 KB |
Output is correct |
34 |
Correct |
2840 ms |
162340 KB |
Output is correct |
35 |
Correct |
3023 ms |
162332 KB |
Output is correct |
36 |
Correct |
3218 ms |
162316 KB |
Output is correct |
37 |
Correct |
3114 ms |
162332 KB |
Output is correct |
38 |
Correct |
3156 ms |
162284 KB |
Output is correct |
39 |
Correct |
2767 ms |
162300 KB |
Output is correct |
40 |
Correct |
2972 ms |
162360 KB |
Output is correct |
41 |
Runtime error |
190 ms |
322656 KB |
Execution killed with signal 11 |
42 |
Halted |
0 ms |
0 KB |
- |