Fence Repair
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 51346   Accepted: 16857

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).
题目大意:FJ需要修补牧场的围栏,他需要 N 块长度为 Li 的木头(N planks of woods)。开始时,FJ只有一块无限长的木板,因此他需要把无限长的木板锯成 N 块长度
为 Li 的木板,Farmer Don提供FJ锯子,但必须要收费的,收费的标准是对应每次据出木块的长度,比如说测试数据中 5 8 8,一开始,FJ需要在无限长的木板上锯下长度 21 的木板(5+8+8=21),
第二次锯下长度为 5 的木板,第三次锯下长度为 8 的木板,至此就可以将长度分别为 5 8 8 的木板找出
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(false)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int main()
{
ll n,x;
while(scanf("%lld",&n)!=EOF)
{
priority_queue<ll,vector<ll>,greater<ll> >q;
for(int i=;i<n;i++)
{
scanf("%lld",&x);
q.push(x);
}
ll ans=;
while(q.size()>)
{
int a=q.top();
q.pop();
int b=q.top();
q.pop();
ans+=a+b;
q.push(a+b);
}
printf("%lld\n",ans);
}
return ;
}

POJ Fence Repair(优先队列)的更多相关文章

  1. poj 3253 Fence Repair 优先队列

    poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...

  2. POJ - 3253 Fence Repair 优先队列+贪心

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

  3. Poj3253 Fence Repair (优先队列)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 67319   Accepted: 22142 De ...

  4. poj 3053 Fence Repair(优先队列)

    题目链接:http://poj.org/problem?id=3253 思路分析:题目与哈夫曼编码原理相同,使用优先队列与贪心思想:读入数据在优先队列中,弹出两个数计算它们的和,再压入队列中: 代码如 ...

  5. poj 3253 Fence Repair (优先队列,哈弗曼)

    题目链接:http://poj.org/problem?id=3253 题意:给出n块木板的长度L1,L2...Ln,求在一块总长为这个木板和的大木板中如何切割出这n块木板花费最少,花费就是将木板切割 ...

  6. poj 3253 Fence Repair(优先队列+huffman树)

    一个很长的英文背景,其他不说了,就是告诉你锯一个长度为多少的木板就要花多少的零钱,把一块足够长(不是无限长)的木板锯成n段,每段长度都告诉你了,让你求最小花费. 明显的huffman树,优先队列是个很 ...

  7. 区间DP与贪心算法的联系(uav Cutting Sticks &amp;&amp; poj Fence Repair(堆的手工实现))

    由于,这两题有着似乎一样的解法所以将其放在一起总结比較,以达到更好的区分二者的差别所在. 一.区间DP uva的Cutting Sticks是一道典型的模板题. 题目描写叙述: 有一根长度为l的木棍, ...

  8. POJ 3253 Fence Repair (优先队列)

    POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...

  9. [ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25274   Accepted: 8131 Des ...

随机推荐

  1. hdoj-1164-Eddy&#39;s research I【分解质因数】

    Eddy's research I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...

  2. 《机器学习系统设计》之应用scikit-learn做文本分类(上)

    前言: 本系列是在作者学习<机器学习系统设计>([美] WilliRichert)过程中的思考与实践,全书通过Python从数据处理.到特征project,再到模型选择,把机器学习解决这个 ...

  3. 关于nios 中printf 的问题

    在nios中,有printf的程序,在线调试没有什么问题,但是下到flash里面,程序跑了一段时间就死掉了!JTAG_UART是阻塞式输出,他只是将数据输出到buffer中,等待你上位机读取,当你的b ...

  4. BZOJ5204: [CodePlus 2018 3 月赛]投票统计

    [传送门:BZOJ5204] 简要题意: 有n个选手,每个选手会选择一道题投票,求出投票最多的题目个数和这些题目的编号,如果所有题目的投票数相同,则输出-1 题解: 直接搞 离散化,然后判断就可以了 ...

  5. 技术报告:APT组织Wekby利用DNS请求作为C&C设施,攻击美国秘密机构

    技术报告:APT组织Wekby利用DNS请求作为C&C设施,攻击美国秘密机构 最近几周Paloalto Networks的研究人员注意到,APT组织Wekby对美国的部分秘密机构展开了一次攻击 ...

  6. vue --- cli build 后的项目,图片路径出错

    今天在插入背景图片过程中,遇到了路径错误的问题,通过网上查询,找到了解决的办法,但是大部分都没有讲造成这种问题的原因,故我简单地总结了一下,并加入了一些自己的理解,欢迎共同探讨~ 当用vue-cli自 ...

  7. refresh table tablename ;MSCK REPAIR TABLE table_name;

    在更改分区内的文件后刷新表 refresh table tablename ; 我们平时通常是通过alter table add partition方式增加Hive的分区的,但有时候会通过HDFS p ...

  8. vue.js原生组件化开发(二)——父子组件

    前言 在了解父子组件之前应先掌握组件开发基础.在实际开发过程中,组件之间可以嵌套,也因此生成父子组件. 父子组件创建流程 1.构建父子组件 1.1 全局注册 (1)构建注册子组件 //构建子组件chi ...

  9. ES6特性-带标签的模板字符串(tagged template)

    tagged template: 加在模板字符串前面加一个标签(函数). let dessert = = '甜品' drink = '茶' let breakfast = kitchen`今天的早餐是 ...

  10. 学习NLP:《自然语言处理原理与技术实现(罗刚)》PDF+代码

    自然语言处理技术已经深入我们的日常生活.我们经常用到的搜索引擎就用到了自然语言理解等自然语言处理技术.自然语言处理是一门交叉学科,涉及计算机.数学.语言学等领域的知识. <自然语言处理原理与技术 ...