AcWing 482 chorus formation

Title description:

N students stand in a row, and the music teacher will invite (NK) students out of them to make the remaining K students line up in 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 height satisfies T1< …<Ti>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 be out of the queue, so that the remaining classmates can be formed into a chorus formation.

Input format

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

There are n integers in the second line, separated by spaces, 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 get out of the list.

data range

2≤N≤100,
130≤Ti≤2^30

Input sample:

8
186 186 150 200 160 130 197 220

Sample output:

4
#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
const int MAX = 109;

int a[MAX], dpl[MAX], dpr[MAX];
int n;

int main()
{
    scanf("%d", &n);

    for(int i = 0; i < n; i++)
        scanf("%d", &a[i]);

    dpl[0] = 1;
    for(int i = 0; i < n; i++)
    {
        int maxx = 0;
        for(int j = 0; j < i; j++)
        {
            if(a[j] < a[i])
                maxx = max(maxx, dpl[j]);
        }
        dpl[i] = maxx + 1;
    }

    for(int i = n - 1; i >= 0; i--)
    {
        int maxx = 0;
        for(int j = n; j > i; j--)
        {
            if(a[j] < a[i])
                maxx = max(maxx, dpr[j]);
        }
        dpr[i] = maxx + 1;
    }

    int ans = 0;
    for(int i = 0; i < n; i++)
        ans = max(ans, dpl[i] + dpr[i] - 1);

    printf("%d\n", n - ans);

    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44620183/article/details/113691228