HDU-4972-A Simple Dynamic Programming Problem (Mathematics + dp)

Source: Internet
Author: User

Question: In an NBA game, the two sides scored n times (n <= 100000). Whichever side, they scored one goal (the score can only be 1, 2, 3 ), record the score once (the absolute value of the score difference between the two teams) and ask the number of scores of the last two teams.

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4972

--> How can I obtain the score if I know the last score is k? If the final score of one party with a lower score is X, the final score of the other party is x + K, and the final total score of both parties is S, then x = (S-k)/2, the score can be obtained from both parties .. Therefore, as long as you know the sum of the total score of both parties, you can determine the score of both parties .. As a result, the question is transformed into the sum of the total points and quantity of the two sides.

After knowing the sum of the Double score, you can find X: x + k and x + K: X. Therefore, if X! = X + k is k! When the value is 0, the final result is multiplied by 2 ..

Status: DP [I] indicates the sum of the total scores of both sides after the first goal.

State transition equation: DP [I] = DP [I-1] + 1 (when (diff [I] = 1 & diff [I-1] = 2) | (when diff [I] = 2 & diff [I-1] = 1)

Border: DP [0] = 1, diff [0] = 0

Note: If the last score difference is 2 and the score difference is 1, the loser scores 1 or 3, and two total scores are produced .. The last score difference is 1, and the current score difference is 2 ..

Submit g ++ and use ABS (INT). Ce (check the cmath in mingw. ABS does not provide the int parameter). c ++ won't (go over msdn, the provided parameter has an int value ).. I always use ABS to calculate the absolute value of an integer, and use fabs to calculate the absolute value of a floating point ..

#include <cstdio>#include <cmath>using std::abs;int main(){    int T, N, diff, kase = 0;    scanf("%d", &T);    while (T--)    {        int dp = 1, last = 0;        bool ok = true;        scanf("%d", &N);        while (N--)        {            scanf("%d", &diff);            if (!ok) continue;            if (abs(diff - last) > 3 || (diff == last && diff != 1))            {                ok = false;            }            else            {                if ((diff == 2 && last == 1) || (diff == 1 && last == 2))                {                    dp++;                }                last = diff;            }        }        if (diff != 0)        {            dp <<= 1;        }        printf("Case #%d: ", ++kase);        ok ? printf("%d\n", dp) : puts("0");    }    return 0;}


HDU-4972-A Simple Dynamic Programming Problem (Mathematics + dp)

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.