首页 > 代码库 > HDU 4972 A simple dynamic programming problem(推理)

HDU 4972 A simple dynamic programming problem(推理)

HDU 4972 A simple dynamic programming problem

题目链接

推理,会发现只有前一个和当前一个分数为(1, 2)或(2, 1)的时候,会有两种加分方法,其他情况最多就一种情况,所以只要统计(1, 2),(2, 1)的个数,最后判断分差是否为0,如果不为0,那么可能是正或负,那就是两倍

代码:

#include <cstdio>
#include <cstring>

const int N = 100005;
int t, n, a[N];

int solve() {
    int ans = 1;
    for (int i = 1; i < n; i++) {
	if ((a[i] == 2 && a[i - 1] == 1) || (a[i] == 1 && a[i - 1] == 2)) ans++;
	if (a[i] - a[i - 1] > 3 || a[i - 1] - a[i] > 3) return 0;
	if (a[i] == a[i - 1] && a[i] != 1) return 0;
    }
    if (a[n - 1]) ans *= 2;
    return ans;
}

int main() {
    int cas = 0;
    scanf("%d", &t);
    while (t--) {
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	    scanf("%d", &a[i]);
	printf("Case #%d: %d\n", ++cas, solve());
    }
    return 0;
}