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 ...
随机推荐
- C# 反射与dynamic最佳组合
在 C# 中反射技术应用广泛,至于什么是反射.........你如果不了解的话,请看下段说明,否则请跳过下段.广告一下:喜欢我文章的朋友请关注一下我的blog,这也有助于提高本人写作的动力. 反射:当 ...
- tracert的应用
tracert IP //检查网络各个节点路由情况: 如果是在10个路有点之内访问到了该站点,说明访问速度良好,若是在10~15之间,说明站点访问状况就一般了. ipconfig /flushdns ...
- 织梦CMS建站入门学习(一)
一.下载与安装. 首先到织梦官网下载软件,可选择UTF8或GBK不同编码格式,如果电脑没有PHP环境,还要下载dede自带的PHP环境软件. 将软件中的upload文件夹内容复制到WWW文件夹下,然后 ...
- html的body内标签之超链接
一,先来个简单的练练手,target="_blank" 的作用是在新的tab中打开页面.href 是超链接的意思. <!DOCTYPE html> <html l ...
- Selector 模型
1.服务器端: import selectors import socket sel = selectors.DefaultSelector() #生成一个select对象 def accept(so ...
- BZOJ3747:[POI2015]Kinoman——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3747 https://www.luogu.org/problemnew/show/P3582 共有 ...
- BZOJ1008:[HNOI2008]越狱——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=1008 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中 ...
- BZOJ2588:Count on a tree——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2588 Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你 ...
- HTTPS证书生成方法,也适用于APP
步骤: 一.登录服务器,创建ioscert目录 cd ~/servers/APP mkdir ioscert 二.cd到ioscert目录下,执行以下命令: 1.openssl genrsa -out ...
- [Leetcode] pascals triangle ii 帕斯卡三角
Given an index k, return the k th row of the Pascal's triangle. For example, given k = 3,Return[1,3, ...