首页 > 其他 > 详细

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

时间:2019-08-20 11:01:43      阅读:107      评论:0      收藏:0      [点我收藏+]

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;
}

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

原文:https://www.cnblogs.com/GarrettWale/p/11381022.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!