题目链接:http://poj.org/problem?id=3253

题目大意:

有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度

给定各个要求的小木板的长度,及小木板的个数n,求最小费用

3

5 8 5为例:

先从无限长的木板上锯下长度为 21 的木板,花费 21

再从长度为21的木板上锯下长度为5的木板,花费5

再从长度为16的木板上锯下长度为8的木板,花费8

总花费 = 21+5+8 =34

解题思路:哈夫曼编码模板

代码:

使用数组的普通方法:

 #include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
//输入
int n,a[]; //使用数组维护最大最小值(每次维护复杂度O(n))
void solve1(){
ll ans=; while(n>){
int mi1=,mi2=;//mi1最小值的下标,mi2次小值的下标
if(a[mi1]>a[mi2]) swap(mi1,mi2);
for(int i=;i<n;i++){
if(a[mi1]>a[i]){
mi2=mi1;
mi1=i;
}
else if(a[mi2]>a[i]){
mi2=i;
}
}
int t=a[mi1]+a[mi2];
ans+=t;
if(mi1==n-) swap(mi1,mi2);
a[mi1]=t;
a[mi2]=a[n-];
n--;
}
cout<<ans<<endl;
}
int main(){
cin>>n;
for(int i=;i<n;i++){
cin>>a[i];
}
solve1();
return ;
}

使用STL优先队列:

 #include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
//输入
int n,a[];
//优先队列维护最小与次小值(初始化复杂度O(n),每次维护复杂度O(log2n))
void solve2(){
priority_queue<ll,vector<ll>,greater<ll> >q;
ll ans=;
for(int i=;i<n;i++){
q.push(a[i]);
}
while(q.size()>){
ll mi1=q.top();//最小
q.pop();
ll mi2=q.top();//次小
q.pop();
q.push(mi1+mi2);//合并
ans+=mi1+mi2;
}
cout<<ans<<endl;
}
int main(){
cin>>n;
for(int i=;i<n;i++){
cin>>a[i];
}
solve2();
return ;
}

POJ 3253 Fence Repair(哈夫曼编码)的更多相关文章

  1. Poj 3253 Fence Repair(哈夫曼树)

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

  2. poj 3253 Fence Repair (哈夫曼树 优先队列)

    题目:http://poj.org/problem?id=3253 没用long long wrong 了一次 #include <iostream> #include<cstdio ...

  3. BZOJ 3253 Fence Repair 哈夫曼树 水题

    http://poj.org/problem?id=3253 这道题约等于合并果子,但是通过这道题能够看出来哈夫曼树是什么了. #include<cstdio> #include<c ...

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

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

  5. POJ 3253 Fence Repair(简单哈弗曼树_水过)

    题目大意:原题链接 锯木板,锯木板的长度就是花费.比如你要锯成长度为8 5 8的木板,最简单的方式是把21的木板割成13,8,花费21,再把13割成5,8,花费13,共计34,当然也可以先割成16,5 ...

  6. POJ 3253 Fence Repair(修篱笆)

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

  7. poj 3253 Fence Repair 优先队列

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

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

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

  9. POJ 3253 Fence Repair STL 优先队列

    这题做完后觉得很水,主要的想法就是逆过程思考,原题是截断,可以想成是拼装,一共有n根木棍,最后要拼成一根完整的,每两根小的拼成一根大的,拼成后的木棍长度就是费用,要求费用最少.显然的是一共会拼接n-1 ...

随机推荐

  1. NOIP2015运输计划题解报告

    这题在洛谷上可以找到提交 P2680运输计划 题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航 ...

  2. 深入详解windows安全认证机制ntlm&Kerberos

    0x01 为什么要理解windows 安全认证机制: 加深对后续各种漏洞利用的理解深度,还是那句话,要知其然,更要知其所以然,不废话,咱们直接开始 0x02 windows认证协议主要有以下两种: 基 ...

  3. 用Visual C#开发基于OpenCV的Windows应用程序

    http://blog.163.com/wangxh_jy/blog/static/28233883201001581640283/ 关于详细的配置及程序运行截图,请下载:http://downloa ...

  4. Educational Codeforces Round 24 A 水 B stl C 暴力 D stl模拟 E 二分

    A. Diplomas and Certificates time limit per test 1 second memory limit per test 256 megabytes input ...

  5. 关于GCD的几个结论

    设a和b的最大公约数是d,那么: 1. d是用sa+tb(s和t都是整数)能够表示的最小正整数 证明:设x=sa+tb是sa+tb能够表示出的最小正整数.首先,有d|x,证明如下: 因此有x>= ...

  6. laravel 5.1 单元测试 Cannot modify header information 错误

    运行phpunit的时候加上参数 --stderr ./vendor/bin/phpunit --stderr

  7. mysql ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)错误解决办法

    我的电脑是win10,所用的是mysql5.7.14 近期在学习mysql数据库的时候,遇到了这个错误,我的密码错误了.突如其来的问题,很是蒙蔽,因为我没对数据库设置过密码.通过网上查询,可以通过进入 ...

  8. Redis 为什么用跳表而不用平衡树

    Redis 为什么用跳表而不用平衡树? 本文是<Redis内部数据结构详解>系列的第六篇.在本文中,我们围绕一个Redis的内部数据结构--skiplist展开讨论. Redis里面使用s ...

  9. Linux iptables:规则原理和基础

    什么是iptables? iptables是Linux下功能强大的应用层防火墙工具,但了解其规则原理和基础后,配置起来也非常简单. 什么是Netfilter? 说到iptables必然提到Netfil ...

  10. K8S Link

    https://www.cnblogs.com/linuxk/p/9783510.html https://www.cnblogs.com/fengzhihai/p/9851470.html