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

 

题意:

有一位农夫要把一个木板钜成n块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木板的长度

给定各个要求的小木板的长度,及小木板的个数n,求最小的费用;

PS:

3

5 8 5为例:

先从无限长的木板上锯下长度为 21 的木板,花费 21

再从长度为21的木板上锯下长度为5的木板,花费5

再从长度为16的木板上锯下长度为8的木板,花费8

总花费 = 21+5+8 =34

思路:

要使总费用最小,那么每次只选取最小长度的两块木板相加,再把这些“和”累加到总费用中即可;

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
struct cmp
{
bool operator()(long long x, long long y)
{
return x > y;
}
};
priority_queue<long long ,vector<long long>,cmp>q;
int main()
{
int n;
int i;
long long x,y;
long long s = ;
cin >> n;
for (i = ; i <= n; i++)
{
cin >> x;
q.push(x);
}
while (q.size()>=)
{
long long r;
x = q.top(); q.pop();
y = q.top(); q.pop();
s = s + x + y;
r = x + y;
q.push(r);
}
cout << s << endl;
return ;
}

poj 3253 Fence Repair (贪心,优先队列)的更多相关文章

  1. POJ 3253 Fence Repair 贪心+优先队列

    题意:农夫要将板割成n块,长度分别为L1,L2,...Ln.每次切断木板的花费为这块板的长度,问最小花费.21 分为 5 8 8三部分.   思路:思考将n部分进行n-1次两两合成最终合成L长度和题目 ...

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

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

  3. POJ 3253 Fence Repair (贪心)

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

  4. POJ 3253 Fence Repair 贪心 优先级队列

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 77001   Accepted: 25185 De ...

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

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

  6. poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...

  7. POJ 3253 Fence Repair(优先队列,哈夫曼树,模拟)

    题目 //做哈夫曼树时,可以用优先队列(误?) //这道题教我们优先队列的一个用法:取前n个数(最大的或者最小的) //哈夫曼树 //64位 //超时->优先队列,,,, //这道题的优先队列用 ...

  8. POJ 3253 Fence Repair STL 优先队列

    这题做完后觉得很水,主要的想法就是逆过程思考,原题是截断,可以想成是拼装,一共有n根木棍,最后要拼成一根完整的,每两根小的拼成一根大的,拼成后的木棍长度就是费用,要求费用最少.显然的是一共会拼接n-1 ...

  9. poj 3253 Fence Repair 优先队列

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

  10. POJ 3253 Fence Repair(修篱笆)

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

随机推荐

  1. git在idea中的使用,如何构远程git方仓库

    git 下载:http://learning.happymmall.com/git/   配置用户名:$ git config --glob user.name "forever" ...

  2. java学习笔记 --- 集合(续)

    1.map集合 1.1.特点:将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值. 1.2.Map集合和Collection集合的区别? Map集合存储元素是成对出现的,Map集 ...

  3. DevExpress v17.2—WPF篇(一)

    用户界面套包DevExpress v17.2终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WPF v17.2 新的Hamburger Menu.Sched ...

  4. java发生邮件(转)

    参考链接:http://www.runoob.com/java/java-sending-email.html package test.mail; import com.sun.mail.util. ...

  5. anu - component

    import { extend, isFn, options, clearArray, noop } from "./util"; import { CurrentOwner } ...

  6. Alpha阶段敏捷冲刺---Day7

    一.Daily Scrum Meeting照片 二.今天冲刺情况反馈     今天是Alpha阶段敏捷冲刺的最后一天,今天我们将对这一阶段的任务进行扫尾工作,我们打算完成之前设想的程序的所有功能,包括 ...

  7. Grunt新手入门篇

    今天看到一篇通俗易懂的Grunt入门文章,博主写得很用心,原文请戳:http://yujiangshui.com/grunt-basic-tutorial/ 当时学习 Grunt 的时候,真是很头疼. ...

  8. 9.1 UDP协议

    TCP 协议是面向连接的基于流的,可靠的传输服务.UDP是无连接的,基于数据报的,不可靠的传输服务,UDP没有粘包,但是会产生丢包. UDP模型如下: 可以看到,服务器端不用listen,也不用acc ...

  9. 51Nod 1066:Bash游戏 (巴什博弈)

    1066 Bash游戏  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次最少拿1颗,最多拿K颗,拿到 ...

  10. HDU 4035 Maze 概率DP 搜索

    解题报告链接: http://www.cnblogs.com/kuangbin/archive/2012/10/03/2711108.html 先推公式,设计状态,令DP[i]表示在房间i退出要走步数 ...