uva 10954 - Add All

点击打开链接uva 10954

题目意思:     有n个数需要求和,每一次求和都要付出和当前和相等的代价,例如1+2 = 3,那么这一次的代价就是3,问我们怎么选择求和的次序才能使得这个代价的总和最小。

解题思路:     1:贪心
                       2:先看一下计算n个数需要几步:
                             n =  1    0
                             n =  2    1
                             n =  3    2
                             .........
                             n =  k     k-1
                       3:由于n个数的总计算次数是一定,所以只要满足每一步的计算都是最小的代价,那么最后的总代价就是最小的。所以我们可以用一个multiset来存储当前的元素,只要每一次都让multiset的前两位相加,然后删除这两位并把和插入multiset,直到multiset的元素个数为1.

代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <stack>
#include <queue>
#include <set>
using namespace std;
#define MAXN 100010

int n , ans;
multiset<int>s;

void solve() {
    int sum  ; ans = 0;
    multiset<int>::iterator it1 , it2;
    while(1){
        if(s.size() == 1) break;
        it1 = s.begin() ; it2 = it1++;
        sum = (*it1)+(*it2);
        s.erase(it1) ; s.erase(it2);
        s.insert(sum);
        ans += sum;
    }
    printf("%d\n" , ans);
}

int main() {
    //freopen("input.txt" , "r" , stdin);
    int a;
    while(scanf("%d" , &n) && n){
        s.clear();
        for(int i = 0 ; i < n ; i++){
            scanf("%d" , &a) ; s.insert(a);
        }
        solve();
    }
    return 0;
}
时间: 2024-09-02 07:46:57

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

链接: 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 wri

UVa 11076 Add Again (组合数学)

11076 - Add Again Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=20 17 Summation of sequence of integers is always a common problem in Computer Science

UVa 10018 Reverse and Add (数学&amp;amp;利克瑞尔数)

10018 - Reverse and AddTime limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=959 The Problem The "reverse and add" method is simple: choose a number,

UVa 10183 How Many Fibs? (统计斐波那契数个数&amp;amp;高精度)

10183 - How Many Fibs? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1124 Recall the definition of the Fibonacci numbers:    f1 := 1    f2 := 2    fn :

UVa 10400

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1341 类型:回溯 原题: A game show in Britain has a segment where it gives its contestants a sequence of positive numbers and a target

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 101 The Blocks Problem 数据结构专题

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=37 题目类型: 数据结构, 二叉树 题意: 有N个位置, 编号为 0-N-1, 初始下,各个位置上放置这和位置编号相同的砖块,即砖块1,砖块2--砖块N-1. 然后有四种命令操作方式: 1.move a onto b :把砖a移动到砖b上面,如果a