题意:

有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度

给定各个要求的小木板的长度,及小木板的个数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)的更多相关文章

  1. Greedy:Fence Repair(POJ 3252)

    Fence Repair 问题大意:农夫约翰为了修理栅栏,要将一块很长的木块切割成N块,准备切成的木板的长度为L1,L2...LN,未切割前的木板的长度恰好为切割后木板的长度的总和,每次切断木板的时候 ...

  2. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

  3. poj 3253:Fence Repair(堆排序应用)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23913   Accepted: 7595 Des ...

  4. POJ 30253 Fence Repair (二叉树+优先队列)

    题目链接 Description Farmer John wants to repair a small length of the fence around the pasture. He meas ...

  5. Fence Repair (二叉树求解)(优先队列,先取出小的)

    题目链接:http://poj.org/problem?id=3253 Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  6. poj 3253 Fence Repair (STL优先队列)

    版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/34805369 转载请注明出 ...

  7. poj-3253 fence repair(贪心题)

    题目描述: Farmer John wants to repair a small length of the fence around the pasture. He measures the fe ...

  8. POJ 3253 Fence Repair (优先队列)

    POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...

  9. POJ 3253 Fence Repair(哈夫曼树)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26167   Accepted: 8459 Des ...

随机推荐

  1. Code Review Engine Learning

    相关学习资料 https://www.owasp.org/index.php/Code_review https://www.owasp.org/images/8/8e/OWASP_Code_Revi ...

  2. JSP登录验证并显示信息

    加入C标签: 加入jstl.jar 和standard.jar加入Lib文件夹中 将c.tld放入WEB-Info文件夹中 index.jsp <%@ page language="j ...

  3. ubuntu 12.04安装telnet和ssh服务

    ubuntu安装telnet服务 1. sudo apt-get install xinetd telnetd sudo vi /etc/inetd.conf并加入以下一行,假如没有发现这个文件,自己 ...

  4. php两种导出excel的方法

    所需要的:jquery库,phpexcel插件,页面导出excel效果测试文件explode.php,excel导出功能实现文件exp.php和explode_excel.php,文件相关内容在此文下 ...

  5. $.ajax返回的JSON格式的数据后无法执行success的解决方法

    近段时间做项目,在项目使用了ajax技术,遇到了一个奇怪的问题:"$.ajax返回的JSON格式的数据无法执行success",代码是这样写的: 1 $.ajax({ 2 .. 3 ...

  6. Laravel教程 八:queryScope 和 setAttribute

    Laravel教程 八:queryScope 和 setAttribute 此文章为原创文章,未经同意,禁止转载. Laravel Eloquent Database 直接就是按照上一节所说的那样,我 ...

  7. nginx try_files命令

    location / { index index.html index.htm index.php l.php; autoindex on; try_files $uri $uri/ /index.p ...

  8. 使用log4net连接Mysql数据库配置

    log4net配置: //Author:GaoBingBing [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net ...

  9. Xcode6中添加pch全局引用文件

    前沿:xcode6中去掉了pch,为了一些琐碎的头文件引用,加快了 编译速度! xcode6添加pch文件方法 1. 右键Supporting File,选择“New File” 2. 选择Other ...

  10. Socket网络编程(1)

    TCP/IP 简单介绍 应用层 (Application):应用层是个很广泛的概念,有一些基本相同的系统级 TCP/IP 应用以及应用协议,也有许多的企业商业应用和互联网应用. 传输层 (Transp ...