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 ...
随机推荐
- DataTable 相关
1.对表的初始化 //创建表 DataTable table = new DataTable(); //添加列 table.Columns.Add("ID", typeof(Int ...
- 八使用Shell函数
在Shell脚本中,将一些需要重复使用的操作,定义为公共的语句块,即可称为函数 使用函数的好处? 使脚本代码更简洁,增强易读性 提高Shell脚本的执行效率 函数定义方法 基本格式1 function ...
- 12、PPP和HDLC
PPP主要包括三个部分1. 在串行链路上封装上层数据报文的方法2. LCP(link control protocals): 链路控制协议来配置和测试数据通信链路,协商PPP协议的配置参数 ...
- JavaScript 替换所有匹配内容
由于JavaScript 的 replace 只能替换一次,因此另外编写一个能现替换全部匹配内容方法,代码如下: /*把 content 中所有的 searchValue 替换为 replaceVal ...
- 死磕dtd(1)
看到安卓开发里大量的xml文件和layout里的Android UI开始复习一下xml xml的校验规则依据dtd dtd里面大小写敏感.....查找了好久才发现这个问题 <?xml versi ...
- edltplus使用正则表达式替换多余空行
24-7 <font style="font-weight:bold;">24-7</font><div class="tab_conten ...
- 【Pycharm使用者必看】自定义【光标快速定位到行尾】的按键
1.问题描述 使用Pycharm写代码时,有很多比较方便的快捷键,比如:Shift+Enter快速切换到下一行, 但每次切换到多个括号外或者想移动到行尾,就必须按 End 键或者鼠标点击, 这样操作幅 ...
- POJ_1979_dfs
题目描述: 每组数据给你一张字符的图,'@'代表起点,'.'代表可走的路,'#'代表墙,求从起点出发,可到达的位置的数量,包括起点. 思路: dfs基础题,从起始点开始,每一次所在的点,只要不出界并且 ...
- SpringBoot项目版本升级:从1.5.3升级到2.1.8版本
SpringBoot项目版本升级:从1.5.3升级到2.1.8版本 前言 简单记录一次本人在自己的SpringBoot项目project-template中,把1.5.3版本升级到2.1.8版本时升级 ...
- 怎么理解Laravel的核心架构
使用过larave框架的朋友都知道laravel框架里面除了提供一些基本的功能(如控制器.视图.模型)之外,还有中间件.门面.契约等,这些东西是如何在laravel框架运用起来的呢?今天就和大家详聊一 ...