地址 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. C#中PadLeft,PadRight的用法

    今天在遇到一个需求的时候,需要一个字符串实现自增.是根据数据库中一个自增的int类型的值,实现自增的.但是要加上前缀.比如,数据库中有一个自增的值,为,2.那么这个自增的值后面的值就位3.4.5.6. ...

  2. java后台树形结构展示---懒加载

    一.数据库设计 二.实体类:entity import com.joyoung.cloud.security.common.validatedGroup.Add;import com.joyoung. ...

  3. Dynamics 365利用Web API对视图进行查询

    我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...

  4. SAP用户角色分配函数权限

    事务码 PFCG修改角色 选择[权限缺省] 选择 RFC 输入的函数只能是允许远程连接的函数,否则不能调用和添加到角色. 完成添加.

  5. Docker 常用操作命令

    一. docker安装  方式1 本地安装: 1)下载docker安装文件: 2)执行安装命令  yum localinstall *: 3)安装完之后 重启 systemctl restart do ...

  6. 如何使用python远程操作linux

    在云服务测试中,往往需要我们进入云服务内容进行相关内容的测试.这测试可以使用平台自身的noVNC.外部辅助xshell等工具连接到云服务内部进行测试.但是在如此反复的测试操作中,就需要用到自动化测试方 ...

  7. zhy2_rehat6_mysql03 - MHA_搭建.txt

    export LANG=en_US 机器 VPN ip linux 账号/密码manager2 172.28.20.131 10.1.1.11 mysql2 - z(主) 172.28.20.133 ...

  8. 安装CentOS 6.x报错"Disk sda contains BIOS RAID metadata"解决方法

    今天在安装CentOS 6.2的时候,当进到检测硬盘的时候,总是过不去,报错如下: Disk sda contains BIOS RAID metadata, but is not part of a ...

  9. web安全技术--XSS和CSRF

    Xss一般是脚本代码,主要是JS的,但是也有AS和VBS的. 主要分为反射型,存储型,DOM型三个大类. 一般来讲在手工测试的时候先要考虑的地方就是哪里有输入那里有输出. 然后是进行敏感字符测试,通常 ...

  10. Java描述设计模式(13):迭代器模式

    本文源码:GitHub·点这里 || GitEE·点这里 一.迭代器模式 1.基础概念 迭代器模式又叫游标模式,是对象的行为模式.迭代器模式可以顺序地访问一个聚集中的元素而不必暴露聚集的内部表象. 2 ...