#include<bits/stdc++.h>
using namespace std;
#define INIT ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll
inline char get_ (){
const int oo=1000005;
static char buf[oo], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, oo, stdin), p1 == p2) ? EOF : *p1 ++;
}
int read_ () {
char c = get_();
int sum = 0;
while(!(c >= '0' && c <= '9')) c = get_();
while(c >= '0' && c <= '9') sum = sum * 10 + (c - '0'), c = get_();
return sum;
}
int n, H[5001];
int h[5001];
int fw[5001];
int ord[5001];
void update(int i, int x){
for(int j=i; j<=n; j+=(j&(-j))){
fw[j]+=x;
}
return;
}
int query(int i){
int res=0;
for(int j=i; j>0; j-=(j&(-j)) ){
res+=fw[j];
}
return res;
}
int get_cost(int i, int j){
for(int k=1; k<i; k++){
h[k]=k;
}
for(int k=1, cnt=i; k<=n; k++){
//ord[H[k] ]=k;
if(H[k]>=i){
h[cnt]=H[k];
ord[h[cnt] ]=cnt;
cnt++;
}
}
//ordonat corect;
int mn1=0;
for(int i=1; i<=n; i++){
fw[i]=0;
}
for(int k=i; k<=j; k++){
int add=ord[k]+query(ord[k])-k;
mn1+=add;
update(ord[k], 1);
}
//ordonat descrescator
int mn2=0;
for(int i=1; i<=n; i++){
fw[i]=0;
}
for(int k=i; k<=j; k++){
int add=ord[j-(k-i)]+query(ord[j-(k-i)])-(k);
mn2+=add;
update(ord[j-(k-i) ], 1);
}
return min(mn1, mn2);
}
int dp[5001], opt[5001];
void dc(int l, int r){
int m=(l+r)>>1ll;
if(l!=r){
if(m>l){dc(l, m-1);}
}
int mn=1e18;
int optm=0;
int optl=opt[m-1];
for(int j=optl; j<m; j++){
int ac=get_cost(j+1, m)+dp[j];
if( ac<mn ){
optm=j, mn=ac;
}
}
dp[m]=mn;
if(l!=r){
dc(m+1, r);
}
}
int32_t main(){
INIT
cin>>n;
for(int i=1; i<=n; i++){
cin>>H[i];
}
dc(1, n);
cout<<dp[n];
return 0;
}
Compilation message
Main.cpp: In function 'void dc(long long int, long long int)':
Main.cpp:108:5: warning: variable 'optm' set but not used [-Wunused-but-set-variable]
108 | int optm=0;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 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 |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
0 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 |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
56 ms |
332 KB |
Output is correct |
22 |
Correct |
58 ms |
340 KB |
Output is correct |
23 |
Correct |
58 ms |
332 KB |
Output is correct |
24 |
Correct |
61 ms |
332 KB |
Output is correct |
25 |
Correct |
59 ms |
332 KB |
Output is correct |
26 |
Correct |
59 ms |
332 KB |
Output is correct |
27 |
Correct |
61 ms |
332 KB |
Output is correct |
28 |
Correct |
59 ms |
332 KB |
Output is correct |
29 |
Correct |
61 ms |
332 KB |
Output is correct |
30 |
Correct |
62 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 |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
56 ms |
332 KB |
Output is correct |
22 |
Correct |
58 ms |
340 KB |
Output is correct |
23 |
Correct |
58 ms |
332 KB |
Output is correct |
24 |
Correct |
61 ms |
332 KB |
Output is correct |
25 |
Correct |
59 ms |
332 KB |
Output is correct |
26 |
Correct |
59 ms |
332 KB |
Output is correct |
27 |
Correct |
61 ms |
332 KB |
Output is correct |
28 |
Correct |
59 ms |
332 KB |
Output is correct |
29 |
Correct |
61 ms |
332 KB |
Output is correct |
30 |
Correct |
62 ms |
332 KB |
Output is correct |
31 |
Correct |
4094 ms |
480 KB |
Output is correct |
32 |
Correct |
4054 ms |
452 KB |
Output is correct |
33 |
Correct |
4067 ms |
480 KB |
Output is correct |
34 |
Correct |
4117 ms |
488 KB |
Output is correct |
35 |
Correct |
4075 ms |
372 KB |
Output is correct |
36 |
Correct |
4072 ms |
356 KB |
Output is correct |
37 |
Correct |
4062 ms |
456 KB |
Output is correct |
38 |
Correct |
4083 ms |
360 KB |
Output is correct |
39 |
Correct |
4070 ms |
360 KB |
Output is correct |
40 |
Correct |
4078 ms |
356 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 |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
56 ms |
332 KB |
Output is correct |
22 |
Correct |
58 ms |
340 KB |
Output is correct |
23 |
Correct |
58 ms |
332 KB |
Output is correct |
24 |
Correct |
61 ms |
332 KB |
Output is correct |
25 |
Correct |
59 ms |
332 KB |
Output is correct |
26 |
Correct |
59 ms |
332 KB |
Output is correct |
27 |
Correct |
61 ms |
332 KB |
Output is correct |
28 |
Correct |
59 ms |
332 KB |
Output is correct |
29 |
Correct |
61 ms |
332 KB |
Output is correct |
30 |
Correct |
62 ms |
332 KB |
Output is correct |
31 |
Correct |
4094 ms |
480 KB |
Output is correct |
32 |
Correct |
4054 ms |
452 KB |
Output is correct |
33 |
Correct |
4067 ms |
480 KB |
Output is correct |
34 |
Correct |
4117 ms |
488 KB |
Output is correct |
35 |
Correct |
4075 ms |
372 KB |
Output is correct |
36 |
Correct |
4072 ms |
356 KB |
Output is correct |
37 |
Correct |
4062 ms |
456 KB |
Output is correct |
38 |
Correct |
4083 ms |
360 KB |
Output is correct |
39 |
Correct |
4070 ms |
360 KB |
Output is correct |
40 |
Correct |
4078 ms |
356 KB |
Output is correct |
41 |
Execution timed out |
5042 ms |
484 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |