Stay Real(HDU-6645)

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

题意:t 组数据,每组给出 n 个数,这 n 个数放在一个小根堆里,对于每个点来说,只有拿完其子结点才能拿其父结点,现在有S、E 两个人轮流拿,最后谁拿的结点的和大谁获胜,两人都希望能最大化自己的分数,输出两人的得分

思路:

看似是一个博弈题,但仔细想一下其实是一个贪心

首先,所有的数都存放在小根堆里,那么可知,叶结点的值一定是最大值

由于两人轮流取数,每个人都希望自己的得分尽量大,那么每个人都会取叶结点

因此,题目本质就是将所有数排序后,两人交叉取数,然后累加结果最后输出即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-10;
const int MOD = 998244353;
const int N = 1000000+5;
const int dx[] = {-1,1,0,0,1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

LL a[N];
bool cmp(LL a, LL b) {
    return a > b; 
}
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
            scanf("%lld", &a[i]);
        sort(a + 1, a + 1 + n, cmp);

        LL sumS = 0, sumE = 0;
        for (int i = 1; i <= n; i++) {
            if (i % 2)
                sumS += a[i];
            else
                sumE += a[i];
        }
        printf("%lld %lld\n", sumS, sumE);
    }
}
发布了1871 篇原创文章 · 获赞 702 · 访问量 194万+

猜你喜欢

转载自blog.csdn.net/u011815404/article/details/102497079
今日推荐