Fence Repair(poj 3253)
题意:
有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度
给定各个要求的小木板的长度,及小木板的个数n,求最小费用
提示:
以
3
5 8 5为例:
先从无限长的木板上锯下长度为 21 的木板,花费 21
再从长度为21的木板上锯下长度为5的木板,花费5
再从长度为16的木板上锯下长度为8的木板,花费8
总花费 = 21+5+8 =34
思路:典型的哈夫曼树问题,但因为数据量较大,应该用优先队列优化
#include<cstdio>
#include<iostream>
#include<queue>
#define LL long long
using namespace std;
int main()
{
int n;
while(scanf("%d",&n)==)
{
priority_queue<int,vector<int>,greater<int> > q;
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
q.push(x);
}
LL sum=;
if(q.size()==)
{
int a=q.top();
sum+=a;
q.pop();
}
while(q.size()>)
{
int t=;
int a=q.top();
t+=a;
q.pop();
int b=q.top();
t+=b;
q.pop();
sum+=t;
q.push(t);
}
printf("%lld",sum);
}
}
Fence Repair(poj 3253)的更多相关文章
- Greedy:Fence Repair(POJ 3252)
Fence Repair 问题大意:农夫约翰为了修理栅栏,要将一块很长的木块切割成N块,准备切成的木板的长度为L1,L2...LN,未切割前的木板的长度恰好为切割后木板的长度的总和,每次切断木板的时候 ...
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- poj 3253:Fence Repair(堆排序应用)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23913 Accepted: 7595 Des ...
- POJ 30253 Fence Repair (二叉树+优先队列)
题目链接 Description Farmer John wants to repair a small length of the fence around the pasture. He meas ...
- Fence Repair (二叉树求解)(优先队列,先取出小的)
题目链接:http://poj.org/problem?id=3253 Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- poj 3253 Fence Repair (STL优先队列)
版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/34805369 转载请注明出 ...
- poj-3253 fence repair(贪心题)
题目描述: Farmer John wants to repair a small length of the fence around the pasture. He measures the fe ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
- POJ 3253 Fence Repair(哈夫曼树)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26167 Accepted: 8459 Des ...
随机推荐
- Redis操作+python
自动化接口测试中需要向redis中插入测试数据: 1. 连接redis: import redisself.r = redis.StrictRedis(host=env.REDIS_HOST, por ...
- Mac上的终端(Terminal)启动缓慢
最近重装10.9系统,装完后,发现终端(Terminal)启动之前1秒都不用,现在却需要5-10秒,搜寻了下,发现是终端的统日志导致的问题,只需要执行下下面的命令,终端就又身轻如燕了! sudo rm ...
- hdu 2160 母猪的故事(睡前随机水一发)(斐波那契数列)
解题思路: 一只母猪生下第二头后立马被杀掉,可以这样想即,生下第二头便被杀掉,可以看成母猪数量没变 第一天 1 第二天 2 第三天 3 :第一头生第二头后杀掉还是1头,第二头再加上第二头生下的,一共三 ...
- ReactiveCocoa初步
[self.usernameTextField.rac_textSignal subscribeNext:^(id x) { NSLog(@"%@", x); }]; 打印结果 - ...
- PHP iconv函数字符串转码导致截断问题
1.iconv函数原型 string iconv ( string $in_charset , string $out_charset , string $str ) in_charset:输入的字符 ...
- php导出excel
感觉技术掌握的有些太杂了,一会儿鼓捣java,一会儿鼓捣php,一边还搞着.net, maybe this just is life. 此前同事给某县政法委做的一套维稳信息平台,数据库是封装了mysq ...
- 图解equals与hashcode方法相等/不相等的互相关系
图解:比如equals相等的箭头指向hashcode相等,表示equals相等那么必有hashcode相等.而有两个箭头指向别人的表示可能是其中之一,比如hashcode相等,那么有可能equals相 ...
- ASP.NET WebForm中用async/await实现异步出人意料的简单
1. 在.aspx中添加异步标记 <%@ Page Language="C#" Async="true"%> 2. 在.aspx.cs或者.ascx ...
- 百度文库,linux下安装oracle客户端
linux单独安装oracle client(oracle客户端) 更新:2013-10-17 18:30 | 标签:linux oracle 1.要远程使用oracle,先下载下面三个文件,注意 ...
- editplus快捷键大全之editplus编辑快捷键
前面我们说了editplus快捷键大全之editplus文件快捷键和editplus快捷键大全之editplus光标快捷键,这里我们讲一下editplus快捷键大全之editplus编辑快捷键 删除光 ...