POJ 3253 - 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: 21562 Accepted: 6886

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).

==============================

看Hint就知道这题是什么意思了

最开始的思路是从大到小排个序,花费从总数开始,每次减去之前砍掉的部分的长度,一发WA发现问题。

给一组数据:4 2 2 3 3,按我之前的做法得到21,但实际结果是20。先切成4 6,再分别切2 2和3 3。

发现好像是哈夫曼树,从来没写过参考了下别人的代码,是用优先队列写的

顺便学会了优先队列直接int类型从小到大排列的方法:priority_queue< int,vector<int>,greater<int> >que; 

注意答案用long long


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>

using namespace std;

int main()
{
    int n,a[22222];
    scanf("%d",&n);
    long long ans=0;
    priority_queue< int,vector<int>,greater<int> >que;//从小到大。从大到小用less<int>,不过从大到小的话直接用priority_queue<int>que;默认的就行了
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        que.push(a[i]);
    }
    for(int i=0;i<n-1;i++)
    {
        int a=que.top(); que.pop();
        int b=que.top(); que.pop();
        //cout<<"a="<<a<<" b="<<b<<endl;
        ans+=(a+b);
        que.push(a+b);
    }
    printf("%I64d\n",ans);
    return 0;
}


svn co --depth和svn up --set-depth 只check out下一层文件 只更新指定文件 只更新下一层的用法 svn的工程可能很大 有时我们只需要部分文件即可 这里介绍几个命令 1.svn co(check out) --depth --depth=empty,files,immediates,infinity 假设某svn结构如下 svn://root/PRO/A_dir/a svn://root/PRO/B_dir/b svn://root/PRO/c_file svn co --depth=emptysvn://root/PRO 此时只会在本地check out出 PRO/的空目录。 sv. 阅读详情

相关推荐

基于ESP32-P4的低成本旋转式激光雷达系统设计

激光雷达(LiDAR)作为机器人环境感知的核心传感器,其本质是融合光学测距、机电控制与实时数据处理的嵌入式系统。理解时间飞行(ToF)原理与机械旋转耦合机制,是实现高精度点云采集的基础;而系统级误差源——如计时抖动、电机惯性、信号串扰和温漂——直接决定实际建图鲁棒性。在资源受限场景下,选用RISC-V双核MCU(如ESP32-P4)可兼顾实时控制与网络服务能力,通过硬件时间戳捕获、S型加减速轨迹规划、旋转变压器供电及双Bank OTA等工程手段,有效平衡分辨率、帧率与稳定性。本文围绕360°旋转式激光雷达的

weixin_36231030的博客 433

poj3253 huffman tree

<br />Poj 3253 <br /> <br />armer 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) u

bupt_xiaoz 461

5分钟快速验证lvgl界面:VScode模拟器极简配置法(免环境变量版)

本文介绍了一种5分钟快速验证LVGL界面的极简配置方法,使用VSCode模拟器免环境变量配置,适合UI设计师和产品经理快速预览界面效果。通过预编译的SDL2库和配置好的VSCode工程模板,实现开箱即用的LVGL界面验证,大幅提升工作效率。

weixin_30790841的博客 314

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

深度学习(Deep Learning, DL)简介

深度学习(Deep Learning, DL)是机器学习的一个子领域,主要使用多层神经网络来处理复杂的数据和任务。深度学习通过模拟人脑的神经网络结构,能够自动提取特征并进行高效的模式识别和预测。

qq_28791753的博客 1万+

VS Code配置MASM32汇编环境:从安装到调试的完整指南

本文提供了一份详尽的指南,教你如何在VS Code中配置MASM32汇编开发环境,实现从安装、环境变量设置到编写、编译和调试32位Windows汇编窗口程序的完整工作流。通过集成现代编辑器的便利性与经典汇编工具链,旨在降低学习门槛,提升底层开发效率。

nn3456的博客 484

poj 3253 Fence Repair (STL优先队列)

poj 3253 Fence Repair (STL优先队列)

3600

新手也能懂的Simulink汽车二自由度建模:从公式推导到模型搭建全流程

本文为新手提供了在Simulink中搭建汽车二自由度模型的完整实战指南。文章从模型核心概念讲起,详细讲解了如何将车辆动力学方程转化为状态空间模型,并逐步指导在Simulink中实现从参数设置、模块搭建到仿真验证的全过程。通过阶跃转向测试和参数影响分析,帮助读者直观理解车辆转向特性,并提供了实用的模型扩展思路与常见问题排查技巧。

weixin_29047919的博客 213

编程算法 - 篱笆修理(Fence Repair) 代码(C)

篱笆修理(Fence Repair) 代码(C)本文地址: http://blog.csdn.net/caroline_wendy题目: 把一块木板切成N块, 每次切两块, 切割的开销是木板长度, 求将木板切割完的最小开销.贪心算法, 类似二叉树型结构, 最短板和次短板是兄弟结点, 选取两个最小木板, 最后进行切割, 合并两个最小木板, 依次递推.代码:/* * main.cpp * *

AGI 3822

银行监管报送系统介绍(十五):金融审计平台

金审平台主要包括基础、财务、结算、信贷和中间业务五大板块,主要服务于审计署对中国银行的各项审计任务,同时也可为行内审计、稽核检查、数据分析、总分行提供基础数据服务,为实现基础数据共享提供了有力的数据保障。2019年1月某全国性股份制商业银行收到审计署2019-23号文,文中指出为进一步提高审计数据质量,审计署审计组派员前往9家重点商业银行,就完善标准化数据的相关情况与各银行有关部门进行讨论协商。关于数据报送频率及报送内容:数据从2019年4月开始,按月报送,即于每月25日之前报送上月数据;

银行信息系统应用架构详解 2054

(代码可运行)半导体器件和工艺模拟 使用Silvaco设计构建NMOS并提取各项工艺及器件参数

win10。

qq_20388971的博客 110

poj 3253 Fence Repair 贪心

poj 3253 Fence Repair 题目链接: http://poj.org/problem?id=3253 思路: 哈夫曼树型贪心 自底向上每次挑选两个最小的数值相加,相加的值从新合并到数组中,继续挑选相加 如果每次都要排序的话会超时.... 代码: #include <iostream> #include <algorithm> #include ...

ddqb3235的博客 236

使用timeROC包的compare函数比较不同标记物下两个多时间AUC值是否具有统计显著性

时间依赖的接收器操作特征曲线(time-dependent receiver operating characteristic curve, time-dependent ROC curve)是评估和比较标记物在不同时间点上预测能力的重要工具。在R语言中,timeROC包提供了丰富的函数和工具,可用于分析和比较多时间点的AUC值。时间点(timepoints)表示观察的时间点,标记物1(marker1)和标记物2(marker2)分别表示在每个时间点上的预测概率。最后,我们输出比较结果。

2301_79331387的博客 921

【题解】POJ 3253 Fence Repair(贪心)

POJ 3253 Fence Repair https://vjudge.net/problem/POJ-3253 原题 农场主约翰想修理牧场周围一小段篱笆。他测量了栅栏,发现他需要N(1≤N≤20,000)块木板,每块都有整数Li(1≤Li≤50,000)个单位。然后他买了一块长木板,刚好能锯进N块厚木板(也就是木板,其长度为长度Li的和)。FJ忽略了“切缝”,即锯切时锯屑失去的额外长度;你也应该忽略它。 FJ悲伤地意识到他没有锯子来锯木头,所以他就用这块长木板来到农民Don的农场,礼貌地问他是否可

bu_shushu的博客 1516

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

【贪心】POJ 3253:Fence Repair

一、题目内容 POJ 3253 原题地址 二、题意解释 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度,给定各个要求的小木板的长度,及小木板的个数n,求最小费用 如 3 8 8 5: 先从无限长的木板上锯下长度为 21 的木板,花费 21 再从长度为21的木板上锯下长度为5的木板,花费5 再从长度为16的木板上锯下长度为8的木板,花费8 总花费 = 21+5+8 =34 三、代码及注释 #include<stdio.h> #inclu

szy_fasheng的博客 407

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

poj3253 Fence Repair 贪心

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

kyoma的博客 461

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

POJ 3253 Fence Repair【贪心】

传送门:POJ 3253 Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 70430 Accepted: 23193 Description Farmer John wants to repair a small length of the fence around the pasture. ...

Waiting For You 242

贪心算法——Fence Repair(POJ 3253)

题目连接 http://poj.org/problem?id=3253 Fence Repair(POJ 3253) 题目描述 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 ≤...

Uitachi1的博客 480

POJ 3253 fence Repair 一个很有趣的贪心解法

Fence Repair 农夫约翰为了修理栅栏,要将

童凌的技术博客 1346

【OJ】贪心法 Fence Repair POJ 3253 霍夫曼(Huffman)编码原理 acmclub 12326

题目链接: /* 贪心法 Fence Repair POJ 3253 霍夫曼(Huffman)编码原理 */ #include #include typedef long long LL; using namespace std; int l[50010]; int main(){ int j,m=0,n;cin>>n; LL s=0,ans=0; for(int i=0;i>l

Wu_Being 的优快云 博客 1059

poj 3253 Fence Repair 贪心

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

smilesundream的博客 310

ACM-贪心入门:POJ3253 Fence Repair

ACM-贪心入门:POJ3253 Fence Repair中文题意输入输出示例输入示例输出解决思路AC代码 为简化文章,原题请直接看原题链接 中文题意 给你一块长木板,其长度为L,要求将其切割成若干块指定大小的木板,大小分别为a,b,c,d… 其中L恰好等于a+b+c+d+…,每切割一次,所需的花费为当前执行切割操作的木板的的长度。比如将长度为P的木板切割为K和H,则花费为P。求最小的花费的切割方...

LiJgQg的博客 401

POJ 3253 Fence Repair (贪心)

题目链接:点击打开链接 O(N^2)的贪心算法 题目类似于赫夫曼树,每次选取最小的两个进行组合即可得到代价最小的组合 尴尬地水过= = AC代码如下: #include #include #include using namespace std; int main() { int N; int num[20005]; while(cin>>N)

LFhase‘s Blog 433

POJ 3253 Fence Repair(贪心)

http://poj.org/problem?id=3253 挑战上的题,但是我是实在不会。后来看到他的文字解释慢慢理解了。 一开始我的贪心策略很简单,我们直接想着每次都把最长的给去掉即可,这样下一次得到的木板长度是该处操作去掉任何一个长度木板中最短的了。但是的贪心是有反例的。比如4 4 4 4,最小的花费应该是32,而这样贪心算到的是36. 那么我们就要换换思路了。其实我们上面的贪心几乎接

llzhh的博客 363

POJ(3253)FenceRepair

POJ(3253) Fence Repair Descriptions: 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 ...

LordTech的博客 268

POJ 3253——Fence Repair(贪心)

链接:http://poj.org/problem?id=3253 题解 #include<iostream> #include<queue> #include<vector> using namespace std; typedef long long ll; const int MAX_L=5e4+50; int N; // N是木板切割块数...

享受孤独 277
上一篇: POJ 3617 - Best Cow Line(贪心)
下一篇: 杂记
coco430
博客等级 码龄14年 9粉丝 107原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值