HDU-4972 A Simple Dynamic Programming Problem

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4972

++ And + 1 are different.

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 descriptiondragon 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 cute uncertain! Dragon loves uncertain, and he wants to know how many ResultsCocould the game has gone?

 

Inputthe 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.

 

Outputeach output shocould occupy one line. Each line shocould start with "case # I:", with I implying the case number. Then for each case just puts an integer, implying the number ResultCocould the game has gone.

 

Sample input222 341 3 5 7

 

Sample outputcase #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\n",k++,ans);             continue;        }        if(a[n-1]==0)            ans=cnt+1;       else          ans=2*cnt+2;      printf("Case #%d: %d\n",k++,ans);    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.