首页 > 其他 > 详细

贪心//优先队列

时间:2017-04-25 22:11:30      阅读:204      评论:0      收藏:0      [点我收藏+]
n堆苹果,每次花费的体力是两堆苹果的总和,问花费的最少体力。
优先队列,很好处理。
#include <bits/stdc++.h>
using namespace std;

int main()
{
    priority_queue<int,vector<int>,greater<int> > que; 
    int n,t;
    int sum=0;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>t;
        que.push(t);
    }
    for(int i=0;; i++)
    {
        int s1=0,s2=0;
        s1 = que.top();
        //cout<<s1<<" ";
        que.pop();
        if(que.empty())
        {
            que.push(s1);
            break;
        }
        s2 = que.top();
        que.pop();
        int s = s1+s2;
        sum+=s;
        if(que.empty())
        {
            que.push(s);
            break;
        }
        que.push(s);
        if(que.empty())
        {
            que.push(s1);
            break;
        }
    }
    cout<<sum<<endl;


    return 0;
}

 

贪心//优先队列

原文:http://www.cnblogs.com/upstart/p/6764476.html

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