首页 > 代码库 > HDU 4972 A simple dynamic programming problem

HDU 4972 A simple dynamic programming problem


随机输出保平安

#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 100005;
int a[N];
int main() {
    int T, cas = 0;
    scanf("%d", &T);
    while(T-- > 0) {
        int n; scanf("%d", &n);
        bool flag = 1;
        a[0] = 0;
        for(int i = 1; i <= n; i ++) {
            scanf("%d", &a[i]);
            if(abs(a[i] - a[i-1]) > 3) flag = false;
            if(a[i] == a[i-1] && a[i] != 1) flag = false;
        }
        if(!flag) {
            printf("Case #%d: 0\n", ++cas);
            continue;
        }
        int ans = 1;
        for(int i = 2; i <= n; i ++) {
            if(a[i] == 1 && a[i-1] == 2) ans ++;
            else if(a[i] == 2 && a[i-1] == 1) ans ++;
        }
        printf("Case #%d: ", ++cas);
        if(a[n] == 0) printf("%d\n", ans);
        else printf("%d\n", ans * 2);
    }
    return 0;
}