#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=5005;
int ar[N][N], p[N][N],b[N], dp[N];
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n; cin >> n;
vector<int> a(n);
for(auto &i: a) cin >> i;
for(int i=1; i<=n; i++) dp[i]=1e9;
for(int i=0; i<n; i++){
for(int l=0; l<i; l++){
ar[a[i]][a[l]]++;
}
for(int l=n-1; l>=0; l--){
ar[a[i]][l]+=ar[a[i]][l+1];
}
}
//her i l için bir prefix sum
for(int i=0; i<=n; i++){
for(int l=0; l<=n; l++){
p[i][l]=ar[l][i];
if(l!=0) p[i][l]+=p[i][l-1];
}
b[i]+=ar[i][i];
if(i!=0) b[i]+=b[i-1];
}
for(int i=1; i<=n; i++){
int v=0;
for(int l=i-1; l>=0; l--){
int v=0;
v+=p[i+1][i]-p[i+1][l];
v+=p[l+1][i]-p[l+1][l];
v-=b[i]-b[l];
dp[i]=min(dp[i], dp[l]+v);
}
// cout<<"-----"<<dp[i]<<"\n";
}
cout<<dp[n];
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:37:13: warning: unused variable 'v' [-Wunused-variable]
37 | int v=0;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 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 |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 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 |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2408 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2648 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 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 |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2408 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2648 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
3 ms |
16984 KB |
Output is correct |
22 |
Correct |
2 ms |
16988 KB |
Output is correct |
23 |
Correct |
3 ms |
16988 KB |
Output is correct |
24 |
Correct |
2 ms |
16984 KB |
Output is correct |
25 |
Correct |
2 ms |
17240 KB |
Output is correct |
26 |
Correct |
2 ms |
16988 KB |
Output is correct |
27 |
Correct |
3 ms |
16988 KB |
Output is correct |
28 |
Correct |
3 ms |
16988 KB |
Output is correct |
29 |
Correct |
3 ms |
16988 KB |
Output is correct |
30 |
Correct |
2 ms |
16988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 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 |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2408 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2648 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
3 ms |
16984 KB |
Output is correct |
22 |
Correct |
2 ms |
16988 KB |
Output is correct |
23 |
Correct |
3 ms |
16988 KB |
Output is correct |
24 |
Correct |
2 ms |
16984 KB |
Output is correct |
25 |
Correct |
2 ms |
17240 KB |
Output is correct |
26 |
Correct |
2 ms |
16988 KB |
Output is correct |
27 |
Correct |
3 ms |
16988 KB |
Output is correct |
28 |
Correct |
3 ms |
16988 KB |
Output is correct |
29 |
Correct |
3 ms |
16988 KB |
Output is correct |
30 |
Correct |
2 ms |
16988 KB |
Output is correct |
31 |
Correct |
33 ms |
64600 KB |
Output is correct |
32 |
Correct |
15 ms |
64348 KB |
Output is correct |
33 |
Correct |
14 ms |
64544 KB |
Output is correct |
34 |
Correct |
15 ms |
64604 KB |
Output is correct |
35 |
Correct |
14 ms |
64604 KB |
Output is correct |
36 |
Correct |
14 ms |
64664 KB |
Output is correct |
37 |
Correct |
14 ms |
64600 KB |
Output is correct |
38 |
Correct |
13 ms |
64608 KB |
Output is correct |
39 |
Correct |
15 ms |
64672 KB |
Output is correct |
40 |
Correct |
14 ms |
64672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 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 |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2408 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2648 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
3 ms |
16984 KB |
Output is correct |
22 |
Correct |
2 ms |
16988 KB |
Output is correct |
23 |
Correct |
3 ms |
16988 KB |
Output is correct |
24 |
Correct |
2 ms |
16984 KB |
Output is correct |
25 |
Correct |
2 ms |
17240 KB |
Output is correct |
26 |
Correct |
2 ms |
16988 KB |
Output is correct |
27 |
Correct |
3 ms |
16988 KB |
Output is correct |
28 |
Correct |
3 ms |
16988 KB |
Output is correct |
29 |
Correct |
3 ms |
16988 KB |
Output is correct |
30 |
Correct |
2 ms |
16988 KB |
Output is correct |
31 |
Correct |
33 ms |
64600 KB |
Output is correct |
32 |
Correct |
15 ms |
64348 KB |
Output is correct |
33 |
Correct |
14 ms |
64544 KB |
Output is correct |
34 |
Correct |
15 ms |
64604 KB |
Output is correct |
35 |
Correct |
14 ms |
64604 KB |
Output is correct |
36 |
Correct |
14 ms |
64664 KB |
Output is correct |
37 |
Correct |
14 ms |
64600 KB |
Output is correct |
38 |
Correct |
13 ms |
64608 KB |
Output is correct |
39 |
Correct |
15 ms |
64672 KB |
Output is correct |
40 |
Correct |
14 ms |
64672 KB |
Output is correct |
41 |
Correct |
656 ms |
392588 KB |
Output is correct |
42 |
Correct |
587 ms |
392568 KB |
Output is correct |
43 |
Correct |
554 ms |
392828 KB |
Output is correct |
44 |
Correct |
563 ms |
392732 KB |
Output is correct |
45 |
Correct |
557 ms |
392744 KB |
Output is correct |
46 |
Correct |
551 ms |
392532 KB |
Output is correct |
47 |
Correct |
557 ms |
392528 KB |
Output is correct |
48 |
Correct |
568 ms |
392660 KB |
Output is correct |
49 |
Correct |
552 ms |
392436 KB |
Output is correct |
50 |
Correct |
559 ms |
392612 KB |
Output is correct |
51 |
Correct |
555 ms |
392752 KB |
Output is correct |
52 |
Correct |
561 ms |
392532 KB |
Output is correct |
53 |
Correct |
559 ms |
392500 KB |
Output is correct |
54 |
Correct |
572 ms |
392820 KB |
Output is correct |
55 |
Correct |
575 ms |
392784 KB |
Output is correct |