POJ 3253 Fence Repair 贪心 优先级队列
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 77001 | Accepted: 25185 |
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).
Source
#include <cstdio>
#include <iostream>
#include <queue> using namespace std; const int max_n=;
const int max_L=; typedef long long LL; int n;
int L[max_n]; void solve()
{
LL ans=;
// 建立最小优先队列
priority_queue< int,vector<int>,greater<int> > que;
for(int i=;i<n;++i)
{
que.push(L[i]);
} int tmp;
while(que.size()>=)
{
tmp=que.top();
que.pop();
tmp+=que.top();
que.pop(); ans+=tmp;
que.push(tmp);
} printf("%lld",ans);
} int main()
{
scanf("%d",&n);
for(int i=;i<n;++i)
{
scanf("%d",&L[i]);
}
solve();
return ;
}
嗯,代码和行数只有之前的一半,不经思考直接调库还不损耗脑细胞。
21294701 | LIUYUANHAO | 3253 | Accepted | 220K | 844MS | C++ | 1741B |
2020-02-02 18:11:44 |
21310136 | LIUYUANHAO | 3253 | Accepted | 344K | 0MS | C++ | 665B |
2020-02-05 17:31:23 |
POJ 3253 Fence Repair 贪心 优先级队列的更多相关文章
- POJ 3253 Fence Repair (贪心)
Fence Repair Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...
- POJ 3253 Fence Repair 贪心+优先队列
题意:农夫要将板割成n块,长度分别为L1,L2,...Ln.每次切断木板的花费为这块板的长度,问最小花费.21 分为 5 8 8三部分. 思路:思考将n部分进行n-1次两两合成最终合成L长度和题目 ...
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- 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 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53645 Accepted: 17670 De ...
- POJ - 3253 Fence Repair 优先队列+贪心
Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- poj 3253 Fence Repair(priority_queue)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 40465 Accepted: 13229 De ...
随机推荐
- Leetcode 题目整理 climbing stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- IIS 无法访问请求的页面,因为该页的相关配置数据无效。
解决方法:控制面板-->程序和功能-->打开或关闭windows功能-->角色的这里,如果还未安装“web服务器(IIS)”,则选择“添加”.如果已经安装了,则选择“web服务器(I ...
- Serverless 的资源评估与成本探索
Serverless 布道师在讲解 Serverless 架构和云主机等区别的时候,总会有类似的描述: 传统业务开发完成想要上线,需要评估资源使用.根据评估结果,购买云主机,并且需要根据业务的发展不断 ...
- 时序数据库 Apache-IoTDB 源码解析之文件索引块(五)
上一章聊到 TsFile 的文件组成,以及数据块的详细介绍.详情请见: 时序数据库 Apache-IoTDB 源码解析之文件数据块(四) 打一波广告,欢迎大家访问IoTDB 仓库,求一波 Star. ...
- 大话IDL之(基本操作流程)
这里将对ENVI-IDL二次开发程序的一个通用流程做一个总结. 1.首先是文件打开和数据读取: 文件打开work_dir = dialog_pickfile(title='选择路径',/directo ...
- 关于ThinkPHP在Nginx服务器下因PATH_INFO出错的解决方法
参考:https://www.linuxidc.com/Linux/2011-11/46871.htm 这是一个ningx设置的问题,和TP无关.TP默认使用PATH_INFO来做CURD,而ngin ...
- 5.7.20 多实例——MGR部署实战
数据库 | MySQL:5.7.20 多实例——MGR部署实战 MGR介绍 基于传统异步复制和半同步复制的缺陷——数据的一致性问题无法保证,MySQL官方在5.7.17版本正式推出组复制(MySQL ...
- php 安装扩展插件实例-gd库
今天给php 安装一个扩展插件 gd库 一.gd库是什么 gd库是一个开源的图像处理库,它通过提供一系列用来处理图片的API,使其可以用来创建图表.图形.缩略图以及其他图像的处理操作. gd库支持 ...
- 2014.1.21 DNS大事故(dns原理、网络封锁原理)
1.21那天发生了什么,由1.21联想补充…… 很多网站都上不去,域名解析都到了65.49.2.178这个IP地址 先科普,再深挖 dns查询类型 递归查询,迭代查询 DNS解析过程,这里使用 ...
- java实现交集,并集,包括对象和基本类型
//java实现求交集,并集,包括元素为对象和基本类型,主要是利用hashMap,set不允许元素重复等特性来进行实现去重,利用反射机制来灵活配置以对象某个属性来进行去重./** * Gaoxl * ...