#include <bits/stdc++.h>
using namespace std;
#include <bits/extc++.h>
using namespace __gnu_pbds;
#include <ext/rope>
using namespace __gnu_cxx;
typedef tree<long long, null_type, less<long long>,
rb_tree_tag, tree_order_statistics_node_update> pbds;
//less_equal for identical elements
#define DEBUG
#ifdef DEBUG
#define debug(...) printf(__VA_ARGS__);
#else
#define debug(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb emplace_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
mt19937 rng(time(0));
#define maxn 5005
int n,h[maxn],pos[maxn],ft[maxn],cost[maxn][maxn],pfx[maxn],dp[maxn];
void up(int x){
while(x<=n)++ft[x],x+=x&-x;
}
int qry(int x){
int res=0;
while(x)res+=ft[x],x-=x&-x;
return res;
}
int main(){
sf("%d",&n);
for(int i=1;i<=n;++i){
sf("%d",&h[i]);
pos[h[i]]=i;
}
for(int i=1;i<=n;++i){
memset(ft,0,sizeof ft);
memset(pfx,0,sizeof pfx);
for(int j=1;j<i;++j)++pfx[pos[j]];
for(int j=1;j<=n;++j)pfx[j]+=pfx[j-1];
for(int j=i;j<=n;++j){
//cost to make j...i, only considering stuff in [i,n]
cost[i][j]=cost[i][j-1];
cost[i][j]+=qry(pos[j]); //number of stuff before it
cost[i][j]+=i-1-pfx[pos[j]];
up(pos[j]);
}
}
for(int i=1;i<=n;++i){
dp[i]=INF;
for(int j=1;j<=i;++j){
dp[i]=min(dp[i],dp[j-1]+cost[j][i]);
}
}
pf("%d\n",dp[n]);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:55:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | sf("%d",&n);
| ^
Main.cpp:57:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | sf("%d",&h[i]);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
432 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
432 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
1228 KB |
Output is correct |
22 |
Correct |
2 ms |
1228 KB |
Output is correct |
23 |
Correct |
2 ms |
1228 KB |
Output is correct |
24 |
Correct |
2 ms |
1228 KB |
Output is correct |
25 |
Correct |
2 ms |
1228 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1228 KB |
Output is correct |
28 |
Correct |
2 ms |
1228 KB |
Output is correct |
29 |
Correct |
2 ms |
1228 KB |
Output is correct |
30 |
Correct |
2 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
432 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
1228 KB |
Output is correct |
22 |
Correct |
2 ms |
1228 KB |
Output is correct |
23 |
Correct |
2 ms |
1228 KB |
Output is correct |
24 |
Correct |
2 ms |
1228 KB |
Output is correct |
25 |
Correct |
2 ms |
1228 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1228 KB |
Output is correct |
28 |
Correct |
2 ms |
1228 KB |
Output is correct |
29 |
Correct |
2 ms |
1228 KB |
Output is correct |
30 |
Correct |
2 ms |
1228 KB |
Output is correct |
31 |
Correct |
13 ms |
4784 KB |
Output is correct |
32 |
Correct |
13 ms |
4764 KB |
Output is correct |
33 |
Correct |
14 ms |
4812 KB |
Output is correct |
34 |
Correct |
14 ms |
4784 KB |
Output is correct |
35 |
Correct |
13 ms |
4824 KB |
Output is correct |
36 |
Correct |
13 ms |
4816 KB |
Output is correct |
37 |
Correct |
14 ms |
4812 KB |
Output is correct |
38 |
Correct |
14 ms |
4772 KB |
Output is correct |
39 |
Correct |
14 ms |
4812 KB |
Output is correct |
40 |
Correct |
14 ms |
4892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
432 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
1228 KB |
Output is correct |
22 |
Correct |
2 ms |
1228 KB |
Output is correct |
23 |
Correct |
2 ms |
1228 KB |
Output is correct |
24 |
Correct |
2 ms |
1228 KB |
Output is correct |
25 |
Correct |
2 ms |
1228 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1228 KB |
Output is correct |
28 |
Correct |
2 ms |
1228 KB |
Output is correct |
29 |
Correct |
2 ms |
1228 KB |
Output is correct |
30 |
Correct |
2 ms |
1228 KB |
Output is correct |
31 |
Correct |
13 ms |
4784 KB |
Output is correct |
32 |
Correct |
13 ms |
4764 KB |
Output is correct |
33 |
Correct |
14 ms |
4812 KB |
Output is correct |
34 |
Correct |
14 ms |
4784 KB |
Output is correct |
35 |
Correct |
13 ms |
4824 KB |
Output is correct |
36 |
Correct |
13 ms |
4816 KB |
Output is correct |
37 |
Correct |
14 ms |
4812 KB |
Output is correct |
38 |
Correct |
14 ms |
4772 KB |
Output is correct |
39 |
Correct |
14 ms |
4812 KB |
Output is correct |
40 |
Correct |
14 ms |
4892 KB |
Output is correct |
41 |
Correct |
615 ms |
67280 KB |
Output is correct |
42 |
Correct |
610 ms |
67268 KB |
Output is correct |
43 |
Correct |
616 ms |
67268 KB |
Output is correct |
44 |
Correct |
620 ms |
67488 KB |
Output is correct |
45 |
Correct |
619 ms |
67256 KB |
Output is correct |
46 |
Correct |
624 ms |
67420 KB |
Output is correct |
47 |
Correct |
620 ms |
67384 KB |
Output is correct |
48 |
Correct |
607 ms |
67396 KB |
Output is correct |
49 |
Correct |
617 ms |
67500 KB |
Output is correct |
50 |
Correct |
626 ms |
67400 KB |
Output is correct |
51 |
Correct |
619 ms |
67376 KB |
Output is correct |
52 |
Correct |
612 ms |
67408 KB |
Output is correct |
53 |
Correct |
620 ms |
67268 KB |
Output is correct |
54 |
Correct |
615 ms |
67316 KB |
Output is correct |
55 |
Correct |
619 ms |
67288 KB |
Output is correct |