poj3235 Fence Repair 优先队列 哈弗曼树

数据结构与算法 Farmer John 问题 农夫锯木板问题 Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ... 阅读详情

Fence Repair
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 36244 Accepted: 11744

Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input

Line 1: One integer N, the number of planks 
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank

Output

Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

Sample Input

3
8
5
8

Sample Output

34

Hint

He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8. 
The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).


用一个优先队列求哈弗曼树,对分支点求和,注意数据范围,累加时要用long long


#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

struct node {
	long long n;
	node() {}
	node(long long n) : n(n) {}
	bool operator < (const node& on) const {
		return n > on.n;  //最小堆 
	}
};

int N;
priority_queue<node> Q;

int main()
{
	while (~scanf("%d", &N)) {
		while (!Q.empty()) Q.pop();
		for (int i = 1; i <= N; i++) {
			long long tem;
			scanf("%I64d", &tem);
			Q.push(node(tem));
		}
		long long ans = 0;
		while (!Q.empty()) {
			node t1, t2;
			t1 = Q.top();
			Q.pop();
			//当只剩一个时不能继续合成,直接break
			if (Q.empty()) break; 
			t2 = Q.top();
			Q.pop();
			ans = ans + (t1.n + t2.n);
			Q.push(node(t1.n + t2.n));
		}
		printf("%I64d\n", ans);
	}
	return 0;
}





POJ3253-POJ3253-Fence Repair【STL优先队列】 北大POJ3253-POJ3253-Fence Repair【STL优先队列】 解题报告+AC代码 立即下载

相关推荐

OpenStack 8 节点生产级高可用(HA)集群实施文档

本文详细介绍了生产级高可用OpenStack集群的架构设计与实施方案。通过采用3节点控制集群(HAProxy+Keepalived)、2节点计算资源池和3节点Ceph分布式存储,实现了控制平面、计算资源和存储系统三个层面的高可用。方案重点阐述了VIP漂移、MariaDB Galera集群、RabbitMQ镜像队列等关键组件的配置方法,并提供了完整的部署流程和故障验证方案。该架构确保任意单节点故障不会影响平台整体可用性,满足生产环境对稳定性、扩展性和可维护性的要求,为构建无单点故障的OpenStack生产环境

彭大帅的博客 779

Fence Repair(优先队列)

A - Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a si

weixin_46029049的博客 480

别再死磕文档了!用国产MBSES软件5分钟上手MBSE建模(保姆级图文教程)

本文通过国产MBSES软件,详细演示了如何在5分钟内完成智能家居系统的MBSE建模。从模块图定义到状态机图构建,再到参数约束验证,全程图文指导,帮助工程师快速掌握基于模型的系统工程方法,显著提升复杂系统设计效率。

weixin_26833503的博客 237

借锯子的农民

#原题: Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ...

wddmc的博客 370

【异常】Openclaw Gateway进程异常停止问题分析与解决方案

本文分析了Gateway服务异常停止的问题,提出了三种解决方案。核心问题是缺乏进程守护和自动恢复机制,导致服务崩溃后无法自行重启。推荐方案是使用PM2进程管理工具(方案1),提供进程监控、自动重启和开机自启功能。替代方案包括自定义守护脚本(方案2)和supervisord管理(方案3)。这些方案都能有效解决当前服务脆弱性问题,显著提升服务可用性,其中PM2方案成熟度高、配置简单,是首选解决方案。实施后可将服务从人工干预转变为自动恢复,确保业务连续性。

奶爸的编程之路,也就一周冷个三天 455

Raspberry Pi Pico SDK开发-Timer-定时

RP2040 由 Raspberry Pi 设计,具有双核 Arm Cortex-M0+ 处理器和 264KB 内部 RAM,并支持高达 16MB 的片外闪存。 广泛的灵活 I/O 选项包括 I2C、SPI 和独特的可编程 I/O (PIO)。 本实例将演示如何使用Timer的定时功能。

视觉与物联智能 2040

哈夫曼树-Fence Repair

Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32424 Accepted: 10417DescriptionFarmer John wants to repair a small length of the fence around the pasture. He meas

绝尘 835

实例详解C++程序的五大内存分区

本文详细讲述C++程序在运行时的5大内存分区。 下面使用日常开发中的编程实例,详细介绍一下这5个分区,以便大家能更深刻的理解这5大内存分区。 1、栈内存区 栈内存区是我们用的最多的分区,只要有函数的地方都会使用到这个分区。栈分区是用来存放函数参数及函数局部变量值的内存区,是由编译器在编译时自动分配和释放的。 函数中的参数与函数中的局部变量占用的内存是代码执行到函数(进入函数)是分配的,在............

dvlinker的技术专栏 1万+

Fence Repair(P3253)

要用long long 不然就是一直在WA,,发现数组不够大的时候,有时会显示WA,有时会显示Rumtime Error #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; int m

jiangjiashi的博客 671

基于BEV(鸟瞰图)的3D检测:PointPillars与CenterPoint

基于BEV(鸟瞰图)的3D检测:PointPillars与CenterPoint​ ,人工智能,计算机视觉,大模型,AI,本文围绕基于BEV的3D检测,解析了PointPillars与CenterPoint两种算法。PointPillars用点柱特征编码网络处理点云,通过2D卷积网络提取特征,检测头为解耦头;CenterPoint将三维物体表为点,分两阶段检测,先找中心点及属性,再细化预测。两者都可借特定骨干网生成BEV特征图且基于BEV检测,但检测头设计和物体表示方式不同。

不踩坑,不知道坑有多深;不总结,踩过的坑白踩。 27万+

58子站安居,经纪人营销管理平台登录接口加密逆向

实战场景password 实战分析 本次要解决的站点依旧来源自78技术人社群,有朋友希望再次说明一下其登录模块的加密参数寻找与逆向逻辑,本文我们就一起分析一下。 站点地址为:vip.anjuke.com/portal/login 通过开发者工具,可以简简单单的寻找到相应的数据。

梦想橡皮擦,专栏100例写作模式先行者,现象级专栏 《Python 爬虫 100 例》作者、《滚雪球学 Python 专栏》原创者 2万+

农夫约翰修篱笆-堆排序

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN(1 ≤N≤ 20,000) planks of wood, each having some integer lengthLi(1 ≤Li...

weixin_46204099的博客 918

[ vulhub漏洞复现篇 ] Django SQL注入漏洞复现 CVE-2021-35042

[ vulhub漏洞复现篇 ] Django SQL注入漏洞复现 CVE-2021-35042 Django 组件存在 SQL 注入漏洞,该漏洞是由于对 QuerySet.order_by()中用户提供数据的过滤不足,攻击者可利用该漏洞在未授权的情况下,构造恶意数据执行 SQL 注入攻击,最终造成服务器敏感信息泄露。

qq_51577576的博客 3228

POJ 3253 Fence Repair(优先队列+思维贪心)(哈夫曼树算法)

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ...

nka_kun的博客 529

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

poj3252 Fence Repair 哈夫曼树

Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 51065   Accepted: 16781 Description Farmer John wants to repair a small length of the fence arou

残梦听雨 407

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Fence Repair_哈夫曼树_优先队列2018_2_19

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer lengt...

ujn20161222的博客 279

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

[ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)

Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25274   Accepted: 8131 Description Farmer John wants to repair a small length of the fence aro

逆风的方向 更适合飞翔 2068

【POJ】3253围栏维修

【POJ】3253围栏维修 原题 Fence Repair Time Limit: 2000MS Memory Limit: 65536K Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having som

weixin_46975572的博客 702

POJ3253 Fence Repair【哈夫曼树+优先队列】

Fence RepairTime Limit: 2000MS Memory Limit: 65536KTotal Submissions: 48026 Accepted: 15784DescriptionFarmer John wants to repair a small length of the fence around the pasture. He measures the fence ...

海岛Blog 1622

【POJ】poj 3253 (贪心)

Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN(1 ≤N≤ 20,000) planks of wood, each having some integer le...

ZLei 567

poj3253 Fence Repair _贪心

Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45619   Accepted: 14882 Description Farmer John wants to repair a small length of the fence around the pastu...

wylu 301

poj 3253 Fence Repair之优先队列解法

Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 50747 Accepted: 16646 DescriptionFarmer John wants to repair a small length of the fence around the pasture. He me

天行健,君子以自强不息 561

编程练习三(贪心算法)

A:Fence Repair题目:Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer le

Alex_Liuyuren的博客 1116

堆应用 Fence Repair

题目介绍Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer

codechairmanyuan的博客 411

POJ 3253 Fence Repair (堆排序)

题目连接:~( ̄▽ ̄~)(~ ̄▽ ̄)~ 用堆排序建立小根堆每回选取2个最小的值相加,再把得到的值放回堆中排序 code: #include int num[20009], n = 0; __int64 sum = 0, sum2 = 0; void sift(int l); void heapsort(); int main() { int i = 0; while

Cifer 752

【BZOJ 1724】[Usaco2006 Nov]Fence Repair 切割木板 堆+贪心

堆对于stl priority_queue ,我们自己定义的类自己重载<,对于非自定义类我们默认大根堆,如若改成小根堆则写成std::priority<int,vector<int>,greator<int> >。时间复杂度除了pop push是O(log)外都是O(1)。当然手打会比stl快不少,下面介绍手打堆。对于手打堆他出来用于优先队列之外还能用于堆...

weixin_30723433的博客 218
上一篇: poj1011-Sticks dfs各种剪枝
下一篇: codeforces624B Making a String 贪心 排序
Daemoonn
博客等级 码龄11年 42粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值