poj2513 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
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).
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int n; struct Heap
{
long long a[];
int sz1; void clear()
{
sz1=;
} void push(long long x)
{
int i=sz1++;
while(i>)
{
int p=(i>>);
if(a[p]<x)
{
break;
}
a[i]=a[p];
i=p;
}
a[i]=x;
} long long top()
{
return a[];
} long long pop()
{
int ans=a[];
int x=a[--sz1];
int i=;
while((i<<)<sz1)
{
int ls=i<<;
int rs=i<<|;
if(rs<sz1&&a[rs]<a[ls])
{
ls=rs;
}
if(a[ls]>x)
{
break;
}
a[i]=a[ls];
i=ls;
}
a[i]=x;
return ans;
} int sz()
{
return sz1;
}
}heap; int main()
{
long long b,ans=;
scanf("%d",&n);
heap.clear();
for(int i=;i<=n;i++)
{
scanf("%lld",&b);
heap.push(b);
}
while(heap.sz()>)
{
long long x=heap.pop();
long long y=heap.pop();
ans+=x+y;
heap.push(x+y);
}
printf("%lld\n",ans);
}
poj2513 Fence Repair(小根堆)的更多相关文章
- 区间DP与贪心算法的联系(uav Cutting Sticks && poj Fence Repair(堆的手工实现))
由于,这两题有着似乎一样的解法所以将其放在一起总结比較,以达到更好的区分二者的差别所在. 一.区间DP uva的Cutting Sticks是一道典型的模板题. 题目描写叙述: 有一根长度为l的木棍, ...
- POj 3253 Fence Repair(修农场栅栏,锯木板)(小根堆 + 哈弗曼建树得最小权值思想 )
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 28359 Accepted: 9213 Des ...
- POJ 3253 Fence Repair【二叉堆】
题意:给出n根木板,需要把它们连接起来,每一次连接的花费是他们的长度之和,问最少需要多少钱. 和上一题果子合并一样,只不过这一题用long long 学习的手写二叉堆的代码,再好好理解= = #inc ...
- 【BZOJ 1724】[Usaco2006 Nov]Fence Repair 切割木板 堆+贪心
堆对于stl priority_queue ,我们自己定义的类自己重载<,对于非自定义类我们默认大根堆,如若改成小根堆则写成std::priority<int,vector<int& ...
- bzoj 1724: [Usaco2006 Nov]Fence Repair 切割木板【堆】
如果反着看,看成合并木板,就和合并果子一样了,把若干块放进一个小根堆,然后每次取出两个合并,把合并结果加进答案和堆里 代码里小根堆用优先队列实现(懒 #include<iostream> ...
- POJ 3253 Fence Repair(哈夫曼树)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26167 Accepted: 8459 Des ...
- Greedy:Fence Repair(POJ 3252)
Fence Repair 问题大意:农夫约翰为了修理栅栏,要将一块很长的木块切割成N块,准备切成的木板的长度为L1,L2...LN,未切割前的木板的长度恰好为切割后木板的长度的总和,每次切断木板的时候 ...
- poj 3253:Fence Repair(堆排序应用)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23913 Accepted: 7595 Des ...
- BZOJ 1724: [Usaco2006 Nov]Fence Repair 切割木板
题目 1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec Memory Limit: 64 MB Description Farmer ...
随机推荐
- ajax同步异步
test.html <a href="javascript:void(0)" onmouseover="testAsync()"> asy.js f ...
- maven命令行创建web项目报错:java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils
早上一上班就想新建一个web项目玩玩,没想到一敲命令创建就失败了,真是出师不利.各种折腾无果,当然我也可以用eclipse直接创建的,就是不甘心被这破问题给耍了.刚刚才发现问题原因,这个结果我也是醉了 ...
- 1088 Rational Arithmetic
题意: 给出两个分式(a1/b1 a2/b2),分子.分母的范围为int型,且确保分母不为0.计算两个分数的加减乘除,结果化为最简的形式,即"k a/b",其中若除数为0的话,输出 ...
- json、pickle\shelve模块(超级好用~!)讲解
json.pickle模块讲解 见我前面的文章:http://www.cnblogs.com/itfat/p/7456054.html shelve模块讲解(超级好用~!) json和pickle的模 ...
- JS比较实用的时间控件
使用方法: 下载下来压缩包,文件的地方不要改变,就可以了 http://www.my97.net/dp/down.asp html的代码: <input readonly="reado ...
- python学习(十八) 程序打包
18.1 Distutils基础 18.2 打包 18.2.1 建立存档文件 18.2.2 创建Windows安装程序或RPM包 18.3 编译扩展 18.4 使用py2exe创建可执行程序
- 易酷cms本地包含漏洞拿shell
测试版本:易酷cms2.5 包含一句话:http://localhost/ekucms2.5/?s=my/show/id/{~eval($_POST[x])} 会在网站的/temp/Logs/目录下生 ...
- springboot成神之——spring的文件上传
本文介绍spring的文件上传 目录结构 配置application DemoApplication WebConfig TestController 前端上传 本文介绍spring的文件上传 目录结 ...
- 9.solr学习速成之group
Group与Facet的区别 facet的查询结果主要是分组信息:有什么分组,每个分组包括多少记录:但是分组中有哪些数据是不可知道的,只有进一步搜索. group则类似于关系数据库的g ...
- leetcode717
class Solution { public: bool isOneBitCharacter(vector<int>& bits) { int len = bits.size() ...