#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<ii,ii>
#define fi first
#define se second
int n;
ll bit[5001];
ll get(int ind){
ll ans=0;
ind++;
while(ind>0){
ans+=bit[ind];
ind-=(ind&(-ind));
}
return ans;
}
void update(int ind,int val){
ind++;
while(ind<=n){
bit[ind]+=val;
ind+=(ind&(-ind));
}
}
void solve(){
cin >> n;
int a[n];
for(int i=0;i<n;i++){
cin >> a[i];
a[i]--;
}
int ainv[n];
for(int i=0;i<n;i++) ainv[a[i]]=i;
ll dp[n+1];
dp[0]=0;
for(ll i=1;i<=n;i++){
int b[i];
int ind=0;
for(int j=0;j<n;j++){
if(a[j]<i){
b[ind]=a[j];
ind++;
}
}
ll binv[i];
for(int j=0;j<i;j++) binv[b[j]]=j;
for(int j=0;j<=i;j++) bit[j]=0;
dp[i]=1000000000000000000;
ll tadd=0;
for(ll j=1;j<=i;j++){
tadd+=get(i-1-binv[i-j]);
update(i-1-binv[i-j],1);
tadd+=(i-j-binv[i-j]);
dp[i]=min(dp[i],tadd+dp[i-j]);
}
}
cout << dp[n];
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
//cin >> t;
t=1;
while(t--) solve();
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:33:9: warning: variable 'ainv' set but not used [-Wunused-but-set-variable]
33 | int ainv[n];
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
7 ms |
348 KB |
Output is correct |
32 |
Correct |
7 ms |
484 KB |
Output is correct |
33 |
Correct |
7 ms |
348 KB |
Output is correct |
34 |
Correct |
7 ms |
480 KB |
Output is correct |
35 |
Correct |
7 ms |
348 KB |
Output is correct |
36 |
Correct |
7 ms |
480 KB |
Output is correct |
37 |
Correct |
7 ms |
488 KB |
Output is correct |
38 |
Correct |
7 ms |
344 KB |
Output is correct |
39 |
Correct |
7 ms |
344 KB |
Output is correct |
40 |
Correct |
8 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
7 ms |
348 KB |
Output is correct |
32 |
Correct |
7 ms |
484 KB |
Output is correct |
33 |
Correct |
7 ms |
348 KB |
Output is correct |
34 |
Correct |
7 ms |
480 KB |
Output is correct |
35 |
Correct |
7 ms |
348 KB |
Output is correct |
36 |
Correct |
7 ms |
480 KB |
Output is correct |
37 |
Correct |
7 ms |
488 KB |
Output is correct |
38 |
Correct |
7 ms |
344 KB |
Output is correct |
39 |
Correct |
7 ms |
344 KB |
Output is correct |
40 |
Correct |
8 ms |
348 KB |
Output is correct |
41 |
Correct |
330 ms |
1100 KB |
Output is correct |
42 |
Correct |
331 ms |
848 KB |
Output is correct |
43 |
Correct |
327 ms |
852 KB |
Output is correct |
44 |
Correct |
339 ms |
596 KB |
Output is correct |
45 |
Correct |
326 ms |
824 KB |
Output is correct |
46 |
Correct |
323 ms |
592 KB |
Output is correct |
47 |
Correct |
341 ms |
844 KB |
Output is correct |
48 |
Correct |
331 ms |
576 KB |
Output is correct |
49 |
Correct |
329 ms |
864 KB |
Output is correct |
50 |
Correct |
324 ms |
864 KB |
Output is correct |
51 |
Correct |
322 ms |
596 KB |
Output is correct |
52 |
Correct |
325 ms |
464 KB |
Output is correct |
53 |
Correct |
329 ms |
572 KB |
Output is correct |
54 |
Correct |
330 ms |
628 KB |
Output is correct |
55 |
Correct |
323 ms |
852 KB |
Output is correct |