Fence Repair
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

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).

Source

 
题意: 切木板问题 给出你  要求的n块木板的大小  并且定义每次切板 的代价数值上等于原木板的大小
          为完成要求 输出代价最小值。
 
题解: 每次选取最少的两个合并,直到剩一个。

然而这样,每次都要排序以确定最少两个木板,代码复杂度、时间复杂度和空间复杂度都要上升

        STL优先队列处理 将 要求的n块木板入队,每次将最小的两个s1,s2 出队    求和s1+s2并累加到sum 说明s1+s2大小的木板是 s1,s2的原板
        取出最小的两个是为了满足  用最小的代价得到 当前最小的两块木板  之后将s1+s2入队  直到队列中只剩下 一个元素
        输出sum
 
 #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 优先队列处理的更多相关文章

  1. POJ 2431 优先队列

    汽车每过一单位消耗一单位油,其中有给定加油站可加油,问到达终点加油的最小次数. 做法很多的题,其中优先对列解这题是很经典的想法,枚举每个加油站,判断下当前油量是否小于0,小于0就在前面挑最大几个直至油 ...

  2. poj 3053 Fence Repair(优先队列)

    题目链接:http://poj.org/problem?id=3253 思路分析:题目与哈夫曼编码原理相同,使用优先队列与贪心思想:读入数据在优先队列中,弹出两个数计算它们的和,再压入队列中: 代码如 ...

  3. POJ 2442(优先队列 k路归并 堆)

    Description Given m sequences, each contains n non-negative integer. Now we may select one number fr ...

  4. poj 1511 优先队列优化dijkstra *

    题意:两遍最短路 链接:点我 注意结果用long long #include<cstdio> #include<iostream> #include<algorithm& ...

  5. Moo University - Financial Aid POJ 2010 优先队列(最大堆)

    题目:http://poj.org/problem?id=2010 题目大意: 奶牛上大学.因为经济问题,每头奶牛都需要一定的补助需求,学校会提供一定的资金用于补助 每头牛都有自己的分数,学校招收的名 ...

  6. POJ 1442 优先队列

    题意:有一些ADD和GET操作.n次ADD操作,每次往序列中加入一个数,由ADD操作可知序列长度为1-n时序列的组成.GET操作输入一个序列长度,输出当前长度序列第i大的元素的值.i初始为0,每次GE ...

  7. Labeling Balls POJ - 3687 优先队列 + 反向拓扑

    优先队列 + 反向拓扑 //#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include ...

  8. Sunscreen(POJ 3614 优先队列)

    Sunscreen Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5898   Accepted: 2068 Descrip ...

  9. POJ 3635 优先队列BFS

    (感谢lyd学长的幻灯片) 注意vis数组的应用 在vis[i][j]中 i表示到了第i个点 j表示还剩j升油 vis[i][j]表示最小话费. 这样只需搜到话费比它少的更新入堆就OK了 //By: ...

随机推荐

  1. vue项目跨域问题

    跨域 了解同源政策:所谓"同源"指的是"三个相同". 协议相同 域名相同 端口相同 解决跨域 jsonp 缺点:只能get请求 ,需要修改B网站的代码 cors ...

  2. CentOS7 ngnix 的安装和配置

    刚开始我也在纠结到底是该用Apache呢还是Nginx?然后网上各种查看了它俩的对比,总结了它俩最大区别在于apache是同步多进程模型,在处理动态有优势:nginx是异步的,并发性能比较好,cpu内 ...

  3. 谈谈Integer中的静态类IntegerCache

            学习的本质就是一个赋值的过程,用新知识来覆盖你的旧知识或者无知(null).掌握知识是自己的, 分享知识,才能帮助更多的人,创造更大的价值.学贵以恒,以此自勉,与君共享.----曦阳X ...

  4. redis redis-cli 操作指令

    默认选择 db库是 0 redis-cli -p 6379   查看当前所在“db库”所有的缓存key redis 127.0.0.1:6379> keys *   选择 db库 redis 1 ...

  5. python简单实用gunicorn部署

    linux 安装 pyuthon 安装   pip install gunicorn manage.py 文件 from app import create_app app = create_app( ...

  6. nginx Keepalived高可用集群

    一.Keepalived高可用 1.简介 Keepalived软件起初是专为LvS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能.因此, ...

  7. [译]The Python Tutorial#12. Virtual Environments and Packages

    [译]The Python Tutorial#Virtual Environments and Packages 12.1 Introduction Python应用经常使用不属于标准库的包和模块.应 ...

  8. Fliptile POJ - 3279 (开关问题)

    Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16483   Accepted: 6017 Descrip ...

  9. (原创)task和function语法的使用讨论(Verilog,CPLD/FPGA)

    1. Abstract function和task语句的功能有很多的相似之处,在需要有多个相同的电路生成时,可以考虑使用它们来实现.因为个人使用它们比较少,所以对它们没有进行更深的了解,现在时间比较充 ...

  10. Aizu 2560 Point Distance FFT

    题意: 有一个\(N \times N\)的方阵,第\(x\)行第\(y\)列有\(C_{x,y}\)个点\((0 \leq C_{x,y} \leq 9)\). 任选两个不同的点,求两点欧几里德距离 ...