POJ - 3253 Fence Repair 优先队列+贪心
Fence Repair
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 Nplanks. 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
Lines 2.. N+1: Each line contains a single integer describing the length of a needed plank
Output
Sample Input
3
8
5
8
Sample Output
34
Hint
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).
#include<stdio.h>
#include<queue>
using namespace std; int main()
{
int n,x,a,b,i;
long long min;
priority_queue<int,vector<int>,greater<int> >q;
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%d",&x);
q.push(x);
}
min=;
while(q.size()>){
a=q.top();q.pop();
b=q.top();q.pop();
min+=a+b;
q.push(a+b);
}
printf("%lld\n",min);
return ;
}
POJ - 3253 Fence Repair 优先队列+贪心的更多相关文章
- poj 3253 Fence Repair 优先队列
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- poj 3253 Fence Repair (贪心,优先队列)
Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- poj 3253 Fence Repair (优先队列,哈弗曼)
题目链接:http://poj.org/problem?id=3253 题意:给出n块木板的长度L1,L2...Ln,求在一块总长为这个木板和的大木板中如何切割出这n块木板花费最少,花费就是将木板切割 ...
- POJ 3253 Fence Repair (贪心)
题意:将一块木板切成N块,长度分别为:a1,a2,……an,每次切割木板的开销为当前木板的长度.求出按照要求将木板切割完毕后的最小开销. 思路:比较奇特的贪心 每次切割都会将当前木板一分为二,可以按切 ...
- poj 3253 Fence Repair(优先队列+huffman树)
一个很长的英文背景,其他不说了,就是告诉你锯一个长度为多少的木板就要花多少的零钱,把一块足够长(不是无限长)的木板锯成n段,每段长度都告诉你了,让你求最小花费. 明显的huffman树,优先队列是个很 ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53645 Accepted: 17670 De ...
- POJ 3253 Fence Repair (贪心)
Fence Repair Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
随机推荐
- EasyDarwin手机直播是如何实现的快速显示视频的方法
前言 经常有人提到最近比较火的映客直播.花椒直播这种,是如何做到在打开手机直播中的某一个主播房间后,立即就能显示出主播视频,非常非常快,而且延时也比较小,是怎么做到的? 其实,这并不是什么高深的技术, ...
- Sparksql 取代 Hive?
sparksql hive https://databricks.com/blog/2014/07/01/shark-spark-sql-hive-on-spark-and-the-future-o ...
- accessor mothod mutator mothod 更改器方法 访问器方法 类的方法可以访问类的任何一个对象的私有域!
LocalDate.plusDate String.toUpperCase GregorianCalendar.add import java.time.*; public class Calenda ...
- Message-oriented middleware
en.wikipedia.org/wiki/Message-oriented_middleware Message-oriented middleware (MOM) is software or h ...
- Windows10搭建FTP服务器
配置FTP服务器步骤: 第一步: 打开控制面板--->选择程序--->启动或关闭Windows功能--->勾选FTP服务器等.如下图: 第二步: 右键此电脑--->点击管理-- ...
- (转)windows一台电脑添加多个git账号
版权声明:技术总结来自互联网,书籍,以及工作积累.如果觉得我的总结有帮助,欢迎分享. https://blog.csdn.net/qq1332479771/article/details/701496 ...
- 第一个自定义开发的Arcgis地图
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 人生苦短之Python枚举类型enum
枚举类型enum是比较重要的一个数据类型,它是一种数据类型而不是数据结构,我们通常将一组常用的常数声明成枚举类型方便后续的使用.当一个变量有几种可能的取值的时候,我们将它定义为枚举类型.在Python ...
- mac安装python3
http://www.jianshu.com/p/51811fa24752 brew install python3 安装路径:/usr/local/Cellar 使用: 执行python3即可 配置 ...
- MYSQL进阶学习笔记十三:MySQL 内存优化!(视频序号:进阶_31)
知识点十四:MySQL 内存的优化(31) 一.优化MySQL SERVER 7组后台进程: masterthread:主要负责将脏缓存页刷新到数据文件,执行purge操作,触发检查点,合并插入缓冲区 ...