From 84a7cf36513e7e348beeb6ce3b64dd22ca0db291 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 16 Jun 2022 20:47:17 +0800 Subject: [PATCH] =?UTF-8?q?P2157=20[SDOI2009]=20=E5=AD=A6=E6=A0=A1?= =?UTF-8?q?=E9=A3=9F=E5=A0=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/77474202 --- Luogu/P2157/P2157.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Luogu/P2157/P2157.cpp diff --git a/Luogu/P2157/P2157.cpp b/Luogu/P2157/P2157.cpp new file mode 100644 index 00000000..89469ff3 --- /dev/null +++ b/Luogu/P2157/P2157.cpp @@ -0,0 +1,68 @@ +#include +#include + +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 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; +}