POJ 3253 Fence Repair【二叉堆】

时间:2021-12-01 10:05:25

题意:给出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&lpar;修农场栅栏,锯木板&rpar;(小根堆 &plus; 哈弗曼建树得最小权值思想 )

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

  6. POJ 3253 Fence Repair &lpar;贪心&rpar;

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

  7. poj 3253&colon;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&lpar;priority&lowbar;queue&rpar;

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

随机推荐

  1. Web 入门之 XML

      160916   1. 什么是XML?   XML 是 EXtensible Markup Language 的缩写,称为可扩展标记语言,所谓可扩展指用户可根据XML规则自定义标记.例子1-1 = ...

  2. sqlserver 通用分页存储过程&lpar;转&rpar;

    USE [AAA_TYDC] GO /****** Object: StoredProcedure [dbo].[proc_DataPagination] Script Date: 11/20/201 ...

  3. java项目导入IntelliJ IDEA

    (0)之所以有第0步,是因为第一次倒入失败,所以从删除上次倒入的数据开始- 开始删除数据. 启动Intelli J,点击右键删除上次的导入的项目 把配置拷贝到.m2文件夹下,并且删除上次下载的一些依赖 ...

  4. hdu2094 set初体验

    有一群人,打乒乓球比赛,两两捉对撕杀,每两个人之间最多打一场比赛.球赛的规则如下:如果A打败了B,B又打败了C,而A与C之间没有进行过比赛,那么就认定,A一定能打败C.如果A打败了B,B又打败了C,而 ...

  5. crontab 移动日志-超越昨天的自己系列&lpar;12&rpar;

    linux上定时执行某些脚本是管理服务器的时候比较常用的场景,比如定时检查进程是否存在,定时启动或关闭进程,定时检查日志删除日志等. 当我打开google百度crontab时长篇大论的一大堆,详细解释 ...

  6. PHP获取当前日期和时间的方法

    PHP获取当前日期和时间的方法 来源:wikiHow   时间:2014-12-04 14:49:45   阅读数:7240 分享到:0 [导读] PHP是用来创建网络中动态内容的常见语言,因此PHP ...

  7. Ensemble Learning 之 Gradient Boosting 与 GBDT

    之前一篇写了关于基于权重的 Boosting 方法 Adaboost,本文主要讲述 Boosting 的另一种形式 Gradient Boosting ,在 Adaboost 中样本权重随着分类正确与 ...

  8. RFID FDX HDX Technology

    Got a tough RF environment? Turn to TI’s proven LF technology TI’s low-frequency (LF) technology has ...

  9. RANSAC算法详解

    给定两个点p1与p2的坐标,确定这两点所构成的直线,要求对于输入的任意点p3,都可以判断它是否在该直线上.初中解析几何知识告诉我们,判断一个点在直线上,只需其与直线上任意两点点斜率都相同即可.实际操作 ...

  10. Android应用程序绑定服务(bindService)的过程源码分析

    Android应用程序组件Service与Activity一样,既能够在新的进程中启动,也能够在应用程序进程内部启动:前面我们已经分析了在新的进程中启动Service的过程,本文将要介绍在应用程序内部 ...