poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3253

题解
本题是<挑战程序设计>一书的例题
根据树中描述 所有切割的代价 可以形成一颗二叉树
而最后的代价总和是与子节点和深度相关的 由于切割的次数是确定的 该二叉树的节点就是确定的。
也就是说我们可以贪心的处理 最小长度的子节点放在最下面
如图

ac代码如下 使用了堆 记录每次最小的元素
堆的使用真的不是很方便 , 另外还需要注意 爆int 所以需要使用long long 记录元素的和
#include <iostream>
#include <algorithm>
#include <vector>
#include <assert.h> using namespace std; /*
poj 3253
有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度
给定各个要求的小木板的长度,及小木板的个数n,求最小费用
提示:
以
3
8 8 5为例: 先从无限长的木板上锯下长度为 21 的木板,花费 21
再从长度为21的木板上锯下长度为5的木板,花费5
再从长度为16的木板上锯下长度为8的木板,花费8
总花费 = 21 + 5 + 8 = 34
*/ int main()
{
int n; long long ret = ;
cin >> n;
vector<int> wood(n);
for (int i = ; i < n; i++) {
cin >> wood[i];
}
make_heap(wood.begin(), wood.end(), greater<int>()); while (wood.size() != ) { pop_heap(wood.begin(), wood.end(), greater<int>());
long long total = wood.back();
wood.pop_back(); pop_heap(wood.begin(), wood.end(), greater<int>());
total += wood.back();
wood.pop_back(); ret += total;
wood.push_back(total);
push_heap(wood.begin(), wood.end(), greater<int>());
} cout << ret << endl; return ;
}
#include <iostream>
#include <queue> #include <stdio.h> using namespace std; typedef long long LL; const int MAX_N = ;
int n, 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]);
} while (que.size() > ) {
int l1, l2;
l1 = que.top();
que.pop();
l2 = que.top();
que.pop(); ans += l1 + l2;
que.push(l1 + l2);
}
printf("%lld\n", ans);
} int main()
{
scanf("%d", &n); for (int i = ; i < n; i++) {
scanf("%d", &L[i]);
} solve();
}
stl 堆
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 贪心 优先级队列
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 77001 Accepted: 25185 De ...
- 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: 28359 Accepted: 9213 Des ...
- 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【哈弗曼树/贪心/优先队列】
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53645 Accepted: 17670 De ...
随机推荐
- 在 Python 3.x 版本后,ConfigParser.py 已经更名为 configparser.py 所以出错!
在 Python 3.x 版本后,ConfigParser.py 已经更名为 configparser.py 所以出错!
- Mysql悲观锁乐观锁区别与使用场景
本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...
- 学习DDD的初步尝试,从最基础的开始,业务介绍,划分限界上下文 ,建立模型
Conference业务简介 Conference是这样一个系统,它提供了一个在线创建会议以及预订会议座位的平台.这个系统的用户有两类: 1:客户,可以创建和管理会议. 2:会议座位预定者,可以预订会 ...
- GitLab-怎样使用GitLab托管项目
场景 Docker Compose部署GitLab服务,搭建自己的代码托管平台(图文教程): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/det ...
- CSS入门(边框、轮廓、元素的分类、盒子模型的三个构成部分)
一.边框属性 作用:给元素加上一个边框 第一种: border-top border-bottom border-left boder-right 三个属性值: 粗细 线型 颜色 第二种: borde ...
- ORA-17627: ORA-12577:关于文件存储满的问题
问题描述:搭建DG的时候,要rman从orcl恢复到orclstd数据库来,dup复制了半天,结果最后报错:ORA-17627: ORA-12577: Message 12577 not found; ...
- Sqlite—数据库管理与表管理
数据库管理 创建数据库,创建完成之后自动进入 [root@localhost ~]# sqlite3 /www/wwwroot/task.db 使用数据库,如果 /www/wwwroot 路径下面没有 ...
- Office批量打印助手(Excel 批量打印、Word 批量打印)
最新版本:1.0.6664.34636(更新日期:2018年3月31日) 下载地址:点击下载 程序简介: 本程序能批量打印 Word 文件.Excel 工作簿. 使用程序前请先安装 .NET Fra ...
- Css 设置固定表格头部,内容可滚动
效果图:
- 使用vue-cli创建工程的时候提示vue-cli Failed to download repo vuejs-templates/webpack-simple:self signed certificate in certificate chain的解决方法
最近在使用 vue-cli 脚手架创建项目的时候,遇到 webpack-simple 模板下载不成功的情况,提示 vue-cli Failed to download repo vuejs-templ ...