Stay Real

 In computer science, a heap is a specialized tree-based data structure which is essentially an almost complete tree that satisfies the heap property: in a min heap, for any given node C, if P is a parent node of C, then the key(the value) of P is less than or equal to the key of C. The node at the ``top'' of the heap(with no parents) is called the root node.

Usually, we may store a heap of size n in an array h1,h2,…,hn, where hi denotes the key of the i-th node. The root node is the 1-th node, and the parent of the i(2≤i≤n)-th node is the ⌊i2⌋

-th node.

Sunset and Elephant is playing a game on a min heap. The two players move in turns, and Sunset moves first. In each move, the current player selects a node which has no children, adds its key to this player's score and removes the node from the heap.

The game ends when the heap is empty. Both players want to maximize their scores and will play optimally. Please write a program to figure out the final result of the game.

Input

The first line of the input contains an integer T(1≤T≤10000)

, denoting the number of test cases.

In each test case, there is one integer n(1≤n≤100000) in the first line, denoting the number of nodes.

In the second line, there are n integers h1,h2,...,hn(1≤hi≤109,h⌊i2⌋≤hi), denoting the key of each node.

It is guaranteed that ∑n≤106

.

Output

For each test case, print a single line containing two integers S

and E

, denoting the final score of Sunset and Elephant.

Sample Input

1
3
1 2 3

Sample Output

4 2

 题意:一人拿一个数,怎样才能让第一个人拿的和最大

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
using namespace std;
typedef long long ll;
const int idata=1e6+5;

ll n,m,t,_;
int i,j,k;
int cnt,num,ans;
int minn,maxx;
stack<int>stk;
priority_queue<ll,vector<ll>,less<ll> >q;
map<ll,int>mp;
ll a[idata];

int main()
{
    /*cin.tie(0),cout.tie(0);
    iostream::sync_with_stdio(false);*/
    rush()
    {
        scanf("%d",&n);
        for(i=0;i<n;i++){
            scanf("%d",a+i);
        }
        sort(a,a+n);
        int num=0;
        ll sun=0,ele=0;
        for(i=n-1;i>=0;i--){
            num++;
            if(num & 1) sun+=a[i];
            else ele+=a[i];
        }
        printf("%lld %lld\n",sun,ele);

        //超时代码
        /*scanf("%d",&n);
        while(n--){
            scanf("%d",&_);
            q.push(_);
        }
        num=0;
        ll sun=0,ele=0;
        while(!q.empty()){
            num++;
            if(num & 1){
                sun+=q.top();
                q.pop();
            }
            else{
                ele+=q.top();
                q.pop();
            }
        }
        printf("%lld %lld",sun,ele);*/
    }
    return 0;
}
原创文章 410 获赞 16 访问量 3万+

猜你喜欢

转载自blog.csdn.net/C_Dreamy/article/details/106029006