#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define F first
#define S second
#define FOR(i,a,b) for (auto i = (a); i <= (b); ++i)
#define NFOR(i,a,b) for(auto i = (a); i >= (b); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) lli(x.size())
#define mp(i,a) make_pair(i,a)
#define pb(a) push_back(a)
#define bit(x,b) (x&(1LL<<b))
typedef int lli;
typedef pair <lli,lli> ii;
typedef pair <ii,lli> iii;
typedef vector <lli> vi;
typedef tree<lli,null_type,less<lli>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
int n;
int h[5001],dp[5001][5001],p[5001],ans[5001];
int dp1[5001][5001],dp2[5001][5001];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
for(int i=0;i<n;++i)
{
cin>>h[i];
h[i]--;
p[h[i]]=i;
}
for(int i=0;i<n;++i)
{
int v=0,v1=0;
for(int j=i-1;j>=0;--j)
{
if(p[j]<p[i])
{
v=1;
v1=0;
}
else
{
v1=1;
v=0;
}
dp2[i][j]=dp2[i][j+1]+v1;
dp1[i][j]=dp1[i][j+1]+v;
}
for(int j=i+1;j<n;++j)
{
if(p[j]<p[i])
v=1;
else
v=0;
dp1[i][j]=dp1[i][j-1]+v;
}
}
for(int i=0;i<n;++i)
{
dp[i][i]=0;
for(int j=i+1;j<n;++j)
dp[i][j]=dp[i][j-1]+dp1[j][i];
}
for(int i=0;i<n;++i)
{
int v1=0,v2;
int ans1=n*n;
for(int j=i;j>=0;--j)
{
v1-=dp1[j][i];
v1+=dp2[j][0];
if(j-1==-1)
v2=0;
else
v2=ans[j-1];
ans1=min(ans1,v1+dp[j][i]+v2);
}
ans[i]=ans1;
}
cout<<ans[n-1]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
376 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
376 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
1 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
640 KB |
Output is correct |
18 |
Correct |
1 ms |
620 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
376 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
1 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
640 KB |
Output is correct |
18 |
Correct |
1 ms |
620 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
4 ms |
3052 KB |
Output is correct |
22 |
Correct |
3 ms |
3052 KB |
Output is correct |
23 |
Correct |
3 ms |
3052 KB |
Output is correct |
24 |
Correct |
3 ms |
3052 KB |
Output is correct |
25 |
Correct |
3 ms |
3052 KB |
Output is correct |
26 |
Correct |
3 ms |
3052 KB |
Output is correct |
27 |
Correct |
3 ms |
3052 KB |
Output is correct |
28 |
Correct |
3 ms |
3052 KB |
Output is correct |
29 |
Correct |
3 ms |
3052 KB |
Output is correct |
30 |
Correct |
3 ms |
3052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
376 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
1 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
640 KB |
Output is correct |
18 |
Correct |
1 ms |
620 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
4 ms |
3052 KB |
Output is correct |
22 |
Correct |
3 ms |
3052 KB |
Output is correct |
23 |
Correct |
3 ms |
3052 KB |
Output is correct |
24 |
Correct |
3 ms |
3052 KB |
Output is correct |
25 |
Correct |
3 ms |
3052 KB |
Output is correct |
26 |
Correct |
3 ms |
3052 KB |
Output is correct |
27 |
Correct |
3 ms |
3052 KB |
Output is correct |
28 |
Correct |
3 ms |
3052 KB |
Output is correct |
29 |
Correct |
3 ms |
3052 KB |
Output is correct |
30 |
Correct |
3 ms |
3052 KB |
Output is correct |
31 |
Correct |
21 ms |
14956 KB |
Output is correct |
32 |
Correct |
20 ms |
15084 KB |
Output is correct |
33 |
Correct |
21 ms |
15084 KB |
Output is correct |
34 |
Correct |
23 ms |
15084 KB |
Output is correct |
35 |
Correct |
21 ms |
15112 KB |
Output is correct |
36 |
Correct |
21 ms |
15084 KB |
Output is correct |
37 |
Correct |
21 ms |
15084 KB |
Output is correct |
38 |
Correct |
21 ms |
15084 KB |
Output is correct |
39 |
Correct |
23 ms |
15084 KB |
Output is correct |
40 |
Correct |
22 ms |
15084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
376 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
1 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
640 KB |
Output is correct |
18 |
Correct |
1 ms |
620 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
4 ms |
3052 KB |
Output is correct |
22 |
Correct |
3 ms |
3052 KB |
Output is correct |
23 |
Correct |
3 ms |
3052 KB |
Output is correct |
24 |
Correct |
3 ms |
3052 KB |
Output is correct |
25 |
Correct |
3 ms |
3052 KB |
Output is correct |
26 |
Correct |
3 ms |
3052 KB |
Output is correct |
27 |
Correct |
3 ms |
3052 KB |
Output is correct |
28 |
Correct |
3 ms |
3052 KB |
Output is correct |
29 |
Correct |
3 ms |
3052 KB |
Output is correct |
30 |
Correct |
3 ms |
3052 KB |
Output is correct |
31 |
Correct |
21 ms |
14956 KB |
Output is correct |
32 |
Correct |
20 ms |
15084 KB |
Output is correct |
33 |
Correct |
21 ms |
15084 KB |
Output is correct |
34 |
Correct |
23 ms |
15084 KB |
Output is correct |
35 |
Correct |
21 ms |
15112 KB |
Output is correct |
36 |
Correct |
21 ms |
15084 KB |
Output is correct |
37 |
Correct |
21 ms |
15084 KB |
Output is correct |
38 |
Correct |
21 ms |
15084 KB |
Output is correct |
39 |
Correct |
23 ms |
15084 KB |
Output is correct |
40 |
Correct |
22 ms |
15084 KB |
Output is correct |
41 |
Correct |
807 ms |
231976 KB |
Output is correct |
42 |
Correct |
808 ms |
232044 KB |
Output is correct |
43 |
Correct |
817 ms |
232172 KB |
Output is correct |
44 |
Correct |
812 ms |
232300 KB |
Output is correct |
45 |
Correct |
816 ms |
232172 KB |
Output is correct |
46 |
Correct |
813 ms |
232300 KB |
Output is correct |
47 |
Correct |
816 ms |
232172 KB |
Output is correct |
48 |
Correct |
816 ms |
232300 KB |
Output is correct |
49 |
Correct |
810 ms |
232556 KB |
Output is correct |
50 |
Correct |
807 ms |
232300 KB |
Output is correct |
51 |
Correct |
809 ms |
232300 KB |
Output is correct |
52 |
Correct |
818 ms |
232300 KB |
Output is correct |
53 |
Correct |
817 ms |
232428 KB |
Output is correct |
54 |
Correct |
816 ms |
232300 KB |
Output is correct |
55 |
Correct |
813 ms |
232172 KB |
Output is correct |