poj 3253 优先队列 堆

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

Fence Repair
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 39924 Accepted: 13022

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 lengthLi (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into theN 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 theN-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 theN 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 makeN-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).



题意:

就是把木头不断的砍开

比如说,砍开长为 20 的,那么就要花费二十的费用



题解:

使用最小堆实现优先队列:


先取出最小的保存

然后维护最小堆

然后再取最小的和之前的相加

继续维护最小堆




#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define father(i) (i>>1)
#define left(i) (i<<1)
#define right(i) (i<<1|1)
#define swap(a,b) {int t=a;a=b;b=t;}

int heap_size;
int a[20010];

void min_heapify(int i)
{
    int l=left(i);
    int r=right(i);
    int largest;
    if(l<=heap_size&&a[l]<a[i])
        largest=l;
    else
        largest=i;

    if(r<=heap_size&&a[r]<a[largest])
        largest=r;

    if(largest!=i){
        swap(a[i],a[largest]);
        min_heapify(largest);
    }
}

void build_min_heap()
{
    for(int i=heap_size/2;i>=1;i--)
        min_heapify(i);
}

void heap_sort()
{
    for(int i=heap_size;i>=2;i--){
        swap(a[1],a[i]);
        heap_size--;
        min_heapify(1);
    }
}

int main()
{
    int n;
    //freopen("in.txt","r",stdin);
    scanf("%d",&n);
    heap_size=n;
    a[0]=0;
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);

    long long ans=0;
    build_min_heap();

    for(int i=1;i<n;i++){
        int temp=a[1];
        a[1]=a[heap_size];
        heap_size--;
        min_heapify(1);

        a[1]+=temp;
        ans+=a[1];
        min_heapify(1);
        temp=a[1];

    }

    printf("%lld\n",ans);
    return 0;
}


AUTOSAR开发实战:NvM模块如何解决汽车电子中的EEPROM与Flash存储难题? 本文深入探讨了AUTOSAR中的NvM模块如何有效解决汽车电子系统中EEPROM与Flash存储的难题。通过分层架构设计,NvM模块实现了硬件抽象与统一管理,支持数据一致性检查、错误恢复等高级功能,显著提升了存储可靠性和耐久性。文章结合实战案例,详细解析了NvM的核心机制及工程优化方案,为汽车电子开发者提供宝贵参考。 阅读详情

相关推荐

SQLite性能优化:从WAL模式到内存映射的深度解析

本文深入解析SQLite性能优化策略,重点介绍WAL模式和内存映射技术的应用。通过合理配置WAL日志、同步策略和事务批处理,SQLite的IO吞吐量可提升3-5倍。结合内存映射和缓存优化,性能可进一步提升至默认配置的50倍,适用于高并发读写场景。

weixin_29231027的博客 460

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

从零开始的BW(五) 外部系统供数之OData测试

5、激活添加的服务,如提示下图中的激活失败,根据生成URI在事务代码SICF目录中找到对应的服务,设置超时时间,设置完之后重新激活即可,详情可参考SAP note 1986626。如果刚才设置BY Odata的query在这没有显示,SE38执行程序EQ_RS_AUTOSETUP,选中需要生成服务的query,生成服务后再去添加服务。测试get方法,URI中默认的参数是?4、事务代码/IWFND/MAINT_SERVICE中添加服务,如果提示用户不再登陆,在事务代码前加上/O或者/N即可。

m0_61386064的博客 1115

借锯子的农民

#原题: 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的博客 371

LLC混合滞回控制 vs 电压模/电流模:UCC25640x仿真对比与选型指南

在开关电源设计中,LLC谐振变换器的环路补偿是核心难点。传统电压模控制(VMC)和电流模控制(CMC)在LLC拓扑上存在环路增益漂移、动态响应慢等局限。混合滞回控制通过滞回比较器直接调制开关频率,并融合输出电压与谐振腔电流信息,实现了自适应环路增益和快速瞬态响应。该技术显著提升轻载效率与动态性能,适用于高密度适配器、快充及车载充电机等场景。本文基于UCC25640x系列,通过仿真对比三种控制策略,为工程选型与调试提供实践参考。

weixin_33925615的博客 343

JetBrains Air:多Agent协同的IDE运行时架构解析

IDE智能编程正从单模型增强迈向多Agent协同范式。多Agent架构通过职责分离、上下文精简与运行时隔离,显著提升代码生成稳定性与合规性;其核心依赖轻量级嵌入式运行时(如ARL),支持AST级内存共享、eBPF安全拦截与毫秒级冷启动。该架构天然适配本地化部署、离线开发与高安全合规场景(如PCI-DSS、金融内网),并为Java/Python/TS等语言提供可扩展的规则驱动开发流水线。JetBrains Air正是这一技术路径的工程化落地代表。

weixin_30443075的博客 590

哈夫曼树-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

绝尘 836

当迁移学习遇见室内定位:基于UJIIndoorLoc的跨场景适配实践

本文探讨了迁移学习在跨建筑室内定位中的应用,重点解析了UJIIndoorLoc数据集的特性及其在解决设备差异和环境变化中的关键作用。通过特征工程、迁移学习策略和小样本微调技术,显著提升了定位模型的跨场景适配能力,为实际部署提供了有效解决方案。

weixin_29038155的博客 398

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的博客 672

循环卷积和线性卷积(矩阵视角)

矩阵视角的循环卷积核线性卷积

shengsong 3586

Cadence Virtuoso Layout 版图绘制

画好原理图,打好pin脚(pin最好以全大写的形式书写,以防后续操作中可能出现Bug) 查看所使用工艺库的design rule文件,确定栅格单位设置大小 在准备绘制的原理图界面启动layout XL/GXL 在layout界面按e,设置网格大小与design rule匹配 直接根据原理图生成版图 根据原理图连接关系显示版图中未连接的飞线 查看和更改原理图与版图中器件对应的链接关系 shift + f:显示器件版图细节 版图的构成:元器件(工艺厂商提供)、器件连接、阱连接、IO和PAD模拟电路

bobo_supper的博客 2万+

poj 3253 最小堆 优先队列 哈夫曼树

import java.io.*; class Heap { private final int maxn = 50010; int[] data; int r; Heap() { data = new int[maxn]; r = 0; } public int size() { return r; } void swap(int a, int b) {

aksdrxzd的专栏 475

一、Abaqus CAE 入门操作(薄膜应力计算)

一、Abaqus CAE 入门操作(薄膜应力计算)

A20802013的博客 4372

poj3253 堆/优先队列

如题:http://poj.org/problem?id=3253 Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28608   Accepted: 9295 Description Farmer John wants to repai

zht的专栏 464

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

POJ 3253 Fence Repair 堆的应用,优先队列

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

churehill123的专栏 1004

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

poj_3253 Fence Repair(优先队列 / 堆)

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

凳子 337

POJ3253 Fence Repair 贪心+优先队列(堆维护)

1.题目描述: Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45130   Accepted: 14715 Description Farmer John wants to repair a small length of

终有绿洲摇曳在沙漠 434

poj 3253 Fence Repair (哈夫曼树 优先队列)

题目:http://poj.org/problem?id=3253 没用long long wrong 了一次 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<stack&gt...

weixin_30505485的博客 73

poj3253--pat---优先队列---堆

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

海水梦悠悠的博客 778

POJ 3253 Fence Repair (哈夫曼树,堆)

一道很明显的哈夫曼树题 看discuz有人不知道为什么是哈夫曼树,这里稍微证明一下 题目说砍木板,收费等于要砍的木板的长度,要求费用最少 其实这里有点跟贪心的策略相同,总是要砍这些长度的,所以先把“长的”(不一定是单条长的,可以是合起来长的)先砍掉,这样很明显优于把“长的”留在后面砍的情况(因为那个长度至少要被收费两次) 现在回想一下哈夫曼树的结构,不知道哈夫曼树的请看:http://ba

xtq的专栏 1144

一、实用数据结构

主要知识点:并查集、优先队列

Shanhj的博客 611

c语言二叉堆《学习记录》poj3253

两个小时的泪水啊,http://poj.org/problem?id=3253,各种bug,下次做题前先思考久一点,免得和这次一样做没用的劳动#include #define INF 50001 __int64 heap[INF],arr[INF], size = 0; void push(__int64 x){ __int64 i = size++; while(i > 0){ __i

wu__peng的博客 470

堆和优先队列

参考链接:https://www.cnblogs.com/xzxl/p/7266404.html 优先队列能够完成下列操作的数据结构叫做优先队列。 插入一个数值 取出最小的数值(获得数值,并且删除) 能够使用二叉树高效地解决上述问题的,是一种叫做“堆” 的数据结构。 堆的性质,主要是通过堆排序来体现。 Java和c++中都有相...

weixin_30617797的博客 180

【POJ3253】Fence Repair(合并果子的翻版)

题面:【POJ3253】Fence Repair 考虑到每一次切割所花费的代价是所切割的木板的长度,那我们就可以反向思考,合并两块木板所花费的代价是这两块木板的长度和,那么问题就转化为了将输入中给出的N块木板给两两合并在一起。于是,我们会惊奇地发现这道题其实就是合并果子的翻版,一道用小根堆来优化的贪心题! 当然了,手打堆是比较麻烦的,相比之下,C++自带的优先队列就好用多了。代码见下: #...

CXR的博客 306

[解题报告]POJ 1521 ZOJ 2399 POJ 3253 哈夫曼树,优先队列

这三题都是哈夫曼树的应用,使用了STL中的优先队列 priority_queue,即在每次插入后都会保证队列中的数按优先级有序.关于priority_queuegreater >, 第一个参数为队列中的数据类型,第二个参数为容器类型,第三个参数为比较方法,常用

飞熊入梦 2118
上一篇: poj 3169 差分约束
下一篇: poj 1511 SPFA
「已注销」
博客等级 码龄11年 120粉丝 326原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值