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

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).
 
题意:要切N块板子,将长度为L的板子切成两块花费L元,给出N块板子的长度,求最小花费
 
解析:Huffman tree 思想,每次将最短的两块板子取出,再将两者长度之和累加到费用中,再将两者长度之和作为新一块板子的长度并把这块板子放入剩余板子的集合中,重复以上操作直到只剩一块板子。
具体操作可以利用STL大法,强行优先队列
代码如下:

#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
int n;
long long ans;
priority_queue<long long ,vector<long long>,greater<long long> > Q;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
long long a;
scanf("%lld",&a);
Q.push(a);
}
while(Q.size()!=1)
{
long long u=Q.top();Q.pop();
long long v=Q.top();Q.pop();
ans+=(u+v);
u+=v;
Q.push(u);
}
printf("%lld",ans);
return 0;
}

poj_3253 Fence Repair的更多相关文章

  1. poj 3253 Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 42979   Accepted: 13999 De ...

  2. Greedy:Fence Repair(POJ 3252)

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

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

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

  4. 哈夫曼树-Fence Repair 分类: 树 POJ 2015-08-05 21:25 2人阅读 评论(0) 收藏

    Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32424 Accepted: 10417 Descri ...

  5. POJ 3253 Fence Repair(修篱笆)

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

  6. POJ 3253 Fence Repair (贪心)

    Fence Repair Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  7. fence repair(队列水过)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32916   Accepted: 10638 点我 ...

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

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

  9. 1724: [Usaco2006 Nov]Fence Repair 切割木板( 贪心 )

    倒过来看 , 每次总是选择最短的两块木板合并 , 用heap维护 ------------------------------------------------------------------- ...

随机推荐

  1. PHP-引入文件(include)后,页面错位,不居中解决办法

    1.把include文件放在head里,不要放在html或doctype上面,这样可以解决居中的问题,空白行的话可以用<div style="display:none"> ...

  2. POJ 2083 Fractal 分形题目

    这两天自学了一线算法导论里分治策略的内容,秉着只有真正投入投入编程,才能更好的理解一种算法的思想的想法,兴致勃勃地找一些入门的题来学习. 搜了一下最后把目光锁定在了Poj fractal这一个题上.以 ...

  3. 多线程,线程同步,synchronized关键字的用法

    一.什么是多线程 Java多线程实现方式主要有四种:继承Thread类.实现Runnable接口.实现Callable接口通过FutureTask包装器来创建Thread线程.使用ExecutorSe ...

  4. 数据绑定到ADO.NET

    // Define a DataSet with a single DataTable. DataSet dsInternal = new DataSet(); dsInternal.Tables.Ad ...

  5. Go的基本类型与变量

    基本类型 布尔型:bool 长度:1字节 取值范围:true,false 注意:不可以用数字代表true或false 整型:int/uint 根据运行平台可能为32或64位 8位整型:int8/uin ...

  6. 行为型设计模式之模板方法(TEMPLATE METHOD)模式 ,策略(Strategy )模式

    1 模板方法(TEMPLATE METHOD)模式: 模板方法模式把我们不知道具体实现的步聚封装成抽象方法,提供一些按正确顺序调用它们的具体方法(这些具体方法统称为模板方法),这样构成一个抽象基类.子 ...

  7. java设计模式-菜鸟网络

    http://www.runoob.com/design-pattern/singleton-pattern.html

  8. CUBA Studio 8.0 发布,企业级应用开发平台

    CUBA Platform 是一款开源且免费的企业级应用开发框架,已有将近10年的发展历史,由俄罗斯的 Haulmont  公司开发,CUBA Platform 近期将正式登陆中国,将提供中文网站.中 ...

  9. 并发编程之 Java 三把锁

    前言 今天我们继续学习并发.在之前我们学习了 JMM 的知识,知道了在并发编程中,为了保证线程的安全性,需要保证线程的原子性,可见性,有序性.其中,synchronized 高频出现,因为他既保证了原 ...

  10. [日常] Go语言圣经-可变参数习题

    1.参数数量可变的函数称为为可变参数函数,例子就是fmt.Printf和类似函数2.参数列表的最后一个参数类型之前加上省略符号“...”3.虽然在可变参数函数内部,...int 型参数的行为看起来很像 ...