hdu4597 Play Game

來源:互聯網
上載者:User
Play Game

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 54 Accepted Submission(s): 36

Problem DescriptionAlice 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?

InputThe first line contains an integer T (T≤100), indicating the number of cases.

Each case contains 3 lines. The first line is the N (N≤20). The second line contains N integer ai (1≤ai≤10000). The third line contains N integer bi (1≤bi≤10000).

OutputFor 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 

Source2013 ACM-ICPC吉林通化全國邀請賽——題目重現

Recommendliuyiding這題就是一個區間dp,因為,每一次取都,可以取兩個隊列的頭和尾,所以,就是一個二維的區間dp,我們用dp[al][ar][bl][br]表示,從第一個數列從al 到ar,第二個數列從,bl到br,當前這個人具有第一選擇權的最大分值,那麼我們,可以取兩個隊列的頭和尾,就有4個狀態轉移方程了!用一個記憶化搜尋就可以了!

#include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>using namespace std;#define MAXN 26int suma[MAXN],sumb[MAXN],pa[MAXN],pb[MAXN],dp[MAXN][MAXN][MAXN][MAXN];int dfs(int al,int ar,int bl ,int br){    if(dp[al][ar][bl][br]!=-1)    return dp[al][ar][bl][br];    dp[al][ar][bl][br]=0;    if(al<=ar)    dp[al][ar][bl][br]=suma[ar]-suma[al-1]+sumb[br]-sumb[bl-1]-dfs(al+1,ar,bl,br);    if(al<=ar)    dp[al][ar][bl][br]=max(dp[al][ar][bl][br],suma[ar]-suma[al-1]+sumb[br]-sumb[bl-1]-dfs(al,ar-1,bl,br));    if(bl<=br)    dp[al][ar][bl][br]=max(dp[al][ar][bl][br],suma[ar]-suma[al-1]+sumb[br]-sumb[bl-1]-dfs(al,ar,bl+1,br));    if(bl<=br)    dp[al][ar][bl][br]=max(dp[al][ar][bl][br],suma[ar]-suma[al-1]+sumb[br]-sumb[bl-1]-dfs(al,ar,bl,br-1));    return dp[al][ar][bl][br];}int main (){    int n,i,tcase;    scanf("%d",&tcase);    while(tcase--)    {        scanf("%d",&n);        suma[0]=sumb[0]=0;        for(i=1;i<=n;i++)        {            scanf("%d",&pa[i]);            suma[i]=suma[i-1]+pa[i];        }        for(i=1;i<=n;i++)        {            scanf("%d",&pb[i]);            sumb[i]=sumb[i-1]+pb[i];        }        memset(dp,-1,sizeof(dp));        printf("%d\n",dfs(1,n,1,n));    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.