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. 咕咕咕,skkyk的博客生活从此开始了

    2019-5-9 咕咕咕,skkyk的博客生活从此开始了 我是划水时长两个半小时的个人OIer skkyk,我喜欢打表,暴零,骗分,瞎搞(律师函警告); 2019-6-14 中考结束的第二天 我们苦逼 ...

  2. luogu2526 [SHOI2001]小狗散步

    注意一个景点只能去一次. #include <iostream> #include <cstring> #include <cstdio> #include < ...

  3. app审核相关

    app加急审核通道:https://developer.apple.com/contact/app-store/?topic=expedite

  4. 巴厘岛的雕塑 BZOJ 4069

    巴厘岛的雕塑 题解: 题意是要求分组使每组的和按位取或的值最小 那么考虑贪心,尽量使高位为0 于是枚举位置,从最高位枚举 假设当前枚举到第l位. 令 f[i][j] 表示前 i 个数分成 j 组,满足 ...

  5. Linux编译安装Apache+PHP

    Linux编译安装Apache+PHP 来自:自学it网,http://www.zixue.it/. 1]编译安装Apache+PHP 1.安装程序依赖库和开发环境   为了省事把所需要的库文件全都安 ...

  6. Wormholes(spfa判负环)

      POJ - 3259—— Wormholes Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & % ...

  7. C51单片机头文件和启动文件

    STARTUP.A51//启动文件. 清理RAM.设置堆栈等.即执行完start.a51后跳转到.c文件的main函数 <reg51.h>  //特殊寄存器的字节地址和位地址,sfr定义字 ...

  8. LeetCode第一题以及时间复杂度的计算

    问题描述:给定一组指定整数数组,找出数组中加和等于特定数的两个数. 函数(方法)twoSum返回这两个数的索引,index1必须小于index2. 另外:你可以假设一个数组只有一组解. 一个栗子: I ...

  9. keras模型总结

    https://keras.io/zh/ https://keras.io/zh/models/about-keras-models/ 在 Keras 中有两类主要的模型:Sequential 顺序模 ...

  10. 为什么一个目录里放超过十个Mp4文件会导致资源管理器和播放程序变卡变慢?

    最近<鬼吹灯之精绝古城>大火,我也下载了剧集放在移动硬盘里. 起初还没事,当剧集超过十个时发现资源管理器变慢了,表现为上方的绿条总是在闪动前进,给文件改名都缓慢无比. 当剧集超过十五个时, ...