地址 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 贪心 最小堆 题解《挑战程序设计竞赛》的更多相关文章

  1. POJ 3253 Fence Repair (贪心)

    Fence Repair Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  2. POJ 3253 Fence Repair 贪心 优先级队列

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 77001   Accepted: 25185 De ...

  3. POJ 3253 Fence Repair 贪心+优先队列

    题意:农夫要将板割成n块,长度分别为L1,L2,...Ln.每次切断木板的花费为这块板的长度,问最小花费.21 分为 5 8 8三部分.   思路:思考将n部分进行n-1次两两合成最终合成L长度和题目 ...

  4. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

  5. POJ 3253 Fence Repair (优先队列)

    POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...

  6. poj 3253 Fence Repair 优先队列

    poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...

  7. POj 3253 Fence Repair(修农场栅栏,锯木板)(小根堆 + 哈弗曼建树得最小权值思想 )

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28359   Accepted: 9213 Des ...

  8. POJ - 3253 Fence Repair 优先队列+贪心

    Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  9. POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 53645   Accepted: 17670 De ...

随机推荐

  1. python函数编程-装饰器decorator

    函数是个对象,并且可以赋值给一个变量,通过变量也能调用该函数: >>> def now(): ... print('2017-12-28') ... >>> l = ...

  2. 一起学SpringMVC之国际化

    随着网络的发展,在Web开发中,系统的国际化需求已经变得非常的普遍.本文主要讲解SpringMVC框架对多语言的支持,仅供学习分享使用,如有不足之处,还请指正. 什么是国际化? 国际化(interna ...

  3. 龙芯电脑上Electron应用开发

    背景 最近在一台龙芯电脑(系统是中兴新支点,Linux)上开发electron应用. PS:龙芯是国产的cpu,采用是mips架构,类似x86.arm. 安装NodeJS 安装步骤请查看:https: ...

  4. vue-preview vue图片预览插件+缩略图样式

    一.安装 npm i vue-preview -S 二.main.js中  导入组件 //vue-preview 开始 import VuePreview from 'vue-preview'; // ...

  5. ABAP分享三 批量上传数据到内表简单示例

    tYPE-POOLS: truxs. DATA: BEGIN OF build, name(10) TYPE c,   age(3)   TYPE c,   sex(2)   TYPE c,   sp ...

  6. linux终端 tty pty pts等

    linux终端 tty pty pts等 20140608 Chenxin整理 系统变量TERM不知是用来干什么的?它的值有vt100,vt220等,这些值代表什么意思? 环境变量TERM设置为终端机 ...

  7. TCP 三次握手与四次挥手

    TCP是什么      TCP(Transmission Control Protocol 传输控制协议)是一种面向连接(连接导向)的.可靠的. 基于IP的传输层协议.       TCP有6种标示: ...

  8. Linux系统学习 二十、SAMBA服务—介绍、安装、端口

    1.简介 网络数据文件共享服务器 可以和Windows中的网上邻居通用 数据共享的方法: Windows中最常用的是“网上邻居”.网上邻居使用的文件系统是CIFS(通用互联网文件系统)协议进行数据共享 ...

  9. 新版Notepad++加十六进制查看的插件HexEditor

    Notepad++新版虽然去掉了在线插件商店功能,但是依然可以使用自定义插件 Notepad++下载地址 腾讯(请务必点普通下载):https://pc.qq.com/detail/0/detail_ ...

  10. OpenCV-3.4.3图像通道处理

    图像通道处理 图像读取和处理都是按BGR通道顺序进行的 #include <iostream> #include <opencv2/opencv.hpp> #include & ...