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

Source

Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 4. Algorithm Design

Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Data Structures and Libraries :: Non Linear Data Structures with Built-in Libraries :: C++
STL priority_queue (Java PriorityQueue)

Root :: Prominent Problemsetters :: Md. Kamruzzaman (KZaman)

Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Data Structures and Libraries :: Non Linear Data Structures with Built-in Libraries :: C++
STL priority_queue (Java PriorityQueue)

Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 8. Algorithm Design :: Examples

Submit Status

思路:每次加两个最小的,然后将两个最小的删去,将和插入到剩余的数中,直到只剩一个数

AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;

int N;
LL a[5005];

int main() {
	while(scanf("%d", &N), N) {
		for(int i = 0; i < N; i++) {
			scanf("%lld", &a[i]);
		}
		sort(a, a + N);
		LL ans = 0;
		for(int i = 0; i < N - 1; i++) {
			LL tmp = a[i + 1] + a[i];
			ans += tmp;
			a[i + 1] = tmp;
			if(i < N - 2) {
				int t = i + 2;
				while(t <= N - 1 && a[t] < tmp) {
					a[t - 1] = a[t];
					a[t] = tmp;
					t++;
				}
			}
		}
		//for(int i = 0; i < N; i++) printf("%d ", a[i]);
		printf("%lld\n", ans);
	}
	return 0;
}
时间: 2024-11-05 20:32:08

UVA - 10954 - Add All (贪心)的相关文章

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(排序)

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

【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 全部相加 (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 10317 - Equating Equations 贪心 dfs

UVA 10317 - Equating Equations 贪心 dfs ACM 题目地址:UVA 10317 - Equating Equations 题意: 给一个等式,但是这个等式不一定是正确的,要你对等式中的数字重新排序,使得等式成立.等式只有+和-,数字个数小于16. 分析: 以a + b - c = d - e为例子. 1. 我们把等式右边的各项都换到左边,a + b - c - d + e = 0 2. 把+项和-项放一起,就变成(a + b + e) - (c + d) = 0

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

UVA 12130 - Summits(BFS+贪心)

UVA 12130 - Summits 题目链接 题意:给定一个h * w的图,每个位置有一个值,现在要求出这个图上的峰顶有多少个.峰顶是这样定义的,有一个d值,如果一个位置是峰顶,那么它不能走到不大于该峰顶高度 - d的位置,如果满足这个条件下,并且无法走到更高的山峰,那么它就是峰顶 思路:利用贪心的策略,把所有点丢到优先队列,每次取出最高的峰值开始找,进行广搜,搜的过程中记录下最大值的点的个数,如果这个是峰顶,就加上这个数.判断是不是峰顶的方法为,如果广搜过程中,不会找到一个点的能到的最高峰