(dp+two-way) acwing 482. Chorus formation

482. Chorus Formation

Subject link https://www.acwing.com/problem/content/484/

N students stand in a row, and the music teacher asks (N−K) of them to stand out, so that the remaining K students form a chorus formation.

Chorus formation refers to a formation: Suppose K students are numbered 1, 2..., K from left to right, and their heights are T1, T2,..., TK, then their heights satisfy
T1< …Ti+1>…>TK(1≤i≤K).

Your task is to know the heights of all N classmates, and calculate at least a few classmates to get out of the queue, so that the remaining classmates can be formed into a chorus formation.

The first line of the input format is an integer N, which represents the total number of students.

There are N integers in the second line, separated by spaces, and the i-th integer Ti is the height (cm) of the i-th student.

Output format The output consists of one line, this line only contains an integer, that is, at least a few students are required to be listed.

Data range 2≤N≤100, 130≤Ti≤230 Input sample: 8 186 186 150 200 160 130 197 220 Output sample:
4

Idea: Slide down from a certain point to the left and right (lower than that point), find the maximum sum with this point as the vertex, and finally subtract the maximum sum from the total number of people

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    
    
    int n;
    cin>>n;
    int a[110],f[110],g[110];
    for(int i=1;i<=n;i++){
    
    
        cin>>a[i];
    }
    for(int i=1;i<=n;i++){
    
    
        f[i]=1;
        for(int j=1;j<i;j++){
    
    
            if(a[i]>a[j]){
    
    
                f[i]=max(f[i],f[j]+1);
                
            }
        }
    }
    for(int i=n;i>=1;i--){
    
    
        g[i]=1;
        for(int j=n;j>i;j--){
    
    
            if(a[i]>a[j]){
    
    
                g[i]=max(g[i],g[j]+1);
                
            }
        }
    }
    int res=0;
    for(int i=1;i<=n;i++){
    
    
        res=max(res,g[i]+f[i]-1);
    }
    cout<<n-res;
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46028214/article/details/115257409