poj 3276

尺选法、反转。

//212k, 344ms
#include <iostream>
#include <cstring>

using namespace std;

const int MAX_N = 5005;

int n;
int dir[MAX_N], f[MAX_N];

int calc(int K) {
    memset(f, 0, sizeof(f));
    int res = 0, sum = 0;
    for(int i=0; i+K<=n; i++) {
        if((dir[i] + sum ) % 2 != 0) {
            res++;
            f[i] = 1;
        }
        sum += f[i];
        if(i - K + 1 >= 0)
            sum -= f[i-K+1];
    }

    for(int i=n-K+1; i<n; i++) {
        if((dir[i] + sum) % 2 != 0)
            return -1;
        if(i-K+1 >= 0)
            sum -= f[i-K+1];
    }

    return res;
}

void solve() {
    int K = 1, M = n;
    for(int k=1; k<=n; k++) {
        int m = calc(k);
        if(m >= 0 && M > m) {
            M = m;
            K = k;
        }
    }
    printf("%d %d\n", K, M);
}

int main() {

    freopen("in.txt", "r", stdin);

    scanf("%d", &n);
    getchar();
    char c[2];
    for(int i=0; i<n; i++) {
        scanf("%s", c);
        if(c[0] == 'B')
            dir[i] = 1;
        else if(c[0] == 'F')
            dir[i] = 0;
    }

    solve();

    fclose(stdin);

    return 0;
}
posted @ 2016-11-16 13:21  StevenLuke  阅读(127)  评论(0编辑  收藏  举报