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

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<vector>
#include<queue>
using namespace std; #define LL long long
using namespace std;
const int N = +;
int n;
int L;
int a,b;
priority_queue<int,vector<int>,greater<int> > q;
int main()
{
cin>>n;
LL ans=;
for(int i=;i<n;i++)
{
cin>>L;
q.push(L);
}
while(q.size()>)
{
a=q.top();
q.pop(); b=q.top();
q.pop(); ans += a+b;
q.push(a+b); }
cout<<ans<<endl;
while(!q.empty()) //清空队列
q.pop();
return ;
}

哈夫曼裸题

POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】的更多相关文章

  1. [ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25274   Accepted: 8131 Des ...

  2. poj 3253 Fence Repair(模拟huffman树 + 优先队列)

    题意:如果要切断一个长度为a的木条需要花费代价a, 问要切出要求的n个木条所需的最小代价. 思路:模拟huffman树,每次选取最小的两个数加入结果,再将这两个数的和加入队列. 注意priority_ ...

  3. POJ 3253 Fence Repair(修篱笆)

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

  4. poj 3253 Fence Repair 优先队列

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

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

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

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

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

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

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

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

随机推荐

  1. 《Cracking the Coding Interview》——第4章:树和图——题目3

    2014-03-19 03:34 题目:给定一个排好序的数组,设计算法将其转换为一棵二叉搜索树,要求树的高度最小. 解法:递归生成平衡二叉树,使左右子树的节点数尽量相等,所以对半开最好了.其实也可以生 ...

  2. 每天一个Linux命令(7):pwd命令

    pwd命令以绝对路径的方式显示用户当前工作目录. 语法 pwd(选项) 选项 --help:显示帮助信息: --version:显示版本信息. 实例 [root@localhost ~]# pwd / ...

  3. NGUI-使用UILabel呈现图片和不同格式的文字

    1.可以使用BBCode标记 [b]Bold[/b]                      粗体[i]italic[/i]                         斜体[u]underli ...

  4. [洛谷P4889]kls与flag

    题目大意:有$n$根竹竿,第$i$根竹竿在$i$位置,第​$i$根竹竿高度为​$h_i$,每根竹竿可以向左倒或向右倒,问有几对竹竿倒下后顶端重合. 题解:求出每根竹竿倒下后的位置,离散化,记录一下每个 ...

  5. 【HDU 3336 Count the string】

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  6. Tomcat学习笔记(二)

    Servlet浅析 javax.servlet.Servlet是一个接口,所有的Servlet必须实现接口里面的方法. 该接口在tomcat/bin中的servlet-api.jar包中. Servl ...

  7. 《c程序设计语言》读书笔记-4.12-递归整数转字符串

    #include <stdio.h> #include <math.h> #include <stdlib.h> void itoa_num(int n, char ...

  8. display 垂直居中

    /* Center slide text vertically */ display: -webkit-box; display: -ms-flexbox; display: -webkit-flex ...

  9. 转:Android 的进程与线程总结

    当一个Android应用程序组件启动时候,如果此时这个程序的其他组件没有正在运行,那么系统会为这个程序 以单一线程的形式启动一个新的Linux 进程. 默认情况下,同一应用程序下的所有组件都运行再相同 ...

  10. poj 3729 Facer’s string

    Facer’s string Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 2155   Accepted: 644 Des ...