#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <queue>
//#define int long long
#define sqrt sqrtl
using namespace std;
const long long oo = 1e9;
long long sum, ans = 0, mx = 0, mn = 1000000000, num, pos;
/*
ViHHiPuh
(( `'-""``""-'` ))
)-__-_.._-__-(
/ --- (o _ o) --- \
\ .-* ( .0. ) *-. /
_'-. ,_ '=' _, .-'_
/ `;#'#'# - #'#'#;` \
\_)) -----'#'----- ((_/
# --------- #
'# ------- ------ #'
/..-'# ------- #'-.\
_\...-\'# -- #'/-.../_
((____)- '#' -(____))
cout << fixed << setprecision(6) << x;
freopen ( "sum.in", "r", stdin )
*/
int n, a[ 5001 ], t[ 20001 ], tt[ 20001 ], b[ 5001 ];
int dp[ 5002 ], d[ 5002 ][ 5002 ];
void build( int v, int tl, int tr ) {
if ( tl == tr ) {
t[ v ] = 1;
return;
}
int mid = ( tl + tr ) / 2;
build( v + v, tl, mid );
build( v + v + 1, mid + 1, tr );
t[ v ] = t[ v + v ] + t[ v + v + 1 ];
}
void build1( int v, int tl, int tr ) {
tt[ v ] = 0;
if ( tl == tr ) {
return;
}
int mid = ( tl + tr ) / 2;
build1( v + v, tl, mid );
build1( v + v + 1, mid + 1, tr );
}
void upd( int v, int tl, int tr, int pos ) {
if ( tl > pos || tr < pos )
return;
if ( tl == tr ) {
t[ v ] = 0;
tt[ v ] = 1;
return;
}
int mid = ( tl + tr ) / 2;
upd( v + v, tl, mid, pos );
upd( v + v + 1, mid + 1, tr, pos );
t[ v ] = t[ v + v ] + t[ v + v + 1 ];
tt[ v ] = tt[ v + v ] + tt[ v + v + 1 ];
}
void gett( int v, int tl, int tr, int l, int r ) {
if ( tl > r || tr < l )
return;
if ( tl >= l && tr <= r ) {
num += t[ v ];
sum += tt[ v ];
return;
}
int mid = ( tl + tr ) / 2;
gett( v + v, tl, mid, l, r );
gett( v + v + 1, mid + 1, tr, l, r );
}
main () {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for ( int i = 1; i <= n; i ++ ) {
cin >> a[ i ];
b[ a[ i ] ] = i;
dp[ i ] = oo;
}
build( 1, 1, n );
for ( int i = 1; i <= n; i ++ ) {
build1( 1, 1, n );
for ( int j = i; j >= 1; j -- ) {
d[ j ][ i ] = d[ j + 1 ][ i ];
upd( 1, 1, n, b[ j ] );
num = 0;
gett( 1, 1, n, 1, b[ j ] );
d[ j ][ i ] += num;
sum = 0;
if ( b[ j ] < n )
gett( 1, 1, n, b[ j ] + 1, n );
d[ j ][ i ] += sum;
// cout << j << " " << i << " - " << d[ j ][ i ] << "\n";
}
}
for ( int i = 1; i <= n; i ++ ) {
for ( int j = i - 1; j >= 0; j -- ) {
dp[ i ] = min( dp[ i ], dp[ j ] + d[ j + 1 ][ i ] );
}
}
cout << dp[ n ];
}
/*
h[ i ] + i * 2 < h[ i + 1 ] + i * 2 + 4
h[ i ] - h[ i + 1 ] < 4
3 5 2 4 1
5 9 8 14 13
*/
Compilation message
Main.cpp:98:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
98 | main () {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
4 ms |
1236 KB |
Output is correct |
22 |
Correct |
4 ms |
1236 KB |
Output is correct |
23 |
Correct |
4 ms |
1236 KB |
Output is correct |
24 |
Correct |
5 ms |
1152 KB |
Output is correct |
25 |
Correct |
4 ms |
1248 KB |
Output is correct |
26 |
Correct |
4 ms |
1224 KB |
Output is correct |
27 |
Correct |
4 ms |
1232 KB |
Output is correct |
28 |
Correct |
4 ms |
1236 KB |
Output is correct |
29 |
Correct |
4 ms |
1236 KB |
Output is correct |
30 |
Correct |
4 ms |
1212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
4 ms |
1236 KB |
Output is correct |
22 |
Correct |
4 ms |
1236 KB |
Output is correct |
23 |
Correct |
4 ms |
1236 KB |
Output is correct |
24 |
Correct |
5 ms |
1152 KB |
Output is correct |
25 |
Correct |
4 ms |
1248 KB |
Output is correct |
26 |
Correct |
4 ms |
1224 KB |
Output is correct |
27 |
Correct |
4 ms |
1232 KB |
Output is correct |
28 |
Correct |
4 ms |
1236 KB |
Output is correct |
29 |
Correct |
4 ms |
1236 KB |
Output is correct |
30 |
Correct |
4 ms |
1212 KB |
Output is correct |
31 |
Correct |
78 ms |
4824 KB |
Output is correct |
32 |
Correct |
79 ms |
4728 KB |
Output is correct |
33 |
Correct |
76 ms |
4812 KB |
Output is correct |
34 |
Correct |
88 ms |
4824 KB |
Output is correct |
35 |
Correct |
75 ms |
4724 KB |
Output is correct |
36 |
Correct |
76 ms |
4724 KB |
Output is correct |
37 |
Correct |
76 ms |
4812 KB |
Output is correct |
38 |
Correct |
75 ms |
4756 KB |
Output is correct |
39 |
Correct |
73 ms |
4756 KB |
Output is correct |
40 |
Correct |
76 ms |
4832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
4 ms |
1236 KB |
Output is correct |
22 |
Correct |
4 ms |
1236 KB |
Output is correct |
23 |
Correct |
4 ms |
1236 KB |
Output is correct |
24 |
Correct |
5 ms |
1152 KB |
Output is correct |
25 |
Correct |
4 ms |
1248 KB |
Output is correct |
26 |
Correct |
4 ms |
1224 KB |
Output is correct |
27 |
Correct |
4 ms |
1232 KB |
Output is correct |
28 |
Correct |
4 ms |
1236 KB |
Output is correct |
29 |
Correct |
4 ms |
1236 KB |
Output is correct |
30 |
Correct |
4 ms |
1212 KB |
Output is correct |
31 |
Correct |
78 ms |
4824 KB |
Output is correct |
32 |
Correct |
79 ms |
4728 KB |
Output is correct |
33 |
Correct |
76 ms |
4812 KB |
Output is correct |
34 |
Correct |
88 ms |
4824 KB |
Output is correct |
35 |
Correct |
75 ms |
4724 KB |
Output is correct |
36 |
Correct |
76 ms |
4724 KB |
Output is correct |
37 |
Correct |
76 ms |
4812 KB |
Output is correct |
38 |
Correct |
75 ms |
4756 KB |
Output is correct |
39 |
Correct |
73 ms |
4756 KB |
Output is correct |
40 |
Correct |
76 ms |
4832 KB |
Output is correct |
41 |
Correct |
4158 ms |
67468 KB |
Output is correct |
42 |
Correct |
4086 ms |
67660 KB |
Output is correct |
43 |
Correct |
4134 ms |
67592 KB |
Output is correct |
44 |
Correct |
4161 ms |
67460 KB |
Output is correct |
45 |
Correct |
4083 ms |
67480 KB |
Output is correct |
46 |
Correct |
4137 ms |
67560 KB |
Output is correct |
47 |
Correct |
4119 ms |
67580 KB |
Output is correct |
48 |
Correct |
4085 ms |
67784 KB |
Output is correct |
49 |
Correct |
4136 ms |
67400 KB |
Output is correct |
50 |
Correct |
4379 ms |
67492 KB |
Output is correct |
51 |
Correct |
4607 ms |
67500 KB |
Output is correct |
52 |
Correct |
4796 ms |
67484 KB |
Output is correct |
53 |
Correct |
4600 ms |
67420 KB |
Output is correct |
54 |
Execution timed out |
5021 ms |
67536 KB |
Time limit exceeded |
55 |
Halted |
0 ms |
0 KB |
- |