POJ 3253-Fence Repair(堆)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 27055 | Accepted: 8800 |
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
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank
Output
Sample Input
3
8
5
8
Sample Output
34
哈曼夫树。
题意 :把一根长度为x的木板切成n段。每段的长度已给出来。切某块木板时。花费的费用为该木板的长度。一開始以为仅仅要每次选出最长的木板然后从总的中切掉就是最小花费。实际上不是,其有用脑子想想也知道了,假设总长非常长的话,而须要的木板的长度又非常小,每次从总长中切掉花费将会非常大,所以这样的思路是不正确的,看了讨论区才明确,我们能够把切这个过程逆回去。就是把这n段木板连接起来,每次花费的钱为待连接的两段木板的长度。(细致想想,切跟连花费的钱是同样的,即是等价的,画一下图能够看出来) 然后有一种思想是:初始化一个最小堆,每次从堆中选出最小的两段木板进行连接。连接完之后在放入到堆中。总共n-1次连接就可以
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
ll a[20010];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
scanf("%lld",a+i);
make_heap(a,a+n,greater<int>());
int t=n-1;ll ans=0,tem;
while(t--)
{
tem=a[0];pop_heap(a,a+n,greater<int>());--n;
tem+=a[0];pop_heap(a,a+n,greater<int>());--n;
ans+=tem;a[n++]=tem;
push_heap(a,a+n,greater<int>());
}
printf("%lld\n",ans);
}
return 0;
}
POJ 3253-Fence Repair(堆)的更多相关文章
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- 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 优先队列
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...
- POj 3253 Fence Repair(修农场栅栏,锯木板)(小根堆 + 哈弗曼建树得最小权值思想 )
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 28359 Accepted: 9213 Des ...
- POJ 3253 Fence Repair (贪心)
Fence Repair Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- poj 3253:Fence Repair(堆排序应用)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23913 Accepted: 7595 Des ...
- POJ 3253 Fence Repair(哈夫曼树)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26167 Accepted: 8459 Des ...
- poj 3253 Fence Repair(priority_queue)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 40465 Accepted: 13229 De ...
- POJ 3253 Fence Repair 贪心 优先级队列
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 77001 Accepted: 25185 De ...
随机推荐
- linux RAID10测试
mdadm命令用于管理系统软件RAID硬盘阵列,格式为:"mdadm [模式] <RAID设备名称> [选项] [成员设备名称]". mdadm命令能够在Linux系统 ...
- HeidiSQL数据库mysql/sql-server连接工具
HeidiSQL,是一款可以显示表在存储中占得空间,体积小的mysql.sql-server连接工具! 下载地址: https://www.heidisql.com/download.php 中文版: ...
- Android C/C++ 开发
标准的做法是 把 src 放到 /Android/develop/ 目录下去,然后建立 Android.mk 文件, 然后 make modue ...., 但是,这种方式 太麻烦. 下面介绍一种方 ...
- IBM InfoSphere DataStage 8.1 DataStage Job 开发具体解释
简单介绍 DataStage 使用了 Client-Server 架构,server端存储全部的项目和元数据,client DataStage Designer 为整个 ETL 过程提供了一个图形化的 ...
- linux(red hat)下安装jenkins
Jenkins的安装能够分为在线安装和下载软件本地安装.我这里用的是另外一种方法,将其下载后是一个应用程序直接点击安装就能够.等安装完后配置一下jdk的路径就ok啦!接下来进行具体的说明: 一.前提 ...
- SpringBoot自定义HttpMessageConverter
Spring就是一个大大的插线板,上面插着各种各样的Bean. SpringBoot大大简化了Spring的配置,将原来放在XML中的配置大量的在代码中使用注解实现.这么做有利有弊,总体上利大于弊. ...
- SpringBoot定时器
使用Component注解注解一个类,这个类就变成了一个组件.组件可以有很多不同的特性,比如Scheduled注解为组件的某个函数添加了定时的特性. @Component public class M ...
- POJ 1364 King (差分约束)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8660 Accepted: 3263 Description ...
- DBA_实践指南系列2_Oracle Erp R12系统安装配置设定Setup(案例)
2013-12-02 Created By BaoXinjian
- jquery中常见问题及解决办法小结
1 在开发开放聊天室的过程中,遇到使用ajax提交表单插入数据库时会插入两条数据的情况 解决办法,在ajax函数返回后,return false. $("#btn").click( ...