zoukankan      html  css  js  c++  java
  • 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 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
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    int abs(int x)
    {
        if(x>0)
          return x;
        else
          return -x;
    }
    int main()
    {
        int i,t,n,a[100005],ans,k=1;
        scanf("%d",&t);
        while(t--)
        {
            memset(a,0,sizeof(a));
            ans=0;
          scanf("%d",&n);
          for(i=0;i<n;i++)
            scanf("%d",&a[i]);
            int cnt=0;
            int flag=0;
          for(i=0;i<n&&(i+1)<n;i++)
          {
              if(((a[i+1]==a[i])&&a[i]!=1)||abs(a[i+1]-a[i])>3)
                  {
                      flag=1;
                      break;
                  }
             if((a[i]==1&&a[i+1]==2)||(a[i]==2&&a[i+1]==1))
                  cnt++;
          }
    
            if(flag==1)
            {
               printf("Case #%d: %d
    ",k++,ans);
                 continue;
            }
            if(a[n-1]==0)
                ans=cnt+1;
           else
              ans=2*cnt+2;
          printf("Case #%d: %d
    ",k++,ans);
        }
        return 0;
    }
  • 相关阅读:
    苹果 iOS 8 新固件新功能特性总结汇总 (苹果 iPhone/iPad 最新移动操作系统)
    为什么魔兽世界里男性玩家爱选女性角色?
    腾讯PK微软 王者之战一触即发
    策略模式
    代理模式
    模板模式
    适配器模式
    工厂模式
    随机森林步骤(转)
    马氏距离通俗理解(转)
  • 原文地址:https://www.cnblogs.com/cancangood/p/3928311.html
Copyright © 2011-2022 走看看