题目描述:

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).
 
题目大意:两根木棒拼在一起花费的费用是两根木棒的长度和,问最小的费用使得所有的木棒拼为一根
思路:每次都找最短的两条合并起来,然后直至拼成只有一条,比较菜的用了优先队列
 
 AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
typedef long long int ll;
const int maxn=2e4+5;
const int mod=1e9+7; int main()
{
priority_queue<int,vector<int>,greater<int> >que;
int n,a[maxn];
ll ans=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
que.push(a[i]);
}
while(que.size()>1)
{
int l1,l2;
l1=que.top();
que.pop();
l2=que.top();
que.pop();
ans=ans+l1+l2;
que.push(l1+l2);
}
cout<<ans<<endl;
}

poj-3253 fence repair(贪心题)的更多相关文章

  1. POJ 3253 Fence Repair (贪心)

    Fence Repair Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

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

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

  3. poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...

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

    题意:农夫要将板割成n块,长度分别为L1,L2,...Ln.每次切断木板的花费为这块板的长度,问最小花费.21 分为 5 8 8三部分.   思路:思考将n部分进行n-1次两两合成最终合成L长度和题目 ...

  5. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

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

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

  7. poj 3253 Fence Repair 优先队列

    poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...

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

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

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

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

  10. poj 3253 Fence Repair

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

随机推荐

  1. springboot相关的pom依赖文件

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  2. js 和C# ashx之间数组参数传递问题

    js在进行ajax提交时,如果提交的参数是数组,js无法直接进行提交,及时提交上去,解析也是比较麻烦 ajax在提交数组时,需要设置参数:  traditional: true,  //参数作为数组传 ...

  3. 使用PM2守护Node.js应用

    PM2简介 PM2是node进程管理工具,可以利用它来简化很多node应用管理的繁琐任务,如性能监控.自动重启.负载均衡等,而且使用非常简单.   安装PM2 $ npm install pm2 -g ...

  4. H5移动端原生长按事件

    // 函数名longpress// 参数为: 需长按元素的id.长按之后处理函数func function longPress(id, func,timeout=500) { var timeOutE ...

  5. Mysql5.7.6安装和主从配置手册

    Mysql5.7.6+ 安装手册 linux server版本   1.下载 http://dev.mysql.com/downloads/mysql/#downloads  2. 检查库文件是否存在 ...

  6. Customers Who Never Order

    Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL qu ...

  7. 前端Json数据模拟神器mockJs使用教程

    一般项目做法: <html> <head> <script src="http://requirejs.org/docs/release/2.1.16/comm ...

  8. js对jsonArray的操作

    上一篇写了关于java中Gson对jsonArray的排序处理,这里介绍js中对jsonArray处理, <!DOCTYPE html> <html> <head> ...

  9. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:10.项目介绍之架构(2)

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 前言 前面我们讲到<迷你微信>服务器端的主架构,现在我们来描述一下它的模块详细信息. 网络模块 从上图我 ...

  10. 消除ImageButton背景图片

    下图被选为作为ImageButton的Src,可它自带了个灰色的背景图,而我只想用这个圆圈作为imageButton的src,这怎么办呢? 遇到此情况可以设置imagebutton的backgroun ...