Files
python/TangDou/LanQiaoBei/ZhongGaoJi/LanQiao14STEMA202301/BC5.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

61 lines
1.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 110;
double a[N];
int n;
int f1[N], f2[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
// 以每棵树为保留的,并且是最高的那棵树,分别求出最长上升序列长度x 和 最长下降序列长度y, x+y-1就是最终保留的整体序列长度,
// 去掉的数量就是 n-(x+y-1)的值。然后求min()
// 需要注意的是默认值设置为-1,比如 5 4 3 2 1 ,我们取哪棵为最高点都行不通,不存在左侧上侧到峰值的情况,右侧即使符合也不行的。
// 求最长上升
for (int i = 1; i <= n; i++) {
f1[i] = 1;
for (int j = 1; j < i; j++)
if (a[i] > a[j]) f1[i] = max(f1[i], f1[j] + 1);
}
// 求最长下降
for (int i = n; i >= 1; i--) {
f2[i] = 1;
for (int j = n; j > i; j--)
if (a[i] > a[j]) f2[i] = max(f2[i], f2[j] + 1);
}
// f1[i],f2[i]全都是1,那么输出-1
bool flag = true;
for (int i = 1; i <= n; i++)
if (f1[i] != 1) {
flag = false;
break;
}
if (flag) {
cout << -1 << endl;
exit(0);
}
flag = true;
for (int i = 1; i <= n; i++)
if (f2[i] != 1) {
flag = false;
break;
}
if (flag) {
cout << -1 << endl;
exit(0);
}
int mx = -1;
for (int i = 1; i <= n; i++) {
int t = f1[i] + f2[i];
mx = max(mx, t);
}
cout << n - mx + 1 << endl;
return 0;
}