Fence Repair
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 27055   Accepted: 8800

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
哈曼夫树。
题意 :把一根长度为x的木板切成n段。每段的长度已给出来。切某块木板时。花费的费用为该木板的长度。一開始以为仅仅要每次选出最长的木板然后从总的中切掉就是最小花费。实际上不是,其有用脑子想想也知道了,假设总长非常长的话,而须要的木板的长度又非常小,每次从总长中切掉花费将会非常大,所以这样的思路是不正确的,看了讨论区才明确,我们能够把切这个过程逆回去。就是把这n段木板连接起来,每次花费的钱为待连接的两段木板的长度。(细致想想,切跟连花费的钱是同样的,即是等价的,画一下图能够看出来) 然后有一种思想是:初始化一个最小堆,每次从堆中选出最小的两段木板进行连接。连接完之后在放入到堆中。总共n-1次连接就可以
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
ll a[20010];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
scanf("%lld",a+i);
make_heap(a,a+n,greater<int>());
int t=n-1;ll ans=0,tem;
while(t--)
{
tem=a[0];pop_heap(a,a+n,greater<int>());--n;
tem+=a[0];pop_heap(a,a+n,greater<int>());--n;
ans+=tem;a[n++]=tem;
push_heap(a,a+n,greater<int>());
}
printf("%lld\n",ans);
}
return 0;
}

POJ 3253-Fence Repair(堆)的更多相关文章

  1. POJ 3253 Fence Repair(修篱笆)

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

  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 优先队列

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

  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: 28359   Accepted: 9213 Des ...

  6. POJ 3253 Fence Repair (贪心)

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

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

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

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

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

  9. poj 3253 Fence Repair(priority_queue)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 40465   Accepted: 13229 De ...

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

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

随机推荐

  1. PHP开发安全问题

    1.不相信表单 对于一般的Javascript前台验证,由于无法得知用户的行为,例如关闭了浏览器的javascript引擎,这样通过POST恶意数据到服务器.需要在服务器端进行验证,对每个php脚本验 ...

  2. MSSQL-SQL SERVER 分页原理

    项目中用到的, 用心琢磨一下此SQL语句即可: SELECT TOP $row * FROM (        SELECT ROW_NUMBER() OVER (ORDER BY [ID] desc ...

  3. cxf之Exception in thread "main" java.lang.NoSuchMethodError: org.apache.cxf.jaxrs.provider.ProviderFactory.<init>(Lorg/apache/cxf/Bus;)V

    pom.xml中关于cxf的配置jar包冲突??? 1.http://blog.csdn.net/yzl_8877/article/details/53216923 2.https://www.cnb ...

  4. 调试解决iOS内存泄漏

    这里讲述在没有ARC的情况下,如何使用Instruments来查找程序中的内存泄露,以及NSZombieEnabled设置的使用. 本文假设你已经比较熟悉Obj-C的内存管理机制. 实验的开发环境:X ...

  5. javascript中function和object的区别,以及javascript如何实现面向对象的编程思想.

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  6. URL 长度有限制吗?

    众所周知,传递小量参数(在没有其他原因,例如隐藏参数值的情况下)推荐使用GET方法,传递大量参数推荐使用POST方法.原因是什么呢? 原因是传说GET方法是通过URL来传递,而URL的长度是受限的,而 ...

  7. Python练习笔记——字符串反转

    请输入一段字符串,不利用反转函数,编写一段代码,将其反转. def list_reverse(a): list_long = len(a) list_long_half = list_long // ...

  8. AP_创建标准发票后会计科目的变化(概念)

    2014-06-04 Created By BaoXinjian 1. 创建Invoice,并查看所创建的科目

  9. 计算机科学基础知识(一)The Memory Hierarchy

    一.前言 最近一个问题经常萦绕在我的脑海:一个学习电子工程的机械师如何称为优秀的程序员?(注:本文作者本科学习机械设计,研究生转到电子工程系学习,毕业后却选择了系统程序员这样的职业).经过思考,我认为 ...

  10. 图解IIS配置过程

    环境介绍: 操作系统:win7,64位.IIS7 开发软件:VS2010,32位 图解IIS配置过程: 此过程之前确保已安装了IIS. 1.在"控制面板"中找到"管理工具 ...