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 ...
随机推荐
- 使用任务Task 简化异步编程
使用任务简化异步编程 Igor Ostrovsky 下载代码示例 异步编程是实现与程序其余部分并发运行的较大开销操作的一组技术. 常出现异步编程的一个领域是有图形化 UI 的程序环境:当开销较大的操作 ...
- 【WebKit内核 CEF3 】 第一篇:下载分支代码并本地编译
关于CEF Chromium Embedded Framework 简单说就是 WebKit内核的 对外绑定. 当前主流浏览器内核 一.Trident内核代表产品Internet Explorer ...
- MySQL插入性能优化
目录 MySQL插入性能优化 代码优化 values 多个 一个事务 插入字段尽量少,尽量用默认值 关闭 unique_checks bulk_insert_buffer_size 配置优化 inno ...
- X86 寻址方式、AT&T 汇编语言相关知识、AT&T 与 Intel 汇编语言的比较、gcc 嵌入式汇编
注:本分类下文章大多整理自<深入分析linux内核源代码>一书,另有参考其他一些资料如<linux内核完全剖析>.<linux c 编程一站式学习>等,只是为了更好 ...
- rpm安装PostgreSQL
一.首先去官网下载相关的安装包 https://yum.postgresql.org/rpmchart.php 二.下载安装包 1. 最小的数据库服务器安装包: postgresql96--1PGDG ...
- ubuntu16安装配置nginx
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev ...
- 【转】用SQL实现树的查询
树形结构是一类重要的非线性结构,在关系型数据库中如何对具有树形结构的表进行查询,从而得到所需的数据是一个常见的问题.本文笔者以 SQL Server 2000 为例,就一些常用的查询给出了相应的算法与 ...
- OpenGl学习 SelectObject函数
SelectObject 函数功能:该函数选择一对象到指定的设备上下文环境中,该新对象替换先前的相同类型的对象. 函数原型:HGDIOBJ SelectObject(HDC hdc, HGDIOB ...
- Java8 新特性简介
Java8是2014年发布的,至今也已经有快三年的时间了,之前虽然有学习过,但是学的比较零散,不成系统,而且也没有覆盖到Java8所有的特性. 由于公司已经使用了JDK1.8,所以工作中能使用Java ...
- Untrusted 游戏 通关攻略
这个游戏还不错,用了两个晚上的时间通过并写下解法.这个游戏通过修改JS代码来通关的游戏.很考验玩家的解决问题能力,同时也有一定的可玩性. 游戏地址 http://alexnisnevich.githu ...