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 ...
随机推荐
- java---时间戳
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );// 格式化时间Date date = ne ...
- 下拉框移动 jquery
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html& ...
- Bug 29041775 : ORA-41401: Define character set () does not match database character set ()
oracle版本12.2.0.1 Errors in file /u01/app/oracle/diag/rdbms/sibcyb1/CYB111/trace/CYB111_q003_166752.t ...
- Mac录制或保存视频后如何放大?
想要在录制和拍摄视频后在喜欢的场景(例如Mark)中放大视频吗?本文将向您展示如何放大视频并通过裁剪视频和“平移和缩放”效果来制作Ken Burns效果.Filmora9是一款功能强大的视频编辑器,具 ...
- Excel 2003 与 Excel 2007之间有什么不同?
如果您使用Excel 2003已有数年,您可能会意识到使用更多最新版本的Excel(2007.2010.2013或Excel 2016)的人员或组织的数量正在增加.您甚至可能收到了自己的Excel工作 ...
- Leaving Google for a couple of devices-Kasper Lund
原文链接https://medium.com/@kasper.lund/building-for-billions-bcb48814d864 一年多以前,我辞去了我在Google的出色工作,离开了一群 ...
- 删除文件linux
bool LxDeleteFile(const char* src){ int32_t iRe = remove(src); ) return true; else return false; }
- csp2019后的感慨
你还记得曾经加入oi的初衷吗? ... 我们都不想输,可谁都没有赢... --前言 没有太大的感想,也不配去写感想...就记录一下初学者失败的原因吧.希望看过的人能引以为戒. 做题的时候,不到万不得已 ...
- 题解 P2286 【[HNOI2004]宠物收养场】
这是题目链接 大家好,这个题我调了很久过了,所以想写题解 我用的平衡树是AVL树,平衡树界的老爷爷 这个树并不会很慢,主要是我初学,常数巨大 而且题目的 $ n = 80000$,可以接受 \(sol ...
- java自学-数组
1.数组是什么 前边说过java的基本数据类型,数组,就是装这些基本类型的容器.每个基本类型的变量都是单个的,数组就是这些单个元素的组合. 2.创建数组 方式一 格式: 数组存储的数据类型[] 数组名 ...