POJ 3253
发布日期:2021-06-30 15:31:09 浏览次数:2 分类:技术文章

本文共 4308 字,大约阅读时间需要 14 分钟。

Fence Repair

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 63146   Accepted: 20826

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

3858

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

Source

大致题意:

有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度(锯之前的长度)

给定各个要求的小木板的长度,及小木板的个数n,求最小费用

提示:

3

8 8 5为例:

先从无限长的木板(其实是21)上锯下长度为 13 的木板,即花费 21

再从长度为13的木板上锯下长度为5的木板,花费13

最终得到 8,8,5

总花费 = 21+5+8 =34

如果直接按由小到大顺序一个一个锯

先从无限长的木板(其实是21)上锯下长度为 5 的木板,即花费 21

再从长度为16的木板上锯下长度为8的木板,花费16

最终得到 5,8,8

总花费 = 21+16=37

解题思路:

利用Huffman思想,要使总费用最小,那么每次只选取最小长度的两块木板相加,再把这些“和”累加到总费用中即可

本题虽然利用了Huffman思想,但是直接用HuffmanTree做会超时,可以用优先队列做

因为朴素的HuffmanTree思想是:

(1)先把输入的所有元素升序排序,再选取最小的两个元素,把他们的和值累加到总费用

(2)把这两个最小元素出队,他们的和值入队,重新排列所有元素,重复(1),直至队列中元素个数<=1,

         则累计的费用就是最小费用

HuffmanTree超时的原因是每次都要重新排序,极度浪费时间,即使是用快排。

一个优化的处理是:

(1)只在输入全部数据后,进行一次升序排序 (以后不再排序)

(2)队列指针p指向队列第1个元素,然后取出队首的前2个元素,把他们的和值累计到总费用,再把和值sum作为一个新元素插入到队列适当的位置 

由于原队首的前2个元素已被取出,因此这两个位置被废弃,我们可以在插入操作时,利用后一个元素位置,先把队列指针p+1,使他指向第2个废弃元素的位置,然后把sum从第3个位置开始向后逐一与各个元素比较,若大于该元素,则该元素前移一位,否则sum插入当前正在比较元素(队列中大于等于sum的第一个元素)的前一个位置

(3)以当前p的位置作为新队列的队首,重复上述操作 另一种处理方法是利用STL的优先队列,priority_queue,非常方便简单高效,虽然priority_queue的基本理论思想还是上述的优化思想,但是STL可以直接用相关的功能函数实现这些操作,相对简单。

priority_queue的详解见下

从测试数据看得到是存在大数的情况的,要使用 __int64或者Long long

代码:

#include 
#include
#include
#include
using namespace std;int main(){ int n; while(cin>>n) { priority_queue
,greater
> qu; for(int i=1; i<=n; i++) { int x; cin>>x; qu.push(x); } __int64 res =0,a=0,b=0; while(true) { a = qu.top(); qu.pop(); if(qu.empty()) { break; } b = qu.top(); qu.pop(); res+=a+b; qu.push(a+b); } printf("%I64d\n",res); } return 0;}

#include 
#include
#include
using namespace std; int n;int l[20001]; int main() { while(~(scanf("%d", &n))) { priority_queue
, greater
> q; for(int i=0; i
> l[i]; q.push(l[i]); } long long ans = 0; while(q.size() > 1) { int m1, m2; m1 = q.top(); q.pop(); m2 = q.top(); q.pop(); int t = m1 + m2; ans += t; q.push(t); } cout << ans << endl; } return 0;}

#include 
using namespace std; int n;int l[20001]; int main() { while(cin>>n) { for(int i=0; i
> l[i]; int len = n; long long ans = 0; while(len > 1) { int m1 = 0, m2 = 1; if(l[m1] > l[m2])//m1占据最小的位置,m2为第二小 swap(m1, m2); for(int i=2; i

 

转载地址:https://joycez.blog.csdn.net/article/details/83274031 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:POJ 2513
下一篇:POJ 2503

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月07日 02时04分06秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

Java开发对泛型的认识和理解 2019-05-01
Android开发之Context认识和运用 2019-05-01
Android数据库GreenDao的使用完全解析 2019-05-01
简单选择排序 2019-05-01
冒泡排序 2019-05-01
快速排序 2019-05-01
【Java习题】 -- 设计⼀个⽅法,将⼀个字符串中的⼤⼩写字⺟翻转 2019-05-01
【大数据开发】Java基础-总结1-可变参数的注意点 2019-05-01
【Java习题】 -- 设计⼀个⽅法,将⼀个数组中的元素倒序排列(注意,不是降序) 2019-05-01
【Java习题】(难)设计⼀个⽅法,找出⼀个整型数组中的第⼆⼤的值 2019-05-01
【Java习题】-- 已知⽅法 public static int[] combine(int[] arr1, int[] arr2) 的作⽤是,合并 两个数组,并对合并后的数组进⾏升序排序 2019-05-01
【Java习题】(难)已知⽅法 public static int[] delete(int[] arr, int ele) 的作⽤是删除数组中第 ⼀次出现的ele元素,并返回删除后的数组 2019-05-01
【大数据开发】Java基础-总结2-面向对象与面向过程的区别 2019-05-01
【大数据开发】Java基础-总结3-面向对象的特点和注意点 2019-05-01
MySQL5.7版本修改了my.ini配置文件后mysql服务无法启动问题 2019-05-01
【大数据开发】Java基础 -总结21-Hashmap和HashTable的区别 2019-05-01
MySQL中关于decimal与double字段类型的区别 2019-05-01
Exception in thread “main“ java.sql.SQLException错误之一: Column Index out of range, 0 < 1. 2019-05-01
C3p0连接池连接mysql出现: com.mchange.v2.resourcepool.BasicResourcePool 2019-05-01
webview加载网页或富文本oom 2019-05-01