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. dedecms批量修改文章为待审核稿件怎么操作

    dedecms批量修改文章为待审核稿件要怎么操作呢?因为我们有时会出于某些原因要把文章暂时先隐藏掉,dedecms有一个比较好的功能是将文件状态设为未审核前台就可以看不到了,那要怎么批量设置呢?到后台 ...

  2. 20170728 Celery项目 后台处理SQL SERVER的一个异常

    -- 1. 感觉是访问DB 失败了,失败原因不明确 -- 2. 考虑解决问题的解决办法,后台记录异常,并重新发送任务. 具体如何来实现

  3. 唯美MACD

    上证30 分钟产生背离,60 分钟高位,但并没有背离,若调整,也是30F 级别的.算是上涨途中的歇脚. 1.股票如果非要从MACD 上去判断怎么拿,那么一句话:要大跌,一般都需要背离.所以,没有背离的 ...

  4. numpy的searchsorted细品

      import numpy as np a= np.arange(20) pos_left = a.searchsorted(3)    #也可以写成np.searchsorted(a, 3), 注 ...

  5. springboot测试

    一.单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. 1.在pom包中添加spring-boot-starter-test包引用 <depend ...

  6. (转)EOS中账户、钱包和密钥的关系

    EOS对于账户的设计与ETH有很大的不同,引入了Account账户, Wallet钱包, 钱包密码, Key公私钥, Permission权限等众多概念,刚入门的时候感觉一头雾水.本文希望通过对这些概 ...

  7. 删除 clean tomcat7:run

    1.在eclipse中运行的绿色箭头旁边有个下箭头,点击: 2.选择Run Configurations... 3.在Maven Builder下删除不想要的

  8. Permission denied: user=root, access=WRITE, inode="/":hadoopuser:supergroup:drwxr-xr-x

    提示往HDFS写文件是不容许的. 在conf/hdfs-site.xml中加入: <property> <name>dfs.permissions</name> & ...

  9. MySQL数据类型--与MySQL零距离接触2-14MySQL默认约束

    创建一个数据表,多加一个字段sex性别,1男,2女,3保密.默认的是3,也就是保密. 但是查看表的时候,会将默认值3赋予这个字段.

  10. Linq测试/查看工具——LinqPad

    LinqPad 官方:http://www.linqpad.net/ 介绍: 即时测试任何C#/ F#/ VB片段或程序LINQ支持--SQL / CE / Azure,Oracle,SQLite和M ...