POJ 30253 Fence Repair (二叉树+优先队列)
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).
** 分析:**
首先,切割的方法可以参见二叉树来描述,该二叉树的每一个叶子节点就对应了切割出的一块块木板,叶子节点的深度就对应了为了得到对应木板所需的切割次数,开销的合计就是各叶子节点的模板的长度*节点的深度的总和。
则可以得到最短的板和次短的板的节点应该是兄弟节点。
将Li按照大小顺序排列,则最短的板应该是L1,次短的板应该是L2,也就意味着他们是从一块长度为L1+L2的板切割得来的。由于切割顺序是自由的,不妨当做是最后被切割。则在此次切割之前有(L1+L2),L3,L4···,LN,这样的N-1块木板存在。之后的思路同上。
代码:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
long long int ans=0;
int N;
scanf("%d",&N);
int a[20009];
for(int i=0; i<N; i++)
scanf("%d",&a[i]);
///N的值这里需要注意的是在不断地减少的,因为每一次的两个木板和并后,木板的个数就减少一个
while(N>1)
{
int min1=0,min2=1;
if(a[min1]>a[min2]) swap(min1,min2);///首先我们将第一个当做最小的,第二值当做第二小
for(int i=2; i<N; i++)
{
///其余的值分别于剩下的值比较,找出整个数组里面的最小和次小
{
if(a[i]<a[min1])
min2=min1;
min1=i;
}
else if(a[i]<a[min2])
{
min2=i;
}
}
int t=a[min1]+a[min2];///两个木板的长度即为这次的开销
ans+=t;
if(min1==N-1) swap(min1,min2);///因为要把最后一个去掉,所以min1的下标不能是最后一个
///把之前数组的值刷新
a[min1]=t;
a[min2]=a[N-1];
N--;
}
printf("%lld\n",ans);
return 0;
}
由于只需要从板的集合中取出最小的两块,并且把两块板长度之和加入到结合中即可,因此可以用优先队列高效的实现。
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
int main()
{
int n,num;
long long int ans=0;
priority_queue<int ,vector<int>,greater<int> >que;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d",&num);
que.push(num);
}
int num1,num2;
while(que.size()>1)
{
num1=que.top();
que.pop();
num2=que.top();
que.pop();
ans+=num1+num2;
que.push(num1+num2);
}
printf("%lld\n",ans);
return 0;
}
POJ 30253 Fence Repair (二叉树+优先队列)的更多相关文章
- 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 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...
- POJ 3253 Fence Repair(优先队列,哈夫曼树,模拟)
题目 //做哈夫曼树时,可以用优先队列(误?) //这道题教我们优先队列的一个用法:取前n个数(最大的或者最小的) //哈夫曼树 //64位 //超时->优先队列,,,, //这道题的优先队列用 ...
- POJ 3253 Fence Repair STL 优先队列
这题做完后觉得很水,主要的想法就是逆过程思考,原题是截断,可以想成是拼装,一共有n根木棍,最后要拼成一根完整的,每两根小的拼成一根大的,拼成后的木棍长度就是费用,要求费用最少.显然的是一共会拼接n-1 ...
- POJ 3253 Fence Repair 贪心+优先队列
题意:农夫要将板割成n块,长度分别为L1,L2,...Ln.每次切断木板的花费为这块板的长度,问最小花费.21 分为 5 8 8三部分. 思路:思考将n部分进行n-1次两两合成最终合成L长度和题目 ...
- 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(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- POJ - 3253 Fence Repair 优先队列+贪心
Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- [ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25274 Accepted: 8131 Des ...
随机推荐
- 【week2】Scrum中的站立会议
Scrum站立会议 站立会议给我的第一印象就是站着开会,在经过我查阅资料之后,发现也是差不多的意思.学术一点的分析就是在Sprint开始后,团队将会在每个工作日特定时间举行一个简短会议,每次会议 ...
- 通过设置窗体的AcceptButton属性,可以设置窗体的“接受”按钮,若此设计,则用户每次按下Enter键都相当于单击该按钮
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- perf使用的问题,再看perf record,perf record 设置的采样频率,采样频率是如何体现在
当perf stat -e branches 是统计 再看perf record,perf record是为了是记录时间发生的时候的调用栈, 在我的测试代码中总共有200,000,000条branch ...
- 系统管理员需知:25个Linux服务器安全技巧(转)
来源:51CTO 作者:51CTO 大家都认为 Linux 默认是安全的,我大体是认可的 (这是个有争议的话题).Linux默认确实有内置的安全模型.你需要打开它并且对其进行定制,这样才能 ...
- OpenStack Queens版本Horizon定制化开发
工具环境 1.VMware workstation 12+: 2.Ubuntu系统+Linux Pycharm: 3.OpenStack Queens版本Horizon代码: 问题及解决 1.项目代码 ...
- [转载]看看大牛们是怎样获得英文综述(reviews)的?
对于做实验的童鞋来说,平时看综述必不可少,可是如何获得自己想要的却并不是一件容易的事情,这里整理并推荐几种行之有效的方法: 1. http://www.annualreviews.org/,这是一个顶 ...
- (转)linux sort,uniq,cut,wc命令详解
linux sort,uniq,cut,wc命令详解 sort sort 命令对 File 参数指定的文件中的行排序,并将结果写到标准输出.如果 File 参数指定多个文件,那么 sort 命令将这些 ...
- [洛谷P2106]Sam数
题目大意:问长度为$n$的$Sam$数有几个,$Sam$数的定义为没有前导零,相邻两个数字之差绝对值小于等于$2$的数 题解:发现转移方程一定,可以矩阵快速幂. 卡点:没有特判$n=1$的情况 C++ ...
- POJ3678:Katu Puzzle——题解
http://poj.org/problem?id=3678 总觉得这题比例题简单. 设a为x取0的点,a+n为x取1的点. 我们还是定义a到b表示取a必须取b. 那么我们有: 当AND: 1.当c= ...
- UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)
UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用) 题意分析 绝对的好题. 先说做完此题的收获: 1.对数据结构又有了宏观的上的认识; 2.熟悉了常用STL ...