0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:05:26 +00:00

P2157 [SDOI2009] 学校食堂

https://www.luogu.com.cn/record/77474202
This commit is contained in:
Baoshuo Ren 2022-06-16 20:47:17 +08:00
parent cd46b6f9b6
commit 84a7cf3651
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

68
Luogu/P2157/P2157.cpp Normal file
View File

@ -0,0 +1,68 @@
#include <iostream>
#include <cstring>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1005;
const int INF = 0x3f3f3f3f;
int c, n, f[N][1 << 8][20];
std::pair<int, int> a[N];
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> c;
while (c--) {
memset(f, 0x3f, sizeof(f));
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].first >> a[i].second;
}
f[1][0][7] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 1 << 8; j++) {
for (int k = -8; k <= 7; k++) {
if (f[i][j][k + 8] != INF) {
if (j & 1) { // 当前同学吃了,无需考虑后方
f[i + 1][j >> 1][k + 7] = std::min(f[i + 1][j >> 1][k + 7], f[i][j][k + 8]);
} else {
int t = INF;
for (int l = 0; l <= 7; l++) {
if (j & 1 << l) continue;
if (i + l > t) break;
if (i + l <= n) t = std::min(t, i + l + a[i + l].second);
else t = std::min(t, i + l);
if (i + k) {
int x = i + k <= n ? a[i + k].first : 0,
y = i + l <= n ? a[i + l].first : 0;
f[i][j | 1 << l][l + 8] = std::min(f[i][j | 1 << l][l + 8], f[i][j][k + 8] + (x ^ y));
} else {
f[i][j | 1 << l][l + 8] = std::min(f[i][j | 1 << l][l + 8], f[i][j][k + 8]);
}
}
}
}
}
}
}
int ans = INF;
for (int i = 0; i <= 8; i++) {
ans = std::min(ans, f[n + 1][0][i]);
}
cout << ans << endl;
}
return 0;
}