[Cometoj#3 B]棋盘_状压dp
Posted shurak
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Cometoj#3 B]棋盘_状压dp相关的知识,希望对你有一定的参考价值。
棋盘
题目链接:https://cometoj.com/contest/38/problem/B?problem_id=1535
数据范围:略。
题解:
因为行数特别小,所以$dp$的时候可以状压起来。
之后就非常傻逼了....
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #define N 1000010 using namespace std; char *p1, *p2, buf[100000]; #define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++ ) int rd() { int x = 0, f = 1; char c = nc(); while (c < 48) { if (c == ‘-‘) f = -1; c = nc(); } while (c > 47) { x = (((x << 2) + x) << 1) + (c ^ 48), c = nc(); } return x * f; } int a[N], b[N], f[N][2]; int main() { int n = rd(); for (int i = 1; i <= n; i ++ ) { a[i] = rd(); } for (int i = 1; i <= n; i ++ ) { b[i] = rd(); } int l = N, r = 1; for (int i = 1; i <= n; i ++ ) { if (a[i] || b[i]) { l = i; break; } } for (int i = n; i; i -- ) { if (a[i] || b[i]) { r = i; break; } } for (int i = l; i <= r; i ++ ) { f[i][0] = f[i - 1][0] + !a[i]; f[i][1] = f[i - 1][1] + !b[i]; f[i][0] = min(f[i][0], f[i][1] + !a[i]); f[i][1] = min(f[i][1], f[i][0] + !b[i]); } cout << min(f[r][0], f[r][1]) << endl ; return 0; }
以上是关于[Cometoj#3 B]棋盘_状压dp的主要内容,如果未能解决你的问题,请参考以下文章
[BZOJ5248] 2018九省联考 D1T1 一双木棋 | 博弈论 状压DP