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

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).
 
要用long long类型
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
int main()
{
ll n,x;
while(cin>>n){
priority_queue<int,vector<int>,greater<int> > pq;
while(n--){
cin>>x;
pq.push(x);
}
ll sum=;
while(pq.size()>){
ll x1=pq.top();
pq.pop();
ll x2=pq.top();
pq.pop();
sum+=x1+x2;
pq.push(x1+x2);
}
cout<<sum<<endl;
}
return ;
}

Poj3253 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. 优先队列 poj3253 Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 51411   Accepted: 16879 De ...

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

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

  4. POJ Fence Repair(优先队列)

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

  5. poj3253 Fence Repair【哈夫曼树+优先队列】

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

  6. poj-3253 fence repair(贪心题)

    题目描述: Farmer John wants to repair a small length of the fence around the pasture. He measures the fe ...

  7. poj3253 Fence Repair

    http://poj.org/problem?id=3253 Farmer John wants to repair a small length of the fence around the pa ...

  8. POJ3253 Fence Repair(贪心)

    分割木板的顺序是自由的,所以每次选择两块最短的板,组合在一起,增加队列,原来两个板出队,直到队列中为空或者仅仅剩下一个板时结束.这里使用优先队列较为方便. #include<iostream&g ...

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

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

随机推荐

  1. SQL SERVER BCP的用法

    转自:https://www.cnblogs.com/fishparadise/p/4809014.html 前言 SQL SERVER提供多种不同的数据导出导入的工具,也可以编写SQL脚本,使用存储 ...

  2. loadrunner压测工具

    https://www.cnblogs.com/xxyBlogs/p/4579509.html https://www.cnblogs.com/jaazz/p/8668214.html

  3. MongoDB 查询$关键字 $in $or $all

    属于:$in 满足其中一个元素的数据把age=13,73 的数据显示 > db.user.find({age: { $in:[13,73]}}) { "_id" : Obje ...

  4. 报错解决——SSL: CERTIFICATE_VERIFY_FAILED

    SSL: CERTIFICATE_VERIFY_FAILED Python 升级到 2.7.9 之后引入了一个新特性,当使用urllib.urlopen打开一个 https 链接时,会验证一次 SSL ...

  5. 如何为DigitalOcean上的服务器配置SSH密钥

    本指南适用于mac OS和Linux,windows用户请绕道. 通常,当你启动DigitalOcean droplets时,只要过程完成,你会收到一封电子邮件,让你知道droplets的IP地址和密 ...

  6. ntldr is missing

    开机时出现“ntldr is missing,这是因为引导文件丢失了,或者系统找不到引导文件,下面教解决方式. 1.u盘pe启动盘. 2.win03PE2013增强版. 3.pe系统桌面,点击“win ...

  7. chkdsk 命令对Raid盘检测和查错、修复

    C:\Documents and Settings\Administrator>chkdsk /?检查磁盘并显示状态报告. CHKDSK [volume[[path]filename]]] [/ ...

  8. Spark partitionBy

    partitionBy 重新分区, repartition默认采用HashPartitioner分区,自己设计合理的分区方法(比如数量比较大的key 加个随机数 随机分到更多的分区, 这样处理数据倾斜 ...

  9. DES、RC4、AES等加密算法优势及应用

    [IT168 技术]1篇文章,1部小说被盗取,全靠维(si)权(bi)捍卫自己的原创权利.程序员捍卫自己珍贵的代码,全靠花式的加密算法.代码加密有多重要?程序员半年做出的产品,盗版者可能半天就能完全破 ...

  10. 【Java】-NO.11.Java.1.Log4j.1.001-【Log4j Manual】-

    1.0.0 Summary Tittle:[Java]-NO.11.Java.1.Log4j.1.001-[Log4j2 Manual]- Style:Java Series:Log4j Since: ...