原题
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 55;
int n;
int ans;
int a[N];
int up[N];
int down[N];
void dfs(int z, int u, int d){
if(u + d >= ans) return;
if(z == n){
ans = u + d;
return ;
}
int k = 0;
while(k < u && up[k] >= a[z]) k ++ ;
int t = up[k];
up[k] = a[z];
if(k < u) dfs(z + 1, u, d);
else dfs(z + 1, u + 1, d);
up[k] = t;
k = 0;
while(k < d && down[k] <= a[z]) k ++ ;
t = down[k];
down[k] = a[z];
if(k < d) dfs(z + 1, u, d);
else dfs(z + 1, u , d + 1);
down[k] = t;
}
int main()
{
while(cin>>n, n){
for(int i = 0; i < n; i ++ ) cin>>a[i];
ans = n;
dfs(0, 0, 0);
cout<<ans<<endl;
}
return 0;
}
|