14多校 B-A simple dynamic programming problem

题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4972

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. The only way to gain scores is to throw the basketball into the basket. Each time after throwing into the basket, the score gained by the team is 1, 2 or 3. However due to the uncertain factors in the game, it’s hard to predict which team will get the next goal”.

Dragon is a crazy fan of Miami Heat so that after each throw, he will write down the difference between two team’s score regardless of which team keeping ahead. For example, if Heat’s score is 15 and the opposite team’s score is 20, Dragon will write down 5. On the contrary, if Heat has 20 points and the opposite team has 15 points, Dragon will still write down 5.

Several days after the game, Dragon finds out the paper with his record, but he forgets the result of the game. It’s also fun to look though the differences without knowing who lead the game, for there are so many uncertain! Dragon loves uncertain, and he wants to know how many results could the game has gone?

Input
The first line of input contains only one integer T, the number of test cases. Following T blocks, each block describe one test case.

For each test case, the first line contains only one integer N(N<=100000), which means the number of records on the paper. Then there comes a line with N integers (a1, a2, a3, … , an). ai means the number of i-th record.

Output
Each output should occupy one line. Each line should start with “Case #i: “, with i implying the case number. Then for each case just puts an integer, implying the number of result could the game has gone.

Sample Input
2
2
2 3
4
1 3 5 7

Sample Output
Case #1: 2
Case #2: 2

Author
BJTU

Source
2014 Multi-University Training Contest 10

大意:多组数据,NBA比赛。每次进一个球,记一次比分的差值, 3:1 和 1:3 都记为 2 ,求最后比分结果有几种情况。

题解:

初始化 cnt=0;
①因为知道最终比赛的分差,我们只要知道比赛分数的总和就能确定最终比分
②只有在 1 2 或 2 1的情况下才会有增量为 1 或 3 的情况,cnt++
③最终比分差不为 0 ,ans=2*(cnt+1) 否则ans=cnt+1
⑤相邻分差大于等于 3,或相邻分差相等且不为 1,不存在

#include<bits/stdc++.h>
using namespace std;
const int N=100005;  
int 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(abs( a[i]-a[i-1] )>3) return 0;
        if(a[i]==a[i-1] && a[i]!=1) return 0;
    }  
    if(a[n-1]!=0) ans*=2;
    return ans;  
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int ii=1;ii<=t;ii++)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        printf("Case #%d: %d\n",ii,solve());
    }
    return 0;  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值