#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
const ll NMAX = 5002;
const ll INF = (1LL << 60);
const ll nrbits = 20;
const int MOD = 998244353;
const int bucket = 320;
int mici[NMAX][NMAX];
int v[NMAX];
int dp[NMAX];
int st[NMAX][NMAX];
int main() {
#ifdef HOME
ifstream cin(".in");
ofstream cout(".out");
#endif // HOME
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, i;
cin >> n;
for(i = 1; i <= n; i++){
cin >> v[i];
}
for(i = 1; i <= n; i++){
for(int j = i + 1; j <= n; j++){
mici[v[i]][v[j]]++;
}
for(int j = 1; j <= i; j++){
st[v[i]][v[j]]++;
}
for(int j = 1; j <= n; j++){
st[v[i]][j] += st[v[i]][j - 1];
mici[v[i]][j] += mici[v[i]][j - 1];
}
}
for(int i = n; i >= 1; i--) dp[i] = 1e9;
for(int i = n; i >= 1; i--){
int sum = 0;
for(int j = i; j <= n; j++){
int nou = j;
sum += (mici[nou][i - 1] + st[nou][nou - 1] - st[nou][i - 1]);
dp[i] = min(dp[i], dp[j + 1] + sum);
}
}
cout << dp[1];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2524 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2524 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4556 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4564 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2524 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4556 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4564 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
2 ms |
10584 KB |
Output is correct |
25 |
Correct |
2 ms |
10588 KB |
Output is correct |
26 |
Correct |
1 ms |
10588 KB |
Output is correct |
27 |
Correct |
1 ms |
10588 KB |
Output is correct |
28 |
Correct |
1 ms |
10588 KB |
Output is correct |
29 |
Correct |
1 ms |
10588 KB |
Output is correct |
30 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2524 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4556 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4564 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
2 ms |
10584 KB |
Output is correct |
25 |
Correct |
2 ms |
10588 KB |
Output is correct |
26 |
Correct |
1 ms |
10588 KB |
Output is correct |
27 |
Correct |
1 ms |
10588 KB |
Output is correct |
28 |
Correct |
1 ms |
10588 KB |
Output is correct |
29 |
Correct |
1 ms |
10588 KB |
Output is correct |
30 |
Correct |
1 ms |
10588 KB |
Output is correct |
31 |
Correct |
8 ms |
35164 KB |
Output is correct |
32 |
Correct |
6 ms |
35164 KB |
Output is correct |
33 |
Correct |
6 ms |
35164 KB |
Output is correct |
34 |
Correct |
6 ms |
35164 KB |
Output is correct |
35 |
Correct |
6 ms |
35420 KB |
Output is correct |
36 |
Correct |
6 ms |
35164 KB |
Output is correct |
37 |
Correct |
6 ms |
35420 KB |
Output is correct |
38 |
Correct |
6 ms |
35164 KB |
Output is correct |
39 |
Correct |
7 ms |
35164 KB |
Output is correct |
40 |
Correct |
6 ms |
35280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2524 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4556 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4564 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
2 ms |
10584 KB |
Output is correct |
25 |
Correct |
2 ms |
10588 KB |
Output is correct |
26 |
Correct |
1 ms |
10588 KB |
Output is correct |
27 |
Correct |
1 ms |
10588 KB |
Output is correct |
28 |
Correct |
1 ms |
10588 KB |
Output is correct |
29 |
Correct |
1 ms |
10588 KB |
Output is correct |
30 |
Correct |
1 ms |
10588 KB |
Output is correct |
31 |
Correct |
8 ms |
35164 KB |
Output is correct |
32 |
Correct |
6 ms |
35164 KB |
Output is correct |
33 |
Correct |
6 ms |
35164 KB |
Output is correct |
34 |
Correct |
6 ms |
35164 KB |
Output is correct |
35 |
Correct |
6 ms |
35420 KB |
Output is correct |
36 |
Correct |
6 ms |
35164 KB |
Output is correct |
37 |
Correct |
6 ms |
35420 KB |
Output is correct |
38 |
Correct |
6 ms |
35164 KB |
Output is correct |
39 |
Correct |
7 ms |
35164 KB |
Output is correct |
40 |
Correct |
6 ms |
35280 KB |
Output is correct |
41 |
Correct |
174 ms |
196180 KB |
Output is correct |
42 |
Correct |
174 ms |
196228 KB |
Output is correct |
43 |
Correct |
174 ms |
196436 KB |
Output is correct |
44 |
Correct |
167 ms |
196228 KB |
Output is correct |
45 |
Correct |
192 ms |
196300 KB |
Output is correct |
46 |
Correct |
175 ms |
196408 KB |
Output is correct |
47 |
Correct |
174 ms |
196312 KB |
Output is correct |
48 |
Correct |
181 ms |
196316 KB |
Output is correct |
49 |
Correct |
170 ms |
196340 KB |
Output is correct |
50 |
Correct |
196 ms |
196280 KB |
Output is correct |
51 |
Correct |
181 ms |
196424 KB |
Output is correct |
52 |
Correct |
173 ms |
196180 KB |
Output is correct |
53 |
Correct |
199 ms |
196180 KB |
Output is correct |
54 |
Correct |
172 ms |
196312 KB |
Output is correct |
55 |
Correct |
174 ms |
196312 KB |
Output is correct |