10954 - Add All

Time limit: 3.000 seconds

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 12 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.

样例输入:

3

1 2 3

4

1 2 3 4

0

 

样例输出:

9

19

题意:

给定n个数,任意两数相加的花费为相加的和,和放入队中继续相加,求全部加完时的最小值。

定义优先队列最小值优先,队首两元素相加并分别出队,总值+=和,若队列此时不为空,则将和入队,继续循环直到队列为空。

附AC代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std; struct cmp{//重载“()”运算符,自定义最小值优先
bool operator()(int &a,int &b){
return a>b;
}
}; int n,a; int main(){
priority_queue<int,vector<int>,cmp> q; //定义优先队列q
while(~scanf("%d",&n)&&n!=){
for(int i=;i<n;i++){//入队
scanf("%d",&a);
q.push(a);
}
int sum=;
while(q.size()){//当队列不为空时
int x=q.top();//最小的两个相加并出队
q.pop();
int y=q.top();
q.pop();
sum+=x+y;//总和
if(q.size())//若加完队列不为空,则将和入队
q.push(x+y);
}
printf("%d\n",sum);
}
return ;
}

UVa-10954的更多相关文章

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

    Yup!! The problem name reects your task; just add a set of numbers. But you may feel yourselvesconde ...

  2. UVa 10954 (Huffman 优先队列) Add All

    直接用一个优先队列去模拟Huffman树的建立过程. 每次取优先队列前两个数,然后累加其和,把这个和在放入到优先队列中去. #include <cstdio> #include <q ...

  3. UVA 10954 Add All 哈夫曼编码

    题目链接: 题目 Add All Time Limit:3000MS Memory Limit:0KB 问题描述 Yup!! The problem name reflects your task; ...

  4. UVA 10954 Add All

    题意: 给出n个数,要将n个数相加,每次相加所得的值为当次的计算量,完成所有的求和运算后,要求总的计算量最小. 分析: 直接一个优先队列,由小到大排序,每次前两个相加就好. 代码: #include ...

  5. UVa 10954,Add All

    Huffman编码简化版,优先队列搞定. 1A 调试的时候发现一个问题..木有想明白...问题代码里给出,哪位大神给解释下. #include <iostream> #include &l ...

  6. UVa 10954 全部相加(Huffman编码)

    https://vjudge.net/problem/UVA-10954 题意:有n个数的集合S,每次可以从S中删除两个数,然后把它们的和放回集合,直到剩下一个数.每次操作的开销等于删除的两个数之和, ...

  7. 8-11 Add All uva 10954

    有n n小于等于五千 个数的集合s  每次可以从s中删除两个数 然后把他们的和放回集合 直到剩下一个数 每次操作的开销等于删除两个数二之和  求最小总开销 思路:就是每次取最小的两个数即可 用优先级队 ...

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

    题意:给你n个数的集合,每次选两个删除,把它们的和放回集合,直到集合的数只剩下一个,每次操作的开销是那两个数的和,求最小开销. Huffman编码.Huffman编码对于着一颗二叉树,这里的数对应着单 ...

  9. UVa 10954 Add All(优先队列)

    题意  求把全部数加起来的最小代价  a+b的代价为(a+b) 越先运算的数  要被加的次数越多  所以每次相加的两个数都应该是剩下序列中最小的数  然后结果要放到序列中  也就是优先队列了 #inc ...

  10. 紫书 例题8-11 UVa 10954 (优先队列)

    解法和合并果子是一样的, 每次取最小的两个, 更新答案, 加入队列 #include<cstdio> #include<queue> #define REP(i, a, b) ...

随机推荐

  1. HTML5开发移动web应用—JQuery Mobile(1)

    JQuery Mobile是一个简单易用的web移动app开发框架.使用它就像使用jQuery一样,引入必要的文件就可以. 最基础的jQuery Mobile文件的结构代码例如以下: <body ...

  2. caffe搭建--ubuntu标准平台的搭建

    http://caffe.berkeleyvision.org/install_apt.html Ubuntu Installation General dependencies sudo apt-g ...

  3. GuozhongCrawler系列教程 (1) 三大PageDownloader

    GuozhongCrawler  QQ群 202568714 教程源代码下载地址:http://pan.baidu.com/s/1pJBmerL GuozhongCrawler内置三大PageDown ...

  4. Java中的class类的cast方法和asSubclass方法

    一般来说cast是转型的意思,但是学java的时间也不短了,class类居然还有cast这个方法,这里来学习一下这个cast有何用. 第一次看到这个cast是在Spring的源码中, spring-f ...

  5. Nexus 5更新 Android5.0 失败解决方法

    Android 5.0最终推出了正式版,今天也及时刷到了Android5.0 (LRX21O),官方链接:https://developers.google.com/android/nexus/ima ...

  6. 1367: [Baltic2004]sequence

    1367: [Baltic2004]sequence Time Limit: 20 Sec  Memory Limit: 64 MB Submit: 1090  Solved: 432 [Submit ...

  7. 负载均衡实现,一个域名对应多个IP地址

    负载均衡实现,一个域名对应多个IP地址 - 宏宇 - 博客园 https://www.cnblogs.com/cuihongyu3503319/archive/2012/07/09/2583129.h ...

  8. ALTER USER USER() IDENTIFIED BY 'password';

    [root@test mysql]# bin/mysqld 2018-08-04T14:09:33.831318Z 0 [Warning] [MY-011070] [Server] 'Disablin ...

  9. mybatis入门(五)

    根据用户名称模糊查询用户信息 @Test public void findUserByNameTest() throws IOException { // 通过工厂得到SqlSession SqlSe ...

  10. shell---rpm

    [root@master src]# rpm -qpl epel-release-latest-6.noarch.rpm         ##查询该rpm包安装了什么warning: epel-rel ...