POJ 3253 Fence Repair【二叉堆】
题意:给出n根木板,需要把它们连接起来,每一次连接的花费是他们的长度之和,问最少需要多少钱。
和上一题果子合并一样,只不过这一题用long long
学习的手写二叉堆的代码,再好好理解= =
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#define mod=1e9+7;
using namespace std; typedef long long LL;
const int maxn=;
int l[maxn];
int n,h,minn;
LL ans; void heap_sort(int x){//这是一个小根堆
int lg,lr; //lg代表头的左边的节点, lr代表头的右边的节点
while((x<<)<=h){
lg=x<<;
lr=(x<<)+; if(lr<=h&&l[lg]>l[lr])
lg=lr;
if(l[x]>l[lg]){
swap(l[x],l[lg]);
x=lg;
}
else
break;
}
} int main(){
int i;
while(scanf("%d",&n)!=EOF){
for(i=;i<=n;i++)
scanf("%d",&l[i]); h=n;
for(i=n/;i>;i--)
heap_sort(i); ans=; while(h>){
minn=l[];//取出当前的根,也就是当前最小的一个数
l[]=l[h];h--;//把最后一个数放到第一个位置
heap_sort();//下降操作 minn+=l[];//下降完之后,又取出当前的根,也即为最小的一个数
l[]=minn;//将当前新和成的这一堆又加进去
heap_sort();//下降操作 ans+=minn;
} printf("%I64d\n",ans);
}
return ;
}
把上一题代码稍微一改,用优先队列
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#define mod=1e9+7;
using namespace std; typedef long long LL; int main(){ int n,a;
while(scanf("%d",&n)!=EOF){
priority_queue<int,vector<int>,greater<int> > pq;
LL ans=; for(int i=;i<=n;i++){
scanf("%d",&a);
pq.push(a);
} while(pq.size()>){
int x=pq.top();pq.pop();
int y=pq.top();pq.pop();
ans+=x+y;
pq.push(x+y);
}
printf("%I64d\n",ans);
}
return ;
}
POJ 3253 Fence Repair【二叉堆】的更多相关文章
- poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...
- 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 ...
- 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(修农场栅栏,锯木板)(小根堆 + 哈弗曼建树得最小权值思想 )
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 28359 Accepted: 9213 Des ...
- POJ 3253 Fence Repair (贪心)
Fence Repair Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- poj 3253:Fence Repair(堆排序应用)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23913 Accepted: 7595 Des ...
- POJ 3253 Fence Repair(哈夫曼树)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26167 Accepted: 8459 Des ...
- poj 3253 Fence Repair(priority_queue)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 40465 Accepted: 13229 De ...
随机推荐
- 【转载】OpenStack Swift学习笔记
免责声明: 本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除. 原文作者:崔炳华 原文地址:http://blog.csdn.net/i_ch ...
- Matlab中transpose函数的使用
就是转置的意思,和'一个意思,但是并不重复,因为在cellfun中你无法'这样吧,所以有了这个函数,’只是符号. K>> aa = magic(4) aa = 16 2 3 13 5 11 ...
- 因SELinux引起的用户登录问题解决案例
增强安全性带来的负作用往往是牺牲便利性,就像北京地铁的安检一样,但有些时候我们确实需要它. 案例是,用户有一台安装了KylinOS(国产麒麟,使用的是redhat的内核)的系统,当我们对其系统文件 ...
- POJ 1666
#include<iostream> using namespace std; int main() { int num_stu; int i; ; do{ time=; cin>& ...
- javascript背景淡入淡出
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 深入浅出ES6(七):箭头函数 Arrow Functions
作者 Jason Orendorff github主页 https://github.com/jorendorff 箭头符号在JavaScript诞生时就已经存在,当初第一个JavaScript教 ...
- Sublime Text 3 搭建 React.js 开发环境
sublime有很强的自定义功能,插件库很庞大,针对新语言插件更新很快,配合使用可以快速搭建适配语言的开发环境. 1. babel 支持ES6, React.js, jsx代码高亮,对 JavaScr ...
- 0环境设置 - Statspack设置
简单说明 Statspack主要用于永久存储performance statistics 信息 只有作为sysdba连接时才能安装Statspack. 然后改目录到#cd $ORACLE_HOME/r ...
- FileOutputStream和FileInputStream
package one.string; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFound ...
- lintcode:Matrix Zigzag Traversal 矩阵的之字型遍历
题目: 矩阵的之字型遍历 给你一个包含 m x n 个元素的矩阵 (m 行, n 列), 求该矩阵的之字型遍历. 样例 对于如下矩阵: [ [1, 2, 3, 4], [5, 6, 7, 8], [9 ...