Fence Repair

Time Limit: 2000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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

Line 1: One integer N, the number of planks
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank

Output

Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

Sample Input

3
8
5
8

Sample Output

34

Hint

He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8.
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哈夫曼树的更多相关文章

  1. 哈夫曼树---POJ3253

    http://poj.org/problem?id=3253 这就是 最典型的哈夫曼树的题型,我们就根据这道题学习一下哈夫曼树 这是最开始我们把21据下来之后我们据下8,然后再据下5得到34,可以看出 ...

  2. poj3253 Fence Repair【哈夫曼树+优先队列】

    Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  3. (哈夫曼树)HuffmanTree的java实现

    参考自:http://blog.csdn.net/jdhanhua/article/details/6621026 哈夫曼树 哈夫曼树(霍夫曼树)又称为最优树. 1.路径和路径长度在一棵树中,从一个结 ...

  4. 数据结构之C语言实现哈夫曼树

    1.基本概念 a.路径和路径长度 若在一棵树中存在着一个结点序列 k1,k2,……,kj, 使得 ki是ki+1 的双亲(1<=i<j),则称此结点序列是从 k1 到 kj 的路径. 从 ...

  5. C++哈夫曼树编码和译码的实现

    一.背景介绍: 给定n个权值作为n个叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman Tree).哈夫曼树是带权路径长度最短的树,权值较大的 ...

  6. 数据结构图文解析之:哈夫曼树与哈夫曼编码详解及C++模板实现

    0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...

  7. 哈夫曼树(三)之 Java详解

    前面分别通过C和C++实现了哈夫曼树,本章给出哈夫曼树的java版本. 目录 1. 哈夫曼树的介绍 2. 哈夫曼树的图文解析 3. 哈夫曼树的基本操作 4. 哈夫曼树的完整源码 转载请注明出处:htt ...

  8. 哈夫曼树(二)之 C++详解

    上一章介绍了哈夫曼树的基本概念,并通过C语言实现了哈夫曼树.本章是哈夫曼树的C++实现. 目录 1. 哈夫曼树的介绍 2. 哈夫曼树的图文解析 3. 哈夫曼树的基本操作 4. 哈夫曼树的完整源码 转载 ...

  9. 哈夫曼树(一)之 C语言详解

    本章介绍哈夫曼树.和以往一样,本文会先对哈夫曼树的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现:实现的语言虽不同,但是原理如出一辙,选择其中之一进行了解即可.若 ...

随机推荐

  1. POJ3678 Katu Puzzle

    原题链接 \(2-SAT\)模板题. 将\(AND,OR,XOR\)转换成\(2-SAT\)的命题形式连边,用\(tarjan\)求强连通分量并检验即可. #include<cstdio> ...

  2. MAC/Xcode简单操作命令

    快捷键: command(windows) + c: 复制 command + V : 粘贴 command + x: 剪切(只在当前应用程序内有效) 在mac系统下表示剪切功能: 先command ...

  3. The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone问题解决

    从错误即可知道是时区的错误,因此只要将时区设置为你当前系统时区即可 因此使用root用户登录mysql,按照如下图所示操作即可. 把时区设置为所在地时区(即东八区的时区)后,再连接数据库就可以了

  4. leveldb skiplist的改编非并发去除内存池版本 代码练习

    // MuSkipList.cpp: 定义控制台应用程序的入口点. // #include "stdafx.h" #include <random> #include ...

  5. powerdesigner mysql逆向工程注释不显示问题

  6. m序列c语言实现

    演示,不是算法 void m4() { int a[4]={1,0,0,1}; int m[15]; int temp; for(int i=0;i<15;i++){ m[i] = a[0]; ...

  7. [转载]How To Install Nginx And PHP-FPM On CentOS 6 Via Yum

    http://www.lifelinux.com/how-to-install-nginx-and-php-fpm-on-centos-6-via-yum/ http://blog.csdn.net/ ...

  8. Redis的基操

    redis:通常BOLEAN操作类型,操作成功返回1,操作失败返回0 通常如果往设置的key插入值,但是这个key不存在,redis则会创建 向redis里的某个key插入多个值时,值和值之间用空格隔 ...

  9. org.jsoup.HttpStatusException: HTTP error fetching URL. Status=403

    爬取网站的时候 conn = Jsoup.connect(url).timeout(5000).get();直接用get方法,有些网站可以正常爬取. 但是有些网站报403错误,403是一种在网站访问的 ...

  10. 2018.11.24 poj3261Milk Patterns(后缀数组)

    传送门 后缀数组经典题. 貌似可以用二分答案+后缀数组? 我自己yyyyyy了一个好写一点的方法. 直接先预处理出heightheightheight数组. 然后对于所有连续的k−1k-1k−1个he ...