• Easy Game LightOJ


    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

    2

    4

    4 -10 -20 7

    4

    1 2 3 4

    Sample Output

    Case 1: 7

    Case 2: 10

    题解:dp[ i ][ j ]表示 i~j 序列取完后,先手比后手多得的分数的最大值。

       dp[ i ][ j ]=max(sum[ i ][ j ],sum[ i ][ k ]-dp[ k+1 ][ j ],sum[ k+1 ][ j ]-dp[ i ][ k ]),当先手取一段序列后,剩下的序列,后手变成了先手,且决策都是使两者的差值尽量大

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 using namespace std;
     6 
     7 int n;
     8 int a[105],sum[105],dp[105][105];
     9 
    10 void Solve(int t){
    11     for(int i=n;i>=1;i--){
    12         for(int j=i+1;j<=n;j++){
    13             dp[i][j]=sum[j]-sum[i-1];
    14             for(int k=i;k<j;k++){
    15                 int temp=max(sum[k]-sum[i-1]-dp[k+1][j],sum[j]-sum[k]-dp[i][k]);
    16                 dp[i][j]=max(dp[i][j],temp);
    17             }
    18         }
    19     }
    20     printf("Case %d: %d
    ",t,dp[1][n]);
    21 }
    22 
    23 int main()
    24 {   int kase;
    25     cin>>kase;
    26     for(int t=1;t<=kase;t++){
    27         cin>>n;
    28         sum[0]=0;
    29         memset(dp,0,sizeof(dp));
    30         for(int i=1;i<=n;i++){
    31             cin>>a[i];
    32             dp[i][i]=a[i];
    33             sum[i]=sum[i-1]+a[i];
    34         }
    35         Solve(t); 
    36     }
    37     return 0;
    38 } 
  • 相关阅读:
    2018-10-20-WPF-通过位处理合并图片
    2019-2-11-WPF-获取应用的所有窗口
    2019-2-11-WPF-获取应用的所有窗口
    2018-8-10-WPF-如何在绑定失败异常
    2018-8-10-WPF-如何在绑定失败异常
    类和对象
    类和对象
    什么是可串行化MVCC
    LeetCode-环形链表|+环形链表||
    用js仿探探拖拽卡片的效果、飞卡片的效果,感觉挺酷,最后有美女看哦!
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7500528.html
Copyright © 2020-2023  润新知