90分二分优化求助
查看原帖
90分二分优化求助
689146
mushezi楼主2022/8/10 19:53
#include <bits/stdc++.h>
using namespace std;

int n, h[105];
int minout = INT_MAX;
int up[105], down[105];

void getls(int pos){ // ... pos-1, pos, pos+1 ... maxh = h[pos]
    if(pos == 2 && h[pos-1] >= h[pos]) return ;
    if(pos == n-2 && h[pos+1] >= h[pos]) return ;
    memset(up, 0, sizeof(up));
    memset(down, 0, sizeof(down));
    int u = 1, d = 1;
    up[1] = h[1];
    down[1] = h[pos];
    for(int i = 2; i <= pos; i++){
        if(h[i] >= h[pos]) continue;
        if(h[i] > up[u]) up[++u] = h[i];
        else{
            int p = lower_bound(up+1, up+u+1, h[i]) - up;
            up[p] = h[i];
        }
    }
    for(int i = pos+1; i <= n; i++){
        if(h[i] >= h[pos]) continue;
        if(h[i] < down[d]) down[++d] = h[i];
        else{
            int p = lower_bound(down+1, down+d+1, h[i], greater<int>()) - down;
            down[p] = h[i];
        }
    }
    minout = min(minout, n - (u+d));
}


int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> h[i];
    
    for(int i = 2; i <= n-1; i++) getls(i);

    cout << minout << endl;
    
    return 0;
}

2022/8/10 19:53
加载中...