poj3253
来源:互联网 发布:列宁格勒 知乎 编辑:程序博客网 时间:2024/05/21 09:02
Memory: 284K Time: 500MS
Memory: 488K Time: 79MS
#include <iostream>using namespace std;int n;int l[20001];int main() {while(~(scanf("%d", &n))) {for(int i=0; i<n; i++)cin >> l[i];int len = n;long long ans = 0;while(len > 1) {int m1 = 0, m2 = 1;if(l[m1] > l[m2])swap(m1, m2);for(int i=2; i<len; i++) {if(l[i] < l[m1]) {m2 = m1;m1 = i;} else if(l[i] < l[m2]) {m2 = i;}}int t = l[m1] + l[m2];ans += t;if(m1 == len - 1)swap(m1, m2);l[m1] = t;l[m2] = l[len-1];len --;}cout << ans << endl;}return 0;}
Memory: 488K Time: 79MS
#include <iostream>#include <algorithm>#include <queue>using namespace std;int n;int l[20001];int main() {while(~(scanf("%d", &n))) {priority_queue<int, vector<int>, greater<int> > q;for(int i=0; i<n; i++) {cin >> l[i];q.push(l[i]);}long long ans = 0;while(q.size() > 1) {int m1, m2;m1 = q.top();q.pop();m2 = q.top();q.pop();int t = m1 + m2;ans += t;q.push(t);}cout << ans << endl;}return 0;}
0 0
- poj3253
- POJ3253
- poj3253
- poj3253
- poj3253
- poj3253
- POJ3253
- poj3253
- POJ3253
- poj3253
- poj3253
- POJ3253
- poj3253
- poj3253
- poj3253
- poj3253
- poj3253
- poj3253
- Android进程间通信之--Socket
- 风口的猪-中国牛市(小米2016校招)
- 基于bootstrap和php的项目
- gcc 编译过程
- C++ 字符char相关操作
- poj3253
- merge into和on duplicate key
- 230. Kth Smallest Element in a BST
- 开发 - kafka的一次小坑
- javascript之location对象
- 凸优化学习笔记(1)——凸集
- 天梯赛 L2-012. 关于堆的判断 数据结构
- 直观解释Gamma分布和Poisson分布
- 小京东版权修改