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).
题意:给出x个数,合并两个数的代价为两数之和,求合并所有数字的最小代价
题解:英文版的合并果子……将所有数先装进小根堆,然后每次取出两个最小的,加完后再放回堆中即可,对了,记得用long long!
 
代码如下:
#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(小根堆)的更多相关文章

  1. 区间DP与贪心算法的联系(uav Cutting Sticks &amp;&amp; poj Fence Repair(堆的手工实现))

    由于,这两题有着似乎一样的解法所以将其放在一起总结比較,以达到更好的区分二者的差别所在. 一.区间DP uva的Cutting Sticks是一道典型的模板题. 题目描写叙述: 有一根长度为l的木棍, ...

  2. POj 3253 Fence Repair(修农场栅栏,锯木板)(小根堆 + 哈弗曼建树得最小权值思想 )

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28359   Accepted: 9213 Des ...

  3. POJ 3253 Fence Repair【二叉堆】

    题意:给出n根木板,需要把它们连接起来,每一次连接的花费是他们的长度之和,问最少需要多少钱. 和上一题果子合并一样,只不过这一题用long long 学习的手写二叉堆的代码,再好好理解= = #inc ...

  4. 【BZOJ 1724】[Usaco2006 Nov]Fence Repair 切割木板 堆+贪心

    堆对于stl priority_queue ,我们自己定义的类自己重载<,对于非自定义类我们默认大根堆,如若改成小根堆则写成std::priority<int,vector<int& ...

  5. bzoj 1724: [Usaco2006 Nov]Fence Repair 切割木板【堆】

    如果反着看,看成合并木板,就和合并果子一样了,把若干块放进一个小根堆,然后每次取出两个合并,把合并结果加进答案和堆里 代码里小根堆用优先队列实现(懒 #include<iostream> ...

  6. POJ 3253 Fence Repair(哈夫曼树)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26167   Accepted: 8459 Des ...

  7. Greedy:Fence Repair(POJ 3252)

    Fence Repair 问题大意:农夫约翰为了修理栅栏,要将一块很长的木块切割成N块,准备切成的木板的长度为L1,L2...LN,未切割前的木板的长度恰好为切割后木板的长度的总和,每次切断木板的时候 ...

  8. poj 3253:Fence Repair(堆排序应用)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23913   Accepted: 7595 Des ...

  9. BZOJ 1724: [Usaco2006 Nov]Fence Repair 切割木板

    题目 1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer ...

随机推荐

  1. WS-* 协议

    Web服务作为实现SOA中服务的最主要手段.跟Web Service相关的标准,它们大多以“WS-”作为名字的前缀,所以统称WS-*. Web服务最基本的协议包括UDDI,WSDL和SOAP,通过它们 ...

  2. WebService简单实现

    1. WebService SOAP.WSDL.UDDISOAP(Simple Object Access Protocal,简单对象访问协议),是在分散或在分布式环境中交换信息的简单协议.WSDL( ...

  3. sourcetree 分支的创建合并

    sourcetree 分支的创建合并,提交 https://blog.csdn.net/qq_34975710/article/details/74469068 sourcetree测试版本的配置忽略 ...

  4. redis的安装和类型及基本命令

    一.memcached和redis区别 1. redis 可以存储,memcached用来缓存, 2. 数据类型,memcached只有string:redis有string,链表,哈希结构,集合,有 ...

  5. shell编程中用户输入处理(shell 04)

    shell编程中用户输入处理1.命令行参数2.脚本运行时获取输入 命令行参数 通过空格来进行分割的位置参数 :$+position $0,$1,$2 ....$0 :程序名$1,$2,$3 ... $ ...

  6. 搭建Dynamic Web Project(动态web项目)的springmvc工程2

    本文转载自:http://blog.csdn.net/typa01_kk/article/details/45905129 此篇为“创建Dynamic Web Projec工程,”搭建Dynamic ...

  7. AngularJS:控制器

    ylbtech-AngularJS:控制器 1.返回顶部 1. AngularJS 控制器 AngularJS 控制器 控制 AngularJS 应用程序的数据. AngularJS 控制器是常规的  ...

  8. [PYTHON 实作] 算100

    问题:编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性.例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100 ...

  9. PL/SQL 训练04--事务

    --pl/sql通过SQL和ORACLE数据库紧密的整合在一起--在pl/sql中可以执行任何操作语句(DML语句),包括INSERT,UPDATE,DELETE,MERGE,也包括查询语句--可否执 ...

  10. C#应用调试C++ dll的方法

    最近碰到个C#应用闪退的问题,由于通讯部分调用了C++工程写的dll,下面介绍一种调试的方法. 右键 启动项目,分别配置常规和和调试即可,如下图. 常规中,输出目录设置为安装目录中dll对应的目录: ...