#include<bits/stdc++.h>
using namespace std;
int dp[5010],a[5010][5010]={},ori[5010],pre[5010][5010]={};
int q(int li,int ri,int dj,int uj){
return pre[ri][uj]-pre[li-1][uj]-pre[ri][dj-1]+pre[dj-1][li-1];
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>ori[i];
dp[i]=1000000000;
}
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(ori[i]<ori[j]){
a[ori[i]][ori[j]]=1;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
pre[i][j]=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1]+a[i][j];
}
}
for(int i=1;i<=n;i++){
dp[i]=dp[i-1]+i-1-q(1,i,i,i);
for(int j=0;j<i;j++){
dp[i]=min(dp[i],q(j+1,i,j+1,i)+(i-j)*j-q(1,j,j+1,i)+dp[j]);
}
}
cout<<dp[n]<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
472 KB |
Output is correct |
21 |
Correct |
2 ms |
2132 KB |
Output is correct |
22 |
Correct |
2 ms |
2132 KB |
Output is correct |
23 |
Correct |
1 ms |
2116 KB |
Output is correct |
24 |
Correct |
1 ms |
2140 KB |
Output is correct |
25 |
Correct |
2 ms |
2144 KB |
Output is correct |
26 |
Correct |
2 ms |
2144 KB |
Output is correct |
27 |
Correct |
2 ms |
2144 KB |
Output is correct |
28 |
Correct |
2 ms |
2144 KB |
Output is correct |
29 |
Correct |
2 ms |
2144 KB |
Output is correct |
30 |
Correct |
1 ms |
2144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
472 KB |
Output is correct |
21 |
Correct |
2 ms |
2132 KB |
Output is correct |
22 |
Correct |
2 ms |
2132 KB |
Output is correct |
23 |
Correct |
1 ms |
2116 KB |
Output is correct |
24 |
Correct |
1 ms |
2140 KB |
Output is correct |
25 |
Correct |
2 ms |
2144 KB |
Output is correct |
26 |
Correct |
2 ms |
2144 KB |
Output is correct |
27 |
Correct |
2 ms |
2144 KB |
Output is correct |
28 |
Correct |
2 ms |
2144 KB |
Output is correct |
29 |
Correct |
2 ms |
2144 KB |
Output is correct |
30 |
Correct |
1 ms |
2144 KB |
Output is correct |
31 |
Correct |
9 ms |
10464 KB |
Output is correct |
32 |
Correct |
10 ms |
10444 KB |
Output is correct |
33 |
Correct |
9 ms |
10504 KB |
Output is correct |
34 |
Correct |
10 ms |
10424 KB |
Output is correct |
35 |
Correct |
10 ms |
10464 KB |
Output is correct |
36 |
Correct |
10 ms |
10532 KB |
Output is correct |
37 |
Correct |
10 ms |
10516 KB |
Output is correct |
38 |
Correct |
10 ms |
10444 KB |
Output is correct |
39 |
Correct |
10 ms |
10452 KB |
Output is correct |
40 |
Correct |
9 ms |
10452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
472 KB |
Output is correct |
21 |
Correct |
2 ms |
2132 KB |
Output is correct |
22 |
Correct |
2 ms |
2132 KB |
Output is correct |
23 |
Correct |
1 ms |
2116 KB |
Output is correct |
24 |
Correct |
1 ms |
2140 KB |
Output is correct |
25 |
Correct |
2 ms |
2144 KB |
Output is correct |
26 |
Correct |
2 ms |
2144 KB |
Output is correct |
27 |
Correct |
2 ms |
2144 KB |
Output is correct |
28 |
Correct |
2 ms |
2144 KB |
Output is correct |
29 |
Correct |
2 ms |
2144 KB |
Output is correct |
30 |
Correct |
1 ms |
2144 KB |
Output is correct |
31 |
Correct |
9 ms |
10464 KB |
Output is correct |
32 |
Correct |
10 ms |
10444 KB |
Output is correct |
33 |
Correct |
9 ms |
10504 KB |
Output is correct |
34 |
Correct |
10 ms |
10424 KB |
Output is correct |
35 |
Correct |
10 ms |
10464 KB |
Output is correct |
36 |
Correct |
10 ms |
10532 KB |
Output is correct |
37 |
Correct |
10 ms |
10516 KB |
Output is correct |
38 |
Correct |
10 ms |
10444 KB |
Output is correct |
39 |
Correct |
10 ms |
10452 KB |
Output is correct |
40 |
Correct |
9 ms |
10452 KB |
Output is correct |
41 |
Correct |
380 ms |
164980 KB |
Output is correct |
42 |
Correct |
384 ms |
165076 KB |
Output is correct |
43 |
Correct |
388 ms |
165168 KB |
Output is correct |
44 |
Correct |
395 ms |
164996 KB |
Output is correct |
45 |
Correct |
387 ms |
165180 KB |
Output is correct |
46 |
Correct |
390 ms |
165164 KB |
Output is correct |
47 |
Correct |
379 ms |
165000 KB |
Output is correct |
48 |
Correct |
409 ms |
165188 KB |
Output is correct |
49 |
Correct |
407 ms |
165228 KB |
Output is correct |
50 |
Correct |
391 ms |
165188 KB |
Output is correct |
51 |
Correct |
376 ms |
165208 KB |
Output is correct |
52 |
Correct |
379 ms |
165140 KB |
Output is correct |
53 |
Correct |
386 ms |
165156 KB |
Output is correct |
54 |
Correct |
387 ms |
165196 KB |
Output is correct |
55 |
Correct |
377 ms |
165088 KB |
Output is correct |