首页 > 代码库 > poj 1836

poj 1836

poj 1836
#include <iostream>
#include <string.h>
using namespace std ;
const int MAX = 1005 ;
double m[MAX] ;
int dp1[MAX] , dp2[MAX] ;
int main()
{
int n ;
while ( cin >> n )
{
int i , j ;
for ( i = 1 ; i <= n ; i++ )
{ cin >> m[i] ; dp1[i]=dp2[i]=1; }

//从头开始计算最长的递增序列
for ( i = 2 ; i <= n ; i++ )
for ( j = i-1 ; j >= 1 ; j-- )
if ( m[j] < m[i] && dp1[i] < dp1[j]+1 )
dp1[i] = dp1[j]+1 ;
//从尾部计算最长的递增序列
for ( i = n-1 ; i >= 1 ; i-- )
for ( j = i+1 ; j <= n ; j++ )
if ( m[j] < m[i] && dp2[i] < dp2[j]+1 )
dp2[i] = dp2[j]+1 ;
int ans = dp1[n] ;//初始化ans
for ( i = 1 ; i < n ; i++ )
for ( j = i+1 ; j <= n ; j++ )
if ( dp1[i] + dp2[j] > ans )
ans = dp1[i] + dp2[j] ;
cout << n-ans << endl ;
}
return 0 ;
}