N 块木板(N≤20000),长度 L_i。每次合并两块,代价为长度和。求合并成一块的最小总代价。
每次取最小两块合并,放回队列。用最小堆(优先队列)实现。
#include#include #include using namespace std; int main() { int N; while (scanf("%d", &N) == 1) { priority_queue , greater > pq; for (int i=0;i 1) { int a = pq.top(); pq.pop(); int b = pq.top(); pq.pop(); ans += a + b; pq.push(a + b); } printf("%lld\n", ans); } return 0; }