POJ 3253 Fence Repair(哈夫曼编码)
题目链接: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(哈夫曼编码)的更多相关文章
- Poj 3253 Fence Repair(哈夫曼树)
Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- poj 3253 Fence Repair (哈夫曼树 优先队列)
题目:http://poj.org/problem?id=3253 没用long long wrong 了一次 #include <iostream> #include<cstdio ...
- BZOJ 3253 Fence Repair 哈夫曼树 水题
http://poj.org/problem?id=3253 这道题约等于合并果子,但是通过这道题能够看出来哈夫曼树是什么了. #include<cstdio> #include<c ...
- POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53645 Accepted: 17670 De ...
- POJ 3253 Fence Repair(简单哈弗曼树_水过)
题目大意:原题链接 锯木板,锯木板的长度就是花费.比如你要锯成长度为8 5 8的木板,最简单的方式是把21的木板割成13,8,花费21,再把13割成5,8,花费13,共计34,当然也可以先割成16,5 ...
- 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 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- 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 STL 优先队列
这题做完后觉得很水,主要的想法就是逆过程思考,原题是截断,可以想成是拼装,一共有n根木棍,最后要拼成一根完整的,每两根小的拼成一根大的,拼成后的木棍长度就是费用,要求费用最少.显然的是一共会拼接n-1 ...
随机推荐
- WPA-PSK无线网络破解原理及过程
原文链接地址:http://www.freebuf.com/articles/wireless/58342.html 本文将主要讲讲WPA-PSK类型的无线网络安全问题,首先我们看下802.11协议相 ...
- BZOJ1072 排列perm 【状压dp】
Description 给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除(可以有前导0).例如123434有90种排列能 被2整除,其中末位为2的有30种,末位为4的有60种. Inpu ...
- codeforces765F Souvenirs
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 使用Empire自动获取域管理员
使用Empire自动获取域管理员 译:backlion 前言 自从Empire和BloodHound被应用来,对AD渗透已经可以获取到内网环境95%的信息量.作者发现自己一遍又一遍地在做同样重复的事 ...
- HTTP协议格式【转】
原文链接:https://www.jianshu.com/p/8fe93a14754c 一.URI结构 HTTP使用统一资源标识符(URI)来传输数据和建立连接.URL(统一资源定位符)是一种特殊种类 ...
- [LeetCode] 25. Reverse Nodes in k-Group ☆☆☆
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- PHP扩展--vld查看opcode代码
vld安装 wget http://pecl.php.net/get/vld-0.13.0.tgz tar zxvf vld-0.13.0.tgz cd vld-0.13.0 /usr/local/p ...
- C#为何不推荐在构造函数中访问虚成员
如果在一个类中定义了虚属性或者虚方法,又在构造函数中访问了这个虚属性或方法,此时VisualStudio是不会给出警告,并且编译也没有问题,但是如果安装了Resharper插件则会给出警告提示:&qu ...
- Codeforces Round #419 (Div. 2) A-E
上紫啦! E题1:59压哨提交成功翻盘 (1:00就做完了调了一个小时,还好意思说出来? (逃)) 题面太长就不复制了,但是配图很可爱所以要贴过来 九条可怜酱好可爱呀 A - Karen and Mo ...
- 聊聊spring的那些扩展机制
1.背景 慎入:本文将会有大量代码出入. 在看一些框架源码的时候,可以看见他们很多都会和Spring去做结合.举个例子dubbo的配置: 很多人其实配置了也就配置了,没有去过多的思考:为什么这么配置s ...