Fence Repair
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 32916   Accepted: 10638

点我

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).

Source

 #include <iostream>
#include <queue>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
int i,k,j,t1;
long long sum=;
priority_queue<int,vector<int>,greater<int> > p;
for(i=;i<n;i++)
{
cin>>k;
p.push(k);
}
while(p.size()!=)
{
t1=p.top();
p.pop();
t1+=p.top();
sum+=t1;
p.pop();
p.push(t1);
}
cout<<sum<<endl;
}
}

fence repair(队列水过)的更多相关文章

  1. POJ 3253 Fence Repair 贪心 优先级队列

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 77001   Accepted: 25185 De ...

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

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

  3. 优先队列 poj3253 Fence Repair

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

  4. POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 53645   Accepted: 17670 De ...

  5. Fence Repair (二叉树求解)(优先队列,先取出小的)

    题目链接:http://poj.org/problem?id=3253 Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  6. poj 3253 Fence Repair(priority_queue)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 40465   Accepted: 13229 De ...

  7. POJ 3253:Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 33114   Accepted: 10693 De ...

  8. poj 3253 Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 42979   Accepted: 13999 De ...

  9. Greedy:Fence Repair(POJ 3252)

    Fence Repair 问题大意:农夫约翰为了修理栅栏,要将一块很长的木块切割成N块,准备切成的木板的长度为L1,L2...LN,未切割前的木板的长度恰好为切割后木板的长度的总和,每次切断木板的时候 ...

随机推荐

  1. php----浅谈一下empty isset is_null的用处

    } }    {      }  {       } } }    {      }  {       } is_null():判断变量是否为null if ($a){} 那这个未声明变量会报noti ...

  2. 配置wamp环境使得在命令行下也能执行socket扩展

    首先在apache中开启socket扩展 php环境安装目录\bin\apache\apache2.2.17\bin\php.ini 去掉前面的';'   extension=php_sockets. ...

  3. jQuery图片延迟加载插件

    在一些图片较多的页面上,如果图片都一起加载网页的速度会比较慢,而且也浪费流量. 使用图片延时加载插件就解决这些问题. 方法: 引入jquery和插件文件 <script src="jq ...

  4. 《Programming WPF》翻译 第9章 6.我们进行到哪里了?

    原文:<Programming WPF>翻译 第9章 6.我们进行到哪里了? 只有当任何内嵌控件都没有提供你需要的底层行为时,你将要写一个自定义控件.当你写一个自定义控件,你将要使用到依赖 ...

  5. 低效的SQL引发的cache buffers chains latch

    1.低效的SQL 低效的SQL语句时发生cache buffers chains 锁存器争用的最重要原因.多个进程同时扫描大范围的索引或表时,可能广泛 地发生cache buffers chains ...

  6. HTTPS证书生成原理和部署细节

    看看下面,部分电信用户访问京东首页的时候,会看到右下角有一个浮动广告: 小白用户以为是京东有意放置的,细心的用户会发现,这个 iframe 一层嵌一层的恶心广告很明显是电信/中间人通过 DNS 劫持注 ...

  7. Eclipse使用之杂七杂八

    使用Eclipse的Marketplace安装1.12x版本的Subclipse插件(SVN)后,新建SVN资源库时出现如下报错: “Can't create session svn: Unable ...

  8. HTTP 协议实现

    一.超文本传输协议及HTTP包    HTTP协议用于在Internet上发送和接收消息.HTTP协议是一种请求-应答式的协议--客户端发送一个请求,服务器返回该请求的应答,所有的请求与应答都是HTT ...

  9. KVO奥秘

    序言 在iOS开发中,苹果提供了许多机制给我们进行回调.KVO(key-value-observing)是一种十分有趣的回调机制,在某个对象注册监听者后,在被监听的对象发生改变时,对象会发送一个通知给 ...

  10. JS 数组扩展函数--求起始项到终止项和

    Array.prototype.sum= function(l,r){ l=l==undefined ? 0 : l; r=r==undefined ? this.length - 1 : r; va ...