#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pb push_back
#define ff first
#define ss second
int a[5001];
int cnt[5001][5001];
int dp[5001];
int ddp[5001];
int pos[200100];
void solve(){
int n;
cin>>n;
for(int i=1; i<=n; i++){
cin>>a[i];
pos[a[i]] = i;
}
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
cnt[i][j] = cnt[i-1][j] + (a[i] >= j ?1 :0);
}
}
for(int i=1; i<=n; i++){
int sum = 0;
dp[i] = 1e9;
for(int j=i; j>=1; j--){
sum += cnt[pos[j]-1][i+1];
sum += (cnt[n][j] - cnt[n][i+1]) - (cnt[pos[j]][j] - cnt[pos[j]][i+1]);
dp[i] = min(dp[i], sum + dp[j-1]);
}
//cout<<dp[i]<<' ';
}
cout<<dp[n];
}
signed main(){
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
4696 KB |
Output is correct |
22 |
Correct |
1 ms |
4796 KB |
Output is correct |
23 |
Correct |
1 ms |
2908 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
25 |
Correct |
1 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 ms |
4696 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4696 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
4696 KB |
Output is correct |
22 |
Correct |
1 ms |
4796 KB |
Output is correct |
23 |
Correct |
1 ms |
2908 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
25 |
Correct |
1 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 ms |
4696 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4696 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
4 ms |
8540 KB |
Output is correct |
32 |
Correct |
4 ms |
8540 KB |
Output is correct |
33 |
Correct |
4 ms |
5976 KB |
Output is correct |
34 |
Correct |
4 ms |
5980 KB |
Output is correct |
35 |
Correct |
5 ms |
7260 KB |
Output is correct |
36 |
Correct |
5 ms |
8796 KB |
Output is correct |
37 |
Correct |
4 ms |
9916 KB |
Output is correct |
38 |
Correct |
4 ms |
10044 KB |
Output is correct |
39 |
Correct |
3 ms |
10076 KB |
Output is correct |
40 |
Correct |
3 ms |
10076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
4696 KB |
Output is correct |
22 |
Correct |
1 ms |
4796 KB |
Output is correct |
23 |
Correct |
1 ms |
2908 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
25 |
Correct |
1 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 ms |
4696 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4696 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
4 ms |
8540 KB |
Output is correct |
32 |
Correct |
4 ms |
8540 KB |
Output is correct |
33 |
Correct |
4 ms |
5976 KB |
Output is correct |
34 |
Correct |
4 ms |
5980 KB |
Output is correct |
35 |
Correct |
5 ms |
7260 KB |
Output is correct |
36 |
Correct |
5 ms |
8796 KB |
Output is correct |
37 |
Correct |
4 ms |
9916 KB |
Output is correct |
38 |
Correct |
4 ms |
10044 KB |
Output is correct |
39 |
Correct |
3 ms |
10076 KB |
Output is correct |
40 |
Correct |
3 ms |
10076 KB |
Output is correct |
41 |
Correct |
219 ms |
98312 KB |
Output is correct |
42 |
Correct |
250 ms |
98128 KB |
Output is correct |
43 |
Correct |
180 ms |
98136 KB |
Output is correct |
44 |
Correct |
224 ms |
98332 KB |
Output is correct |
45 |
Correct |
217 ms |
98216 KB |
Output is correct |
46 |
Correct |
185 ms |
98332 KB |
Output is correct |
47 |
Correct |
237 ms |
98340 KB |
Output is correct |
48 |
Correct |
227 ms |
98136 KB |
Output is correct |
49 |
Correct |
209 ms |
98260 KB |
Output is correct |
50 |
Correct |
201 ms |
98304 KB |
Output is correct |
51 |
Correct |
210 ms |
98344 KB |
Output is correct |
52 |
Correct |
208 ms |
98132 KB |
Output is correct |
53 |
Correct |
197 ms |
98164 KB |
Output is correct |
54 |
Correct |
209 ms |
98192 KB |
Output is correct |
55 |
Correct |
208 ms |
98144 KB |
Output is correct |