#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair < int , int > pii ;
typedef vector < int > vi ;
#define fi first
#define se second
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
const int MAXN = 8007 ;
int n ;
int a[ MAXN ] ;
int dp[ MAXN ][ 4 ] ;
int nw[ MAXN ][ 4 ] ;
void solve ( ) {
cin >> n ;
for ( int i = 1 ; i <= n ; ++ i ) {
cin >> a[ i ] ;
}
for ( int i = 1 ; i <= n ; ++ i ) {
for ( int j = 0 ; j < 4 ; ++ j ) {
dp[ i ][ j ] = 0 ;
nw[ i ][ j ] = MAXN ;
}
}
for ( int len = 2 ; len <= n + 1 ; ++ len ) {
bool done = true ;
for ( int i = 1 ; i <= n ; ++ i ) {
for ( int t = i - 1 ; t >= 1 ; -- t ) {
int id ;
if ( a[ t ] < a[ i ] ) { id = 3 ; }
else { id = 2 ; }
int dx = i - t , dy = abs ( a[ i ] - a[ t ] ) ;
if ( dp[ t ][ id ^ 3 ] < min ( dx , dy ) ) {
nw[ i ][ id ] = min ( nw[ i ][ id ] , max ( dx , dy ) ) ;
}
if ( dp[ t ][ id ^ 2 ] < dx ) {
nw[ i ][ id ] = min ( nw[ i ][ id ] , max ( dx , dy + dp[ t ][ id ^ 2 ] ) ) ;
}
if ( dp[ t ][ id ^ 1 ] < dy ) {
nw[ i ][ id ] = min ( nw[ i ][ id ] , max ( dy , dx + dp[ t ][ id ^ 1 ] ) ) ;
}
nw[ i ][ id ] = min ( nw[ i ][ id ] , max ( dx , dy ) + dp[ t ][ id ] ) ;
if ( dx > nw[ i ][ 2 ] && dx > nw[ i ][ 3 ] ) { break ; }
}
for ( int t = i + 1 ; t <= n ; ++ t ) {
int id ;
if ( a[ t ] < a[ i ] ) { id = 1 ; }
else { id = 0 ; }
int dx = t - i , dy = abs ( a[ i ] - a[ t ] ) ;
if ( dp[ t ][ id ^ 3 ] < min ( dx , dy ) ) {
nw[ i ][ id ] = min ( nw[ i ][ id ] , max ( dx , dy ) ) ;
}
if ( dp[ t ][ id ^ 2 ] < dx ) {
nw[ i ][ id ] = min ( nw[ i ][ id ] , max ( dx , dy + dp[ t ][ id ^ 2 ] ) ) ;
}
if ( dp[ t ][ id ^ 1 ] < dy ) {
nw[ i ][ id ] = min ( nw[ i ][ id ] , max ( dy , dx + dp[ t ][ id ^ 1 ] ) ) ;
}
nw[ i ][ id ] = min ( nw[ i ][ id ] , max ( dx , dy ) + dp[ t ][ id ] ) ;
if ( dx > nw[ i ][ 0 ] && dx > nw[ i ][ 1 ] ) { break ; }
}
if ( nw[ i ][ 0 ] > n - i || nw[ i ][ 0 ] > n - a[ i ] ) { nw[ i ][ 0 ] = MAXN ; }
if ( nw[ i ][ 1 ] > n - i || nw[ i ][ 1 ] > a[ i ] - 1 ) { nw[ i ][ 1 ] = MAXN ; }
if ( nw[ i ][ 2 ] > i - 1 || nw[ i ][ 2 ] > n - a[ i ] ) { nw[ i ][ 2 ] = MAXN ; }
if ( nw[ i ][ 3 ] > i - 1 || nw[ i ][ 3 ] > a[ i ] - 1 ) { nw[ i ][ 3 ] = MAXN ; }
}
for ( int i = 1 ; i <= n ; ++ i ) {
for ( int j = 0 ; j < 4 ; ++ j ) {
dp[ i ][ j ] = nw[ i ][ j ] ;
if ( dp[ i ][ j ] != MAXN ) { done = false ; }
nw[ i ][ j ] = MAXN ;
}
}
if ( done == true ) {
cout << n - ( len - 1 ) << "\n" ;
return ;
}
}
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ; // cin >> t ;
while ( t -- ) { solve ( ) ; }
return 0 ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
472 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 |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
472 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 |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
464 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
472 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 |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
464 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
356 KB |
Output is correct |
21 |
Correct |
1 ms |
356 KB |
Output is correct |
22 |
Correct |
2 ms |
356 KB |
Output is correct |
23 |
Correct |
8 ms |
492 KB |
Output is correct |
24 |
Correct |
15 ms |
356 KB |
Output is correct |
25 |
Correct |
22 ms |
468 KB |
Output is correct |
26 |
Correct |
21 ms |
472 KB |
Output is correct |
27 |
Correct |
23 ms |
348 KB |
Output is correct |
28 |
Correct |
23 ms |
348 KB |
Output is correct |
29 |
Correct |
24 ms |
612 KB |
Output is correct |
30 |
Correct |
25 ms |
356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
472 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 |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
464 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
356 KB |
Output is correct |
21 |
Correct |
1 ms |
356 KB |
Output is correct |
22 |
Correct |
2 ms |
356 KB |
Output is correct |
23 |
Correct |
8 ms |
492 KB |
Output is correct |
24 |
Correct |
15 ms |
356 KB |
Output is correct |
25 |
Correct |
22 ms |
468 KB |
Output is correct |
26 |
Correct |
21 ms |
472 KB |
Output is correct |
27 |
Correct |
23 ms |
348 KB |
Output is correct |
28 |
Correct |
23 ms |
348 KB |
Output is correct |
29 |
Correct |
24 ms |
612 KB |
Output is correct |
30 |
Correct |
25 ms |
356 KB |
Output is correct |
31 |
Correct |
3915 ms |
604 KB |
Output is correct |
32 |
Execution timed out |
7033 ms |
604 KB |
Time limit exceeded |
33 |
Halted |
0 ms |
0 KB |
- |