uva 10954 Add All(排序)

uva 10954 Add All

Yup!! The problem name reflects your task; just add a set of numbers. But you may feel yourselves condescended, to write a C/C++ program just to add a set of numbers. Such a problem will simply question your erudition. So, let’s add some flavor of ingenuity
to it.

Addition operation requires cost now, and the cost is the summation of those two to be added. So, to add
1 and 10, you need a cost of 11. If you want to add
1, 2 and 3. There are several ways –


1 + 2 = 3, cost = 3

3 + 3 = 6, cost = 6

Total = 9


1 + 3 = 4, cost = 4

2 + 4 = 6, cost = 6

Total = 10


2 + 3 = 5, cost = 5

1 + 5 = 6, cost = 6

Total = 11

I hope you have understood already your mission, to add a set of integers so that the cost is minimal.

Input

Each test case will start with a positive number, N (2 ≤ N ≤ 5000) followed by
N positive integers (all are less than 100000). Input is terminated by a case where the value of
N is zero. This case should not be processed.

Output

For each case print the minimum total cost of addition in a single line.

Sample Input                           Output for Sample Input


3

1 2 3

4

1 2 3 4

0

                      

9

19

题目大意:给出n个数,要将n个数相加,每次相加所得的值为当次的计算量,完成所有的求和运算后,要求总的计算量最小。

解题思路:每次都找出最小的两个数相加,记录这个和,然后把和放回原来的数组替换掉之前的两个数,重复这个操作直到剩下一个和,然后把之前记录的和剩下的和全部相加,就是答案。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int num[5005], sum[5005];
int main() {
	int n;
	while (scanf("%d", &n), n) {
		for (int i = 0; i < n; i++) {
			scanf("%d", &num[i]);
		}
		int cnt = 0, cnt2 = 0;
		while (cnt < n - 1) {
			sort(num + cnt, num + n);
			num[cnt + 1] += num[cnt];
			sum[cnt2++] = num[cnt + 1];
			cnt++;
		}
		int Sum = 0;
		for (int i = 0; i < cnt2; i++) {
			Sum += sum[i];
		}
		printf("%d\n", Sum);
	}
	return 0;
}
时间: 2024-11-08 16:42:55

uva 10954 Add All(排序)的相关文章

UVA - 10954 - Add All (贪心)

UVA - 10954 Add All Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem F Add All Input: standard input Output: standard output Yup!! The problem name reflects your task; just add a set of numbers.

【NOIP合并果子】uva 10954 add all【贪心】——yhx

Yup!! The problem name reects your task; just add a set of numbers. But you may feel yourselvescondescended, to write a C/C++ program just to add a set of numbers. Such a problem will simplyquestion your erudition. So, lets add some avor of ingenuity

uva 10954 Add All(哈弗曼编码)

这道题我一开始想错了,这么简单的题都wa了两发...我往贪心上面想了,每次都找一个最小的数相加,结果就是 排序后直接往后加,还在那纳闷为何出错...其实这道题是哈弗曼编码问题,简直是模板题目,就是每次找两个最 小的结点求和后把他们的和放到节点中去,把这两个点删除...用的multiset,其实和set容器差不多,就是可 以存放重复的元素... 代码: #include<iostream> #include<cstdio> #include<cstdlib> #inclu

UVa 10954 Add All 贪心

贪心   每一次取最小的两个数,注意相加的数也要算' #include<cstring> #include<iostream> #include<cstdio> #include<algorithm> #include<string> #include<queue> using namespace std; int main() { long long a[5005],i; long long b[5005],n; priority_

UVA - 10954 Add All (全部相加)(Huffman编码 + 优先队列)

题意:有n(n <= 5000)个数的集合S,每次可以从S中删除两个数,然后把它们的和放回集合,直到剩下一个数.每次操作的开销等于删除的两个数之和,求最小总开销.所有数均小于10^5. 分析:按此操作,最终变成1个数,需要n-1次操作,要想总开销最小,就使每次取出的两数之和最小,优先队列. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstrin

UVA 10954 Add All 全部相加 (Huffman编码)

题意:给你n个数的集合,每次选两个删除,把它们的和放回集合,直到集合的数只剩下一个,每次操作的开销是那两个数的和,求最小开销. 赤果果的Huffman码了.用两个队列类似归并排序,合并一下. #include<bits/stdc++.h> using namespace std; const int maxn = 5010; int q1[maxn]; int q2[maxn]; #define GetMin(x)if(head1>=rear1 || (head2<rear2 &a

UVA - 11076 Add Again (重复元素的排列)

Summation of sequence of integersis always a common problem in Computer Science. Rather than computing blindly,some intelligent techniques make the task simpler. Here you have to find thesummation of a sequence of integers. The sequence is an interes

【uva 10954】Add All(算法效率+Huffman编码+优先队列)

题意:有N个数,每次选2个数合并为1个数,操作的开销就是这个新的数.直到只剩下1个数,问最小总开销. 解法:合并的操作可以转化为二叉树上的操作[建模],每次选两棵根树合并成一棵新树,新树的根权值等于两棵合并前树的根权值和(也与Huffman编码的建立过程类似,选权值最小的两棵树). 这样总开销就是除了叶子结点的权值和  => 每个叶子结点的权值*层数(根节点层数为0)之和  => WPL(树的所有叶子节点的带权路径长度之和,即该节点到根节点路径长度与节点上权的乘积之和). 而Huffman树就

UVa 10954,Add All

Huffman编码简化版,优先队列搞定. 1A 调试的时候发现一个问题..木有想明白...问题代码里给出,哪位大神给解释下. #include <iostream> #include <queue> #define maxn 5000+5 using namespace std; int n; int ans; priority_queue<int, vector<int>, greater<int> >qi; int main() { whil