题意:给出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【二叉堆】的更多相关文章

  1. poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...

  2. POJ 3253 Fence Repair(修篱笆)

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

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

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

  4. poj 3253 Fence Repair 优先队列

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

  5. POj 3253 Fence Repair(修农场栅栏,锯木板)(小根堆 + 哈弗曼建树得最小权值思想 )

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28359   Accepted: 9213 Des ...

  6. POJ 3253 Fence Repair (贪心)

    Fence Repair Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  7. poj 3253:Fence Repair(堆排序应用)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23913   Accepted: 7595 Des ...

  8. POJ 3253 Fence Repair(哈夫曼树)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26167   Accepted: 8459 Des ...

  9. poj 3253 Fence Repair(priority_queue)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 40465   Accepted: 13229 De ...

随机推荐

  1. HTTP - 内容编码

    HTTP 应用程序有时在发送之前需要对内容进行编码.例如,在把很大的 HTML 文档发送给通过慢速连接上来的客户端之前,服务器可能就会对它进行压缩,这样有助于减少传输实体的时间. 内容编码过程 内容编 ...

  2. WPF简单布局 浅尝辄止

            WPF的窗口只能包含一个元素,为了在WPF窗口中放置多个元素并创建更实用的用户界面,需要在窗口上放置一个容器,然后在容器中放置其它元素. 注意:造成这一限制的原因是window类继承自 ...

  3. 【Leetcode】 - Single Number II

    Problem Discription: Suppose the array A has n items in which all of the numbers apear 3 times excep ...

  4. html5上传文件并监听进度

    出处:   http://blog.csdn.net/small_rice_/article/details/21391625

  5. MAC 13信道

    房东的路由器一直连不上,手机却能连上,前天设置了13信道,后来又忘了,最后找到个连接WIFI的方法,在网络偏好设置里选择向导,诊断中可以连上wifi.

  6. maven工程的如何进行代码调试

    1.maven项目的父项目右键选择:maven build    注意:       1.选择Browser workspace,让BaseDirectory变成:${***}形式.       2. ...

  7. 堆栈中的EIP EBP ESP

    EIP,EBP,ESP都是系统的寄存器,里面存的都是些地址.  为什么要说这三个指针,是因为我们系统中栈的实现上离不开他们三个.  我们DC上讲过栈的数据结构,主要有以下特点:  后进先处.(这个强调 ...

  8. uva 757

    贪心  优先队列 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...

  9. 已收录的帝国cms文章被误删除了怎么办?

    我们一直提倡网站要经常备份,但是有时也会遗忘,一不小心被谁删除了那就欲哭无泪了.就像ytkah刚弄了一个站,开了个权限比较高的后台帐号给别人用,居然把两三个栏目都删除了,想发狂啊.刚好又有段时间没备份 ...

  10. zoj 2974 Just Pour the Water (矩阵快速幂,简单)

    题目 对于案例的解释请见下图: 这道要变动提取一下矩阵,之后就简单了 具体解释可看代码: #include <string.h> #include <stdio.h> #inc ...