poj_3253 Fence Repair
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
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<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
int n;
long long ans;
priority_queue<long long ,vector<long long>,greater<long long> > Q;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
long long a;
scanf("%lld",&a);
Q.push(a);
}
while(Q.size()!=1)
{
long long u=Q.top();Q.pop();
long long v=Q.top();Q.pop();
ans+=(u+v);
u+=v;
Q.push(u);
}
printf("%lld",ans);
return 0;
}
poj_3253 Fence Repair的更多相关文章
- poj 3253 Fence Repair
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42979 Accepted: 13999 De ...
- Greedy:Fence Repair(POJ 3252)
Fence Repair 问题大意:农夫约翰为了修理栅栏,要将一块很长的木块切割成N块,准备切成的木板的长度为L1,L2...LN,未切割前的木板的长度恰好为切割后木板的长度的总和,每次切断木板的时候 ...
- poj 3253:Fence Repair(堆排序应用)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23913 Accepted: 7595 Des ...
- 哈夫曼树-Fence Repair 分类: 树 POJ 2015-08-05 21:25 2人阅读 评论(0) 收藏
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32424 Accepted: 10417 Descri ...
- 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:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- fence repair(队列水过)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32916 Accepted: 10638 点我 ...
- BZOJ 1724: [Usaco2006 Nov]Fence Repair 切割木板
题目 1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec Memory Limit: 64 MB Description Farmer ...
- 1724: [Usaco2006 Nov]Fence Repair 切割木板( 贪心 )
倒过来看 , 每次总是选择最短的两块木板合并 , 用heap维护 ------------------------------------------------------------------- ...
随机推荐
- redisTemplate实现轻量级消息队列, 异步处理excel并实现腾讯云cos文件上传下载
背景 公司项目有个需求, 前端上传excel文件, 后端读取数据.处理数据.返回错误数据, 最简单的方式同步处理, 客户端上传文件后一直阻塞等待响应, 但用户体验无疑很差, 处理数据可能十分耗时, 没 ...
- react项目中使用antd
1.antd官网: https://ant.design/docs/react/introduce-cn 2.React中使用Antd 1.安装antd npm install antd --save ...
- spring security xml配置详解
security 3.x <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns= ...
- FutureTask源码解析
在Java中一般通过继承Thread类或者实现Runnable接口这两种方式来创建多线程,但是这两种方式都有个缺陷,就是不能在执行完成后获取执行的结果,因此Java 1.5之后提供了Callable和 ...
- 【转载】配置文件报错:不允许有匹配 [xX][mM][lL] 的处理指令目标
今天编写代码的时候,不小心给一个xml文件的首行加了一行空格,导致了启动报了如题类似的错误,查到一篇文章,顺藤摸瓜发现了这个失误,文章转载如下: 框架整合 的时候,XML 配置文件大多数情况是从另一个 ...
- Python模板库Mako的用法
官网地址:http://www.makotemplates.org/ 文档地址:http://docs.makotemplates.org/ 中文文档基本用法地址:http://www.open-op ...
- 公众号开发之wx-tools+springboot应用实战-音乐爬虫推送[JAVA]
springboot+wx-tools实践!音乐爬虫推送公众号DEMOGitHub地址:wx-tools 最终DEMO源码地址: music_collector 先理一下大概的开发步骤: 1. 创建一 ...
- Netty 出站缓冲区 ChannelOutboundBuffer 源码解析(isWritable 属性的重要性)
目录: 前言 ChannelOutboundBuffer 介绍 addMessage 方法 addFlush 方法 flush0 方法 缓冲区扩展思考 总结 每个 ChannelSocket 的 Un ...
- MVC会员注册
自从写了<数据库数据加密与解密>http://www.cnblogs.com/insus/p/3434735.html.其中也有提及Insus.NET将要在MVC应用程序中实现会员注册的功 ...
- params关键字、工具辅助类与、加密与解密
一.params关键字 在C#中如果给方法的参数加上关键字params则会形成可变参数,在传递时可以是0-n个对象. 示例: using System; using System.Collection ...