TJU ACM Problem 【4158】

Alice and Bob like game. They often play the game together.
Today, they are playing a card game. At first, there are N N 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 remaning N1 N−1 cards.
And then Alice choose one card from the first card and the last card of the remaning N2 N−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 N N 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 T T, indicating the number of test cases. (1T10) (1≤T≤10)
Each case contains two lines, the first line contains one integer N N, means the number of cards. (1N20) (1≤N≤20)
The second contains N N positive numbers means the point on the card. the point on the card is less than 104 104

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值