UVa 10954:Add All

链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1895

原题:

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.

更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

样例输入:

3

1 2 3

4

1 2 3 4

0

样例输出:

9

19

题目大意:

有一些数字, 要把所有这些数字加起来求它们的总和。 没执行一次加法时,这次加法的结果就是一个花费。可以以任意的数序进行加法,所以不同的顺序花费不同的。求总花费最小是多少。

分析总结:

按照贪心的思路,为了让总花费最少,那么要使得每一次的花费最少,最终结果一定是最少的。

可以用优先队列,这样每次取出来用来相加的两个数一定是队列中最小的两个数,花费也是最少的。

代码:

/*
 * UVa: 10954 - Add All
 * Result: Accept
 * Time: 0.024s
 * Author: D_Double
 */
#include<cstdio>
#include<queue>
#define MAXN 5005
using namespace std;  

priority_queue<long long, vector<long long>, greater<long long> >que;  

int main(){
    int N;
    long long m;  

    while(scanf("%d",&N) && N){
        while(!que.empty()) que.pop();
        for(int i=0; i<N; ++i){
            scanf("%lld",&m);
            que.push(m);
        }
        long long sum=0, ans=0, a, b, t;
        while(!que.empty()){
            a=que.top();
            que.pop();
            if(que.empty()) break;
            b=que.top();
            que.pop();
            sum=a+b;
            ans += sum;
            que.push(sum);
        }
        printf("%lld\n", ans);
    }
    return 0;
}

作者:csdn博客 shuangde800

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索队列
, case
, add
, 加法
, of
problem
,以便于您获取更多的相关知识。

时间: 2024-08-17 16:29:52

UVa 10954:Add All的相关文章

UVa 10954 Add All:哈弗曼树

10954 - Add All Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1895 Yup!! The problem name reflects your task; just add a set of numbers. But you may fe

uva 10954 - Add All

点击打开链接uva 10954 题目意思:     有n个数需要求和,每一次求和都要付出和当前和相等的代价,例如1+2 = 3,那么这一次的代价就是3,问我们怎么选择求和的次序才能使得这个代价的总和最小. 解题思路:     1:贪心                        2:先看一下计算n个数需要几步:                              n =  1    0                              n =  2    1          

UVa 10603:Fill,经典倒水问题+隐式图搜索+dfs

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=110&page=show_problem&problem=1544 类型: 隐式图搜索 原题: There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater th

UVa 10125:Sumsets

题目链接: UVa : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1066 poj :   http://poj.org/problem?id=2549 类型: 哈希, 二分查找 原题: Given S, a set of integers, find the largest d such that a

UVa 1422:Processor 任务处理问题

题目大意:有n个任务送到处理器处理,每个任务信息包括r,d,w,r代表开始时间,w代表必须要结束的时间,w指需要多少时间处理. 其中处理器的处理速度可以变速,问处理器最小需要多大速度才能完成工作? 输入: 3 5 1 4 2 3 6 3 4 5 2 4 7 2 5 8 1 6 1 7 25 4 8 10 7 10 5 8 11 5 10 13 10 11 13 5 8 15 18 10 20 24 16 8 15 33 11 14 14 1 6 16 16 19 12 3 5 12 22 25

UVa 10905:Children&#039;s Game

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1846 类型: 排序 There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will

UVa 10763:Foreign Exchange

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1704 原题: Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very succes

UVa 10341: Solve It

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1282 原题: Solve the equation:        p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0        where 0 <= x <= 1. Input

UVa 10057:A mid-summer night&#039;s dream.

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=998 原题: This is year 2200AD. Science has progressed a lot in two hundred years. Two hundred years is mentioned here because thi