#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;
int prefix[5005][5005]; //pos val
int arr[5005];
int pos[5005];
int n=0;
int query(int l, int r, int val, int val2){
if(val2<val) return 0;
if(r<l) return 0;
int hold= prefix[r][val2]-prefix[r][val-1]-prefix[l-1][val2]+prefix[l-1][val-1];
return hold;
}
int memo[5005];
int cost[5005][5005];
int dp(int index){
if(index==n+1) return 0;
if(memo[index]!=-1) return memo[index];
int ans=INT_MAX;
for(int x=index;x<=n;x++){
int add=cost[index][x];
int take=dp(x+1);
ans=min(ans,take+add);
}
return memo[index]=ans;
}
void solve(){
cin >> n;
for(int x=1;x<=n;x++){
cin >> arr[x];
prefix[x][arr[x]]=1;
pos[arr[x]]=x;
}
for(int x=1;x<=n;x++){
for(int y=1;y<=n;y++){
prefix[x][y]+=prefix[x-1][y]+prefix[x][y-1]-prefix[x-1][y-1];
}
}
for(int x=n;x>=1;x--){
int counter=0;
for(int y=x;y>=1;y--){
counter+=query(1,pos[y],x+1,n);
counter+=query(pos[y]+1,n,y+1,x);
cost[y][x]=counter;
//show3(y,y,x,x,counter,counter);
}
}
memset(memo,-1,sizeof(memo));
cout << dp(1);
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
//freopen("in.txt", "r", stdin);
int t=1;
//cin >> t;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4508 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 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 |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4508 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 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 |
4440 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 |
4444 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 |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4508 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 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 |
4440 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 |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
4 ms |
18780 KB |
Output is correct |
22 |
Correct |
2 ms |
18780 KB |
Output is correct |
23 |
Correct |
2 ms |
18780 KB |
Output is correct |
24 |
Correct |
3 ms |
18780 KB |
Output is correct |
25 |
Correct |
2 ms |
18924 KB |
Output is correct |
26 |
Correct |
2 ms |
18780 KB |
Output is correct |
27 |
Correct |
3 ms |
18788 KB |
Output is correct |
28 |
Correct |
2 ms |
18780 KB |
Output is correct |
29 |
Correct |
3 ms |
18780 KB |
Output is correct |
30 |
Correct |
3 ms |
18780 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 |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4508 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 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 |
4440 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 |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
4 ms |
18780 KB |
Output is correct |
22 |
Correct |
2 ms |
18780 KB |
Output is correct |
23 |
Correct |
2 ms |
18780 KB |
Output is correct |
24 |
Correct |
3 ms |
18780 KB |
Output is correct |
25 |
Correct |
2 ms |
18924 KB |
Output is correct |
26 |
Correct |
2 ms |
18780 KB |
Output is correct |
27 |
Correct |
3 ms |
18788 KB |
Output is correct |
28 |
Correct |
2 ms |
18780 KB |
Output is correct |
29 |
Correct |
3 ms |
18780 KB |
Output is correct |
30 |
Correct |
3 ms |
18780 KB |
Output is correct |
31 |
Correct |
16 ms |
66140 KB |
Output is correct |
32 |
Correct |
13 ms |
66140 KB |
Output is correct |
33 |
Correct |
14 ms |
66136 KB |
Output is correct |
34 |
Correct |
13 ms |
66140 KB |
Output is correct |
35 |
Correct |
13 ms |
66056 KB |
Output is correct |
36 |
Correct |
13 ms |
66140 KB |
Output is correct |
37 |
Correct |
13 ms |
66140 KB |
Output is correct |
38 |
Correct |
12 ms |
66136 KB |
Output is correct |
39 |
Correct |
13 ms |
66140 KB |
Output is correct |
40 |
Correct |
14 ms |
66120 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 |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4508 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 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 |
4440 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 |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
4 ms |
18780 KB |
Output is correct |
22 |
Correct |
2 ms |
18780 KB |
Output is correct |
23 |
Correct |
2 ms |
18780 KB |
Output is correct |
24 |
Correct |
3 ms |
18780 KB |
Output is correct |
25 |
Correct |
2 ms |
18924 KB |
Output is correct |
26 |
Correct |
2 ms |
18780 KB |
Output is correct |
27 |
Correct |
3 ms |
18788 KB |
Output is correct |
28 |
Correct |
2 ms |
18780 KB |
Output is correct |
29 |
Correct |
3 ms |
18780 KB |
Output is correct |
30 |
Correct |
3 ms |
18780 KB |
Output is correct |
31 |
Correct |
16 ms |
66140 KB |
Output is correct |
32 |
Correct |
13 ms |
66140 KB |
Output is correct |
33 |
Correct |
14 ms |
66136 KB |
Output is correct |
34 |
Correct |
13 ms |
66140 KB |
Output is correct |
35 |
Correct |
13 ms |
66056 KB |
Output is correct |
36 |
Correct |
13 ms |
66140 KB |
Output is correct |
37 |
Correct |
13 ms |
66140 KB |
Output is correct |
38 |
Correct |
12 ms |
66136 KB |
Output is correct |
39 |
Correct |
13 ms |
66140 KB |
Output is correct |
40 |
Correct |
14 ms |
66120 KB |
Output is correct |
41 |
Correct |
566 ms |
314284 KB |
Output is correct |
42 |
Correct |
575 ms |
314692 KB |
Output is correct |
43 |
Correct |
499 ms |
314668 KB |
Output is correct |
44 |
Correct |
460 ms |
314424 KB |
Output is correct |
45 |
Correct |
465 ms |
336552 KB |
Output is correct |
46 |
Correct |
477 ms |
346816 KB |
Output is correct |
47 |
Correct |
461 ms |
358740 KB |
Output is correct |
48 |
Correct |
449 ms |
359796 KB |
Output is correct |
49 |
Correct |
460 ms |
361024 KB |
Output is correct |
50 |
Correct |
486 ms |
358720 KB |
Output is correct |
51 |
Correct |
453 ms |
362068 KB |
Output is correct |
52 |
Correct |
463 ms |
362080 KB |
Output is correct |
53 |
Correct |
470 ms |
364628 KB |
Output is correct |
54 |
Correct |
433 ms |
368724 KB |
Output is correct |
55 |
Correct |
455 ms |
370656 KB |
Output is correct |