10954 - Add All

水题一发,具体证明见紫书上huffman的讲解

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,x;
    while(~scanf("%d",&n)&&n){
        priority_queue<int, vector<int>, greater<int> > q;
        for(int i=0;i<n;i++) {
            scanf("%d",&x);
            q.push(x);
        }
        int ans = 0;
        for(int i=0;i<n-1;i++) {
            int a = q.top(); q.pop();
            int b = q.top(); q.pop();
            ans += a+b;
            q.push(a+b);
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-25 19:31:35

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.

【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 (全部相加)(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 贪心

贪心   每一次取最小的两个数,注意相加的数也要算' #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

uva10954 - Add All(multiset函数)

题目:10954 - Add All 题目大意:求n个数的和,但是有点不一样的是题目要求计算最少花费.每次两个数相加,得到的那个数就是每次计算的cost. 解题思路:之前没有想到用multiset,自己写好麻烦.要求cost 最小的话,说明每次都将这些数里面最小的两个做相加,这样每次花费都是最小的.用multiset,就可以将每次计算的结果插入这些已经有序的数据中相应的位置,使得这个序列依旧有序. 代码: #include <stdio.h> #include <set> usin

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes