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.
Sample Input
1 3 1 2 3
Sample Output
4 2
本场的签到题,题意是从小根堆里轮流找最大的叶子节点给S和E,美取一个就删除本叶子节点。
然后模拟一遍发现其实就是排序从大到小取就行了。
#include
using namespace std;
#define ll long long
const int MAXN = 1e5 + 7;
int arr[MAXN];
bool cmp(int a, int b)
{
return a>b;
}
int main()
{
ios::sync_with_stdio(0);//不要放在循环里面
int t, n;
ll s, e;//会爆int
cin>>t;
//scanf("%d", &t);
while(t--)
{
cin>>n;
//scanf("%d", &n);
s = e = 0;
for(int i = 0; i < n; i++)
{
cin>>arr[i];
//scanf("%d", &arr[i]);
}
sort(arr, arr+n, cmp);
for(int i = 0; i < n; i++)
{
if(i%2 == 0) s += arr[i];
else e += arr[i];
}
cout<