博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Fence Repair
阅读量:5061 次
发布时间:2019-06-12

本文共 2815 字,大约阅读时间需要 9 分钟。

Fence Repair

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

3858

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

Source

 
 
 
------------------------------------------------------------------------------------------------------------------------------------------
思想:Huffman
 
要注意的是应该用long long 类型。
 
Source Code:
#include 
#include
using namespace std;priority_queue
,greater
>Q; //建立一个小顶堆int main(){ long long N,answer; while(cin>>N){ while(Q.empty()==false) Q.pop(); long long x; for(int i=0;i
>x; Q.push(x); } answer=0; while(Q.size()>1){ long long num1=Q.top(); Q.pop(); long long num2=Q.top(); Q.pop(); answer=answer+num1+num2; Q.push(num1+num2); } cout<
<

 

转载于:https://www.cnblogs.com/Murcielago/p/4221483.html

你可能感兴趣的文章
枚举数与可枚举类型(笔记)
查看>>
marquee标签使用【转载】
查看>>
3.1 查找文本
查看>>
详细的SQL中datediff用法
查看>>
打造属于你的聊天室(WebSocket)
查看>>
Spring Boot 整合 Shiro-登录认证和权限管理
查看>>
P2668 斗地主
查看>>
Sharepoint学习笔记--资料收集--Sharepoint的内建字段
查看>>
.Net 配置的简陋解决方案
查看>>
Python中的单例模式实现
查看>>
EasyPusher:基于live555的DarwinInjector实现的RTSP直播推送程序
查看>>
运算符中的一些小技巧
查看>>
Apache配置HTTPS的过程小记
查看>>
万维网WWW详解
查看>>
MySQL数据库之DML(数据操作语言)
查看>>
(四)适配器Adapter
查看>>
PL/0编译器(java version)–PL0.java
查看>>
【栈】1353:表达式括号匹配(stack)
查看>>
SQLServer中文排序
查看>>
numpy笔记—np.squeeze用法
查看>>