Fence Repair
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19660   Accepted: 6236

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).

Source

 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm> using namespace std; int n; int main(){ //freopen("input.txt","r",stdin); priority_queue<int,vector<int>,greater<int> > q;
while(~scanf("%d",&n)){
int x;
while(!q.empty())
q.pop();
for(int i=;i<n;i++){
scanf("%d",&x);
q.push(x);
}
long long ans=; //注意精度
while(q.size()>){
int a=q.top(); q.pop();
int b=q.top(); q.pop();
int tmp=a+b;
ans+=tmp;
q.push(tmp);
}
cout<<ans<<endl;
}
return ;
}

POJ 3253 Fence Repair (哈夫曼树)的更多相关文章

  1. Poj 3253 Fence Repair(哈夫曼树)

    Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  2. poj 3253 Fence Repair (哈夫曼树 优先队列)

    题目:http://poj.org/problem?id=3253 没用long long wrong 了一次 #include <iostream> #include<cstdio ...

  3. BZOJ 3253 Fence Repair 哈夫曼树 水题

    http://poj.org/problem?id=3253 这道题约等于合并果子,但是通过这道题能够看出来哈夫曼树是什么了. #include<cstdio> #include<c ...

  4. POJ 3253 Fence Repair(哈夫曼编码)

    题目链接:http://poj.org/problem?id=3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个 ...

  5. POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 53645   Accepted: 17670 De ...

  6. POJ 3253 Fence Repair(简单哈弗曼树_水过)

    题目大意:原题链接 锯木板,锯木板的长度就是花费.比如你要锯成长度为8 5 8的木板,最简单的方式是把21的木板割成13,8,花费21,再把13割成5,8,花费13,共计34,当然也可以先割成16,5 ...

  7. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

  8. poj 3253 Fence Repair 优先队列

    poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...

  9. POJ 3253 Fence Repair (优先队列)

    POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...

  10. poj 3253 Fence Repair(优先队列+哈夫曼树)

    题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...

随机推荐

  1. Flask的集中控制

    想通过一个统一的机制,同时允许一些公共的逻辑 {% if args["NoUser"] %} 无用户! {% else %} <!DOCTYPE html PUBLIC &q ...

  2. Idea不能新建package的解决

    右键–>new –> Mark Directory As –> Sources Root (idea需要修改一下目录的性质,改为源文件 )

  3. OpenWRT下实现Portal认证(WEB认证)

    首先简单介绍一下什么是Portal认证,Portal认证,通常也会叫Web认证,未认证用户上网时,设备强制用户登录到特定站点,用户可以免费访问其中的服务.当用户需要使用互联网中的其它信息时,必须在门户 ...

  4. iOS 按钮拖动。

    -(void)testMove { moveBtn = [[UIButton alloc ]init]; moveBtn.frame = CGRectMake(0, 30, 60, 60); move ...

  5. WinForm 之 自定义标题栏的窗体移动

    通过标题栏的鼠标事件实现窗体移动,代码如下: bool m_isMouseDown = false; //窗体是否移动 Point m_mousePos; //记录窗体的位置 /// <summ ...

  6. javascript将算法复杂度从O(n^2)做到O(n)

    compare the difference of two giving array, return results: 1. elements in both array, 2. elements o ...

  7. 原生DOM操作

    注入jQuery var node=document.createElement("script"); node.setAttribute('src','http://common ...

  8. PgSQL · 源码分析· pg_dump分析

    PostgreSQL本身提供了逻辑导出工具pg_dumpall和pg_dump,其中pg_dumpall导出所有的数据库,pg_dump导出单个数据库,两个工具的用法和参数不再详细介绍,本文从代码层面 ...

  9. RPC远程调用概念 &amp;&amp; demo实例

    RPC是指远程过程调用,直观说法就是A通过网络调用B的过程方法. 也就是说两台serverA.B,一个应用部署在Aserver上,想要调用Bserver上应用提供的函数/方法,因为不在一个内存空间,不 ...

  10. vmware中的 CentOS7 虚机磁盘动态扩容

    0.在vmware的配置项中,将虚机的磁盘大小调大,步骤简单,此处略 查看当前状态 文件系统状态 df -h 磁盘状态 lsblkfdisk   -l  1.首先要再创建一个物理分区 (使用fdisk ...