poj3253

来源:互联网 发布:列宁格勒 知乎 编辑:程序博客网 时间:2024/05/21 09:02

Memory: 284K Time: 500MS

#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
原创粉丝点击