如果去关某一个灯,那么途中经过的灯都能关闭,那么就是连续一段区间,区间DP。
f[i][j][0] 表示关完 i, j 这个区间且在 i 这个位置
f[i][j][1] 表示关完 i, j 这个区间且在 j 这个位置
代码
#include <cstdio> #include <cstring> #include <iostream> #define N 1001 #define min(x, y) ((x) < (y) ? (x) : (y)) int n, s; int f[N][N][2], a[N], d[N]; inline int read() { int x = 0, f = 1; char ch = getchar(); for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = -1; for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - ‘0‘; return x * f; } int main() { int i, j; n = read(); s = read(); for(i = 1; i <= n; i++) d[i] = read(), a[i] = read() + a[i - 1]; memset(f, 127 / 3, sizeof(f)); f[s][s][0] = f[s][s][1] = 0; for(i = s; i >= 1; i--) for(j = i + 1; j <= n; j++) { f[i][j][0] = min(f[i][j][0], f[i + 1][j][0] + (d[i + 1] - d[i]) * (a[n] - (a[j] - a[i]))); f[i][j][0] = min(f[i][j][0], f[i + 1][j][1] + (d[j] - d[i]) * (a[n] - (a[j] - a[i]))); f[i][j][1] = min(f[i][j][1], f[i][j - 1][0] + (d[j] - d[i]) * (a[n] - (a[j - 1] - a[i - 1]))); f[i][j][1] = min(f[i][j][1], f[i][j - 1][1] + (d[j] - d[j - 1]) * (a[n] - (a[j - 1] - a[i - 1]))); } printf("%d\n", min(f[1][n][0], f[1][n][1])); return 0; }
时间: 2024-10-07 23:18:31