bzoj 1864

思路:随便dp一下

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define y1 skldjfskldjg
#define y2 skldfjsklejg
using namespace std;

const int N = 1e5 + 7;
const int M = 5e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 20100403;

int n, L[N], R[N], a[N], f[N][3], id, rt, g[N][3];
char s[N];

void dfs(int u, int &x) {
    x = u;
    if(!a[u]) return;
    dfs(++id, L[u]);
    if(a[u] == 2) dfs(++id, R[u]);
}

void dp(int u) {
    if(!a[u]) {
        f[u][0] = 1;
        f[u][1] = f[u][2] = 0;
        g[u][0] = 1;
        g[u][1] = g[u][2] = 0;
    } else if(a[u] == 1) {
        dp(L[u]);
        f[u][0] = min(f[L[u]][1], f[L[u]][2]) + 1;
        f[u][1] = min(f[L[u]][2], f[L[u]][0]);
        f[u][2] = min(f[L[u]][0], f[L[u]][2]);
        g[u][0] = max(g[L[u]][1], g[L[u]][2]) + 1;
        g[u][1] = max(g[L[u]][2], g[L[u]][0]);
        g[u][2] = max(g[L[u]][0], g[L[u]][2]);
    } else {
        dp(L[u]); dp(R[u]);
        f[u][0] = min(f[L[u]][1] + f[R[u]][2], f[L[u]][2] + f[R[u]][1]) + 1;
        f[u][1] = min(f[L[u]][0] + f[R[u]][2], f[L[u]][2] + f[R[u]][0]);
        f[u][2] = min(f[L[u]][0] + f[R[u]][1], f[L[u]][1] + f[R[u]][0]);

        g[u][0] = max(g[L[u]][1] + g[R[u]][2], g[L[u]][2] + g[R[u]][1]) + 1;
        g[u][1] = max(g[L[u]][0] + g[R[u]][2], g[L[u]][2] + g[R[u]][0]);
        g[u][2] = max(g[L[u]][0] + g[R[u]][1], g[L[u]][1] + g[R[u]][0]);
    }
}
int main() {
    scanf("%s", s + 1);
    n = strlen(s + 1);
    for(int i = 1; i <= n; i++) a[i] = s[i] - '0';
    dfs(++id, rt);

//    for(int i = 1; i <= n; i++) printf("%d: %d %d\n", i, L[i], R[i]);
    dp(1);
    printf("%d %d\n", max(g[1][0], max(g[1][1], g[1][2])), min(f[1][0], min(f[1][1], f[1][2])));
    return 0;
}

 

posted @ 2018-09-09 00:39  NotNight  阅读(130)  评论(0编辑  收藏  举报