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 } 
  • 相关阅读:
    DLPAR中关于cpu资源的分配
    官网下载storage manager方法
    vsftpd中关于ftpusers和user_list两个文件的说明以及vsftpd.conf中的userlist_enable和userlist_deny两个配置项的解释
    Windows10搭建FTP服务器
    fdisk分区的起始扇区为什么是2048——linux 分区与启动的小故事
    P740SAS卡报错的处理
    linux 下route命令
    实验01:构建FTP服务器
    IBMx86 x3850 7143机器面板PCI灯亮
    css的选择器汇总
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7500528.html
Copyright © 2011-2022 走看看