AcWing 482. 合唱队形(最长上升子序列)
原题链接
中等
作者:
Value
,
2021-03-03 16:25:47
,
所有人可见
,
阅读 245
#include <iostream>
using namespace std;
const int N = 110;
int h[N];
int up[N], down[N];
int main(){
int n; cin >> n;
for(int i = 0; i < n; i ++ ) cin >> h[i];
up[0] = 1;
for(int i = 1; i < n; i ++ ){
int maxv = 0;
for(int j = 0; j < i; j ++ ){
if(h[j] >= h[i]) continue;
else maxv = max(maxv, up[j]);
}
up[i] = maxv + 1;
}
down[n - 1] = 1;
for(int i = n - 2; i >= 0; i --){
int maxv = 0;
for(int j = n - 1; j > i; j -- ){
if(h[j] >= h[i]) continue;
else maxv = max(maxv, down[j]);
}
down[i] = maxv + 1;
}
int res = 0;
for(int i = 0; i < n; i ++ ) res = max(res, up[i] + down[i] - 1);
cout << n - res << endl;
}