#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=5e3;
int g[N+10];
int f[N+10][N+10];
int c[N+10][N+10];
int dp[N+10];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
g[x]=i;
}
for(int i=1;i<=n;i++)
{
for(int j=n-1;j>=i;j--)
f[i][j]=f[i][j+1]+(g[j+1]<g[i]);
}
for(int i=n;i>=1;i--)
{
for(int j=i;j<=n;j++)
c[i][j]=c[i+1][j]+j-i+2*f[i][j]-f[i][i];
}
//cerr<<f[1][1]<<"\n";
for(int i=1;i<=n;i++)
{
dp[i]=1e9+7;
for(int j=1;j<=i;j++)
dp[i]=min(dp[i],dp[j-1]+c[j][i]);
//cerr<<i<<": "<<dp[i]<<"\n";
}
cout<<dp[n]<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
456 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
0 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
0 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
456 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
0 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
0 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
2 ms |
2000 KB |
Output is correct |
22 |
Correct |
2 ms |
1984 KB |
Output is correct |
23 |
Correct |
2 ms |
2000 KB |
Output is correct |
24 |
Correct |
1 ms |
1988 KB |
Output is correct |
25 |
Correct |
1 ms |
2000 KB |
Output is correct |
26 |
Correct |
1 ms |
2000 KB |
Output is correct |
27 |
Correct |
2 ms |
2000 KB |
Output is correct |
28 |
Correct |
1 ms |
2000 KB |
Output is correct |
29 |
Correct |
1 ms |
2000 KB |
Output is correct |
30 |
Correct |
2 ms |
2000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
456 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
0 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
0 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
2 ms |
2000 KB |
Output is correct |
22 |
Correct |
2 ms |
1984 KB |
Output is correct |
23 |
Correct |
2 ms |
2000 KB |
Output is correct |
24 |
Correct |
1 ms |
1988 KB |
Output is correct |
25 |
Correct |
1 ms |
2000 KB |
Output is correct |
26 |
Correct |
1 ms |
2000 KB |
Output is correct |
27 |
Correct |
2 ms |
2000 KB |
Output is correct |
28 |
Correct |
1 ms |
2000 KB |
Output is correct |
29 |
Correct |
1 ms |
2000 KB |
Output is correct |
30 |
Correct |
2 ms |
2000 KB |
Output is correct |
31 |
Correct |
6 ms |
9260 KB |
Output is correct |
32 |
Correct |
6 ms |
9168 KB |
Output is correct |
33 |
Correct |
8 ms |
9168 KB |
Output is correct |
34 |
Correct |
6 ms |
9296 KB |
Output is correct |
35 |
Correct |
6 ms |
9264 KB |
Output is correct |
36 |
Correct |
6 ms |
9296 KB |
Output is correct |
37 |
Correct |
6 ms |
9296 KB |
Output is correct |
38 |
Correct |
6 ms |
9296 KB |
Output is correct |
39 |
Correct |
6 ms |
9296 KB |
Output is correct |
40 |
Correct |
6 ms |
9296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
456 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
0 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
0 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
2 ms |
2000 KB |
Output is correct |
22 |
Correct |
2 ms |
1984 KB |
Output is correct |
23 |
Correct |
2 ms |
2000 KB |
Output is correct |
24 |
Correct |
1 ms |
1988 KB |
Output is correct |
25 |
Correct |
1 ms |
2000 KB |
Output is correct |
26 |
Correct |
1 ms |
2000 KB |
Output is correct |
27 |
Correct |
2 ms |
2000 KB |
Output is correct |
28 |
Correct |
1 ms |
2000 KB |
Output is correct |
29 |
Correct |
1 ms |
2000 KB |
Output is correct |
30 |
Correct |
2 ms |
2000 KB |
Output is correct |
31 |
Correct |
6 ms |
9260 KB |
Output is correct |
32 |
Correct |
6 ms |
9168 KB |
Output is correct |
33 |
Correct |
8 ms |
9168 KB |
Output is correct |
34 |
Correct |
6 ms |
9296 KB |
Output is correct |
35 |
Correct |
6 ms |
9264 KB |
Output is correct |
36 |
Correct |
6 ms |
9296 KB |
Output is correct |
37 |
Correct |
6 ms |
9296 KB |
Output is correct |
38 |
Correct |
6 ms |
9296 KB |
Output is correct |
39 |
Correct |
6 ms |
9296 KB |
Output is correct |
40 |
Correct |
6 ms |
9296 KB |
Output is correct |
41 |
Correct |
190 ms |
134320 KB |
Output is correct |
42 |
Correct |
208 ms |
134284 KB |
Output is correct |
43 |
Correct |
185 ms |
134312 KB |
Output is correct |
44 |
Correct |
188 ms |
134236 KB |
Output is correct |
45 |
Correct |
197 ms |
134340 KB |
Output is correct |
46 |
Correct |
184 ms |
134340 KB |
Output is correct |
47 |
Correct |
212 ms |
134344 KB |
Output is correct |
48 |
Correct |
202 ms |
134392 KB |
Output is correct |
49 |
Correct |
186 ms |
134344 KB |
Output is correct |
50 |
Correct |
186 ms |
134344 KB |
Output is correct |
51 |
Correct |
205 ms |
134344 KB |
Output is correct |
52 |
Correct |
190 ms |
134344 KB |
Output is correct |
53 |
Correct |
187 ms |
134360 KB |
Output is correct |
54 |
Correct |
189 ms |
134340 KB |
Output is correct |
55 |
Correct |
194 ms |
134468 KB |
Output is correct |