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).
题意:给出x个数,合并两个数的代价为两数之和,求合并所有数字的最小代价
题解:英文版的合并果子……将所有数先装进小根堆,然后每次取出两个最小的,加完后再放回堆中即可,对了,记得用long long!
 
代码如下:
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. int n;
  7. struct Heap
  8. {
  9. long long a[];
  10. int sz1;
  11. void clear()
  12. {
  13. sz1=;
  14. }
  15. void push(long long x)
  16. {
  17. int i=sz1++;
  18. while(i>)
  19. {
  20. int p=(i>>);
  21. if(a[p]<x)
  22. {
  23. break;
  24. }
  25. a[i]=a[p];
  26. i=p;
  27. }
  28. a[i]=x;
  29. }
  30. long long top()
  31. {
  32. return a[];
  33. }
  34. long long pop()
  35. {
  36. int ans=a[];
  37. int x=a[--sz1];
  38. int i=;
  39. while((i<<)<sz1)
  40. {
  41. int ls=i<<;
  42. int rs=i<<|;
  43. if(rs<sz1&&a[rs]<a[ls])
  44. {
  45. ls=rs;
  46. }
  47. if(a[ls]>x)
  48. {
  49. break;
  50. }
  51. a[i]=a[ls];
  52. i=ls;
  53. }
  54. a[i]=x;
  55. return ans;
  56. }
  57. int sz()
  58. {
  59. return sz1;
  60. }
  61. }heap;
  62. int main()
  63. {
  64. long long b,ans=;
  65. scanf("%d",&n);
  66. heap.clear();
  67. for(int i=;i<=n;i++)
  68. {
  69. scanf("%lld",&b);
  70. heap.push(b);
  71. }
  72. while(heap.sz()>)
  73. {
  74. long long x=heap.pop();
  75. long long y=heap.pop();
  76. ans+=x+y;
  77. heap.push(x+y);
  78. }
  79. printf("%lld\n",ans);
  80. }
 
 
 
 
 
 
 
 
 
 

poj2513 Fence Repair(小根堆)的更多相关文章

  1. 区间DP与贪心算法的联系(uav Cutting Sticks &amp;&amp; poj Fence Repair(堆的手工实现))

    由于,这两题有着似乎一样的解法所以将其放在一起总结比較,以达到更好的区分二者的差别所在. 一.区间DP uva的Cutting Sticks是一道典型的模板题. 题目描写叙述: 有一根长度为l的木棍, ...

  2. POj 3253 Fence Repair(修农场栅栏,锯木板)(小根堆 + 哈弗曼建树得最小权值思想 )

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28359   Accepted: 9213 Des ...

  3. POJ 3253 Fence Repair【二叉堆】

    题意:给出n根木板,需要把它们连接起来,每一次连接的花费是他们的长度之和,问最少需要多少钱. 和上一题果子合并一样,只不过这一题用long long 学习的手写二叉堆的代码,再好好理解= = #inc ...

  4. 【BZOJ 1724】[Usaco2006 Nov]Fence Repair 切割木板 堆+贪心

    堆对于stl priority_queue ,我们自己定义的类自己重载<,对于非自定义类我们默认大根堆,如若改成小根堆则写成std::priority<int,vector<int& ...

  5. bzoj 1724: [Usaco2006 Nov]Fence Repair 切割木板【堆】

    如果反着看,看成合并木板,就和合并果子一样了,把若干块放进一个小根堆,然后每次取出两个合并,把合并结果加进答案和堆里 代码里小根堆用优先队列实现(懒 #include<iostream> ...

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

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

  7. Greedy:Fence Repair(POJ 3252)

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

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

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

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

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

随机推荐

  1. 支付宝sdk集成

    支付宝开放平台 http://doc.open.alipay.com/doc2/detail?treeId=59&articleId=103563&docType=1 集成步骤: 1. ...

  2. zk 06之:ZooKeeper命令、命令行工具及简单操作

    常用命令ZooKeeper 支持某些特定的四字命令字母与其的交互.它们大多是查询命令,用来获取 ZooKeeper 服务的当前状态及相关信息.用户在客户端可以通过 telnet 或 nc 向 ZooK ...

  3. Vue.js:自定义指令

    ylbtech-Vue.js:自定义指令 1.返回顶部 1. Vue.js 自定义指令 除了默认设置的核心指令( v-model 和 v-show ), Vue 也允许注册自定义指令. 下面我们注册一 ...

  4. python开发mysql:mysql安装(windows)&密码找回&存储引擎简介&库表的增删改查

    一,mysql安装 下载地址 https://dev.mysql.com/downloads/file/?id=471342 解压后,将目录C:\mysql-5.7.19-winx64\bin添加到计 ...

  5. maven学习1

     1.Maven的约定 src/main/java: 存放项目的java文件. src/main/resources: 存放项目的资源文件,如spring,hibernate的配置文件. src/te ...

  6. 如何检测 51单片机IO口的下降沿

    下降沿检测,说白了就是满足这样一个逻辑,上次检测是1,这次检测是0,就是下降沿. 从这个条件可知,要确保能够正确检测到一个下降沿,负脉冲的宽度,必须大于一个检测周期,当负脉冲宽度小于一个检测周期,就有 ...

  7. js操作一般文件和csv文件

    js操作一般文件和csv文件 将文本文件读成字符串 <input type="file" id="upload"> document.getElem ...

  8. js获取用户实时地理位置

    js获取用户实时地理位置 if(navigator.geolocation) { var id = navigator.geolocation.watchPosition(function(posit ...

  9. flask系列四之SQLAlchemy(二)表关系

    一.SQLAlchemy外键约束 1.创建外键约束表结构 目标:建立两个表“用户表(user)”和“问题表( question)”,其中问题表中的作者id是是用户表的id即外键的关系.(一个用户可以有 ...

  10. Python中特殊函数和表达式 filter,map,reduce,lambda

    1. filter 官方解释:filter(function or None, sequence) -> list, tuple, or string Return those items of ...