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 ...
随机推荐
- linux内核数据结构学习总结
目录 . 进程相关数据结构 ) struct task_struct ) struct cred ) struct pid_link ) struct pid ) struct signal_stru ...
- jsp学习(五)
在进行jsp与jdbc连接时,出现这样一个错误,提示如下: java.net.ConnectException: Connection refused: connect 后来发现是由于mysql数据库 ...
- appium-车友会欢迎界面向右滑动4次点击‘立即体验’进入首屏
代码如下: driver.swipe(610, 2452, 658, 2452, 200) 只是示例滑动1页,可以使用循环,下一页比上一页x坐标大48
- linux下svn命令大全
linux下svn命令大全 1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如:svn checkout svn://192.168.1.1/p ...
- ios 清理缓存
//拿到要清理的路径,其实就是caches的路径,一般像这种很多地方都会用到的地方真好搞成宏,不过现在苹果不提倡用宏了 //在swift中可以定义成全局的常量 //遍历caches,将内部的文件大小计 ...
- centos 搭建gitlab
#修改yum源 yum -y install wget cd /etc/yum.repos.d wget -O CentOS-Base.repo http://mirrors.aliyun.com/r ...
- MySql 创建只读账号
GRANT Select ON *.* TO reader@192.168.1.123 IDENTIFIED BY "123456" GRANT 可以立刻生效 在mysql 5 ...
- Center os FTP配置
原文:http://www.aicoffees.com/itshare/412261137.html
- Javascript的调试利器:Firebug使用详解
转载自:http://blog.csdn.net/tianxiaode/archive/2007/09/02/1769152.aspx 一直在用firebug,可是没有这么精通,今天看到本文章觉得 ...
- Office Web Apps资源
http://www.cnblogs.com/poissonnotes/p/3277280.html#!comments http://www.cnblogs.com/poissonnotes/p/3 ...