其他分享
首页 > 其他分享> > 洛谷P2157[SDOI2009]-学校食堂

洛谷P2157[SDOI2009]-学校食堂

作者:互联网

if (j & 1) f[i + 1][j >> 1][k + 7] = min(f[i + 1][j >> 1][k + 7], f[i][j][k]);
if (i + k == 0) { //(上一道菜做的是空气)
    f[i][j | (1 << l)][l + 8] = min(f[i][j | (1 << l)][l + 8], f[i][j][k + 8];
} else { //(上一道真做菜了)
    f[i][j | (1 << l)][l + 8] = min(f[i][j | (1 << l)][l + 8], f[i][j][k + 8] + (t[i + k] ^ t[i + l]));
}
#include <bits/stdc++.h>

#define MAXN 1100
#define MX 256

using namespace std;

int C, n, t[MAXN], b[MAXN], f[MAXN][MX][20];

int main() {
    scanf("%d", &C);
    while (C--) {
        memset(f, 0x3f, sizeof(f));
        f[1][0][7] = 0;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf("%d%d", &t[i], &b[i]);
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < MX; j++) {
                for (int k = -8; k <= 7; k++) {
                    if (f[i][j][k + 8] == 0x3f3f3f3f) {
                        continue;
                    }
                    if (j & 1) {
                        f[i + 1][j >> 1][k + 7] = min(f[i + 1][j >> 1][k + 7], f[i][j][k + 8]);
                    } else {
                        int lim = 2e9;
                        for (int l = 0; l <= 7; l++) {
                            if (!((j >> l) & 1)) {
                                if (i + l > lim) {
                                    break;
                                }
                                lim = min(lim, i + l + b[i + l]);
                                if (i + k) {
                                    f[i][j | (1 << l)][l + 8] = min(f[i][j | (1 << l)][l + 8], f[i][j][k + 8] + (t[i + k] ^ t[i + l]));
                                } else {
                                    f[i][j | (1 << l)][l + 8] = min(f[i][j | (1 << l)][l + 8], f[i][j][k + 8]);
                                }
                            }
                        }
                    }
                }
            }
        }
        int ans = 2e9;
        for (int i = 0; i <= 8; i++) {
            ans = min(ans, f[n + 1][0][i]);
        }
        printf("%d\n", ans);
    }
    return 0;
}

标签:P2157,le,洛谷,个人,int,lim,学校食堂,打饭,MAXN
来源: https://www.cnblogs.com/chy12321/p/15374945.html