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. Asp.net Core CORS(跨域资源共享)实验

    环境:Asp.Net Core 2 1.问题 最近项目在调用远程UI时遇到点麻,在调用远程CSS文件时无法加载其中的字体文件.远程CSS文件对字体的定义: @font-face { font-fami ...

  2. 用于OpenRISC的Makefile示例

    #* #*********************************************************************************************** ...

  3. Json.net说法——(一)修饰标签,日期序列化

    摘自: http://www.cnblogs.com/jams742003/archive/2009/12/24/1631587.html 通过属性标签自定义JSON序列化 JsonObjectAtt ...

  4. [Android-Demo]Android View的拖动

    ↑ 请善用目录 Demo下载地址:http://download.csdn.net/detail/u011634756/5959637 (免积分哦~) ------------------------ ...

  5. JAVA逐行读取TXT文件

    package help; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; imp ...

  6. 【转】dependency injection 的例子

    Dependency Injection in PHP. Create your own DI container. / blog / PHP By my opinion one of the big ...

  7. php 获取/设置用户訪问页面语言类

    User Language Class 获取/设置用户訪问的页面语言,假设用户没有设置訪问语言.则读取Accept-Language. 依据用户选择的语言显示相应的页面(英文.中文简体,繁体中文) U ...

  8. 这里先发布一个,自己写得unityUI的适配的方案(插播)

    这个适配是依据坐标系的象限的思想来进项适配的.參考了部分的NGUI的适配方案. 在程序的事实上,来測量UI距离相机边界的像素然后依据比例来进行适配,个人认为还不错. 放码! . 有个前提哦就是你要先定 ...

  9. js 倒计时跳转页面

    <script type="text/javascript">var i = 5; var intervalid; intervalid = setInterval(& ...

  10. Android打印机--小票打印格式及模板设置

    小票打印就是向打印设备发送控制打印格式的指令集,而这些打印格式须要去查询相应打印机的API文档,这里我把经常使用的api给封装了一下 文字对齐方式 打印字体大小 字体是否加粗 打印二维码 打印条形码 ...