HDU 4972 A Simple Dynamic Programming Problem (efficient)

Source: Internet
Author: User

Link: HDU 4972 A Simple Dynamic Programming Problem

The two teams are playing basketball. Each time a score is played, the score card is updated. The counting method of the score card is to record the absolute value of the score difference between the two teams, the score for each goal may be 1, 2, or 3. Given the scoring situation in the competition, I would like to ask how many situations the last score has.

Solution: Classification discussion:

    • When the adjacent score is 1-2 or 2-1, there are two possible scores.
    • If the adjacent score difference is greater than 3 or equal and not equal to 1, the input is invalid.
    • In other cases, no new score is generated.
    • When the last score difference is 0, no one wins or loses.
#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 ;}

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.