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).
 
 
每次选取最小的两个木板,拼接成新的长度,加入到木板队列中,直到队列中只有一块木板。
需要注意的是:最终结果的长度可能超过int,用long long
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std; int main()
{
priority_queue<int,vector<int>,greater<int> >wood;
int n;
cin>>n;
for(int i=;i<n;i++)
{
int tmp;
cin>>tmp;
wood.push(tmp);
}
long long ans=;
while(!wood.empty())
{
int l1=wood.top(); wood.pop();
if(wood.empty()) break;
ans+=l1;
int l2=wood.top();
ans+=l2;
wood.pop();
int newone=l1+l2;
//cout<<l1<<" "<<l2<<endl;
wood.push(newone);
}
printf("%I64d\n",ans);
return ;
}

这里注意优先队列对已定义的数据类型的排列,默认是大顶的,变成小顶用到:

priority_queue<int,vector<int>,greater<int> >wood;

自定义优先级:

struct node
{
friend bool operator< (node n1, node n2)
{
return n1.priority < n2.priority;
}
int priority;
int value;
};

poj3253 优先队列的更多相关文章

  1. 优先队列 poj3253 Fence Repair

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

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

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

  3. Poj3253 Fence Repair (优先队列)

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

  4. POJ-3253 Fence Repair---Huffman贪心

    题目链接: https://vjudge.net/problem/POJ-3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长 ...

  5. 堆排序与优先队列——算法导论(7)

    1. 预备知识 (1) 基本概念     如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...

  6. 数据结构:优先队列 基于list实现(python版)

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author: Minion-Xu #list实现优先队列 class ListPriQueueValueE ...

  7. python优先队列,队列和栈

    打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...

  8. 数据结构作业——Sanji(优先队列)

    山治的婚约 Description 我们知道,山治原来是地下有名的杀人家族文斯莫克家族的三子,目前山治的弟弟已经出现,叫做四治,大哥二哥就叫汪(One)治跟突(Two)治好了(跟本剧情无关) .山治知 ...

  9. Java优先队列

    按照Java api的说法: java.util.PriorityQueue.PriorityQueue() Creates a PriorityQueue with the default init ...

随机推荐

  1. ray tracing/shadow,reflection, caustic

    看了一下午终于明白raytracing的算法了 不知道这次能记住多久 ssr我又完全不记得了 按照Henrik所说 理解raytracing的核心在于,它是从Eye到light反着走的 需要一个前序的 ...

  2. centos6.8服务器配置之SVN配置

    version 1.6.11 一.安装:因对版本要求不高,所以采用yum安装 yum install -y svn 二.配置 1.创建仓库,以后所有代码都放在这个下面,创建成功后在svn下面多了几个文 ...

  3. 阿里巴巴Android开发手册(规约)

    阿里巴巴Android开发手册(规约) 学习了:https://www.cnblogs.com/jb2011/p/8487889.html  这个猛 https://blog.csdn.net/ali ...

  4. VP9 Video Codec

    http://www.webmproject.org/vp9/ WebM Repositories libvpx: VP8/VP9 Codec SDK   pull http://git.chromi ...

  5. 快速把web项目部署到weblogic上

    转自:http://weijie.blog.51cto.com/340746/90420/ weblogic简介         BEA WebLogic是用于开发.集成.部署和管理大型分布式Web应 ...

  6. mac本地搭建wordpress

    1 下载安装最新的xampp 2 安装完成后,使用下面的命令开始运行 XAMPP.在终端下以系统管理员 root 的身份登录 管理员root身份登录 sudo su 使用命令启动XAMPP /Appl ...

  7. JS中confirm,prompt用法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. DNS使用的是TCP协议还是UDP协议简析

    DNS使用的是TCP协议还是UDP协议简析   DNS同时占用UDP和TCP端口53是公认的,这种单个应用协议同时使用两种传输协议的情况在TCP/IP栈也算是个另类.但很少有人知道DNS分别在什么情况 ...

  9. STL源码剖析(适配器)

    STL中由三类适配器,它们分别是: 1.容器适配器(stack.queue) 2.迭代器适配器(insert_iterator.reverse_iterator.iostream_iterator) ...

  10. CXF调用方式——使用wsdl2java(Windwos下)

    1.自动生成客户端代码: 先把CXF下到本地,本例中我下的是apache-cxf-3.1.2,然后在命令行里到相应路径执行命令: D:\soft\DevelopSoft\apache-cxf-\bin ...