水题赚访问量之第二篇~
Huffman编码,优先队列使用。
#include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n; while (cin >> n) { priority_queue < long long, vector<long long>, greater<long long> > q; for (int i = 0; i < n; i++) { long long temp; cin >> temp; q.push(temp); } long long mincost = 0; while (q.size()>1) { long long a = q.top(); q.pop(); long long b = q.top(); q.pop(); q.push(a + b); mincost += a + b; } cout << mincost << endl; } } |