1. 程式人生 > >BZOJ1802: [Ahoi2009]checker(性質分析 dp)

BZOJ1802: [Ahoi2009]checker(性質分析 dp)

max rst type getc name template %d problem chm

題意

題目鏈接

Sol

一個不太容易發現但是又很顯然的性質:

如果有兩個相鄰的紅格子,那麽第一問答案為0, 第二問可以推

否則第一問答案為偶數格子上的白格子數,第二問答案為偶數格子上的紅格子數

#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long 
#define LL long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1001, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, a[MAXN];
LL f[MAXN];
signed main() {
    N = read();
    int ans[2] = {0, 0}, flag = 0;
    memset(f, 0x3f, sizeof(f));
    for(int i = 1; i <= N; i++) {
        a[i] = read();
        if(i > 2 && a[i] && a[i] == a[i - 1]) flag = 1;
        if((!(i & 1))) ans[a[i]]++;
        if(a[i]) f[i] = 1;
    }
    if(!flag) {printf("%d\n%d", ans[0], ans[1]); return 0;}
    for(int i = 2; i < N; i++) {
        if(a[i] && a[i + 1]) {
            for(int j = i - 1; j > 1; j--) chmin(f[j], f[j + 1] + f[j + 2]);
            for(int j = i + 2; j < N; j++) chmin(f[j], f[j - 1] + f[j - 2]);
        }
    }
    LL out = 0;
    for(int i = 2; i < N; i += 2) out += f[i];
    cout << 0 << "\n" << out;
    return 0;
}
/*
5

0 0 1 1 0
*/

BZOJ1802: [Ahoi2009]checker(性質分析 dp)