【算法学习笔记】79.STL 优先队列 模拟法 SJTU OJ 4012 合并果子

优先队列自动解决了动态排序问题,非常好用。。。

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int n;
int seeds[10000];
int presum[10000];
priority_queue<int,vector<int>,greater<int> > q;

void init(){
    
    cin>>n;
    for (int i = 0; i < n; ++i){
        int t;
        cin>>t;
        q.push(t);
    }
}

int build(){
    //看错题意结果傻逼了...应该模拟法 数据结构用优先队列....
    int ans = 0;
    while(q.size()>=2){
        int a = q.top(); q.pop();
        int b = q.top(); q.pop();
        q.push(a+b);
        ans += a+b;
    }
    return ans;
}

int main(int argc, char const *argv[])
{
    init();
    cout<<build()<<endl;    
    return 0;
}

 

posted @ 2015-07-22 15:08  雨尘之林  阅读(451)  评论(0编辑  收藏  举报