B

来源:互联网 发布:开源笔记软件 编辑:程序博客网 时间:2024/04/30 10:14

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the Nplanks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input
Line 1: One integer N, the number of planks 
Lines 2.. N+1: Each line contains a single integer describing the length of a needed plank
Output
Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts
Sample Input
3858
Sample Output

34


利用Huffman思想,要使总费用最小,那么每次只选取最小长度的两块木板相加,再把这些“和”累加到总费用中即可 46 本题虽然利用了Huffman思想,但是直接用HuffmanTree做会超时,可以用优先队列做(有待学习) 47 因为朴素的HuffmanTree思想是: 48 (1)先把输入的所有元素升序排序,再选取最小的两个元素,把他们的和值累加到总费用 49 (2)把这两个最小元素出队,他们的和值入队,重新排列所有元素,重复(1),直至队列中元素个数<=1,则累计的费用就是最小费用 50  51   HuffmanTree超时的原因是每次都要重新排序,极度浪费时间,即使是用快排。 52  53 一个优化的处理是: 54 (1)只在输入全部数据后,进行一次升序排序  (以后不再排序) 55 (2)队列指针p指向队列第1个元素,然后取出队首的前2个元素,把他们的和值累计到总费用,再把和值sum作为一个新元素插入到队列适当的位置 56      由于原队首的前2个元素已被取出,因此这两个位置被废弃,我们可以在插入操作时,利用后一个元素位置,先把队列指针p+1,使他指向第2个废弃元素的位置, 57      然后把sum从第3个位置开始向后逐一与各个元素比较,若大于该元素,则该元素前移一位,否则sum插入当前正在比较元素 58     (队列中大于等于sum的第一个元素)的前一个位置 59 (3)以当前p的位置作为新队列的队首,重复上述操作
#include <iostream>#include <algorithm>const int M = 2*1e4;using namespace std;int a[M];int main(){int n;cin >> n;for(int i = 0;i < n;i++)cin >> a[i];sort(a,a+n);long long sum = 0;long long ans = 0;for(int i = 0;i < n-1;i++){sum = a[i] + a[i+1];ans += sum;int j;for(j = i+2;j < n;j++){if(a[j] < sum){a[j-1] = a[j];}else {a[j-1] = sum;break;}}if(j == n)a[j-1] = sum;}cout << ans << endl;return 0;}

另一种处理方法是利用STL的优先队列,priority_queue,非常方便简单高效,虽然priority_queue的基本理论思想还是上述的优化思想,但是STL可以直接用相关的功能函数实现这些操作,
#include <iostream>#include <stdio.h>#include <algorithm>#include <cstring>#include <queue>using namespace std;int main(){priority_queue<int,vector<int>,greater<int> > q;int n;cin >> n;int tmp;for(int i = 0;i < n;i ++){cin >> tmp;q.push(tmp);}long long sum = 0;while(q.size()>1){int l1 = q.top();q.pop();int l2 = q.top();q.pop();sum += l1+l2;q.push(l1+l2);}cout << sum << endl;return 0;}

注意的是:这个题目中的优先队列 实现的是 数越小 优先级越高 ,有以下几种来实现:


#include <iostream>#include <stdio.h>#include <algorithm>#include <cstring>#include <queue>using namespace std;struct node {   int a;   friend bool operator < (node a,node b)   {   return a.a > b.a;   }};int main(){priority_queue<node>q;node ch,l1,l2;int n;cin >> n;int tmp;for(int i = 0;i < n;i ++){cin >> tmp;ch.a = tmp;q.push(ch);}long long sum = 0;while(q.size()>1){l1 = q.top();q.pop();l2 = q.top();q.pop();ch.a = l1.a + l2.a;sum += ch.a;q.push(ch);}cout << sum << endl;return 0;}