zoukankan      html  css  js  c++  java
  • 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 } 
  • 相关阅读:
    mysql 开发进阶篇系列 18 MySQL Server(innodb_buffer_pool_size)
    sql server 备份与恢复系列三 简单恢复模式下的备份与还原
    (后端)SQL Server日期时间函数
    (网页)the server responded with a status of 403 (Forbidden)
    (其他)小程序介绍和开发
    (网页)jQuery UI 实例
    (其他)令人忧虑,不阅读的中国人
    (其他)程序员的8种好习惯
    (后端)NoSuchMethodError
    JsonParseException:非法的unquoted字符((CTRL-CHAR,代码9)):必须被转义
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7500528.html
Copyright © 2011-2022 走看看