UVA 10954- Add All(优先队列)

Add All

Time Limit:3000MS     Memory Limit:0KB     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. 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.

Sample Input                           Output for Sample Input


3

1 2 3

4

1 2 3 4

0

                      

9

19

 


Problem setter: Md. Kamruzzaman, EPS

题意:有n个数的集合s,每次可以从s中删除两个数,然后把他们的和放回集合,直至剩下一个数。每次的开销等于删除的两个数的和,求最小总开销。

思路:使用从小到大排序的优先队列。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
const int inf=0x3f3f3f3f;
int main()
{
    int n,i,a;
    int sum;
    while(~scanf("%d",&n)){
        if(n==0) break;
        priority_queue<int ,vector<int >,greater<int > > q;
        while(n--){
            scanf("%d",&a);
            q.push(a);
        }
        sum=0;
        while(!q.empty()){
            int x=q.top();
            q.pop();
            int y=q.top();
            q.pop();
            sum+=x+y;
            if(!q.empty())
            q.push(x+y);
        }
        printf("%d\n",sum);
    }
}
时间: 2025-01-05 07:26:55

UVA 10954- Add All(优先队列)的相关文章

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

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.

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

【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 贪心

贪心   每一次取最小的两个数,注意相加的数也要算' #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个数的集合,每次选两个删除,把它们的和放回集合,直到集合的数只剩下一个,每次操作的开销是那两个数的和,求最小开销. 赤果果的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 10954 Add All(哈弗曼编码)

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

uva 10537 Toll! Revisited(优先队列优化dijstra及变形)

Toll! Revisited 大致题意:有两种节点,一种是大写字母,一种是小写字母.首先输入m条边,当经过小写字母时需要付一单位的过路费,当经过大写字母时,要付当前财务的1/20做过路费.问在起点最少需要带多少物品使到达终点时还有k个物品.当有多条符合条件的路径时输出字典序最小的一个. 思路:已知终点的权值,那么可以从终点向前推.求终点到起点的最短路径,然后按字典序打印路径. 比较难处理的是:向前推时前驱节点的权值计算.列个方程算算就可以了,主要时不能整除的情况. 计算前驱结点dis值的时候,

UVA - 501 Black Box (优先队列或vector)

Description  Black Box  Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empty and i equals 0. This Black Box processes a sequence of commands (transactions).

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