STL -- 優先佇列
阿新 • • 發佈:2019-01-09
STL真是個好東西
題目描述
在一個果園裡,多多已經將所有的果子打了下來,而且按果子的不同種類分成了不同的堆。多多決定把所有的果子合成一堆。
每一次合併,多多可以把兩堆果子合併到一起,消耗的體力等於兩堆果子的重量之和。可以看出,所有的果子經過 n-1n−1 次合併之後, 就只剩下一堆了。多多在合併果子時總共消耗的體力等於每次合併所耗體力之和。
因為還要花大力氣把這些果子搬回家,所以多多在合併果子時要儘可能地節省體力。假定每個果子重量都為 11 ,並且已知果子的種類 數和每種果子的數目,你的任務是設計出合併的次序方案,使多多耗費的體力最少,並輸出這個最小的體力耗費值。
樣例
輸入 輸出
3 1 2 9 15
#include <bits/stdc++.h> using namespace std; inline int read() { int s = 0, w = 1; char ch = getchar(); while(!isdigit(ch)) { if(ch == '-') w = -1; ch = getchar(); } while(isdigit(ch)) { s = (s << 1) + (s << 3) + ch - '0'; ch = getchar();} return s * w; } #define MAXX 11000 int n; int a[MAXX]; int res = 0; priority_queue<int,vector<int>,greater<int> >q; int main() { n = read(); for(int i = 1; i <= n; ++i) { a[i] = read(); q.push(a[i]); } while(q.size() >= 2) { int a = q.top(); q.pop(); int b = q.top(); q.pop(); res += (a + b); q.push(a + b); } printf("%d\n", res); return 0; }