![在这里插入图片描述](https://img-blog.csdnimg.cn/5dc34bf298e84c458ddb2756ebfae183.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5YGa5LiA5Y-q5aSn54aK54yr,size_20,color_FFFFFF,t_70,g_se,x_16)
题意
- 直接 dfs,dp 不好 dp
代码
#include <bits/stdc++.h>
using namespace std;
#define db double
#define ll long long
#define Pir pair<int, int>
#define fi first
#define se second
#define pb push_back
#define m_p make_pair
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
const int N = 55;
int n, a[N], up[N], dn[N];
int ans = inf;
void dfs(int p, int su, int sd)
{
if (su + sd >= ans) return;
if (p == n)
{
ans = min(ans, su + sd);
return;
}
int i;
for (i = 1; i <= su; i ++) {
if (a[p] > up[i]) break;
}
int tmp = up[i];
up[i] = a[p];
dfs(p + 1, max(su, i), sd);
up[i] = tmp;
for (i = 1; i <= sd; i ++) {
if (a[p] < dn[i]) break;
}
tmp = dn[i];
dn[i] = a[p];
dfs(p + 1, su, max(sd, i));
dn[i] = tmp;
}
int main()
{
while (scanf("%d", &n) && n)
{
ans = inf;
for (int i = 0; i < n; i ++) scanf("%d", &a[i]);
dfs(0, 0, 0);
printf("%d\n", ans);
}
return 0;
}