2 条题解
-
0
int main() {int n; cin >> n; /* 创建最小堆名为pq。*/ priority_queue<int, vector<int>, greater<int> > pq; for (int i = 0; i < n; ++i) { int ai; cin >> ai; pq.push(ai); } long totalLength = 0; /*当仅剩一个纸条时结束。*/ while (pq.size() > 1) { int min1 = pq.top(); pq.pop(); int min2 = pq.top(); pq.pop(); int newLength = min1 + min2; totalLength += newLength; /*将生成的新纸条压入。*/ pq.push(newLength); } cout << totalLength << endl; return 0; }
信息
- ID
- 939
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 9
- 标签
- 递交数
- 260
- 已通过
- 16
- 上传者