爱丽丝和鲍勃正在玩游戏。 有两堆牌。 每一堆都有N张牌,每张牌都有一张分数。 他们轮流从任意一堆拿起顶部或底部牌,并且卡的分数将被添加到他的总分中。 爱丽丝和鲍勃都非常聪明,并且会拿起卡片以获得尽可能

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 a i (1≤a i≤10000). The third line contains N integer b i (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 

dp[la][ra][lb][rb]记录的是在a的区间只剩下la~ra,b的区间只剩下lb~rb的时候,Alice能得到的最大值

那么只需要考虑四种不同的取法并从中取得最优的方案,sum-(Alice上一个状态中Bob拿的值)中取最大

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
int dp[30][30][30][30];
int a[30];
int b[30];
int dfs(int la,int ra,int lb,int rb,int sum)
{
      int maxn=0;
      if(la>ra&&lb>rb)return 0;
      if(dp[la][ra][lb][rb]) return dp[la][ra][lb][rb];
      if(la<=ra)
      {
          maxn=max(maxn,sum-dfs(la+1,ra,lb,rb,sum-a[la]));  //其实就是用总共数的和,减去偶数次抽的数的和。
          maxn=max(maxn,sum-dfs(la,ra-1,lb,rb,sum-a[ra]));
      }
      if(lb<=rb)
      {
          maxn=max(maxn,sum-dfs(la,ra,lb+1,rb,sum-b[lb]));
          maxn=max(maxn,sum-dfs(la,ra,lb,rb-1,sum-b[rb]));
      }
     dp[la][ra][lb][rb]=maxn;
      return maxn;
}
int main()
{
    int n,x;
    cin>>n;
    while(n--)
    {
        memset(dp,0,sizeof(dp));
        int sum=0;
        cin>>x;
        for(int i=1;i<=x;i++)
        {
            cin>>a[i];
            sum+=a[i];
        }
        for(int i=1;i<=x;i++)
        {
            cin>>b[i];
            sum+=b[i];
        }
    cout<<dfs(1,x,1,x,sum)<<endl;
    }
}

 

### 回答1: 首先,从52张牌选择4张牌的组合数为: $${52 \choose 4} = 270725$$ 然后,从剩下的48张牌选择4张牌的组合数为: $${48 \choose 4} = 194580$$ 爱丽丝选择的4张牌可以出现在鲍勃选择的8张牌任意位置,因此可以有以下组合: $${8 \choose 4} = 70$$ 因此,鲍勃包括所有由爱丽丝选择的4张牌的概率为: $$\frac{70}{270725 \times 194580} \approx 0.0000017$$ 换句话说,这种情况发生的概率非常小,几乎可以忽略不计。 ### 回答2: 首先,我们需要计算爱丽丝从52张牌随机选择4张的情况数量:C(52, 4)。其C(n, m)表示从n个元素选择m个的组合数。 接下来,我们需要计算鲍勃从剩下的48张牌随机选择8张的情况数量:C(48, 8)。 爱丽丝选择的4张牌可以在鲍勃选择的8张牌的任何位置出现,所以需要将爱丽丝选择的4张牌插入到鲍勃选择的8张牌:C(8, 4)。 最后,我们需要将以上三个计算结果相乘得到鲍勃得到所有爱丽丝选择的的概率:P = (C(52, 4) * C(48, 8) * C(8, 4)) / C(60, 12)。 其C(60, 12)表示从60张牌选择12张的组合数,因为52张牌有4张是爱丽丝选择的。 最后,将各个组合数进行计算,最终得到的概率P即为所求的结果。 ### 回答3: 首先我们知道,总共有52张牌,我们需要从选出4张作为爱丽丝。所以爱丽丝的选择数是C(52, 4)。 接下来,由于鲍勃选择的是随机的,所以可以等概率地考虑每种可能的情况。鲍勃可以选择剩余的52-4=48张牌任意8张,所以他的选择数是C(48, 8)。 那么鲍勃是否包括了爱丽丝选择的4张牌,我们可以分别考虑每一张是否在鲍勃出现。 第一张是否在鲍勃出现,我们可以看作是从鲍勃的选择选出7张牌来,而不包括爱丽丝的第一张。所以第一张是否在鲍勃出现的概率是C(48-1, 7) / C(48, 8)。 同理,第二张牌是否在鲍勃出现的概率是C(48-2, 7) / C(48, 8),第三张牌是否在鲍勃出现的概率是C(48-3, 7) / C(48, 8),第四张牌是否在鲍勃出现的概率是C(48-4, 7) / C(48, 8)。 最后我们需要计算这四个概率的乘积,即(C(48-1, 7) / C(48, 8))*(C(48-2, 7) / C(48, 8))*(C(48-3, 7) / C(48, 8))*(C(48-4, 7) / C(48, 8))。 最终的结果就是所求的概率。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值