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 w [ N ] ; // 楼房的高度数组
int f [ N ] ; // LIS结果数组, DP结果
int main ( ) {
int T ;
cin > > T ;
while ( T - - ) {
// 保持好习惯,多组测试数据,记得每次清空结果数组
memset ( f , 0 , sizeof f ) ;
int res = 0 ;
cin > > n ;
for ( int i = 1 ; i < = n ; i + + ) cin > > w [ i ] ;
// 从左到右,求一遍最长上升子序列[朴素O(N^2)版本]
for ( int i = 1 ; i < = n ; i + + ) {
f [ i ] = 1 ;
for ( int j = 1 ; j < i ; j + + )
if ( w [ i ] > w [ j ] ) f [ i ] = max ( f [ i ] , f [ j ] + 1 ) ;
res = max ( res , f [ i ] ) ;
}
// 反向求解 LIS问题
for ( int i = n ; i > = 1 ; i - - ) {
f [ i ] = 1 ;
for ( int j = n ; j > i ; j - - )
if ( w [ i ] > w [ j ] ) f [ i ] = max ( f [ i ] , f [ j ] + 1 ) ;
res = max ( res , f [ i ] ) ;
}
printf ( " %d \n " , res ) ;
}
return 0 ;
}