一道简单dp题,自己感觉这类dp题都很简单,只要找到一种状态,从小往大了推就行了,可关键是木读懂题啊。。。。。。开始读的时候,没认真理解where each soldier can see by looking lengthwise the line at least one of the line's extremity (left or right)这句话,结果理解成从一个用n个元素的数组中抽出几个数,使数组非递减或非递增排列,呃~自然WA的很惨,然后,看了题解,发现我错了,它是让找一个先上升在下降的最长序列,于是开始做,呃~有WA,因为漏掉了一种情况,1 , 2 , 3 , 4 , 1 , 1 , 4 , 3 , 2 , 1 ,这种情况下是应该把中间那两个1去掉才是最长序列,所以又改,终于看见AC了,啊,激动啊~
代码:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> #include <queue> #define maxm 1003 #define exp 1e-6 using namespace std ; int dpl[maxm] , dpr[maxm] ; double h[maxm] ; int max ( int x , int y ) { return x > y ? x : y ; } int main() { int i , j , n ; while ( scanf ( "%d" , &n ) != EOF ) { for ( i = 1 ; i <= n ; i++ ) { scanf ( "%lf" , &h[i] ); dpl[i] = 1 ; dpr[i] = 1 ; } int maxx = -1 ; for ( i = 1 ; i <= n ; i++ ) { for ( j = i - 1; j > 0 ; j-- ) { if ( ( h[i] - h[j] ) >= exp ) dpl[i] = max ( dpl[i] , dpl[j] + 1 ); } } for ( i = n - 1 ; i > 0 ; i-- ) { for ( j = n ; j > i ; j-- ) if ( ( h[i] - h[j] ) >= exp ) dpr[i] = max ( dpr[i] , dpr[j] + 1 ); } for ( i = 1 ; i < n ; i++ ) for ( j = i + 1 ; j <= n ; j++ ) if ( dpl[i] + dpr[j] > maxx ) maxx = dpl[i] +dpr[j] ; for ( i = 1 ; i <= n ; i++ ) if ( dpl[i] + dpr[i] -1 > maxx ) maxx = dpl[i] + dpr[j] - 1; printf ( "%d\n" , n - maxx ); } return 0; }