poj3253 优先队列
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
Lines 2.. N+1: Each line contains a single integer describing the length of a needed plank
Output
Sample Input
3
8
5
8
Sample Output
34
Hint
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).
#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 优先队列的更多相关文章
- 优先队列 poj3253 Fence Repair
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 51411 Accepted: 16879 De ...
- poj3253 Fence Repair【哈夫曼树+优先队列】
Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- Poj3253 Fence Repair (优先队列)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 67319 Accepted: 22142 De ...
- POJ-3253 Fence Repair---Huffman贪心
题目链接: https://vjudge.net/problem/POJ-3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长 ...
- 堆排序与优先队列——算法导论(7)
1. 预备知识 (1) 基本概念 如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...
- 数据结构:优先队列 基于list实现(python版)
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author: Minion-Xu #list实现优先队列 class ListPriQueueValueE ...
- python优先队列,队列和栈
打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...
- 数据结构作业——Sanji(优先队列)
山治的婚约 Description 我们知道,山治原来是地下有名的杀人家族文斯莫克家族的三子,目前山治的弟弟已经出现,叫做四治,大哥二哥就叫汪(One)治跟突(Two)治好了(跟本剧情无关) .山治知 ...
- Java优先队列
按照Java api的说法: java.util.PriorityQueue.PriorityQueue() Creates a PriorityQueue with the default init ...
随机推荐
- Tomcat之内存、并发、缓存方面优化方法
一.Tomcat内存优化 Tomcat内存优化主要是对 tomcat 启动参数优化,我们可以在 tomcat 的启动脚本 catalina.sh 中设置 java_OPTS 参数. JAVA_OPTS ...
- python中常用第三方库记录
python中有很多很好用的第三方库,现在记录一下这些库以及如何下载 一.virtualenv,这是一个可以将生产环境隔离开的python库,非常好用 在linux下使用pip install vir ...
- Dx12 occlusion query
https://github.com/Microsoft/DirectX-Graphics-Samples/blob/master/Samples/Desktop/D3D12PredicationQu ...
- jQuery选择器的灵活用法
// 摘自: http://hi.baidu.com/274084093/item/47a4ce696e89e534ad3e836b jQuery中选择器很强大,可以根据元素名称.ID.class等多 ...
- 【云计算】OpenStack项目全面介绍
关于OpenStack孵化项目trove(DBaaS)之我见:http://blog.csdn.net/ddl007/article/details/17253751 OpenStack Trove将 ...
- [NM 状态机2] Container状态机详解
概述 前面已经分析了RM的状态机,接下来将分析NM的状态机,NM状态机包括Container,Application,LocalizedResource三个,其中Container相对较复杂.现在我们 ...
- 转:ios的图片文件上传代码
转自: https://gist.github.com/igaiga/1354221 @interface ImageUploader : NSObject { NSData *theImage; } ...
- [LeetCode][Java] Substring with Concatenation of All Words
题目: You are given a string, s, and a list of words, words, that are all of the same length. Find all ...
- Win7是否可以把光盘当作U盘用
光盘当作U盘用 在Windows 7刻录功能中,之前有看到一篇文章<Windows7 新功能 光盘当U盘用>(感兴趣的同学可以百度一下),文章的意思大致是说作者插入了一张空的DVD光盘,然 ...
- Eclipse中,快捷键使用总结
(1)Alt+shift+L:new ReadItem().readItems(file);的返回对象是Map<String,String>用这个快捷键有两个效果示例1:输入光标停在new ...