lightoj 1031 - Easy Game 【区间dp】360 2017笔试编程题3

Description

You are playing a two player game. Initially there are n integer numbers in an array and playerA andB 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 playerA starts the game then how much more point can playerA get than playerB?

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 containsN space separated integers. You may assume that no number will contain more than4 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居然还有这么玩的==我们可以想到这种题都是小区间合并成大区间的,然而这种类似于博弈的做法实在是让人头疼QAQ,其实倒也不难,既然第一个人的值是有第二个人推导过来的,那么用的值也是第二个人得到的最优值,这么来说,先手后手只是相对概念了==

把[i,j]分成[i,k] [k+1,j]两段,如果取走左边的就是左边的和减去右边那部分后手比先手多的部分,反之亦然


#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[109][109],sum[109];
int t,n,cas;
int main()
{
 //   freopen("cin.txt","r",stdin);
    scanf("%d",&t);
    cas=1;
    while(t--)
    {
        scanf("%d",&n);
        memset(dp,0,sizeof(0));
        for(int i=1;i<=n;i++)scanf("%d",&dp[i][i]);
        sum[1]=dp[1][1];
        for(int i=2;i<=n;i++) sum[i]=sum[i-1]+dp[i][i];
        for(int len=1;len<=n;len++)
            for(int l=1;l+len<=n;l++)
            {
                int r=l+len;
                dp[l][r]=sum[r]-sum[l-1];
                for(int k=l;k<r;k++)
                dp[l][r]=max(dp[l][r],max(sum[k]-sum[l-1]-dp[k+1][r],sum[r]-sum[k]-dp[l][k]));
            }
        printf("Case %d: %d\n",cas++,dp[1][n]);
    }
    return 0;
}


翻博客就是觉得这个题眼熟么,360 2017春招笔试:

分金子(奇虎360 2017春招真题)

题目描述

A、B两伙马贼意外地在一片沙漠中发现了一处金矿,双方都想独占金矿,但各自的实力都不足以吞下对方,经过谈判后,双方同意用一个公平的方式来处理这片金矿。处理的规则如下:他们把整个金矿分成n段,由A、B开始轮流从最左端或最右端占据一段,直到分完为止。 

马贼A想提前知道他们能分到多少金子,因此请你帮忙计算他们最后各自拥有多少金子?(两伙马贼均会采取对己方有利的策略)

输入

测试数据包含多组输入数据。输入数据的第一行为一个正整数T(T<=20),表示测试数据的组数。然后是T组测试数据,每组测试数据的第一行包含一个整数n,下一行包含n个数(n <= 500 ),表示每段金矿的含金量,保证其数值大小不超过1000。


 
样例输入

6

4 7 2 9 5 2

10

140 649 340 982 105 86 56 610 340 879


输出

对于每一组测试数据,输出一行"Case #id: sc1 sc2",表示第id组数据时马贼A分到金子数量为sc1,马贼B分到金子数量为sc2。详见样例。


样例输出

Case #1: 18 11

Case #2: 3206 981


一样的 就是k只能取l+1 或者 r-1

     #include <iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int dp[509][509],sum[509];
    int t,n,cas;
    int main()
    {
        freopen("cin.txt","r",stdin);
        scanf("%d",&t);
        cas=1;
        while(t--)
        {
            scanf("%d",&n);
            memset(dp,0,sizeof(0));
            for(int i=1;i<=n;i++)scanf("%d",&dp[i][i]);
            sum[1]=dp[1][1];
            for(int i=2;i<=n;i++) sum[i]=sum[i-1]+dp[i][i];
            for(int len=1;len<=n;len++)
                for(int l=1;l+len<=n;l++)
                {
                    int r=l+len;
                    dp[l][r]=max(sum[l]-sum[l-1]-dp[l+1][r],sum[r]-sum[r-1]-dp[l][r-1]);
                }
            printf("Case #%d: %d %d\n",cas++,(sum[n]+dp[1][n])/2,(sum[n]-dp[1][n])/2);
        }
        return 0;
    }



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值