hdu 4972 A simple dynamic programming problem(高效)

题目链接:hdu 4972 A simple dynamic programming problem

题目大意:两支球队进行篮球比赛,每进一次球后更新比分牌,比分牌的计数方法是记录两队比分差的绝对值,每次进球的分可能是1,2,3分。给定比赛中的计分情况,问说最后比分有多少种情况。

解题思路:分类讨论:

  • 相邻计分为1-2或者2-1的时候,会对应有两种的的分情况
  • 相邻计分之差大于3或者说相等并且不等于1的话,为非法输入
  • 其他情况下,不会造成新的比分情况产生
  • 对于最后一次比分差为0的情况,就没有谁赢谁输一说。
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxn = 1e5+5;

int N, a[maxn];

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

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

hdu 4972 A simple dynamic programming problem(高效),布布扣,bubuko.com

时间: 2024-12-17 01:44:20

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]; i

hdu 4972 A simple dynamic programming problem (转化 乱搞 思维题) 2014多校10

题目链接 题意:给定一个数组记录两队之间分差,只记分差,不记谁高谁低,问最终有多少种比分的可能性 分析: 类似cf的题目,比赛的时候都没想出来,简直笨到极点..... 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <vector> 7 #include &

HDU 4972 A simple dynamic programming problem(数学思维题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4972 Problem Description Dragon is watching NBA. He loves James and Miami Heat. Here's an introduction of basketball game:http://en.wikipedia.org/wiki/Basketball. However the game in Dragon's version is

hdu - 4972 - A simple dynamic programming problem(数学 + dp)

题意:NBA比赛,双方共N次进球(N<=100000),无论哪方,进一个球(得分只可能为1,2,3),就记录一次(记两队分数差的绝对值),问最后两队的比分有多少种. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4972 -->>知道最后的比分差k,怎么求得比分呢?设分数较低的一方的最后分数为x,则另一方的最后分数为x + k,设双方最后总分和为S,则x  = (S - k) / 2,可得双方的比分..所以,只要知道最后双方的总分和,就可以

hdu 4972 A simple dynamic programming problem (每次记录两队得分差值求结果数)

因为是每次投篮后记录两队得分的差值,所以两个队伍的总分是不断增加的,可以发现只有差值由1-->2或者2-->1的情况才可能产生产生两种总分和的结果如 0:2可以变成2:3和1:2  其他的情况都只能是一种 郜大可的代码: 1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 #defi

HDOJ 4972 A simple dynamic programming problem

找规律...数据可能不合法...输出0 A simple dynamic programming problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 85    Accepted Submission(s): 31 Problem Description Dragon is watching NBA. He loves Ja

【HDOJ】4972 A simple dynamic programming problem

水题. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 5 int abs(int x) { 6 return x<0 ? -x:x; 7 } 8 9 int main() { 10 int t, n; 11 int cur, past; 12 __int64 ans; 13 bool flag; 14 15 #ifndef ONLINE_JUDGE 16 freopen("

HDU-4972 A simple dynamic programming problem

http://acm.hdu.edu.cn/showproblem.php?pid=4972 ++和+1还是有区别的,不可大意. A simple dynamic programming problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 307    Accepted Submission(s): 117 Problem D

HDU4972:A simple dynamic programming problem

Problem Description Dragon is watching NBA. He loves James and Miami Heat. Here's an introduction of basketball game:http://en.wikipedia.org/wiki/Basketball. However the game in Dragon's version is much easier: "There's two teams fight for the winner