Light OJ 1031-easy Game (interval dp)

Source: Internet
Author: User



Title Link: http://www.lightoj.com/volume_showproblem.php?problem=1031



Title: Two players, alternately, can be taken from either end of the array, each time can go to any but only one end, their score is to get all the values and. Now ask for the maximum value of the two players ' score difference.



Problem-solving ideas: Set DP[I][J] for the interval from I to J, the maximum difference that can be obtained, only need to enumerate one of the number C between I and J, as a power outage, then the maximum value should be Max (Sum[c]-sum[i-1]-dp[c+1][j], sum[j]-sum[ C-1]-cou (i, c-1)). Then update this value on the line.



The code is as follows:


#include <bits/stdc++.h>

using namespace std;
const int N = 107;
int sum[N], dp[N][N];

int cou(int i, int j)
{
    if(i > j)
        return 0;

    if(dp[i][j] != INT_MAX)
        return dp[i][j];

    dp[i][j] = sum[j] - sum[i-1];
    for(int c=i; c<=j; ++ c)
    {
        dp[i][j] = max(dp[i][j], sum[c] - sum[i-1] - cou(c+1, j));
        dp[i][j] = max(dp[i][j], sum[j] - sum[c-1] - cou(i, c-1));
    }

    return dp[i][j];
}

void solve(int cases)
{
    int n;
    scanf("%d", &n);

    for(int i=1; i<=n; ++ i)
        for(int j=1; j<=n; ++ j)
            dp[i][j] = INT_MAX;

    sum[0] = 0;
    for(int i=1; i<=n; ++ i)
    {
        scanf("%d", &sum[i]);
        dp[i][i] = sum[i];
        sum[i] += sum[i-1];
    }
    printf("Case %d: %d\n", cases, cou(1, n));
}
int main()
{
    int t;
    scanf("%d", &t);
    for(int i=1; i<=t; ++ i)
        solve(i);
    return 0;
}





Light OJ 1031-easy Game (interval 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.