hdu 4597 Play Game 区间dp

Play Game

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=4597

Description

Alice and Bob are playing a game. There are two piles of cards. There are N cards in each pile, and each card has a score. They take turns to pick up the top or bottom card from either pile, and the score of the card will be added to his total score. Alice and Bob are both clever enough, and will pick up cards to get as many scores as possible. Do you know how many scores can Alice get if he picks up first?

Input

For each case, output an integer, indicating the most score Alice can get.

Output

For each case, output an integer, indicating the most score Alice can get.

Sample Input

2
 
1
23
53
 
3
10 100 20
2 4 3

Sample Output

53
105

 

HINT

题意

有两排数,AB依次拿,每次只能从第一/二排最左边和最右边拿

问你A拿的和是多少,假设两个人都是很聪明的

题解:

出现聪明这个词的时候,这种题不是博弈论就是dp吧

很显然这道题是一个区间dp

直接跑就好了

代码:

 

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)  
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************

int dp[30][30][30][30];

int a[maxn];
int b[maxn];
int temp;
int solve(int l1,int r1,int l2,int r2)
{
    if(dp[l1][r1][l2][r2]!=-1)
        return dp[l1][r1][l2][r2];
    if(l1>r1||l2>r2)
        dp[l1][r1][l2][r2]=0;
    int sum=0;
    int ans=0;
    if(l1<=r1)
        sum+=a[r1]-a[l1-1];
    if(l2<=r2)
        sum+=b[r2]-b[l2-1];
    if(l1<=r1)
    {
        ans=max(ans,sum-solve(l1+1,r1,l2,r2));
        ans=max(ans,sum-solve(l1,r1-1,l2,r2));
    }
    if(l2<=r2)
    {
        ans=max(ans,sum-solve(l1,r1,l2+1,r2));
        ans=max(ans,sum-solve(l1,r1,l2,r2-1));
    }
    return dp[l1][r1][l2][r2]=ans;
}
int main()
{
    //test;
    int t=read();
    while(t--)
    {
        memset(dp,-1,sizeof(dp));
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        int n=read();
        for(int i=1;i<=n;i++)   
        {
            temp=read();
            a[i]+=a[i-1]+temp;
        }
        for(int i=1;i<=n;i++)
        {
            temp=read();
            b[i]+=b[i-1]+temp;
        }
        cout<<solve(1,n,1,n)<<endl;
    }
}

 

 

 

你可能感兴趣的:(game)