#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[ 4 ][ MAXN ] ;
int nw[ 4 ][ MAXN ] ;
vector < pii > hr[ 2 ][ MAXN ] ;
vector < pii > vr[ 2 ][ MAXN ] ;
void solve ( ) {
cin >> n ;
for ( int i = 1 ; i <= n ; ++ i ) {
cin >> a[ i ] ;
}
for ( int j = 0 ; j < 4 ; ++ j ) {
for ( int i = 1 ; i <= n ; ++ i ) {
dp[ j ][ i ] = 0 ;
}
}
for ( int len = 2 ; len <= n + 1 ; ++ len ) {
for ( int j = 0 ; j < 4 ; ++ j ) {
for ( int i = 1 ; i <= n ; ++ i ) {
if ( dp[ j ][ i ] > n ) { continue ; }
if ( ( j % 2 ) == 0 ) {
hr[ 0 ][ a[ i ] ].push_back ( { j , i } ) ;
hr[ 1 ][ a[ i ] + dp[ j ][ i ] ].push_back ( { j , i } ) ;
}
else {
hr[ 1 ][ a[ i ] ].push_back ( { j , i } ) ;
hr[ 0 ][ a[ i ] - dp[ j ][ i ] ].push_back ( { j , i } ) ;
}
if ( ( j / 2 ) == 0 ) {
vr[ 0 ][ i ].push_back ( { j , i } ) ;
vr[ 1 ][ i + dp[ j ][ i ] ].push_back ( { j , i } ) ;
}
else {
vr[ 1 ][ i ].push_back ( { j , i } ) ;
vr[ 0 ][ i - dp[ j ][ i ] ].push_back ( { j , i } ) ;
}
}
}
for ( int j = 0 ; j < 4 ; ++ j ) {
for ( int i = 1 ; i <= n ; ++ i ) {
if ( dp[ j ][ i ] > n ) {
nw[ j ][ i ] = n + 1 ;
continue ;
}
nw[ j ][ i ] = dp[ j ][ i ] ;
while ( nw[ j ][ i ] <= n ) {
int enx = i , eny = a[ i ] ;
int specx , specy ;
if ( ( j / 2 ) == 0 ) { enx += nw[ j ][ i ] ; specx = 1 ; }
else { enx -= nw[ j ][ i ] ; specx = 0 ; }
if ( ( j % 2 ) == 0 ) { eny += nw[ j ][ i ] ; specy = 1 ; }
else { eny -= nw[ j ][ i ] ; specy = 0 ; }
if ( enx < 1 || n < enx ) {
nw[ j ][ i ] = n + 1 ;
break ;
}
if ( eny < 1 || n < eny ) {
nw[ j ][ i ] = n + 1 ;
break ;
}
bool done = false ;
for ( auto [ ori , x ] : vr[ specx ][ enx ] ) {
if ( dp[ ori ][ x ] >= nw[ j ][ i ] ) { continue ; }
if ( ( ori % 2 ) == 0 ) {
if ( ( j % 2 ) == 0 ) {
if ( a[ i ] < a[ x ] && a[ x ] + dp[ ori ][ x ] <= eny ) {
done = true ;
break ;
}
}
else {
if ( eny <= a[ x ] && a[ x ] + dp[ ori ][ x ] < a[ i ] ) {
done = true ;
break ;
}
}
}
else {
if ( ( j % 2 ) == 0 ) {
if ( a[ i ] < a[ x ] - dp[ ori ][ x ] && a[ x ] <= eny ) {
done = true ;
break ;
}
}
else {
if ( eny <= a[ x ] - dp[ ori ][ x ] && a[ x ] < a[ i ] ) {
done = true ;
break ;
}
}
}
}
for ( auto [ ori , x ] : hr[ specy ][ eny ] ) {
if ( dp[ ori ][ x ] >= nw[ j ][ i ] ) { continue ; }
if ( ( ori / 2 ) == 0 ) {
if ( ( j / 2 ) == 0 ) {
if ( i < x && x + dp[ ori ][ x ] <= enx ) {
done = true ;
break ;
}
}
else {
if ( enx <= x && x + dp[ ori ][ x ] < i ) {
done = true ;
break ;
}
}
}
else {
if ( ( j / 2 ) == 0 ) {
if ( i < x - dp[ ori ][ x ] && x <= enx ) {
done = true ;
break ;
}
}
else {
if ( enx <= x - dp[ ori ][ x ] && x < i ) {
done = true ;
break ;
}
}
}
}
if ( done == true ) { break ; }
++ nw[ j ][ i ] ;
}
}
}
for ( int i = 1 ; i <= n ; ++ i ) {
hr[ 0 ][ i ].clear ( ) , hr[ 1 ][ i ].clear ( ) ;
vr[ 0 ][ i ].clear ( ) , vr[ 1 ][ i ].clear ( ) ;
}
bool done = true ;
for ( int j = 0 ; j < 4 ; ++ j ) {
for ( int i = 1 ; i <= n ; ++ i ) {
dp[ j ][ i ] = nw[ j ][ i ] ;
if ( dp[ j ][ i ] < n ) { done = false ; }
}
}
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 ;
}
Compilation message
giraffes.cpp: In function 'void solve()':
giraffes.cpp:86:32: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
86 | for ( auto [ ori , x ] : vr[ specx ][ enx ] ) {
| ^
giraffes.cpp:117:32: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
117 | for ( auto [ ori , x ] : hr[ specy ][ eny ] ) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1368 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1216 KB |
Output is correct |
19 |
Correct |
1 ms |
1112 KB |
Output is correct |
20 |
Correct |
1 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1368 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1216 KB |
Output is correct |
19 |
Correct |
1 ms |
1112 KB |
Output is correct |
20 |
Correct |
1 ms |
1112 KB |
Output is correct |
21 |
Correct |
1 ms |
1112 KB |
Output is correct |
22 |
Correct |
2 ms |
1112 KB |
Output is correct |
23 |
Correct |
4 ms |
1368 KB |
Output is correct |
24 |
Correct |
6 ms |
1360 KB |
Output is correct |
25 |
Correct |
8 ms |
1116 KB |
Output is correct |
26 |
Correct |
9 ms |
1116 KB |
Output is correct |
27 |
Correct |
9 ms |
1112 KB |
Output is correct |
28 |
Correct |
9 ms |
1236 KB |
Output is correct |
29 |
Correct |
9 ms |
1128 KB |
Output is correct |
30 |
Correct |
11 ms |
1384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1368 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1216 KB |
Output is correct |
19 |
Correct |
1 ms |
1112 KB |
Output is correct |
20 |
Correct |
1 ms |
1112 KB |
Output is correct |
21 |
Correct |
1 ms |
1112 KB |
Output is correct |
22 |
Correct |
2 ms |
1112 KB |
Output is correct |
23 |
Correct |
4 ms |
1368 KB |
Output is correct |
24 |
Correct |
6 ms |
1360 KB |
Output is correct |
25 |
Correct |
8 ms |
1116 KB |
Output is correct |
26 |
Correct |
9 ms |
1116 KB |
Output is correct |
27 |
Correct |
9 ms |
1112 KB |
Output is correct |
28 |
Correct |
9 ms |
1236 KB |
Output is correct |
29 |
Correct |
9 ms |
1128 KB |
Output is correct |
30 |
Correct |
11 ms |
1384 KB |
Output is correct |
31 |
Correct |
464 ms |
1980 KB |
Output is correct |
32 |
Correct |
2986 ms |
4188 KB |
Output is correct |
33 |
Correct |
5639 ms |
4900 KB |
Output is correct |
34 |
Correct |
5731 ms |
4980 KB |
Output is correct |
35 |
Correct |
5816 ms |
4776 KB |
Output is correct |
36 |
Correct |
5733 ms |
4936 KB |
Output is correct |
37 |
Correct |
5625 ms |
4780 KB |
Output is correct |
38 |
Correct |
5566 ms |
5032 KB |
Output is correct |
39 |
Correct |
5636 ms |
5020 KB |
Output is correct |
40 |
Correct |
5670 ms |
5272 KB |
Output is correct |