GCJ Qualification Round 2016 B题

经典的翻饼问题,直接做:从下往上看,已翻好的饼忽略掉;从上往下,连续的已翻好的一起翻过来;整个翻过来。

/*
 * Author    : ben
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
typedef long long LL;
char pancakes[110];
bool pan[110];
int pannum;

void inline flippan(int n) {
    bool *first = pan;
    bool *last = first + n;
    while ((first != last) && (first != --last)) {
        bool tmp = !(*last);
        *last = !(*first);
        *first = tmp;
        ++first;
    }
    if (n % 2 == 1) {
        pan[n / 2] = !pan[n / 2];
    }
}

int work() {
    int ans = 0;
    while (pannum > 0) {
        while (pannum > 0 && pan[pannum - 1]) {
            pannum--;
        }
        if (pannum <= 0) {
            break;
        }
        int I = 0;
        while (pan[I]) {
            I++;
        }
        if (I > 0) {
            flippan(I);
            ans++;
        }
        flippan(pannum);
        ans++;
    }
    return ans;
}

int main() {
//    freopen("b.small.in", "r", stdin);
    int T;
    scanf("%d", &T);
    for (int t = 1; t <= T; t++) {
        scanf(" %s", pancakes);
        pannum = strlen(pancakes);
        for (int i = 0; i < pannum; i++) {
            if (pancakes[i] == '-') {
                pan[i] = 0;
            } else {
                pan[i] = 1;
            }
        }
        printf("Case #%d: %d\n", t, work());
    }
    return 0;
}

 

posted @ 2016-04-09 11:17  moonbay  阅读(179)  评论(0编辑  收藏  举报