You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}