地址 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. PlayJava Day028

    1.网络程序中套接字(Socket)用于将应用程序与端口连接起来 套接字是一个假想的连接装置,就像插插头的设备”插座“,用于连接电器与电线,如下所示 客户端:应用程序 <----> Soc ...

  2. maven搭建webservice apache cxf实现

    用 web方式发布 webService 服务端.客户端 一.服务器端搭建 1.首先创建 一个web工程(增加Maven依赖) 2.增加Maven依赖包,如下: <project xmlns=& ...

  3. MySQL删除大表时潜在的问题(drop table,truncate table)

    来源于:https://www.cnblogs.com/CtripDBA/p/11465315.html,侵删,纯截图,避免吸引流量之嫌 case1,删除大表时,因为清理自适应hash索引占用的内容导 ...

  4. rocksdb学习笔记

    rocksdb是在leveldb的基础上优化而得,解决了leveldb的一些问题. 主要的优化点 1.增加了column family,这样有利于多个不相关的数据集存储在同一个db中,因为不同colu ...

  5. Weather with you主题说明

    使用前请确保拥有js权限!!! 源代码: css: /*广告去死*/ #ad_t2 { display: none !important; } #i-amphtml-fill-content { di ...

  6. 09. Go 语言并发

    Go 语言并发 并发指在同一时间内可以执行多个任务.并发编程含义比较广泛,包含多线程编程.多进程编程及分布式程序等.本章讲解的并发含义属于多线程编程. Go 语言通过编译器运行时(runtime),从 ...

  7. 阿里云搭建wordpress博客教程

    一 :搭建环境 1.安装Apache环境 在线安装Apache yum install httpd 启动Apache服务 service httpd start 设置开机自启动 chkconfig h ...

  8. 小程序-picker组件选择数量

    <!-- detail.wxml --> <view class="picker"> <picker range="{{range}}&qu ...

  9. 在CV尤其是CNN领域的一些想法

    现在的CNN还差很多,未来满是变数. 你看,现在的应用领域也无非merely就这么几类----分类识别,目标检测(定位+识别),对象分割......,但是人的视觉可不仅仅这么几个功能啊!是吧. 先说说 ...

  10. JS获取url请求参数

    JS获取url请求参数,代码如下: // 获取url请求参数 function getQueryParams() { var query = location.search.substring(1) ...