题意
思路
对于1~n个导弹爆搜将每个导弹放进上升 || 下降
up[] down[] 存每个上升||下降序列的最后一位
复杂度 O(n2^n)
代码
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MaxN = 55;
int n,a[MaxN],ans,up[MaxN],down[MaxN];
//up[]:每个上升子序列的最后一位4321
void dfs(int u,int su,int sd){//枚举到第几个,sum_up,sum_down
if(su + sd >= ans) return ;
if(u == n){
ans = su + sd;
return ;
}
//cur set into up
int k = 0;
while(k < su && up[k] >= a[u]) k++;
int t = up[k];
up[k] = a[u];
if(k < su) dfs(u + 1,su,sd);
else dfs(u + 1,su + 1,sd);
up[k] = t;
//cur set into down
k = 0;
while(k < sd && down[k] <= a[u]) k++;
t = down[k];
down[k] = a[u];
if(k < sd) dfs(u + 1,su,sd);
else dfs(u + 1,su,sd + 1);
down[k] = t;
}
int main()
{
while(~scanf("%d",&n) && n){
for(int i = 0;i < n; i++) scanf("%d",&a[i]);
ans = n;
dfs(0,0,0);
printf("%d\n",ans);
}
}