問題 K: Length of Bundle Rope
阿新 • • 發佈:2020-09-12
問題 K: Length of Bundle Rope
時間限制:2Sec記憶體限制:1024 MB提交狀態
題目描述
Due to the development of online shopping, the logistics industry which is highly connectedwith goods shipping has been so prosperous that the great amount of employees is needed.Therefore, Alex, a truck driver in this growing industry, was supposed to transport severalparcels scattering in the warehouse to other cities in his daily routine.
According to the official safety requirements to the trucks running in the highway, Alex hadto tie up all the packages tightly so that he could settle the goods safely on his truck. Alexknew that the length of the cords needed for bundling the packages on the truck was based onthe size of the packages themselves. Also, n packages can be tied up well after n − 1 bundles.
Moreover, when bundling goods, Alex could only bundle two packages at one time to avoidscattering. Since the daily consumption of the cord was great and Alex was supposed to pay for it, he hopes to bundle all the goods with the shortest cord.
For example, there are 4 parcels in the size of 8, 5, 14, and 26 respectively. If Alex binds thefirst two together, the needed rope will be in the length of 13( 8+5 = 13) while the needed ropefor the latter two packages will be 40 (14 + 26 = 40). If Alex keeps bundling these two items,the rope length he needs will be 53 (13 + 40 = 53). So the total length of the 4 packages will
be 106 (13 + 40 + 53 = 106). If Alex tries another way by bundling the first two( 8 + 5 = 13),adding up the third one (13 + 14 = 27), and then bundling the last item (27 + 14 = 53), he willonly need the cord in the length of 93 (13 + 27 + 53 = 93). Now your mission is to help Alexfinding the minimum length of the needed cord.
輸入
輸出
For each test case, output the minimum length of the bundle rope required to tie all parcelstogether in one line.樣例輸入Copy
4
6
2 3 4 4 5 7
5
5 15 40 30 10
10
3 1 5 4 8 2 6 1 1 2
9
3 2 1 6 5 2 6 4 3
樣例輸出 Copy
63
205
100
98
提示
• 1 ≤ T ≤ 10• 1 ≤ n ≤ 1000
• The size of each parcel is at most 1000.
#include <iostream> #include <queue> #include <algorithm> using namespace std; int t, n; int main(){ scanf("%d", &t); while(t -- ){ scanf("%d", &n); priority_queue<int ,vector<int>, greater<int>> q; while (n -- ){ int x; scanf("%d", &x); q.push(x); } long long res = 0; while (q.size() > 1){ int x = q.top(); q.pop(); int y = q.top(); q.pop(); res += x + y; q.push(x + y); } printf("%lld\n", res); } return 0; }