UVa 10954 - Add All

传送门UVa 10954 - Add All


题意:找出最小的sum,使过程和最小。


一开始我把它们从小到大排序,认为这样就最小了。。但是我没有考虑例如这样的数据:

6 6 7 11.

一直加下去是61,而如果先加前两个,再加7和11,最后加剩下的两个,这样算出来是60。

原因就是当加上前两个数字之后,变成了7 11 12,这时候应该加7和11的,而我加了7和12,此处多了1.


然后就是上优先队列了。。


#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;

int main()
{
	//freopen("input.txt", "r", stdin);
	priority_queue<int, vector<int>, greater<int> > qu;
	int n, i, j, temp;
	while (scanf("%d", &n), n)
	{
		while (!qu.empty())
			qu.pop();
		for (i = 0; i < n; i++)
		{
			scanf("%d", &temp);
			qu.push(temp);
		}
		int sum = 0;
		while (qu.size() != 1)
		{
			int t = qu.top();
			qu.pop();
			int k = qu.top();
			qu.pop();
			sum += k + t;
			qu.push(k + t);
		}
		printf("%d\n", sum);
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值