题意:根据那个递推式,找找规律即可。
代码:
#include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace std; typedef long long ll; inline bool rd(int &ret) { char c; int sgn; if(c = getchar() , c == EOF) return false; while(c != ‘-‘ && (c < ‘0‘ || c > ‘9‘)) c = getchar(); sgn = (c == ‘-‘) ? -1 : 1; ret = (c == ‘-‘) ? 0 : (c - ‘0‘); while(c = getchar(), c >= ‘0‘ && c <= ‘9‘) ret = ret * 10 + (c - ‘0‘); ret *= sgn; return true; } priority_queue<ll,vector<ll>, greater<ll> > q; int main() { int T; scanf("%d", &T); while (T-- > 0) { int n; rd(n); while (!q.empty()) q.pop(); for (int i = 0; i < n; ++i) { int x; rd(x); q.push(x); } ll ans = 0; while (!q.empty()) { ll x = q.top(); q.pop(); if (q.empty()) break; ll y = q.top(); q.pop(); ans += x + y; q.push(x + y); } printf("%I64d\n", ans); } return 0; }
【哈弗曼树】HDU 5350 MZL's munhaff function
时间: 2024-10-14 03:22:42