Stay Real--2019多校联赛第六场

Problem Description

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 <cstring>
#include <algorithm>
#include <string>
#include <cstdio>
using namespace std;
typedef  long long ll;
int n,m;
int a[100005];

int main()
{
    ios::sync_with_stdio(false);
    ll t;
    scanf("%lld",&t);
    while(t--)
    {
        memset(a,0,sizeof(a));
        ll sum=0,sum1=0,sum2=0;
        int n;
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
           scanf("%d",&a[i]);
            sum+=a[i];
        }
        sort(a,a+n);
        for(int i=0; i<n; i+=2)
        {
            sum1+=a[i];
        }
        sum2=sum-sum1;
        if(sum1>sum2)
        {
            cout<<sum1<<" "<<sum2<<endl;
        }
        else
            cout<<sum2<<" "<<sum1<<endl;
    }                                                                        
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值