HDOJ-6645(简单题+贪心+树)

Stay Real

HDOJ-6645

由小根堆的性质可以知道,当前最大的值就在叶节点上面,所以只需要排序后依次取就可以了。

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
int n;
long long heap[100005];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>heap[i];
        }
        sort(heap+1,heap+n+1);
        bool flag=true;
        long long s=0,e=0;
        for(int i=n;i>=1;i--){
            if(flag){
                s+=heap[i];
            }else{
                e+=heap[i];
            }
            flag=!flag;
        }
        cout<<s<<" "<<e<<endl;
    }
    return 0;
}
posted @ 2019-08-20 09:20  Garrett_Wale  阅读(88)  评论(0编辑  收藏  举报