zoukankan      html  css  js  c++  java
  • HDU

    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(2in) -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(1T10000) , denoting the number of test cases.

    In each test case, there is one integer n(1n100000) in the first line, denoting the number of nodes.

    In the second line, there are n integers h1,h2,...,hn(1hi109,hi2hi) , denoting the key of each node.

    It is guaranteed that n106 .
     
    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
     
    Source
    思路:一开始没看对题,忽略了子节点的值一定不比父节点的小,后来才看懂,明白他是个小顶堆...
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <cmath>
    #include <cstdlib>
    #include <queue>
    #include <stack>
    #include <vector>
    using namespace std;
    #define MAXN 100010
    #define ll long long
    
    int t, n;
    ll h[100000 + 8], sums, sume;
    
    int main()
    {
        scanf("%d", &t);
        while(t--)
        {
            sume = 0;
            sums = 0;
            scanf("%d", &n);
            for(int i = 0; i < n; i++)
                scanf("%lld", &h[i]);
            sort(h, h+n, greater<ll>());
            for(int i = 0; i < n; i += 2)
                sums += h[i];
            for(int i = 1; i < n; i += 2)
                sume += h[i];
            printf("%lld %lld
    ", sums, sume);
        }
        return 0;
    }
  • 相关阅读:
    名门暗战
    redis安装相关下载
    Git:git diff 命令详解
    【教程】Win7-64位安装OpenSSL详细过程
    Linux 下MQ的安装和配置亲测
    用命令创建MySQL数据库
    WebSphere MQ中的CCSID
    Netty:option和childOption参数设置说明
    BeanNameAware接口和BeanFactoryAware接口
    再理解tcp backlog
  • 原文地址:https://www.cnblogs.com/RootVount/p/11357376.html
Copyright © 2011-2022 走看看