hdu4597

来源:互联网 发布:hmmlearn python 教程 编辑:程序博客网 时间:2024/05/16 18:27

Play Game

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


Problem 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
The 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).
 

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

思路;我们可以开有个4维数组dp[x1][y1][x2][y2]来记录a数组只剩下x1到y1时,b数组只剩下x2到y2时能获得最大值,其实只要用总和减去Alice上一状态时Bob能获得的最大值。

代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int dp[25][25][25][25],a[25],b[25];int dfs(int x1,int y1,int x2,int y2,int sum){    int maxn=0;    if(x1>y1&&x2>y2)return 0;    if(dp[x1][y1][x2][y2])return dp[x1][y1][x2][y2];    if(x1<=y1)    {        maxn=max(maxn,sum-dfs(x1+1,y1,x2,y2,sum-a[x1]));        maxn=max(maxn,sum-dfs(x1,y1-1,x2,y2,sum-a[y1]));    }    if(x2<=y2)    {        maxn=max(maxn,sum-dfs(x1,y1,x2+1,y2,sum-b[x2]));        maxn=max(maxn,sum-dfs(x1,y1,x2,y2-1,sum-b[y2]));    }    dp[x1][y1][x2][y2]=maxn;    return maxn;}int main(){    int T;    cin>>T;    while(T--)    {        int n;        scanf("%d",&n);        memset(dp,0,sizeof(dp));        int i;        int sum=0;        for(i=1;i<=n;i++)        {            scanf("%d",&a[i]);            sum+=a[i];        }        for(i=1;i<=n;i++)        {            scanf("%d",&b[i]);            sum+=b[i];        }        int z=dfs(1,n,1,n,sum);        cout<<z<<endl;    }    return 0;}



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 拼多多下单了商家不发货怎么办 店铺扣2分宝贝降权了怎么办 淘宝店没订单没流量怎么办啊 淘宝id账号登录密码忘记了怎么办 淘宝网店铺授权客服联系不上怎么办 卖家发货买家查不到物流信息怎么办 物流信息到了但东西没到怎么办 淘宝详情已更改对方恶意投诉怎么办 淘宝店写的不给退换怎么办 从饿了么商家借款逾期怎么办 谷歌浏览器网页无法复制文字怎么办 复制粘贴在文件本里找不到了怎么办 淘宝商家给我寄错东西怎么办 美团客户点餐商家没有了怎么办 如果银行卡里的钱突然没有了怎么办 微信绑定的银行卡怎么没有了怎么办 西亚超市商品退货赠品不退怎么办 淘宝发布宝贝推荐橱窗位不够怎么办 抖音和微信软件冲突怎么办 淘宝开过店想给别家做客服怎么办 学网上开店别人不主动教你怎么办 淘宝店铺身份信息复核逾期了怎么办 一件代发进货后如果没卖出去怎么办 国外供货商收了钱不发货怎么办 企业被注销之后淘宝企业店怎么办 在淘宝上买了东西店家关门了怎么办 在淘宝上买东西被店家骂了怎么办 淘宝店家错发了两次货怎么办 我的身份证注销了淘宝支付宝怎么办 买了空调坏了店家不管怎么办 支付宝充话费没到账也不退款怎么办 淘宝上店家关铺无法赔偿怎么办 未发货申请退款卖家拒绝怎么办 淘宝订单未发货买家恶意退款怎么办 淘宝被限制购买还有没退款的怎么办 复制粘贴了打字在复制下页怎么办? 得了抑郁症没办法还贷款了怎么办 淘宝东西退了商家不退钱怎么办 京东买东西付款后显示未付款怎么办 不能吃小龙虾的人吃了怎么办 闲鱼退货卖家收到货不退款怎么办