poj 3053 优先队列处理
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 39029 | Accepted: 12681 |
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
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).
Source
然而这样,每次都要排序以确定最少两个木板,代码复杂度、时间复杂度和空间复杂度都要上升
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
#define ll long long
#define mod 1e9+7
#define PI acos(-1.0)
#define bug(x) printf("%%%%%%%%%%%%%",x);
#define inf 1e8
using namespace std;
priority_queue<int, vector<int>, greater<int> > q;
int n;
int a;
ll ans=;
int main()
{
while(scanf("%d",&n)!=EOF)
{
ans=;
for(ll i=;i<=n;i++)
{
scanf("%d",&a);
q.push(a);
}
ll s1,s2;
while(q.size()>)
{
s1=q.top();
q.pop();
s2=q.top();
q.pop();
ans=ans+s1+s2;
q.push(s1+s2);
}
q.pop();
printf("%lld\n",ans);
} return ;
}
poj 3053 优先队列处理的更多相关文章
- POJ 2431 优先队列
汽车每过一单位消耗一单位油,其中有给定加油站可加油,问到达终点加油的最小次数. 做法很多的题,其中优先对列解这题是很经典的想法,枚举每个加油站,判断下当前油量是否小于0,小于0就在前面挑最大几个直至油 ...
- poj 3053 Fence Repair(优先队列)
题目链接:http://poj.org/problem?id=3253 思路分析:题目与哈夫曼编码原理相同,使用优先队列与贪心思想:读入数据在优先队列中,弹出两个数计算它们的和,再压入队列中: 代码如 ...
- POJ 2442(优先队列 k路归并 堆)
Description Given m sequences, each contains n non-negative integer. Now we may select one number fr ...
- poj 1511 优先队列优化dijkstra *
题意:两遍最短路 链接:点我 注意结果用long long #include<cstdio> #include<iostream> #include<algorithm& ...
- Moo University - Financial Aid POJ 2010 优先队列(最大堆)
题目:http://poj.org/problem?id=2010 题目大意: 奶牛上大学.因为经济问题,每头奶牛都需要一定的补助需求,学校会提供一定的资金用于补助 每头牛都有自己的分数,学校招收的名 ...
- POJ 1442 优先队列
题意:有一些ADD和GET操作.n次ADD操作,每次往序列中加入一个数,由ADD操作可知序列长度为1-n时序列的组成.GET操作输入一个序列长度,输出当前长度序列第i大的元素的值.i初始为0,每次GE ...
- Labeling Balls POJ - 3687 优先队列 + 反向拓扑
优先队列 + 反向拓扑 //#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include ...
- Sunscreen(POJ 3614 优先队列)
Sunscreen Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5898 Accepted: 2068 Descrip ...
- POJ 3635 优先队列BFS
(感谢lyd学长的幻灯片) 注意vis数组的应用 在vis[i][j]中 i表示到了第i个点 j表示还剩j升油 vis[i][j]表示最小话费. 这样只需搜到话费比它少的更新入堆就OK了 //By: ...
随机推荐
- CUDA并行存储模型
CUDA将CPU作为主机(Host),GPU作为设备(Device).一个系统中可以有一个主机和多个设备.CPU负责逻辑性强的事务处理和串行计算,GPU专注于执行高度线程化的并行处理任务.它们拥有相互 ...
- es6中的变量声明
目录 es6中的变量声明 变量的声明 es6中的变量声明 变量的声明 for (var i = 0; i < 5; i++) { console.log(i) } var声明 作用域问题 上面的 ...
- SSH实验
跳板机实验1:本地转发 实验环境: 三台主机:A,B,C 目标A与C通过telnet连接 A主机和B,C主机之间有防火墙相隔,A与B之间可以通过SSH协议连接,BC之间可以通过telnet协议连接 环 ...
- linux文件属性之时间戳及文件名属性知识
7 8 9 三列是时间(默认是修改时间) modify 修改时间 -mtime 修改文件内容 change 改变时间 -ctime 文件属性改变 access 访问时间 -atime 访 ...
- 最新手机号正则表达式php包括166等号段
if(!preg_match("/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147)) ...
- 7款公认比较出色的Python IDE,你值得拥有!
Python作为一款比较“简洁”的编程语言,它拥有很多性价比高的性能,造就了它现在比较火热的局面,很多人都来学习Python.Python 的学习过程少不了 IDE 或者代码编辑器,或者集成的开发编辑 ...
- linux的发展过程
1. 操作系统 人与计算机硬件直接的中介 2. Linux系统组成 3. Linux的发展过程 蛋-人-人-人 unix于诞生贝尔实验室 人-谭教授 谭宁邦 minix mini unix. 主要用于 ...
- Codeforces Round #271 (Div. 2) D Flowers【计数dp】
D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input ...
- codeforces 258D DP
D. Little Elephant and Broken Sorting time limit per test 2 seconds memory limit per test 256 megaby ...
- 极简配置phpstorm+xdebug进行断点调试
以前调试的时候各种var_dump()就能得到结果,现在入手别人开发的工作,由于不了解业务和代码逻辑,又要去修改bug,就造成了修改bug效率低,所以又拾起来了xdbug,顺便总结了一下phpstor ...