[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;
}

 

posted @ 2019-10-23 15:06  JZYshuraK_彧  阅读(126)  评论(0编辑  收藏  举报