题目来源
题目难度
3星
程序
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 55;
int n;
int up[N]; //上升的导弹系统的最后一个导弹高度(数组单调递减)
int down[N]; //下降的导弹系统的最后一个导弹高度(数组单调递增)
int num[N];
int ans;
//当前正在考虑num[u]
void dfs(int u, int upcnt, int downcnt)
{
if(upcnt + downcnt >= ans) //剪枝
{
return;
}
if(u > n)
{
ans = min(ans, upcnt+downcnt);
return;
}
//将当前导弹u放入上升的导弹系统up
int k = 0;
while(k < upcnt && up[k] >= num[u])
{
k ++;
}
int t = up[k];
up[k] = num[u];
if(k == upcnt) //开一个新数列
{
dfs(u+1, upcnt+1, downcnt);
}
else
{
dfs(u+1, upcnt, downcnt);
}
up[k] = t;
//将当前导弹u放入下降的导弹系统down
k = 0;
while(k < downcnt && down[k] <= num[u])
{
k ++;
}
t = down[k];
down[k] = num[u];
if(k == downcnt)
{
dfs(u+1, upcnt, downcnt+1);
}
else
{
dfs(u+1, upcnt, downcnt);
}
down[k] = t;
}
int main()
{
while(cin >> n, n != 0)
{
for(int i = 1; i <= n; i ++)
{
cin >> num[i];
}
ans= n;
dfs(1, 0, 0);
cout << ans << endl;
}
return 0;
}
标签:int,downcnt,up,导弹,num,187,ans,upcnt,防御
From: https://www.cnblogs.com/chaosliang/p/17188955.html