1 3 1 2 9
15
本题有点类似huffman编码的前期步骤,每次选择耗费体力最小的两个值合并然后把合并后的值插入优先权队列中
#include <iostream> #include <queue> #include <vector> using namespace std; struct cmp{ bool operator()(long long x,long long y){ return x > y; } }; int main(){ int N; cin >>N; while(N--){ int n ; cin >> n; priority_queue<long long,vector<long long>, cmp> p; for(int i = 0 ; i < n; ++ i){ int a; cin >> a; p.push(a); } long long sum = 0; while(p.size()>1){ long x = p.top();p.pop(); long y = p.top();p.pop(); sum +=(x+y); p.push(x+y); } p.pop(); cout<<sum<<endl; } }
原文:http://www.cnblogs.com/xiongqiangcs/p/3675056.html