TJU ACM Problem 【4158】

本文介绍了一个两人卡片游戏的策略分析,Alice 和 Bob 轮流从剩余卡片中选择两端的卡片来获取分数,目的是获得比对方更高的总分。文章提供了一个程序实现,用于计算最优策略下 Alice 和 Bob 的得分差。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Alice and Bob like game. They often play the game together.
Today, they are playing a card game. At first, there are NN cards on table. Each cards have a number. The rules of this game is follow:
First, Alice choose one card from the first card and the last card.
And then Bob choose one card from the first card and the last card of the remaningN1N−1 cards.
And then Alice choose one card from the first card and the last card of the remaningN2N−2 cards.
...end until there is no card.
they can get points from the number on the cards. The winner whose point is most.
For example, there are 3 cards, 3 7 5. First, Alice can choose 3 and 5. Alice choose 5, and then Bob can choose 3 and 7, Bob choose 7, and then Alice choose 3. The point of Alice is 8, and point of Bob is 7. So Alice is winner, and the difference of points is 1.
Now, give you NN cards. We can assume that Alice and Bob are very smart, you need tell us the difference of the point of Alice and Bob.

Input

The input consists of multiple test cases. The first line contains an integer TT, indicating the number of test cases.(1T10)(1≤T≤10)
Each case contains two lines, the first line contains one integer NN, means the number of cards.(1N20)(1≤N≤20)
The second contains NN positive numbers means the point on the card. the point on the card is less than104104

Output

For each case output the difference of points of Alice and Bob

Sample Input

3
3
3 10 5
4
3 7 8 5
4
1 1 1 1

Sample Output

-2
1
0
#include <iostream>
#include <string>
#include<cstdlib>
using namespace std;

int main()
{
   int t,n,i;
   int a[20];
   cin>>t;//用例组数
   int beg,fin;
   int alice,bob;
   while(t--)
   {
       alice=0;
       bob=0;
       cin>>n;
       beg=0;
       fin=n-1;
       for(i=0;i<n;i++)
        cin>>a[i];
        for(i=1;i<=n;i++)
        {
            if(beg<=fin)
            {
                if(i%2!=0)//alice选择
                {
                    if(a[beg]>=a[fin])
                    {
                        alice+=a[beg];
                        beg++;
                    }
                    else if(a[beg]<=a[fin])
                    {
                        alice+=a[fin];
                        fin--;
                    }
                }
                else//bob选
                {
                     if(a[beg]>=a[fin])
                    {
                        bob+=a[beg];
                        beg++;
                    }
                    else if(a[beg]<=a[fin])
                    {
                        bob+=a[fin];
                        fin--;
                    }
                }
            }
        }
        cout<<alice-bob<<endl;
   }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值