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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章