HRBUST 2010 [simple dp + longest decreasing subsequence]

Question: The so-called second-class formation is arranged in order from large to small, that is, for the sequence a, the second-class formation is any a[i] satisfying: a[i]>a[i+1]. Now given a sequence of length n, what is the minimum number of numbers removed from it to make the sequence into a second-order sequence.

There is no difference between the method and the incremental

#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#define MAX 1000+10
#define INF 0x3f3f3f3f
int n;
int dp[MAX];

using namespace std;

int main(void) {
    int a[MAX];
    while (~scanf("%d", &n)) {
        for (int i = 1; i <= n; i++)
            dp[i] = 1;
        for (int i = 1; i <= n; i++)
            scanf("%d", &a[i]);

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

                }

            }

        }
        int maxx = -INF;
        for (int i = 1; i <= n; i++) {
            maxx = max(dp[i], maxx);

        }
        printf("%d\n", n - maxx);

    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325160782&siteId=291194637