Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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.
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.
For each test case, print a single line containing two integers S and E, denoting the final score of Sunset and Elephant.
1
3
1 2 3
4 2
给出一个完整的二叉树,父节点上的值小于等于子节点上面的值,每一个节点上都有对应权值,只有叶子节点可以被玩家选中,然后将该节点上的权值加到自己的分数上,该节点被删除。两个玩家开始玩游戏,问每人都尽力获得最大的分数是多少?
要是我们倒着推的话,从最后一个被删除的节点来看,一定是最小的,依次下去就是第二小的,第三小的······所以,我们只需要排序,将奇数位的和偶数位的分别加起来,然后算出总和即可。
#include
using namespace std;
int a[100005];
int main(){
int t;
scanf("%d",&t);
while(t--){
memset(a,0,sizeof(a));
int n;
scanf("%d",&n);
for(int i=0;i