#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define bit(n,i) ((n>>i)&1)
#define all(x) x.begin(),x.end()
//#pragma GCC optimize("O2,unroll-loops")
//#pragma GCC target("avx,avx2,bmi,bmi2,sse,sse2,sse3,ssse3,sse4,popcnt")
//#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef double ld;
typedef pair<ld,ld> pdd;
typedef pair<ll,ll> pll;
const ll maxn=5e3+5;
const ll offset=1e18;
const ll block_sz=317;
const ll inf=1e9;
const ll mod=1e9+7;
int n,h[maxn],dd[maxn][maxn],cnt[maxn][maxn],dp[maxn];
void sol()
{
cin >> n;
for1(i,1,n) cin >> h[i];
for1(i,1,n)
{
for1(j,i+1,n)
{
if (h[i]<h[j])
{
cnt[h[i]][h[j]]=1;
}
else
{
dd[h[j]][h[i]]=1;
}
}
}
for1(range,3,n)
{
for1(i,1,n-range+1)
{
int j=i+range-1;
cnt[i][j]=cnt[i][j] + cnt[i][j-1] + cnt[i+1][j] - cnt[i+1][j-1];
//cerr<< i<<' '<<j<<' '<<cnt[i][j]<<'\n';
}
}
// cerr<< cnt[1][5]<<'\n';
for1(i,1,n)
{
for2(j,n,i+1)
{
dd[i][j]+=dd[i][j+1];
}
}
//cerr<< dd[1][3]<<'\n';
for1(j,2,n)
{
for2(i,j-2,1)
{
dd[i][j]+=dd[i+1][j];
}
}
for1(i,1,n) dp[i]=inf;
for1(i,1,n)
{
for1(j,1,i)
{
dp[i]=min(dp[i],dp[j-1]+cnt[j][i]+dd[j][i+1]);
}
//if (i==5) cerr<< dp[1-1]<<' '<<cnt[1][i]<<' '<<dd[1][i+1]<<'\n';
// cerr<< i<<' '<<dp[i]<<'\n';
}
cout << dp[n];
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1;//cin >> t;
while (t--)
{
sol();
}
}
/*
3 5 2 4 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 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 |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 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 |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
21 |
Correct |
3 ms |
10584 KB |
Output is correct |
22 |
Correct |
2 ms |
10584 KB |
Output is correct |
23 |
Correct |
2 ms |
10588 KB |
Output is correct |
24 |
Correct |
2 ms |
10584 KB |
Output is correct |
25 |
Correct |
2 ms |
10584 KB |
Output is correct |
26 |
Correct |
2 ms |
10584 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10584 KB |
Output is correct |
29 |
Correct |
2 ms |
10584 KB |
Output is correct |
30 |
Correct |
2 ms |
10584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 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 |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
21 |
Correct |
3 ms |
10584 KB |
Output is correct |
22 |
Correct |
2 ms |
10584 KB |
Output is correct |
23 |
Correct |
2 ms |
10588 KB |
Output is correct |
24 |
Correct |
2 ms |
10584 KB |
Output is correct |
25 |
Correct |
2 ms |
10584 KB |
Output is correct |
26 |
Correct |
2 ms |
10584 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10584 KB |
Output is correct |
29 |
Correct |
2 ms |
10584 KB |
Output is correct |
30 |
Correct |
2 ms |
10584 KB |
Output is correct |
31 |
Correct |
7 ms |
35416 KB |
Output is correct |
32 |
Correct |
6 ms |
35416 KB |
Output is correct |
33 |
Correct |
7 ms |
35416 KB |
Output is correct |
34 |
Correct |
7 ms |
35276 KB |
Output is correct |
35 |
Correct |
7 ms |
35416 KB |
Output is correct |
36 |
Correct |
7 ms |
35416 KB |
Output is correct |
37 |
Correct |
7 ms |
35160 KB |
Output is correct |
38 |
Correct |
7 ms |
35416 KB |
Output is correct |
39 |
Correct |
7 ms |
35164 KB |
Output is correct |
40 |
Correct |
7 ms |
35416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 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 |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
21 |
Correct |
3 ms |
10584 KB |
Output is correct |
22 |
Correct |
2 ms |
10584 KB |
Output is correct |
23 |
Correct |
2 ms |
10588 KB |
Output is correct |
24 |
Correct |
2 ms |
10584 KB |
Output is correct |
25 |
Correct |
2 ms |
10584 KB |
Output is correct |
26 |
Correct |
2 ms |
10584 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10584 KB |
Output is correct |
29 |
Correct |
2 ms |
10584 KB |
Output is correct |
30 |
Correct |
2 ms |
10584 KB |
Output is correct |
31 |
Correct |
7 ms |
35416 KB |
Output is correct |
32 |
Correct |
6 ms |
35416 KB |
Output is correct |
33 |
Correct |
7 ms |
35416 KB |
Output is correct |
34 |
Correct |
7 ms |
35276 KB |
Output is correct |
35 |
Correct |
7 ms |
35416 KB |
Output is correct |
36 |
Correct |
7 ms |
35416 KB |
Output is correct |
37 |
Correct |
7 ms |
35160 KB |
Output is correct |
38 |
Correct |
7 ms |
35416 KB |
Output is correct |
39 |
Correct |
7 ms |
35164 KB |
Output is correct |
40 |
Correct |
7 ms |
35416 KB |
Output is correct |
41 |
Correct |
225 ms |
195664 KB |
Output is correct |
42 |
Correct |
220 ms |
195664 KB |
Output is correct |
43 |
Correct |
223 ms |
195848 KB |
Output is correct |
44 |
Correct |
216 ms |
195664 KB |
Output is correct |
45 |
Correct |
221 ms |
195856 KB |
Output is correct |
46 |
Correct |
219 ms |
195664 KB |
Output is correct |
47 |
Correct |
222 ms |
196156 KB |
Output is correct |
48 |
Correct |
220 ms |
195768 KB |
Output is correct |
49 |
Correct |
221 ms |
195764 KB |
Output is correct |
50 |
Correct |
216 ms |
195664 KB |
Output is correct |
51 |
Correct |
221 ms |
196008 KB |
Output is correct |
52 |
Correct |
222 ms |
195784 KB |
Output is correct |
53 |
Correct |
218 ms |
195760 KB |
Output is correct |
54 |
Correct |
215 ms |
195632 KB |
Output is correct |
55 |
Correct |
220 ms |
196184 KB |
Output is correct |