LuoguP1799 increase the number of columns _NOI Guide 2010 (dynamic programming)

$ f[j]=max(f[i−1][j],f[i−1][j−1]+(x == j) $

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")

#else

#define D_e_Line ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;

int f[1007];

int main(){
    int n;
    io >> n;
    R(i,1,n){
        int x;
        io >> x;
        nR(j,i,1)
            f[j] = Max(f[j], f[j-1] + (x == j));
    }
    int ans = 0;
    R(i,1,n){
        ans = Max(ans, f[i]);
    }
    printf("%d", ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/bingoyes/p/11206111.html