hdu6645 Stay Real(19年杭電第六場多校賽第12題)

Stay Real
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0

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
題意: 構建一個小頂堆,每次Sunset 和 Elephant取葉子節點的最大值,問最終兩人的最大分數值。
思路: 有小頂堆性質可知,葉子節點的值一定是最大值,然後不斷取出,即排序後交叉取數,輸出結果即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[1000005];
int main() {
	int t, n;	
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		for (int i = 0; i < n; i++) {
			scanf("%lld", &a[i]);
		}
		ll ans1 = 0, ans2 = 0;
		sort(a, a + n);
		int k = 0, i = n - 1;
		while (i >= 0) {
			if (k % 2 == 0) {
				ans1 += a[i];
				i--;	
			}
			else {
				ans2 += a[i];
				i--;
			}		
			k++;
		}
		printf("%lld %lld\n", ans1, ans2);
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章