POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 53645 | Accepted: 17670 |
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
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank
Output
Sample Input
3
8
5
8
Sample Output
34
Hint
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
#include<iostream>
#include<vector>
#include<queue>
using namespace std; #define LL long long
using namespace std;
const int N = +;
int n;
int L;
int a,b;
priority_queue<int,vector<int>,greater<int> > q;
int main()
{
cin>>n;
LL ans=;
for(int i=;i<n;i++)
{
cin>>L;
q.push(L);
}
while(q.size()>)
{
a=q.top();
q.pop(); b=q.top();
q.pop(); ans += a+b;
q.push(a+b); }
cout<<ans<<endl;
while(!q.empty()) //清空队列
q.pop();
return ;
}
哈夫曼裸题
POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】的更多相关文章
- [ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25274 Accepted: 8131 Des ...
- poj 3253 Fence Repair(模拟huffman树 + 优先队列)
题意:如果要切断一个长度为a的木条需要花费代价a, 问要切出要求的n个木条所需的最小代价. 思路:模拟huffman树,每次选取最小的两个数加入结果,再将这两个数的和加入队列. 注意priority_ ...
- 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(简单哈弗曼树_水过)
题目大意:原题链接 锯木板,锯木板的长度就是花费.比如你要锯成长度为8 5 8的木板,最简单的方式是把21的木板割成13,8,花费21,再把13割成5,8,花费13,共计34,当然也可以先割成16,5 ...
- poj 3253 Fence Repair(优先队列+哈夫曼树)
题目地址:POJ 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(哈夫曼树)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26167 Accepted: 8459 Des ...
随机推荐
- 剑指Offer - 九度1371 - 最小的K个数
剑指Offer - 九度1371 - 最小的K个数2013-11-23 15:45 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是 ...
- USACO Section2.3 Cow Pedigrees 解题报告 【icedream61】
nocows解题报告------------------------------------------------------------------------------------------ ...
- SQL Server 分组取 Top 笔记(row_number + over 实现)
先看SQL语句(注意:这是在SQL Server 2005+ [包括2005] 的版本才支持的哦,o(∩_∩)o 哈哈~) SELECT col1,col2,col3 FROM table1 AS a ...
- Webdriver--获得验证信息
title:获得当前页面的标题 current_url:获得当前页面的URL text:前面提到过,获得标签对的文本信息 try: couseTitle = driver.find_element_b ...
- 孤荷凌寒自学python第十六天python的迭代对象
孤荷凌寒自学python第十六天python的迭代对象 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 迭代也就是循环. python中的迭代对象有相关的如下几个术语: A容器 contrai ...
- Jtag To Axi4 debug 读写寄存器的tcl脚本封装
把下列代码保存为.tcl或者.txt文本保存在某个路径下 打开vivado,在tcl concle中输入 “source 文件路径”,将脚本加载至工具中后, 例如读寄存器地址32'h12345678的 ...
- Android 启动项 Activity
使用SDK创建一个App的时候,基本都会看到这段代码: <activity android:name=".ExampleActivity" android:icon=&quo ...
- javascript replace 替换全部字符
$("#pageNote").html($("#note3").val().replace(/ /g, "").replace(/<b ...
- 再探 KMP 算法
$\DeclareMathOperator{\fail}{fail}$ KMP 算法堪称经典中的经典,然而这么多年以来,我却未能完全理解这个算法.我对 KMP 算法掌握的程度,是知其原理,但写不出来. ...
- 洛谷 P2114 [NOI2014]起床困难综合症 解题报告
P2114 [NOI2014]起床困难综合症 题目描述 21世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm一直坚持与起床困难综合症作 ...