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

  1. 3
  2. 8
  3. 5
  4. 8

Sample Output

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

 
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<queue>
  5. #include<cmath>
  6. #include<algorithm>
  7.  
  8. using namespace std;
  9.  
  10. int n;
  11.  
  12. int main(){
  13.  
  14. //freopen("input.txt","r",stdin);
  15.  
  16. priority_queue<int,vector<int>,greater<int> > q;
  17. while(~scanf("%d",&n)){
  18. int x;
  19. while(!q.empty())
  20. q.pop();
  21. for(int i=;i<n;i++){
  22. scanf("%d",&x);
  23. q.push(x);
  24. }
  25. long long ans=; //注意精度
  26. while(q.size()>){
  27. int a=q.top(); q.pop();
  28. int b=q.top(); q.pop();
  29. int tmp=a+b;
  30. ans+=tmp;
  31. q.push(tmp);
  32. }
  33. cout<<ans<<endl;
  34. }
  35. return ;
  36. }

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. w3cscholl的在线代码编辑工具2

    https://www.w3cschool.cn/tryrun/runcode?lang=c

  2. 海马模拟器连不上ADB的解决方法

    http://yunpan.cn/c3xMeYhvVsEIq  访问密码 fa8d先暂时用这个工具吧,官方提供的,不过目前不是最终版,后面会加入到模拟器中 adb connect 127.0.0.1: ...

  3. Oracle——数据库启动与关闭

    本文内容 服务器环境 客户端环境 概述 启动数据库 关闭数据库 补充 参考资料 本文说明 Oracle 数据库的启动和关闭,内容虽然基础,但是在数据库很多操作中都需要,因此,基础而重要,必须深入理解. ...

  4. C语言常见问题 如何用Visual Studio编写C语言程序测试

    新建Win32控制台应用程序   勾选控制台空项目   右击源文件,添加cpp文件   输入范例代码,可以正常运行#include <stdio.h> int main(void) { i ...

  5. 带你走进EJB--将EJB发布为Webservice(4)

    接下来的我们将会自定义一个对象,然后看看EJB是如何对复杂的参数发布成WebService的. 代码如下:在第一个版本的基础之上加上增加用户的方法,参数为User. package com.tgb.e ...

  6. typescript - 前言介绍

    众所周知,JavaScript是弱语言(子承父业都表达不了),因此比较大的企业考虑到可维护性以及安全性来说,都不用它开发,因此Typescript诞生了,它并不是为了替换JavaScript而诞生的, ...

  7. SQL Server 中断开连接到指定数据库的所有连接

    常用的情形是在部署测试数据库时需要通过SQL代码自动重新创建数据库,在删除的时候往往会发生错误,错误信息一般会指出目前有用户连接到这个数据库上,因此不能删除. 实现的方式是通过查询指定数据库中活跃的 ...

  8. vCenter Server Virtual Appliance features and benefits

    http://vmwire.com/tag/vcsa/ Installed on SUSE Linux Enterprise Server 11 x64. OVF when deployed is c ...

  9. 学习 Linux,302(混合环境): Samba 角色

    http://www.ibm.com/developerworks/cn/linux/l-lpic3-310-2/ 概述 在本文中,了解下列概念: Samba 安全模式 核心 Samba 守护程序的角 ...

  10. LUN mask 和zone 区别

    问题: 在SAN网络中,只有一台存储时,该存储带有Map功能,可以将LUN Map到指定主机,那么FC Switch的zoning功能还有意义吗?有没有方法来证明这以意义的存在.即使存储不带有Map功 ...