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. Liquibase被锁

    经常运行过程中出现 Liquibase - Waiting for changelog lock Waiting for changelog lock.... Running the migratio ...

  2. C++windows内核编程笔记day11 win32静态库和动态库的使用

    windows库程序: 静态库: 源码被链接到调用的程序或动态库,被调用时,代码最少有1份,文件后缀.LIB 动态库: 函数被程序或其它动态库调用,被调用时,代码仅仅有1份,文件后缀.DLL 静态库( ...

  3. 一个美丽的java烟花程序

    <img src="http://img.blog.csdn.net/20150625104525974?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi ...

  4. 【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】

    [032-Longest Valid Parentheses(最长有效括号)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string contai ...

  5. C/C++(C++封装)

    封装 当单一变量无法完成描述需求的时候,结构体类型解决了这一问题.可以将多个类型打包成一体,形成新的类型.这是 c 语言中封装的概念.但是,新类型并不包含,对数据类的操作.所的有操作都是通过函数的方式 ...

  6. UESTC 360 Another LCIS

    Another LCIS Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on UESTC. Original ...

  7. win系统下的eclipse连接和使用linux上的hadoop集群

    准备工作 先在win系统的hosts文件中加入下面内容 10.61.6.164master     //hadoop集群的master节点 一.首先在eclipse上安装hadoop插件 下载hado ...

  8. 一入python深似海--对象的属性

    Python中一切皆是对象,每一个对象都能够有多个属性.Python是怎样管理这些属性呢?我们来探讨一下. 属性的__dict__系统 对象的属性包括两部分:类属性和对象属性.对象的属性可能来自于其类 ...

  9. servlet学习(1)

    1.Servlet是sun公司提供的一门用于开发动态web资源的技术. 2.Servlet在web应用的位置: 3.创建Servlet的三种方式: (1)实现servlet的接口 (2)继承Gener ...

  10. LinkedIn微服务框架rest.li

    linkedin/rest.li  https://github.com/linkedin/rest.li LinkedIn微服务框架rest.li摘要:Rest.li是一款REST+JSON框架,使 ...