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 ...
随机推荐
- CSDN
链接:https://blog.csdn.net/shaoyedeboke
- 三、Django学习之单表查询接口
查询接口 all() 查询所有结果,结果是queryset类型 filter(**kwargs) and条件关系:参数用逗号分割表示and关系 models.Student.objects.filte ...
- 使用.NET Core优雅获取并展示最新疫情数据
前言 新型冠状病毒的出现,着实让人紧张.我每天一大早都会去查看今天的最新数据,可是每次的数据都挺让人揪心的.今天突然间很想看看过去的历史的数据,结果查了很多资料都不是很全.反正国家让我们待在家里做贡献 ...
- Spring Boot 2.x基础教程:默认数据源Hikari的配置详解
通过上一节的学习,我们已经学会如何应用Spring中的JdbcTemplate来完成对MySQL的数据库读写操作.接下来通过本篇文章,重点说说在访问数据库过程中的一个重要概念:数据源(Data Sou ...
- 数学建模之优劣解距法(TOPSIS)
优劣解距法简称TOPSIS,是一种综合评价方法,利用原始数据反映各评价方案之间的差距 优劣解距法的步骤通常为: 先将原始数据针具做正向化处理,得到正向化矩阵 再对正向化矩阵标准化处理以消除各指标纲量的 ...
- FFMPEG学习----分离视频里的H.264与YUV数据
#include <stdio.h> extern "C" { #include "libavcodec/avcodec.h" #include & ...
- 使用小书匠及markdown here编辑博客和微信公众号
1. 使用小书匠连接Evernote并发布笔记到博客园 1.1 小书匠初探 我平时的信息收集的主要方法是采用Pocket+Evernote. 简单来说: 如果访问到非常有用,而且是必须要立刻记录的内容 ...
- python库之matplotlib学习---图无法显示中文
在代码前面加上 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] ...
- SpringMVC之Controller层最佳实践
规范设置接口, 有利于项目的可扩展性,提高前后端的交互特性. 请求参数 响应结果
- 面向对象之包装类与Object类
一.包装类(Wrapper) 1.包装类出现原因 为了使8种基本数据类型的变量具有类的特征,引入包装类 2.类型间转化 ① 基本数据类型---->对应的包装类:自动装箱 包装类---->对 ...