POJ3253 Fence Repair stl+哈夫曼树

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情


用顶小堆计算哈夫曼树的总权。

每次取出最小的两个顶点,权值相加,然后加入答案中,然后在讲合并和后的值加入堆中,直到只有一个节点是结束。

因为这种方法,一个叶子节点会被重复加他的深度次

Fence Repair
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 18840 Accepted: 6013

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

Source


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

using namespace std;

int a[20010];
int n;

long long solve()
{
    priority_queue<long long,vector<long long>,greater<long long> > q;
    for(int i=1;i<=n;i++)
        q.push(a[i]);
    long long ans=0;
    long long x,y;
    while(q.size()>1)
    {
        x=q.top();
        q.pop();
        y=q.top();
        q.pop();
        ans+=(x+y);
        q.push(x+y);
       // cout<<x+y<<endl;
    }
    return ans;
}

int main()
{
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
            cin>>a[i];
        cout<<solve()<<endl;
    }
    return 0;
}


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( 阅读详情

相关推荐

POJ3253 Fence Repair(STL优先队列,哈夫曼)

题意: 有一位农夫要把一个木板钜成n块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木板的长度 给定各个要求的小木板的长度,及小木板的个数n,求最小的费用; PS: 以 3 5 8 5为例: 先从无限长的木板上锯下长度为 21 的木板,花费 21 再从长度为21的木板上锯下长度为5的木板,花费5 再从长度为16的木板上锯下长度为8的木板,花费8 总花费 = ...

xx_ii 460

poj 哈夫曼相关之3253 Fence Repair

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

ziqi9216的博客 312

STL-priority_queue优先队列容器简单用法(POJ 3253 Fence Repair哈夫曼))

priority_queue优先队列容器与队列一样,C++头文件需要#include&lt;queue&gt;,只能从队尾插入元素,从队首删除元素,但是它有一个特性,就是队列中最大的元素总是位于队首,并且按先进先出的原则进行。优先队列的好处就是:元素的比较规则默认为按元素的由大到小排序,不过可以重载“&lt;”重新定义比较规则。 优先队列包括入队push()(插入元素),出队pop()(删除元素...

WonderKing'blog 546

poj 3253 Fence RepairSTL优先队列)

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

RPG_Zero的博客 202

poj3253 Fence Repair 优先队列,C++STL中priority_queue的使用

题目链接:http://poj.org/problem?id=3253 题目大意:

basement_boy 852

POJ-3253 Fence Repair

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

Uranuslight的博客 366

POJ 3253 Fence Repair (哈夫曼)Haffman-tree

哈夫曼的一个题,我用 STL 的优先级队列水过的。 can't 弱 any more... 贴代码:(AC,172ms) #include #include using namespace std; typedef long long LL; struct node_{ LL x; bool operator < (node_ a) const { re

Rocking_Bee's Blog 599

[POJ3253]Fence Repair

题目链接:http://poj.org/problem?id=3253 单调队列和优先队列(哈夫曼编码)分别实现: 1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include...

weixin_30354675的博客 90

POJ3253 Fence Repair

如果想明白考察的是哈夫曼。 那么用STL就比较简单了。 #include #include #include #include #include #include #include #include #include #include #include using namespace std; int n; __int64 sum=0; class cmp

265

Fence Repair--POJ 3253

1、解题思路:哈夫曼。 2、注意事项:STL中优先队列容器(priority_queue)的应用,定义__int64位ans。 3、实现方法: 1 #include<iostream> 2 #include<queue> 3 using namespace std; 4 5 struct Node 6 { 7 __int64 x; 8 boo...

weixin_33681778的博客 122

POJ3253 Fence Repair

哈夫曼思想,优先队列解决 手打版

公孙轩辕,一眼千年 556

poj 3253 Fence Repair 哈夫曼最优解STL解法

STL模拟就够了 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define eps 1e-6 #define INF 0x3f3f3

906

POJ 3253-Fence Repair哈夫曼-最小值优先队列)

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

Some. 679

poj 3253 Fence Repair(哈夫曼)

Fence Repair 点击打开题目链接 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26151   Accepted: 8450 Description Farmer John wants to repair a small length of the

656

POJ 3253 Fence Repair哈夫曼

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

kdwycz的刷题空间 778

POJ 3253 Fence Repair

  这道题类似于哈夫曼,每次切付出的代价等于长度,所以我们要将长的先截出来。 而题目是给出N截,以及每截的长度。所以我们每次加上短的,使总代价最少。学会了优 先级队列的STL写法。 /*Accepted 348K 32MS C++ 644B 2012-07-30 16:32:44*/ #include<cstdio> #in...

weixin_30364147的博客 65

POJ 3253 Fence Repair STL 优先队列

这题做完后觉得很水,主要的想法就是逆过程思考,原题是截断,可以想成是拼装,一共有n根木棍,最后要拼成一根完整的,每两根小的拼成一根大的,拼成后的木棍长度就是费用,要求费用最少。显然的是一共会拼接n-1次,如果把每根木棍最初的长度想成叶子节点,两个节点的父节点就是这两个节点的长度相加。那么求的就是这n-1个父节点的总和最少。 为实现这个目标,我们使每个父节点的值尽可能的少,由此想到哈夫曼编码的原理...

weixin_30319097的博客 111
上一篇: LA4123 Glenbow Museum 递推
下一篇: POJ2499 Binary Tree
Albafica
博客等级 码龄15年 98粉丝 359原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值