You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <bits/stdc++.h>
using namespace std ;
const int N = 110 ;
int n ; // 同学的个数
int a [ N ] ; // 山的高度数组
int f [ N ] ; // 从左向右的最长子序列
int g [ N ] ; // 从右向左的最长子序列
int res ;
int main ( ) {
cin > > n ;
for ( int i = 1 ; i < = n ; i + + ) cin > > a [ i ] ;
// 正向求解 LIS问题
for ( int i = 1 ; i < = n ; i + + ) {
f [ i ] = 1 ;
for ( int j = 1 ; j < i ; j + + )
if ( a [ i ] > a [ j ] ) f [ i ] = max ( f [ i ] , f [ j ] + 1 ) ;
}
// 反向求解 LIS问题
for ( int i = n ; i > = 1 ; i - - ) {
g [ i ] = 1 ;
for ( int j = n ; j > i ; j - - )
if ( a [ i ] > a [ j ] ) g [ i ] = max ( g [ i ] , g [ j ] + 1 ) ;
}
// 因为最终的那个中间点, 左边计算了一次, 右边双计算了一次, 需要减1去重复
for ( int i = 1 ; i < = n ; i + + ) res = max ( res , f [ i ] + g [ i ] - 1 ) ;
// 问至少要出队几人, 和问“mx=满足要求的队列, 最长是多长”是直接相关的, 如果算得mx,则n-mx就是答案
printf ( " %d \n " , n - res ) ;
return 0 ;
}