algorithm_2024/P1091/P1091.cpp

35 lines
843 B
C++
Raw Normal View History

2024-10-04 12:04:22 +00:00
#include <iostream>
#include <algorithm>
using std::cin,std::cout,std::max,std::iostream;
constexpr size_t MAX_N {100+5};
int n, arr[MAX_N], go_up[MAX_N], go_down[MAX_N], ans;
int main(){
iostream::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n;
for(int i=0;i<n;i++){
cin>>arr[i];
go_up[i]=1,go_down[i]=1;
}
for(int i=0;i<n;i++){//j < i
for(int j=i-1;j>=0;j--){
if(arr[j]<arr[i] && go_up[i]<go_up[j]+1){
go_up[i] = go_up[j]+1;
}
}
}
for(int i=n-1;i>=0;i--){//i < j
for(int j=i+1;j<n;j++){
if(arr[j]<arr[i] && go_down[i]<go_down[j]+1){
go_down[i] = go_down[j] + 1;
}
}
}
for(int i=0;i<n;i++){
ans = max(ans,go_down[i]+go_up[i]-1);
}
cout<<n-ans<<'\n';
}