Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
这个应该不用构造哈夫曼树,每次找两个最小的就行#include <iostream> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <string> #include <cstdio> #include <climits> #include <queue> #include <map> #include <list> #include <vector> using namespace std; struct cmp { bool operator()(long long a, long long b) { return a > b; } }; int main() { priority_queue<long long, vector<long long>, cmp> pq; int n; scanf("%d", &n); while (n--) { int t; scanf("%d", &t); pq.push(t); } long long sum = 0; while (pq.size() != 1) { long long a = pq.top(); pq.pop(); long long b = pq.top(); pq.pop(); sum += a + b; pq.push(a+b); } printf("%lld\n", sum); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator