poj 3253 Fence Repair

题目连接

http://poj.org/problem?id=3253  

Fence Repair

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 Nplanks (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

哈弗曼编码,注意结果爆int要用long long。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<queue>
#include<set>
using std::set;
using std::sort;
using std::pair;
using std::swap;
using std::string;
using std::priority_queue;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 20010;
const int INF = ~0u>>1;
typedef long long ll;
struct Node {
	ll dat, w;
	Node *ch[2];
	Node() {}
	Node(ll _dat_, ll _w_, Node *l = NULL, Node *r = NULL) {
		dat = _dat_, w = _w_;
		ch[0] = l, ch[1] = r;
	}
	Node(const Node &x) {
		dat = x.dat, w = x.w;
		ch[0] = x.ch[0], ch[1] = x.ch[1];
	}
	inline bool operator<(const Node &x) const {
		return w > x.w;
	}
}A[N];
struct Hoffmancode {
	ll sum;
	Node *root;
	priority_queue<Node> q;
	inline void solve(int n) {
		ll v;
		sum = 0;
		rep(i, n) {
			scanf("%lld", &v);
			A[i] = Node(v, v);
			q.push(A[i]);
		}
		CreateHoffmanTree();
		CreateHoffmanCode(root, "");
		printf("%lld\n", sum);
	}
	inline void CreateHoffmanTree() {
		Node *l = NULL, *r = NULL;
		while (!q.empty() && q.size() != 1) {
			l = new Node(q.top()); q.pop();
			r = new Node(q.top()); q.pop();
			q.push(Node(INF, l->w + r->w, l, r));
		}
		if (1 == q.size()) {
			root = new Node(q.top()); q.pop();
		}
	}
	inline void CreateHoffmanCode(Node *x, string s) {
		if (!x) return;
		if (x->dat != INF) {
			sum += x->dat * s.length();
		}
		CreateHoffmanCode(x->ch[0], s + '0');
		CreateHoffmanCode(x->ch[1], s + '1');
	}
}work;
int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w+", stdout);
#endif
	int n;
	while (~scanf("%d", &n)) {
		work.solve(n);
	}
	return 0;
}

转载于:https://www.cnblogs.com/GadyPu/p/4876096.html

PCB_Project.PrjPcb 4 Not in version control Altium Designer中PCB项目出现"NOT in version control"错误的解决方法:1. 禁用不需要的版本控制集成(DXP→Preferences→Version Control);2. 清理项目缓存(Projects面板右键→Clean Project);3. 检查文件读写权限;4. 如无效可新建项目重新添加文件。如需使用SVN版本控制,需正确配置SVN客户端路径,创建或连接SVN仓库,并将项目文件提交至版本控制系统。注意新建SVN仓库必须使用空目录,建议使用 阅读详情

相关推荐

ThinkPad X230魔改指南:从博通网卡驱动到1080P屏显全攻略(附Sonoma避坑清单)

本文详细介绍了ThinkPad X230的全面改造方案,包括博通网卡驱动(BCM94352HMB)的安装与调试、1080P屏幕改装技术以及macOS Sonoma系统的适配优化。通过硬件升级和软件调优,使这台经典商务本焕发新生,适合DIY爱好者和技术极客参考实践。

weixin_30379531的博客 608

POJ 3253 Fence Repair

POJ 3253 Fence Repair 题目链接:http://poj.org/problem?id=3253 题目大意 农夫约翰想修理牧场周围的一小段篱笆。他测量了栅栏,发现他需要N(1≤N≤20,000)块木板,每块长度为整数Li(1≤Li≤50,000)。然后,他买了一块长木板,刚好能看到N块木板(即,它的长度是长度Li的和)。FJ忽略了“kerf”,即锯切时锯屑损失的额外长度;你也应该...

weixin_30577801的博客 243

CloudCompare里那个CSF地面滤波插件,到底怎么用?手把手教你分离点云里的地面

本文详细介绍了如何在CloudCompare中使用CSF地面滤波插件分离点云中的地面数据。通过解析布料模拟滤波算法的核心参数,如布料刚度和网格分辨率,并提供不同地形(城市、丘陵、山地)的适配策略,帮助用户高效处理复杂点云数据。文章还包含实战案例和优化技巧,确保滤波结果的精度和可靠性。

weixin_42548708的博客 231

【题解】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的博客 1518

【Solidworks学习使用(1)__2022版本保姆级安装方法】

Solidworks 2020安装

coder-kimbell的博客 6390

LAP-D协议概述

Q.921协议由称为LAPD(链路访问协议-D通道),与HDLC的关系密切,它是在ISDN D通道上使用的数据链路协议。 注:http://www.freesoft.org/CIE/Topics/125.htm LAP-D的帧格式 标志、位填充技术和FCS校验计算与HDLC一致。 16位的地址包括C/R字段、SAPI(服务访问点标识)和TEI(端点标识)。TEI用于区分使用相同ISDN...

系统运维 1922

poj 3253 Fence Repair 贪心

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

ddqb3235的博客 237

因果表示学习 (Causal Representation Learning, CRL)

传统的机器学习,尤其是深度学习,非常擅长学习数据中的相关性 (Correlation)。但它常常会走“捷径”,学到一些虚假的、不稳定的“表面联系”。一个AI模型在学习识别“牛”时,如果训练数据里所有的牛都在绿色的草地上,模型很可能会学到一个错误的规则:“牛 = 四条腿的生物 + 绿色背景”。你给它一张牛在海滩上的照片,它就可能不认识了。因果表示学习的目标,就是让模型超越这种表面相关性,去学习数据背后。

Xuxiansir的博客 1196

poj 3253 Fence Repair 优先队列

poj 3253 Fence Repair 优先队列 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) plank...

dengguan2720的博客 189

使用MID360在室内快速定位

在预构建的点云地图中实现实时3D全球定位。通过融合FAST-LIO的低频全局定位(约0.5~0.2Hz)和高频里程计,整个系统的计算效率很高。消除里程计的累计误差初始定位可以通过RVIZ的粗略手动估计或来自另一个传感器/算法的姿势来提供。

CTwinkle的博客 1176

陀螺仪选型避坑指南:为什么IMU660RA是智能车竞赛的性价比之选?

本文针对智能车竞赛场景,提供陀螺仪选型避坑指南,并深度解析为何IMU660RA成为性价比之选。文章指出,竞赛选型应避免盲目追求高性能“黑盒”模块,而应注重原始数据获取与算法开发。IMU660RA以其良好的常温稳定性、适中的噪声水平、极致的性价比和丰富的生态支持,成为帮助参赛者深入理解姿态解算原理、进行“白盒开发”的理想教学与实践平台。

tech5的博客 884

【POJ 3253 --- Fence Repair】

【POJ 3253 --- Fence Repair】 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, eac...

来日浅谈的博客 586

LPDDR6 tdbR2dbR_L cross row技术探讨

LPDDR6 tdbR2dbR_L cross row技术探讨

zilan23的博客 54

POJ3253【Fence Repair】

Fence Repair 题目 POJ3253 解析 看一下题目,突然发现这题就是合并果子的原题 那是不是就直接提交了呢?不,本题不保证答案在int范围内,记得开longlong code: #include<cstdio> #define int long long using namespace std; inline void swap(int &x,int &y){x^=y^=x^=y;} inline bool idigit(char x){return (x<

zhanglili1597895的博客 144

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

POJ 3253 Fence Repair 哈夫曼树

传送门:POJ 3253 Fence Repair分析: 常规模拟代码麻烦,采用STL中的multiset数据结构进行哈夫曼树的模拟构建。代码如下:#include <stdio.h> #include <algorithm> #include <set> using namespace std; int wood[20001],n; typedef long long LL; int main(

知 识 不 是 装 饰。 467

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

poj 哈夫曼树相关之3253 Fence Repair

poj 哈夫曼树相关之3253 Fence Repair 看了别人的代码写的,用了优先队列的相关知识,不然自己做肯定就会写一堆类和指针了。 优先队列的相关知识

ziqi9216的博客 318

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

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

优先队列——Poj 3253 Fence Repair

题目:http://poj.org/problem?id=3253 Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46216 Accepted: 15062 Description Farmer John wants to repair a s

点滴 441

POJ 3253 Fence Repair (优先队列)

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 needsN (1 ≤ N ≤ 20,000) planks of wood, each havin...

weixin_30802273的博客 107

POJ 3253 Fence Repair(修篱笆)

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

weixin_30379911的博客 134

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

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

xtq的专栏 1144

POJ No. 3253 Fence Repair

POJ No. 3253 Fence Repair

代码改变世界 213

POJ3253 Fence Repair(Huffman)

POJ 3253http://poj.org/problem?id=3253 题目大意:将一节树枝折断成两节,均会产生与树枝长度相等的费用,问最低费用是多少。 样例解释: sorted: 5, 8, 8 (greater) first: 5 + 8 = 13, ans = 13; second: 13 + 8 = 21, ans = 13 + 21 = 34; cout...

Sensente的博客 282

POJ3253 Fence Repair

题目链接:http://poj.org/problem?id=3253   和合并果子一样,小根堆   贴代码 #include #include using namespace std; typedef long long ll; const int maxn=20005; ll heap[maxn]; int len,n; void swap(ll &x,ll &y){ ll t;

白云千载空悠悠 249

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 ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ...

Alex 586

POJ 3253 Fence Repair

#include&lt;cstdio&gt; using namespace std; const int N = 50005; int heap[N],len; void push(int x) { int i; i = ++len; while (i &gt; 1) { if (heap[i / 2] &lt;= x) break; heap[i] = heap[i / 2]; ...

Top_xiao的博客 191
上一篇: poj 2785 4 Values whose Sum is 0
下一篇: hdu 2660 Accepted Necklace
dong12276
博客等级 码龄13年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址