博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj-3253Fence Repair(优先对列 求木棍的最小和)
阅读量:4049 次
发布时间:2019-05-25

本文共 2655 字,大约阅读时间需要 8 分钟。

Fence Repair
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 39668   Accepted: 12924

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

3858

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-1次切割将其切成n块FJ想要的木板,对于每块木板,没切割一次,将会消耗和这条木板长度值相等的金钱,问最少需要多少钱,可将木板切成自己想要的n块。
相当于逆向思维 越小的片段越后切 所以先将最小的两个片段相加 看成一整个片段  再将这个片段插回到队列中 因为在没有产生这两个片段之前  原本就是这个样子  
priority_queue<int,vector<int>,greater<int> > que;这个可以查找最小的片段
#include
#include
#include
#include
#include
#include
using namespace std;int n;int a[20005];int main(){ while(~scanf("%d",&n)) { memset(a,0,sizeof(a)); for(int i=0;i
,greater
> que; for(int i=0;i
1) { long long p,q; p=que.top(); que.pop(); q=que.top(); que.pop(); sum+=p+q; que.push(p+q); } printf("%lld\n",sum); } return 0;}

转载地址:http://vxfci.baihongyu.com/

你可能感兴趣的文章
<转>文档视图指针互获
查看>>
从mysql中 导出/导入表及数据
查看>>
HQL语句大全(转)
查看>>
几个常用的Javascript字符串处理函数 spilt(),join(),substring()和indexof()
查看>>
javascript传参字符串 与引号的嵌套调用
查看>>
swiper插件的的使用
查看>>
layui插件的使用
查看>>
JS牛客网编译环境的使用
查看>>
9、VUE面经
查看>>
关于进制转换的具体实现代码
查看>>
Golang 数据可视化利器 go-echarts ,实际使用
查看>>
mysql 跨机器查询,使用dblink
查看>>
mysql5.6.34 升级到mysql5.7.32
查看>>
dba 常用查询
查看>>
Oracle 异机恢复
查看>>
Oracle 12C DG 搭建(RAC-RAC/RAC-单机)
查看>>
Truncate 表之恢复
查看>>
Oracle DG failover 后恢复
查看>>
mysql 主从同步配置
查看>>
为什么很多程序员都选择跳槽?
查看>>