dp套dp 未解决好题 2019 百度之星复赛 hdu Maximum or Sum

 别人的代码

#include 

using namespace std;

const int N = 3030;
const int mo = 1e9 + 7;

int f[2][N], l[2][N], r[2][N], c[N], cnt[N];

int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        int n;
        scanf("%d", &n);
        for(int i = 1; i < n; i++)
            scanf("%d", &c[i]);
        memset(f, 0, sizeof(f));
        memset(cnt, 0, sizeof(cnt));
        int cur = (n - 1) & 1;
        f[cur][1] = 2, l[cur][1] = 1, r[cur][1] = c[n - 1] - 1;
        f[cur][2] = c[n - 1], l[cur][2] = c[n - 1], r[cur][2] = c[n - 1];
        cnt[cur] = 2;
        for(int i = n - 2; i; i--) {
            int ne = i & 1;
            cnt[ne] = 0;
            long long sum = 0;
            int now = 0;
            for(int j = 1; j <= cnt[cur]; j++)
                if(l[cur][j] <= c[i] && c[i] <= r[cur][j]) {
                    now = f[cur][j];
                    break;
                }
            int nowR = 0;
            for(int j = 1; j <= cnt[cur]; j++) {
                int L = l[cur][j], R = r[cur][j];
                if(L <= c[i])sum += 1ll * (min(R, c[i]) - L + 1) * f[cur][j];
                if(L < c[i]) {
                    int nR = c[i] - L, nL = max(c[i] - R, 1);
                    cnt[ne]++;
                    nowR = max(nowR, nR);
                    l[ne][cnt[ne]] = nL;
                    r[ne][cnt[ne]] = nR;
                    f[ne][cnt[ne]] = (f[cur][j] + now) % mo;
                }
            }
            if(nowR < c[i] - 1) {
                cnt[ne]++;
                l[ne][cnt[ne]] = nowR + 1;
                r[ne][cnt[ne]] = c[i] - 1;
                f[ne][cnt[ne]] = now;
            }
            cnt[ne]++;
            l[ne][cnt[ne]] = r[ne][cnt[ne]] = c[i];
            f[ne][cnt[ne]] = sum % mo;
            cur = ne;
        }
        int ans = 0;
        for(int i = 1; i <= cnt[1]; i++)
            (ans += 1ll * f[1][i] * (r[1][i] - l[1][i] + 1) % mo) %= mo;
        printf("%d\n", ans);
    }
}

 

你可能感兴趣的:(未解决,未解决)