POJ 3253 Fence Repair(简单哈弗曼树_水过)
题目大意:原题链接
锯木板,锯木板的长度就是花费。比如你要锯成长度为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(简单哈弗曼树_水过)的更多相关文章
- POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53645 Accepted: 17670 De ...
- PKU 1521 Entropy(简单哈弗曼树_水过)
题目大意:原题链接 给你一个字符串,首先是计算出一个按正常编码的编码长度,其次是计算出一个用霍夫曼编码的编码长度,最后求正常编码的长度除以霍夫曼编码长度的比值,保留一位小数. 解题思路:需要知道 1. ...
- 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(哈夫曼编码)
题目链接:http://poj.org/problem?id=3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个 ...
- 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(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
随机推荐
- jquery cdn加速注意事项
1, <script src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script> 这里 ...
- 《Programming with Objective-C》第五章 Customizing Existing Classes
1.分类里面只新增函数,不要新增变量:虽然新增是语法合法的,但是编译器并不会为你的property合成相应的成员变量.setter和getter Categories can be used to d ...
- Objective-C Runtime初探:self super
题目 上题目,已知A是爷爷,B是爸爸,C是孙子. @interface A : NSObject - (void)f; @end @interface B : A - (void)f; - (void ...
- Python爬虫(七)
源码: import requests import re from my_mysql import MysqlConnect # 获取详情页链接和电影名称 def get_urls(page): u ...
- yii 常用orm
yii2 orwhere andwhere的复杂写法:https://www.codercto.com/a/6513.html $files = XXXX::find() ->andWhere( ...
- Dynamics CRM 2015 Update 1 系列(3): API的那些事 - Old APIs VS New APIs
今天我们来看看API的变化.新系统中,去掉了一些经常使用的数据处理API,比如:SetStateRequest, SetBusinessUnitRequest, SetParentBusinessUn ...
- 如何在Myeclipse中启动多个Tomcat
比如:有两个版本的tomcat,一个5.*,一个6.*,此时由于两个工程分别部署在两个版本的tomcat下,需要同时启动两个tomcat,以下是方法: 1.特别要注意: 不要设置CATALINA_HO ...
- git练习
git commit 提交记录 git branch <branch_name> 建立名为branch_name的分支 git checkout <name>:git comm ...
- 76、android:supportsRtl 和 android:layout_marginEnd
android4.2(SDK版本为17)有一个新特性 layoutRtl,当然是对于开发者而言的,主要是方便开发者去支持阿拉伯语/波斯语等阅读习惯是从右往左的. 可以在manifest的applica ...
- SQL ALTER TABLE 命令
SQL ALTER TABLE 命令 SQL ALTER TABLE 命令用于添加.删除或者更改现有数据表中的列. 你还可以用 ALTER TABLE 命令来添加或者删除现有数据表上的约束. 语法: ...