Fence Repair
Accept:199     Submit:784
Time Limit:1000MS     Memory Limit:65536KB

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 ≤ 1,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个权值作为n个叶子结点,构造一棵二叉树,若带权路径长度达到最小

#include<cstdio>
#include<queue>
#include<cstdlib>
#include<functional>
#define ll long long
using namespace std;
priority_queue<ll,vector<ll>,greater<ll> >data;
int main(){int i,j,n,tmp;scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&tmp),data.push(tmp);ll sum=0;while(data.size()>1){ll min1=data.top();data.pop();ll min2=data.top();data.pop();sum+=min1+min2;data.push(min1+min2);}printf("%lld\n",sum);return 0;
}

老BOJ 07 Fence Repair相关推荐

  1. [BZOJ1724][Usaco2006 Nov]Fence Repair 切割木板

    1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1272  Solved: ...

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

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

  3. POJ 3253 Fence Repair C++ STL multiset 可解

    Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53106 Accepted: 17508 Descri ...

  4. POJ 3253 -- Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 55661   Accepted: 18331 De ...

  5. 编程算法 - 篱笆修理(Fence Repair) 代码(C)

    篱笆修理(Fence Repair) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 把一块木板切成N块, 每次切两块, 分割的开销是木板长度, ...

  6. Fence Repair (二叉树求解)(优先队列,先取出小的)

    题目链接:http://poj.org/problem?id=3253 Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  7. Fence Repair(不会优先队列的看过来)

    Fence Repair(不会优先队列的看过来) Farmer John wants to repair a small length of the fence around the pasture. ...

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

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

  9. bzoj1724[Usaco2006 Nov]Fence Repair 切割木板*

    bzoj1724[Usaco2006 Nov]Fence Repair 切割木板 题意: FJ需要n块木板,第i块木板长度为ai.但他只有一块长度为sigma(i,1,n)ai的木板.每切一次的代价为 ...

最新文章

  1. linux下载python的es库,Elasticsearch py客户端库安装及使用方法解析
  2. 云安全与我们未来的网络息息相关……
  3. python【蓝桥杯vip练习题库】ADV-281特等奖学金
  4. Codeforces Round #297 (Div. 2)D. Arthur and Walls 搜索bfs
  5. 信息系统项目管理师采购管理
  6. nginx: [error] invalid PID number in /run/nginx.pid
  7. python中queue使用_在python2.6中使用Queue类
  8. 【专升本计算机】2021年甘肃省专升本计算机全真模拟试题(五)
  9. 上网登录窗不弹出_配置 Windows XP 正常上网(TLS HTTPS),连接到 NAS
  10. js获取session_学习后端鉴权系列: 基于Cookie, Session认证
  11. 比 TensorFlow Lite 快 15.6 倍!业界首个移动 GPU BNN 加速引擎 PhoneBit 开源
  12. 使用perforce+git处理连线离线工作的pipeline
  13. 【语音识别】基于matlab GUI MFCC+VQ说话人识别系统【含Matlab源码 1153期】
  14. 你还发现了CSDN那些变化
  15. python编写一个程序、判断用户输入的数是正数还是负数_编写一个程序,判断用户输入的数是正数还是负数。_学小易找答案...
  16. j和jk是有区别的_科普向,大家都说jk,jk是什么意思呢?
  17. 嗨,你真的懂this吗? 1
  18. erlang中的ets和dets
  19. 通过Matlab实现离散序列卷积和
  20. [Java GC]Java垃圾回收

热门文章

  1. 线性Transformer应该不是你要等的那个模型
  2. KDD 2019 | 使用神经网络为A*搜索算法赋能:以个性化路径推荐为例
  3. dataset_flickr8k.json与dataset_flickr30k.json的比较
  4. python的dll文件在哪_Python运行DLL文件的方法
  5. 【PostgreSQL+PostGIS离线安装】2天的踩坑及问题解决经验分享(含安装文件postgresql-9.5.9+postgis-2.2.3+多个依赖及测试SQL)
  6. java 投票算法_Boyer and Moore Fast majority vote algorithm(快速选举算法)
  7. 电路图符号大全_电工必备电路图符号大全 图解复杂电路图解析方式 值得收藏!...
  8. C#——WPF的菜单栏、工具栏、状态栏DEMO
  9. IIS+ASP+MySQL8.0+中文乱码解决方案(2019.7)
  10. BugKuCTF WEB 成绩单