Fence Repair
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 25274   Accepted: 8131

Description

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

3
8
5
8

Sample Output

34

Hint

He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8. 

The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into
16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).

Source

解题思路:

对所输入的数据构造Huffman树,当中非叶子节点的值加起来的和为所求。

Huffman树的构造过程: 每次取全部数中的两个最小数,相加起来得到一个非叶子节点的值,下次取的时候曾经取过的两个最小数数不能再取。再相加得到非叶子节点的值能够取。

比方 3 , 4 。 5  。先取  3 ,4 相加为 7 。 然后取 5 ,7(3,4取过不能再取了),相加为12,    7+12 =19,即为所求。

优先队列能够非常好的实现这个操作。

定义int类型优先级从小到大的代码为:

priority_queue<int ,vector<int>,greater<int> > q

代码:

#include <iostream>
#include <queue>
using namespace std;
int main()
{
priority_queue<int ,vector<int>,greater<int> > q;
int n;
int num;
cin>>n;
while(n--)
{
cin>>num;
q.push(num);
}
long long sum=0; while(!q.empty())
{
int n1=q.top();
q.pop();
int n2=q.top();
q.pop();
sum+=n1+n2;
if(q.empty())
break;
q.push(n1+n2);
} cout<<sum<<endl;
return 0;
}

[ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)的更多相关文章

  1. poj 3253 Fence Repair (贪心,优先队列)

    Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  2. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

  3. poj 3253 Fence Repair 优先队列

    poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...

  4. POJ 3253 Fence Repair (优先队列)

    POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...

  5. poj 3253 Fence Repair(优先队列+huffman树)

    一个很长的英文背景,其他不说了,就是告诉你锯一个长度为多少的木板就要花多少的零钱,把一块足够长(不是无限长)的木板锯成n段,每段长度都告诉你了,让你求最小花费. 明显的huffman树,优先队列是个很 ...

  6. poj 3253 Fence Repair(优先队列+哈夫曼树)

    题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...

  7. poj 3253 Fence Repair(模拟huffman树 + 优先队列)

    题意:如果要切断一个长度为a的木条需要花费代价a, 问要切出要求的n个木条所需的最小代价. 思路:模拟huffman树,每次选取最小的两个数加入结果,再将这两个数的和加入队列. 注意priority_ ...

  8. POj 3253 Fence Repair(修农场栅栏,锯木板)(小根堆 + 哈弗曼建树得最小权值思想 )

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28359   Accepted: 9213 Des ...

  9. POJ 3253 Fence Repair(哈夫曼树)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26167   Accepted: 8459 Des ...

随机推荐

  1. python基础学习笔记——迭代器

    我们之前一直在用可迭代对象进行操作,那么到底什么是可迭代对象.我们现在就来讨论讨论可迭代对象.首先我们先回顾下我们 熟知的可迭代对象有哪些: str  list   tuple  dic  set  ...

  2. win7定时关机

    菜单>附件>系统工具>任务计划程序>创建基本任务 alt+r>cmd>shutdown/? 查看相关参数 /l 注销 /s 关机 /r 重启 /g 重启,重启后,重 ...

  3. Linux下制作不用密码可立即登录的SSH用户

    一.客户端建立两把钥匙 (1)本例以客户端的monkey用户为例,首先切换到~/.ssh目录下,如果没有该目录的话,需要进行新建 cd ~ mkdir .ssh chmod 700 .ssh cd ~ ...

  4. 用Go编写的本地文件服务器

    本文来自网易云社区,转载务必请注明出处. 一.前言 一切问题的起源就是来自一个问题"为什么我打的jar包没有注解?",带着这个疑问查了一圈资料,原来问题主要是在没有将源码中的注释进 ...

  5. 四丶人生苦短,我用python【第四篇】

    1 基本数据类型 数字    int 字符串   str 布尔值   bool 列表       list 元组       tuple 字典       dict >>>type( ...

  6. BZOJ 1016 [JSOI2008]最小生成树计数 ——Matrix-Tree定理

    考虑从小往大加边,然后把所有联通块的生成树个数计算出来. 然后把他们缩成一个点,继续添加下一组. 最后乘法原理即可. 写起来很恶心 #include <queue> #include &l ...

  7. TeraTerm设定(解决日文乱码问题)

    首先,字体Font的MS Gothic是有Japanese的,设置为这个比较保险. 其次,在General Setup里将Language设为:English. 原理是什么我也不清楚,试了几个选择,就 ...

  8. [HDU-4825] Xor-Sum (01字典树)

    Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeu ...

  9. 【2018.2.8-】网络流学习笔记(含ISAP!)

    网络流的基础内容就不详细发了,网上到处都是,可自学. 总版点这里 ps:以下有些链接是hihocoder的题目(题面有详细讲解),请确保先登录hihocoder,再点击进入相应题目网页. 最大流 基础 ...

  10. 如何解决"The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path"

    今天我在eclipse上搭建新项目时,莫名其妙的出现这个错误,如下: The superclass "javax.servlet.http.HttpServlet" was not ...