题目大意:原题链接

锯木板,锯木板的长度就是花费。比如你要锯成长度为8 5 8的木板,最简单的方式是把21的木板割成13,8,花费21,再把13割成5,8,花费13,共计34,当然也可以先割成16,5的木板,花费21,再把16割两个8,花费16,总计37,现在就是问你花费最少的情况。

思路:转化为哈弗曼树

解法一:AC

#include<cstdio>
#include<algorithm>
#define maxn 20010
using namespace std;
int n,a[maxn];
void get_min(int x)
{
int p=x;
for(int i=x+;i<=n;i++){
if(a[i]<a[p])
p=i;
}
swap(a[p],a[x]);
}
int main()
{
while(scanf("%d",&n)!=EOF){
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
long long ans=;
for(int i=;i<n;i++){
get_min(i);
ans+=a[i];
get_min(i+);
ans+=a[i+];
a[i+]+=a[i];
}
printf("%lld",ans);
}
}

解法二:优先队列AC

#include<cstdio>
#include<queue>
using namespace std;
int n,d;
priority_queue<int,vector<int>,greater<int> > que;
int main()
{
while(scanf("%d",&n)!=EOF){
while(!que.empty())
que.pop();
for(int i=;i<=n;i++){
scanf("%d",&d);
que.push(d);
}
long long ans=;
while(que.size()!=){
int x=que.top();
que.pop();
int y=que.top();
que.pop();
ans+=(x+y);
que.push(x+y);
}
printf("%lld\n",ans);
}
}

POJ 3253 Fence Repair(简单哈弗曼树_水过)的更多相关文章

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

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

  2. PKU 1521 Entropy(简单哈弗曼树_水过)

    题目大意:原题链接 给你一个字符串,首先是计算出一个按正常编码的编码长度,其次是计算出一个用霍夫曼编码的编码长度,最后求正常编码的长度除以霍夫曼编码长度的比值,保留一位小数. 解题思路:需要知道 1. ...

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

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

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

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

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

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

  6. POJ 3253 Fence Repair(哈夫曼编码)

    题目链接:http://poj.org/problem?id=3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个 ...

  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(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

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

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

随机推荐

  1. hdu 1756:Cupid's Arrow(计算几何,判断点在多边形内)

    Cupid's Arrow Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  2. RabbitMQ组成及原理介绍-3

    rabbitmq作为成熟的企业消息中间件,实现了应用程序间接口调用的解耦,提高系统的吞吐量. 1.RabbitMQ组成 是由 LShift 提供的一个 Advanced Message Queuing ...

  3. maven 完整的settings.xml

    maven 完整的settings.xml <?xml version="1.0" encoding="UTF-8"?> <!-- Licen ...

  4. Qualcomm Vuforia SDK背景

    参考视频:http://edu.csdn.net/course/detail/1467/23125?auto_start=1 一:概述 官网:www.vuforia.com 应用方向:产品交互.虚拟购 ...

  5. django定义app名称

    1.apps.py class AccountConfig(AppConfig): name = 'account' verbose_name = u'用户信息' # app显示中文 2. __ini ...

  6. a标签点击后,给a标签添加样式

    window.onload=function(){ var a = document.getElementById("cate").getElementsByTagName(&qu ...

  7. Linux产生随机数的几种常见方法

    方法1: [root@localhost ~]# echo $RANDOM 方法二 [root@localhost ~]# openssl rand -base64 7X6HMer5hhY= 方法三 ...

  8. videoview 播放视频

    -videoVIew 继承SurfaceView 使用android的VideoView来播放一个视频,步骤是: 1,在xml中创建一个videoView, 2,在java中导入,然后创建一个Medi ...

  9. Objective-C代码学习大纲(3)

    Objective-C代码学习大纲(3) 2011-05-11 14:06 佚名 otierney 字号:T | T 本文为台湾出版的<Objective-C学习大纲>的翻译文档,系统介绍 ...

  10. 给vmware虚拟机设置Ip,使用xshell远程连接Centos

    参考下面两位的分享才弄好,发表之前先对原作者表示感谢! 给Centos配置网络以及使用xshell远程连接Centos http://www.cnblogs.com/fuly550871915/p/4 ...