poj3253哈夫曼树
Fence Repair
Time Limit: 2000 MS Memory Limit: 65536 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
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
Hint
The original board measures 8+5+8=21. The first cut will cost 21,
and should be used to cut the board into pieces measuring 13 and 8. The
second cut will cost 13, and should be used to cut the 13 into 8 and 5.
This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the
second cut would cost 16 for a total of 37 (which is more than 34).
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <queue> using namespace std; int main()
{
int t;
__int64 sum1,sum;
__int64 a[];
bool flag=false;
while(~scanf("%d",&t))
{
priority_queue<int,vector<int>,greater<int> > q;
for(int i=;i<t;i++)
{
cin>>a[i];
q.push(a[i]);
}
sum1=;
sum=;
while()
{
if(t==)
{
cout<<a[]<<endl;
flag=true;
break;
}
int x=q.top();
q.pop();
int y=q.top();
q.pop();
sum1=x+y;
sum+=sum1;
if(q.empty())
break;
q.push(sum1); }
if(!flag)
cout<<sum<<endl;
}
return ;
}
注释:
题意:取原序列里两个最小的 加和放到序列里(保证每次取得都为队列里最小的两个) 最后生一个最大的即为不用亲自割的
1:优先队列
poj3253哈夫曼树的更多相关文章
- 哈夫曼树---POJ3253
http://poj.org/problem?id=3253 这就是 最典型的哈夫曼树的题型,我们就根据这道题学习一下哈夫曼树 这是最开始我们把21据下来之后我们据下8,然后再据下5得到34,可以看出 ...
- poj3253 Fence Repair【哈夫曼树+优先队列】
Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- (哈夫曼树)HuffmanTree的java实现
参考自:http://blog.csdn.net/jdhanhua/article/details/6621026 哈夫曼树 哈夫曼树(霍夫曼树)又称为最优树. 1.路径和路径长度在一棵树中,从一个结 ...
- 数据结构之C语言实现哈夫曼树
1.基本概念 a.路径和路径长度 若在一棵树中存在着一个结点序列 k1,k2,……,kj, 使得 ki是ki+1 的双亲(1<=i<j),则称此结点序列是从 k1 到 kj 的路径. 从 ...
- C++哈夫曼树编码和译码的实现
一.背景介绍: 给定n个权值作为n个叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman Tree).哈夫曼树是带权路径长度最短的树,权值较大的 ...
- 数据结构图文解析之:哈夫曼树与哈夫曼编码详解及C++模板实现
0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...
- 哈夫曼树(三)之 Java详解
前面分别通过C和C++实现了哈夫曼树,本章给出哈夫曼树的java版本. 目录 1. 哈夫曼树的介绍 2. 哈夫曼树的图文解析 3. 哈夫曼树的基本操作 4. 哈夫曼树的完整源码 转载请注明出处:htt ...
- 哈夫曼树(二)之 C++详解
上一章介绍了哈夫曼树的基本概念,并通过C语言实现了哈夫曼树.本章是哈夫曼树的C++实现. 目录 1. 哈夫曼树的介绍 2. 哈夫曼树的图文解析 3. 哈夫曼树的基本操作 4. 哈夫曼树的完整源码 转载 ...
- 哈夫曼树(一)之 C语言详解
本章介绍哈夫曼树.和以往一样,本文会先对哈夫曼树的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现:实现的语言虽不同,但是原理如出一辙,选择其中之一进行了解即可.若 ...
随机推荐
- Luogu 1764 翻转游戏 - 枚举 + 搜索
题目描述 kkke在一个n*n的棋盘上进行一个翻转游戏.棋盘的每个格子上都放有一个棋子,每个棋子有2个面,一面是黑色的,另一面是白色的.初始的时候,棋盘上的棋子有的黑色向上,有的白色向上.现在kkke ...
- refused to Connection
两种情况: .数据库账号密码错误 .mysql挂了
- 如何在 Laravel 中连接多个 MySQL 数据库
第一步.定义数据库链接 config/database.php <?php return [ 'default' => 'mysql', 'connections' => [ # 主 ...
- pytho常用模块2——random
random模块用来生成随机数,有以下几个常用方法: import random random.random() #产生随机数[0-1) random.randint(a,b) #产生随机整数[a,b ...
- Web应用获取文件路径的方法
拥有 HttpServletRequest req 对象 req.getSession().getServletContext().getRealPath("/") ----- ...
- Easyui form 表单中的 radio 无法一开始就选中原因
是因为你在form表单里面,所以会出现样式没有选中状态,必须要从后台去获取数据,然后再绑定.也就是初始化数据.
- centos7 虚拟机安装docker-ce-17.09
1.创建虚拟机使用iso镜像centos-x86_64-7.3.1611 2.安装centos选择桌面版 3.配置命令行环境,网卡(见博客另一篇文章) 4.安装container-selinux-2. ...
- 2019.01.13 bzoj4538: [Hnoi2016]网络(树链剖分)
传送门 树链剖分一眼题. 题意简述: 给定一棵树,有三种操作: 加入一条路径 删除一条已加入的路径 询问不过一个点x的路径的最大值. 思路: 直接树链剖分维护答案. 因为询问的事不过点xxx的最大值, ...
- idea环境下js、css中文乱码
idea2018.2+tomcat8+java8+win10 异常:本地js和css通过tomcat发布时,在页面打印出的中文是乱码.而从数据库读取的中文数据和html的中文显示正常. 解决方法: 步 ...
- jQuery警告/确认/提示弹出对话框效果(替换传统JavaScript下的提示框)
http://www.51xuediannao.com/js/jquery/jquery_tsk/ http://www.jq22.com/demo/jqueryConfirm20160413/