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. editor.md实现Markdown编辑器

    editor.md实现Markdown编辑器 Markdown和Editor.md简介 Markdwon编辑器在技术工作者圈子中已经越来越流行,简单的语法,统一的格式,强大的扩展功能,最重要的是:你可 ...

  2. Linux(Centos)下安装MySQL

    转载:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html 一.mysql简介 说到数据库,我们大多想到的是关 ...

  3. 设置Ubuntu Mysql可以远程链接

    1:修改my.cnf配置文件 $sudo vim /etc/mysql/my.cnf 修改为: bind-address = 0.0.0.0 2:进行授权操作 mysql> grant all ...

  4. CSS3----border-sizing

    #wrapper input[type="text"], #wrapper input[type="password"] { /* display: flex; ...

  5. position: absolute;绝对定位水平居中问题

    position: absolute;绝对定位水平居中问题 用CSS让元素居中显示并不是件很简单的事情—同样的合法CSS居中设置在不同浏览器中的表现行为却各有千秋.让我们先来看一下CSS中常见的几种让 ...

  6. Codeforces 543A Writing Code

    http://codeforces.com/problemset/problem/543/A 题目大意:n个人,一共要写m行程序,每个程序员每行出现的bug数为ai,要求整个程序出现的bug数不超过b ...

  7. Berserk Rook

    Berserk Rook As you may know, chess is an ancient game for which almost everyone has at least a basi ...

  8. 【转】【Android】HAL分析

    原文网址:http://www.cnblogs.com/lcw/p/3335505.html HAL概述 以下是基于android4.0.3,对应其他低版本的代码,可能有所差异,但基本大同小异. An ...

  9. Linux下core文件产生的一些注意问题

    前面转载了一篇文章关于core文件的产生和调试使用的设置,但在使用有一些需要注意的问题,如 在什么情况 才会正确地产生core文件. 列出一些常见问题: 一,如何使用core文件 1. 使用core文 ...

  10. c++ 06

    一.下标操作符 A a (...); cout << a[3] << endl; cout << a.operator[] (3) << endl; c ...