poj-3253-Fence Repair(哈夫曼)
/*
Fence Repair
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 19914 Accepted: 6314
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 USACO 2006 November Gold 题目大意:FJ需要修补牧场的围栏,他需要 N 块长度为 Li 的木头(N planks of woods)。开始时,FJ只有一块无限长的木板,因此他需要把无限长的木板锯成 N 块长度 为 Li 的木板,Farmer Don提供FJ锯子,但必须要收费的,收费的标准是对应每次据出木块的长度,比如说测试数据中 5 8 8,一开始,FJ需要在无限长的木板上锯下长度 21 的木板(5+8+8=21),第二次锯下长度为 5 的木板,第三次锯下长度为 8 的木板,至此就可以将长度分别为 5 8 8 的木板找出 题目可以转化为Huffman树构造问题 : 给定 N planks of woods, 1. 在 N planks 中每次找出两块长度最短的木板,然后把它们合并,加入到集合A中, 2. 在集合中找出两块长度最短的木板,合并加入到集合A中,重复过程,直到集合A中只剩下一个元素 显然,通过每次选取两块长度最短的木板,合并,最终必定可以合并出长度为 Sum(Li)的木板,并且可以保证总的耗费最少
*/
#include <iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
long long int sum;
int i,n,t,a,b;
while(~scanf("%d",&n))
{
priority_queue<int,vector<int>,greater<int> >q;
for(i=; i<n; i++)
{
scanf("%d",&t);
q.push(t);
}
sum=;
if(q.size()==)
{
a=q.top();
sum+=a;
q.pop();
}
while(q.size()>)
{
a=q.top();
q.pop();
b=q.top();
q.pop();
t=a+b;
sum+=t;
q.push(t);
}
printf("%lld\n",sum);
}
return ;
}
poj-3253-Fence Repair(哈夫曼)的更多相关文章
- POJ 3253 Fence Repair(哈夫曼编码)
题目链接:http://poj.org/problem?id=3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个 ...
- Poj 3253 Fence Repair(哈夫曼树)
Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- poj 3253 Fence Repair (哈夫曼树 优先队列)
题目:http://poj.org/problem?id=3253 没用long long wrong 了一次 #include <iostream> #include<cstdio ...
- BZOJ 3253 Fence Repair 哈夫曼树 水题
http://poj.org/problem?id=3253 这道题约等于合并果子,但是通过这道题能够看出来哈夫曼树是什么了. #include<cstdio> #include<c ...
- POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53645 Accepted: 17670 De ...
- POJ 3253 Fence Repair(简单哈弗曼树_水过)
题目大意:原题链接 锯木板,锯木板的长度就是花费.比如你要锯成长度为8 5 8的木板,最简单的方式是把21的木板割成13,8,花费21,再把13割成5,8,花费13,共计34,当然也可以先割成16,5 ...
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- poj 3253 Fence Repair 优先队列
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
- poj 3253 Fence Repair(优先队列+哈夫曼树)
题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...
随机推荐
- Lua中获取table长度
-- table.getn(tableName) 得到一个table的大小,等同于操作符# -- 要注意的是:该table的key必须是有序的,索引是从1开始的. --例如有序的 local xian ...
- Java网络编程学习A轮_08_NIO的Reactor模型
参考资料: 了解 Java NIO 的 Reactor 模型,大神 Doug Lea 的 PPT Scalable IO in Java 必看:http://gee.cs.oswego.edu/dl/ ...
- linux 前端部署
linux.conf user root; worker_processes ; #error_log logs/error.log; #error_log logs/error.log notice ...
- hive grouping sets 等聚合函数
函数说明: grouping sets 在一个 group by 查询中,根据不同的维度组合进行聚合,等价于将不同维度的 group by 结果集进行 union allcube 根据 group b ...
- sshpass使用
sshpass的使用方法 应用范围:可以在命令行直接使用密码来进行远程连接和远程拉取文件. 使用前提:对于未连接过的主机.而又不输入yes进行确认,需要进行sshd服务的优化: # vim /etc/ ...
- poj3281网络流之最大流
加一个源点和汇点,把每头牛拆成两个点,不拆点的话可能会出现多对食物与饮料被一个牛享用的情况,拆点后流量为1,不能同时通过了 然后用最大流处理,每个链接边都是1 #include<map> ...
- canvas绘制进度条(wepy)
<template> <canvas canvas-id="canvas" style="width:{{width+10}}px;height:{{w ...
- java通过文件头来判断文件类型
import java.io.FileInputStream; import java.io.IOException; import java.util.HashMap; import java.ut ...
- 一. Spring框架防XXS跨站攻击
使用 Spring 框架进行 Java Web 开发,可以在 web.xml 文件中设置 HTML encode,在 JSP 文件页面元素 form 中确定实施. web.xml 加上: <co ...
- 修改Intelij IDEA的maven依据下载为国内镜像(阿里)
1.win7环境,默认情况下在用户目录的.m2下自己新建setting文件.QQ群交流:697028234 .m2\settings.xml 2.settings.xml文件内容为: <sett ...