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 ...
随机推荐
- B/S与C/S的区别
参考:http://www.cnblogs.com/groler/articles/2116905.html 一.概念 C/S结构:即Client/Server(客户机/服务器)结构,是大家熟知的软件 ...
- 解决spring 事务管理默认不支持SQLException等运行时异常
公司同事在定位一个bug时,发现spring默认的事务只支持运行时异常的回滚,对于像SQLException这样的非运行时异常,默认的事务机制不能处理,于是找了下解决的办法: 1.在捕获SQLE ...
- java实现一个最简单的tomcat服务
在了解tomcat的基本原理之前,首先要了解tomcatt最基本的运行原理. 1.如何启动? main方法是程序的入口,tomcat也不例外,查看tomcat源码,发现main是在Bootstrap ...
- 地下迷宫探索(dfs)
地下迷宫探索(30 分) 地道战是在抗日战争时期,在华北平原上抗日军民利用地道打击日本侵略者的作战方式.地道网是房连房.街连街.村连村的地下工事,如下图所示. 我们在回顾前辈们艰苦卓绝的战争生活的同时 ...
- 校赛热身 Problem B. Matrix Fast Power
找循环节,肯定在40项以内,不会证明. #include <iostream> #include <cstring> #include <string> #incl ...
- C#获取程序代码执行时长
ArrayList list = new ArrayList(); long startTicks = DateTime.Now.Ticks; for (int i = 0; i < 10000 ...
- cpu,io密集型计算概念
I/O密集型 (CPU-bound) I/O bound 指的是系统的CPU效能相对硬盘/内存的效能要好很多,此时,系统运作,大部分的状况是 CPU 在等 I/O (硬盘/内存) 的读/写,此时 CP ...
- Java如何解决form表单上传文件,以及页面返回处理结果通知!
前端JSP代码 <form id='formSumbit' class='form-horizontal' action='/ncpay/route/chlsubmcht/batchImpor' ...
- arm上sd卡热插拔问题的解决:
首先,保证sd卡驱动是完好,但是sd卡却无法热插拔或者无法识别. 刚开始我的板子上,sd是能够读取的,但是却不支持热插拔,看了几天sd驱动,找到了问题的原因,是驱动中硬件引脚相关设置的问题,具体根绝个 ...
- git配置和使用
1.注册bitbucket用户登录bitbucket站点https://bitbucket.org/注册一个用户,注册后用户名为linjiqin,邮箱为linjiqin@dkhs.com. 2.Cre ...