#include <bits/stdc++.h>
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Forr(i,a,b) for(int i=a;i>=b;i--)
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define eb emplace_back
#define INF (1000000000000000ll);
#define int ll
using namespace std;
using ll=long long;
using pii=pair<int,int>;
#define LO(i) (i&(-i))
struct BIT{
int n;
int a[5010];
void init(int _n){
n=_n;
memset(a,0,sizeof(a));
}
void add(int i,int x){
while(i<=n){
a[i]+=x;
i+=LO(i);
}
}
int ask(int i){
int ans=0;
while(i){
ans+=a[i];
i-=LO(i);
}
return ans;
}
}bit;
int a[5010];
int pos[5010];
int no[5010];
int cost[5010][5010];
int dp[5010];
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(0);
// nachoneko sama & miku sama bless me >/////<
int n; cin>>n;
For(i,1,n){
cin>>a[i];
pos[a[i]]=i;
}
For(l,1,n){
bit.init(n);
int now=0;
// int mi=l;
int cnt=0;
For(i,1,n){
if(a[i]>=l){
cnt++;
no[a[i]]=cnt;
}
}
For(r,l,n){
now+=bit.ask(pos[r]);
bit.add(pos[r],1);
now+=no[r]-(r-l+1);
cost[l][r]=now;
// cout<<l<<" "<<r<<" "<<now<<"\n";
}
}
For(i,1,n){
dp[i]=cost[1][i];
For(j,1,i-1) dp[i]=min(dp[i],dp[j]+cost[j+1][i]);
}
cout<<dp[n]<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
448 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
448 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 |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 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 |
452 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
448 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 |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 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 |
452 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
1228 KB |
Output is correct |
22 |
Correct |
1 ms |
1228 KB |
Output is correct |
23 |
Correct |
2 ms |
1228 KB |
Output is correct |
24 |
Correct |
2 ms |
1356 KB |
Output is correct |
25 |
Correct |
1 ms |
1228 KB |
Output is correct |
26 |
Correct |
1 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1356 KB |
Output is correct |
28 |
Correct |
2 ms |
1304 KB |
Output is correct |
29 |
Correct |
1 ms |
1280 KB |
Output is correct |
30 |
Correct |
1 ms |
1356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
448 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 |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 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 |
452 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
1228 KB |
Output is correct |
22 |
Correct |
1 ms |
1228 KB |
Output is correct |
23 |
Correct |
2 ms |
1228 KB |
Output is correct |
24 |
Correct |
2 ms |
1356 KB |
Output is correct |
25 |
Correct |
1 ms |
1228 KB |
Output is correct |
26 |
Correct |
1 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1356 KB |
Output is correct |
28 |
Correct |
2 ms |
1304 KB |
Output is correct |
29 |
Correct |
1 ms |
1280 KB |
Output is correct |
30 |
Correct |
1 ms |
1356 KB |
Output is correct |
31 |
Correct |
10 ms |
6092 KB |
Output is correct |
32 |
Correct |
9 ms |
6092 KB |
Output is correct |
33 |
Correct |
11 ms |
6092 KB |
Output is correct |
34 |
Correct |
11 ms |
6044 KB |
Output is correct |
35 |
Correct |
10 ms |
6092 KB |
Output is correct |
36 |
Correct |
11 ms |
6156 KB |
Output is correct |
37 |
Correct |
11 ms |
6080 KB |
Output is correct |
38 |
Correct |
10 ms |
6092 KB |
Output is correct |
39 |
Correct |
12 ms |
6072 KB |
Output is correct |
40 |
Correct |
11 ms |
6092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
448 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 |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 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 |
452 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
1228 KB |
Output is correct |
22 |
Correct |
1 ms |
1228 KB |
Output is correct |
23 |
Correct |
2 ms |
1228 KB |
Output is correct |
24 |
Correct |
2 ms |
1356 KB |
Output is correct |
25 |
Correct |
1 ms |
1228 KB |
Output is correct |
26 |
Correct |
1 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1356 KB |
Output is correct |
28 |
Correct |
2 ms |
1304 KB |
Output is correct |
29 |
Correct |
1 ms |
1280 KB |
Output is correct |
30 |
Correct |
1 ms |
1356 KB |
Output is correct |
31 |
Correct |
10 ms |
6092 KB |
Output is correct |
32 |
Correct |
9 ms |
6092 KB |
Output is correct |
33 |
Correct |
11 ms |
6092 KB |
Output is correct |
34 |
Correct |
11 ms |
6044 KB |
Output is correct |
35 |
Correct |
10 ms |
6092 KB |
Output is correct |
36 |
Correct |
11 ms |
6156 KB |
Output is correct |
37 |
Correct |
11 ms |
6080 KB |
Output is correct |
38 |
Correct |
10 ms |
6092 KB |
Output is correct |
39 |
Correct |
12 ms |
6072 KB |
Output is correct |
40 |
Correct |
11 ms |
6092 KB |
Output is correct |
41 |
Correct |
526 ms |
117528 KB |
Output is correct |
42 |
Correct |
524 ms |
117416 KB |
Output is correct |
43 |
Correct |
498 ms |
117540 KB |
Output is correct |
44 |
Correct |
525 ms |
117560 KB |
Output is correct |
45 |
Correct |
517 ms |
117416 KB |
Output is correct |
46 |
Correct |
530 ms |
117652 KB |
Output is correct |
47 |
Correct |
519 ms |
117576 KB |
Output is correct |
48 |
Correct |
532 ms |
117556 KB |
Output is correct |
49 |
Correct |
518 ms |
117576 KB |
Output is correct |
50 |
Correct |
578 ms |
117568 KB |
Output is correct |
51 |
Correct |
511 ms |
117680 KB |
Output is correct |
52 |
Correct |
552 ms |
117528 KB |
Output is correct |
53 |
Correct |
536 ms |
117528 KB |
Output is correct |
54 |
Correct |
537 ms |
117468 KB |
Output is correct |
55 |
Correct |
508 ms |
117700 KB |
Output is correct |