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

//这是比赛时写的代码,答案尽管对的。但有问题

//不是把一个长度为21的砍开吗?我就想着一个開始是21。先砍成8和13,须要21,然后再把13砍成8和5,须要13  13+21=34;然后我就把数组里的素

//从大到小排列,为的是21加的更少,花的金币就更少。就从大到小排列,

//然而错了n次,比赛结束后我看了看别人的代码,是哈夫曼树+优先队列,每次取两最小的数。粘成一个长的,每次都这样

//但我不知道我的代码错在哪

#include<cstdio>
#include<algorithm>
using namespace std;
int cmp(__int64 x,__int64 y)
{
return x>y;
}
__int64 a[20100];
int main()
{
int n,i;
while(~scanf("%d",&n))
{
__int64 s=0;
for(i=0;i<n;++i)
{
scanf("%I64d",a+i);
s+=a[i];
}
sort(a,a+n,cmp);
__int64 sum=0;
for(i=0;i<n;++i)
{
if(s!=a[n-1])
{
sum+=s;
s-=a[i];
}
}
printf("%I64d\n",sum);
}
}

/*
题意:将一块木块砍一刀,当前木块是多长就须要多少金币,
求花最少的金币砍成你想要的木块长度
*/
//哈夫曼树+优先队列 poj 3253 //每次实现最小的两个数相加
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std; int main()
{
int n,i;
__int64 a,b;
__int64 m;
scanf("%d",&n);
{
priority_queue<__int64,vector<__int64>,greater<__int64> >q; for(i=0;i<n;++i)
{
scanf("%I64d",&m);
q.push(m);
}
__int64 sum=0;
while(q.size()>1)
{
a=q.top();
q.pop();
b= q.top();
q.pop();
sum+=a+b;
q.push(a+b);
}
printf("%I64d\n",sum);
}
}

poj3253 Fence Repair【哈夫曼树+优先队列】的更多相关文章

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

    题目:http://poj.org/problem?id=3253 没用long long wrong 了一次 #include <iostream> #include<cstdio ...

  2. Poj 3253 Fence Repair(哈夫曼树)

    Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  3. BZOJ 3253 Fence Repair 哈夫曼树 水题

    http://poj.org/problem?id=3253 这道题约等于合并果子,但是通过这道题能够看出来哈夫曼树是什么了. #include<cstdio> #include<c ...

  4. 【PTA 天梯赛训练】修理牧场(哈夫曼树+优先队列)

    农夫要修理牧场的一段栅栏,他测量了栅栏,发现需要N块木头,每块木头长度为整数L​i​​个长度单位,于是他购买了一条很长的.能锯成N块的木头,即该木头的长度是L​i​​的总和. 但是农夫自己没有锯子,请 ...

  5. POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 53645   Accepted: 17670 De ...

  6. POJ 3253 Fence Repair(哈夫曼编码)

    题目链接:http://poj.org/problem?id=3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个 ...

  7. POJ 3253 Fence Repair(简单哈弗曼树_水过)

    题目大意:原题链接 锯木板,锯木板的长度就是花费.比如你要锯成长度为8 5 8的木板,最简单的方式是把21的木板割成13,8,花费21,再把13割成5,8,花费13,共计34,当然也可以先割成16,5 ...

  8. [tree]合并果子(哈夫曼树+优先队列)

    现在有n堆果子,第i堆有ai个果子.现在要把这些果子合并成一堆,每次合并的代价是两堆果子的总果子数.求合并所有果子的最小代价. Input 第一行包含一个整数T(T<=50),表示数据组数. 每 ...

  9. POJ 3253 Fence Repair(优先队列,哈夫曼树,模拟)

    题目 //做哈夫曼树时,可以用优先队列(误?) //这道题教我们优先队列的一个用法:取前n个数(最大的或者最小的) //哈夫曼树 //64位 //超时->优先队列,,,, //这道题的优先队列用 ...

随机推荐

  1. git 的右键快捷菜单恢复

    最近装了个vs 2017 的社区版的,发现之前安装的git的右键快捷方式不见了,因此在此写备注,只要是git的安装位置是默认位置的,也可以使用此注册表恢复,其他路径请自行修改reg文件 主要是针对gi ...

  2. Visual Studio 2013 中使用断点

    你可能已经很熟悉Visual Studio中的断点的基本功能.你在编辑器里代码的侧边点击,创建一个红色的圆点,然后运行应用程序,线程走到你所点的代码处停下,你可以用调试窗口查看代码状态. 你可能不熟悉 ...

  3. 关于面试总结-linux篇

    前言 现在做测试的出去面试,都会被问到linux,不会几个linux指令都不好意思说自己是做测试的了,本篇收集了几个被问的频率较高的linux面试题 常用指令 1.说出10个linux常用的指令 ls ...

  4. unittest和pytest对比

    一.用例编写规则 1.unittest提供了test cases.test suites.test fixtures.test runner相关的类,让测试更加明确.方便.可控.使用unittest编 ...

  5. Cookie测试的测试点

    1.禁止使用Cookie:设置浏览器禁止使用Cookie,访问网页后,检查存放Cookie文件中未生成相关文件: 2.Cookie寻出路径:按照操作系统和浏览器对Cookie存放路径的设置,检查存放路 ...

  6. VOC Segmentation GT图像颜色表生成分析

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/52185581 PASCAL VOC图像 ...

  7. BZOJ 2194 快速傅立叶之二 ——FFT

    [题目分析] 咦,这不是卷积裸题. 敲敲敲,结果样例也没过. 看看看,卧槽i和k怎么反了. 艹艹艹,把B数组取个反. 靠靠靠,怎么全是零. 算算算,最终的取值范围算错了. 交交交,总算是A掉了. [代 ...

  8. 常州模拟赛d6t3 噪音

    FJ有M个牛棚,编号1至M,刚开始所有牛棚都是空的.FJ有N头牛,编号1至N,这N头牛按照编号从小到大依次排队走进牛棚,每一天只有一头奶牛走进牛棚.第i头奶牛选择走进第p[i]个牛棚.由于奶牛是群体动 ...

  9. __getattr__ 与 __getattribute__的区别

    原文博客地址 http://www.cnblogs.com/bettermanlu/archive/2011/06/22/2087642.html

  10. 素数判定 2(codevs 1702)

    题目描述 Description 一个数,他是素数么? 设他为P满足(P<=263-1) 输入描述 Input Description P 输出描述 Output Description Yes ...