zoukankan      html  css  js  c++  java
  • Light OJ 1031---Easy Game(区间DP)

    题目链接

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

    Description

    You are playing a two player game. Initially there are n integer numbers in an array and player A and B get chance to take them alternatively. Each player can take one or more numbers from the left or right end of the array but cannot take from both ends at a time. He can take as many consecutive numbers as he wants during his time. The game ends when all numbers are taken from the array by the players. The point of each player is calculated by the summation of the numbers, which he has taken. Each player tries to achieve more points from other. If both players play optimally and player A starts the game then how much more point can player A get than player B?

    Input

    Input starts with an integer T (≤ 100), denoting the number of test cases.

    Each case contains a blank line and an integer N (1 ≤ N ≤ 100) denoting the size of the array. The next line contains N space separated integers. You may assume that no number will contain more than 4 digits.

    Output

    For each test case, print the case number and the maximum difference that the first player obtained after playing this game optimally.

    Sample Input

    Output for Sample Input

    2

    4

    4 -10 -20 7

    4

    1 2 3 4

    Case 1: 7

    Case 2: 10

    题意:有n个数排成一行,现在A和B两人从两端取任意个数(每次至少取一个),直到取完所有的数,A先取,求A取得数的和比B大多少?

    思路:区间DP,dp[i][j] 表示区间i~j A取得数的和比B大多少,那么可以这样分析:对于区间i~j  A先取sum[k]-sum[i-1]或sum[j]-sum[k](只能从两端取),然后该B取了,即对于区间k+1~j和i~k  DP转换为B比A大多少了,所以状态转移方程为 dp[i][j]=max(dp[i][j],max(sum[k]-sum[i-1]-dp[k+1][j],sum[j]-sum[k]-dp[i][k])); 

    代码如下:

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    int sum[105];
    int dp[105][105];   ///A比B大多少?
    
    int main()
    {
        int T,Case=1;
        int n;
        cin>>T;
        while(T--)
        {
            sum[0]=0;
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&sum[i]);
                sum[i]+=sum[i-1];
            }
            memset(dp,0,sizeof(dp));
            for(int i=1;i<=n;i++)
                dp[i][i]=sum[i]-sum[i-1];
            for(int len=1;len<n;len++)
            {
                for(int i=1;i<=n;i++)
                {
                    if(i+len>n) break;
                    dp[i][i+len]=sum[i+len]-sum[i-1];
                    for(int k=i;k<i+len;k++)
                    {
                        dp[i][i+len]=max(dp[i][i+len],max(sum[k]-sum[i-1]-dp[k+1][i+len],sum[i+len]-sum[k]-dp[i][k]));
                    }
                }
            }
            printf("Case %d: %d
    ",Case++,dp[1][n]);
        }
    }
  • 相关阅读:
    Effective.Java第67-77条(异常相关)
    淘宝店的图片哪里来的
    SEO高手在扯蛋?
    多少人,没能熬过那最初的三厘米!
    网站要提高权重要总结三点
    真正的干货是什么?
    google打不开怎么办?谷歌打不开的解决方法
    StatCounter
    教你如何抢注一个快过期的域名
    正确识别希捷Backup Plus新睿品1TB正品,杜绝奸商猖獗
  • 原文地址:https://www.cnblogs.com/chen9510/p/5843634.html
Copyright © 2011-2022 走看看