Logu P4063 [JXOI2017] series (dp)

meaning of the title

Title Link

Sol

It's not hard to think about this problem, but it's very troublesome to write, and then I went to look at the shortest code representation of loj, which can only be Orz

First of all, it is not difficult to find a property: the selectable interval must be shrinking, and the new selectable interval must be divided from some position of the old interval.

For example, \ (a {I-1} = x \), when the maximum number less than \ (x \) is \ (l {I-1} \), and the minimum number greater than \ (x \) is \ (R {I-1} \), I choose a \ (a {I = t \), then when we consider \ (a {I + 1} \). Obviously, if \ (T < x \), the minimum number greater than \ (t \) is \ (x \), the maximum number less than \ (t \) is \ (l \), \ (T > x \) similarly.

Then you can set \ (f[i][l][r] \) to represent the number of schemes whose location is in \ ([l,r] \). The transfer needs to be reversed.

Direct memory call search

Complexity \ (O(nr^3) \)

#include<bits/stdc++.h>
#define Fin(x) freopen(#x".in", "r", stdin);
using namespace std;
const int MAXN = 50001, mod = 998244353;
template<typename A, typename B> inline bool chmax(A &x, B y) {return x < y ? x = y, 1 : 0;}
template<typename A, typename B> inline bool chmin(A &x, B y) {return x > y ? x = y, 1 : 0;}
template<typename A, typename B> inline A mul(A x, B y) {return 1ll * x * y % mod;}
template<typename A, typename B> inline void add2(A &x, B y) {x = x + y >= mod ? x + y - mod : x + y;}
template<typename A, typename B> inline int add(A x, B y) {return x + y >= mod ? x + y - mod : x + y;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, a[51], f[51][152][152];
int dfs(int x, int l, int r) {
    if(x > N) 
        return 1;
    int &res = f[x][l][r];
    if(~res) return res; res = 0;
    for(int i = max(1, l); i <= min(a[x], r); i++) {
        if(i == l || i == r) add2(res, dfs(x + 1, i, i));
        else add2(res, add(add(dfs(x + 1, l, i), dfs(x + 1, i, r)), -dfs(x + 1, i, i) + mod));
    }
    return res;
}
signed main() {
    memset(f, -1, sizeof(f));
    N = read();
    int mx = 0;
    for(int i = 1; i <= N; i++) a[i] = read(), chmax(mx, a[i]);
    cout << dfs(1, 0, mx + 1);
    return 0;
}

Tags: C++ less

Posted on Mon, 02 Dec 2019 09:34:35 -0500 by dancahill