题目链接

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).

** 分析:**

首先,切割的方法可以参见二叉树来描述,该二叉树的每一个叶子节点就对应了切割出的一块块木板,叶子节点的深度就对应了为了得到对应木板所需的切割次数,开销的合计就是各叶子节点的模板的长度*节点的深度的总和。

则可以得到最短的板和次短的板的节点应该是兄弟节点。

将Li按照大小顺序排列,则最短的板应该是L1,次短的板应该是L2,也就意味着他们是从一块长度为L1+L2的板切割得来的。由于切割顺序是自由的,不妨当做是最后被切割。则在此次切割之前有(L1+L2),L3,L4···,LN,这样的N-1块木板存在。之后的思路同上。

代码:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
long long int ans=0;
int N;
scanf("%d",&N);
int a[20009];
for(int i=0; i<N; i++)
scanf("%d",&a[i]); ///N的值这里需要注意的是在不断地减少的,因为每一次的两个木板和并后,木板的个数就减少一个
while(N>1)
{
int min1=0,min2=1;
if(a[min1]>a[min2]) swap(min1,min2);///首先我们将第一个当做最小的,第二值当做第二小
for(int i=2; i<N; i++)
{
///其余的值分别于剩下的值比较,找出整个数组里面的最小和次小
{
if(a[i]<a[min1])
min2=min1;
min1=i;
}
else if(a[i]<a[min2])
{
min2=i;
}
} int t=a[min1]+a[min2];///两个木板的长度即为这次的开销
ans+=t; if(min1==N-1) swap(min1,min2);///因为要把最后一个去掉,所以min1的下标不能是最后一个
///把之前数组的值刷新
a[min1]=t;
a[min2]=a[N-1];
N--;
}
printf("%lld\n",ans);
return 0;
}

由于只需要从板的集合中取出最小的两块,并且把两块板长度之和加入到结合中即可,因此可以用优先队列高效的实现。

#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
int main()
{
int n,num;
long long int ans=0;
priority_queue<int ,vector<int>,greater<int> >que;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d",&num);
que.push(num);
}
int num1,num2;
while(que.size()>1)
{
num1=que.top();
que.pop();
num2=que.top();
que.pop();
ans+=num1+num2;
que.push(num1+num2);
}
printf("%lld\n",ans);
return 0;
}

POJ 30253 Fence Repair (二叉树+优先队列)的更多相关文章

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

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

  2. poj 3253 Fence Repair(优先队列+哈夫曼树)

    题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...

  3. POJ 3253 Fence Repair(优先队列,哈夫曼树,模拟)

    题目 //做哈夫曼树时,可以用优先队列(误?) //这道题教我们优先队列的一个用法:取前n个数(最大的或者最小的) //哈夫曼树 //64位 //超时->优先队列,,,, //这道题的优先队列用 ...

  4. POJ 3253 Fence Repair STL 优先队列

    这题做完后觉得很水,主要的想法就是逆过程思考,原题是截断,可以想成是拼装,一共有n根木棍,最后要拼成一根完整的,每两根小的拼成一根大的,拼成后的木棍长度就是费用,要求费用最少.显然的是一共会拼接n-1 ...

  5. POJ 3253 Fence Repair 贪心+优先队列

    题意:农夫要将板割成n块,长度分别为L1,L2,...Ln.每次切断木板的花费为这块板的长度,问最小花费.21 分为 5 8 8三部分.   思路:思考将n部分进行n-1次两两合成最终合成L长度和题目 ...

  6. poj 3253 Fence Repair 优先队列

    poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...

  7. POJ 3253 Fence Repair(修篱笆)

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

  8. POJ - 3253 Fence Repair 优先队列+贪心

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

  9. [ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25274   Accepted: 8131 Des ...

随机推荐

  1. TCP系列32—窗口管理&流控—6、TCP zero windows和persist timer

    一.简介 我们之前介绍过,TCP报文中的window size表示发出这个报文的一端准备多少bytes的数据,当TCP的一端一直接收数据,但是应用层没有及时读取的话,数据一直在TCP模块中缓存,最终受 ...

  2. TCP系列27—窗口管理&流控—1、概述

    在前面的内容中我们依次介绍了TCP的连接建立和终止过程和TCP的各种重传方式.接着我们在这部分首先关注交互式应用TCP连接相关内容如延迟ACK.Nagle算法.Cork算法等,接着我们引入流控机制(f ...

  3. lol人物模型提取(二)

      两个dds文件怎么导入到一个模型上呢?这模型又不能拆开.   一开始我想的是用两个材质球来完成,一个材质球对应一个dds文件,然而行不通.   一个材质球对应两个dds文件还不太会弄,于是我想着干 ...

  4. Jrebel 工具学习

    Jrebel 可快速实现热部署,节省了大量重启时间,提高了个人开发效率.网上可搜索到破解版. http://baike.baidu.com/link?url=wuzv7Wa7SMUKltJr-dyta ...

  5. jconsole工具监控java运行情况

    jconsole是jdk自带的工具.所以要先安装jdk  1.jconsole工具的路径: 通过which jconsole来查看 /usr/local/jdk1.7.0_79/bin/jconsol ...

  6. 查询MySQL某字段相同值得重复数据

    1.先查询重复的id: SELECT book_id,COUNT(*) AS COUNT FROM xs_book_source WHERE site_id=5 GROUP BY book_id HA ...

  7. tcp中的发送窗口是啥意思?

    初始的三次握手: 02:52:36.585412 IP 127.0.0.1.59764 > 127.0.0.1.8000: Flags [S], seq 3800457532, win 4369 ...

  8. js滚动大于多少菜单就开始固定

    //导航置顶 $(window).scroll(function () { var pos = $(window).scrollTop(); ) { $("#menu").addC ...

  9. 《转》HTTP 协议入门

    HTTP 协议是互联网的基础协议,也是网页开发的必备知识,最新版本 HTTP/2 更是让它成为技术热点. 本文介绍 HTTP 协议的历史演变和设计思路. 一.HTTP/0.9 HTTP 是基于 TCP ...

  10. Python fileinput模块详解

    Python的fileinput模块可以快速对一个或多个文件进行循环遍历. import fileinput for line in fileinput.input(): process(line) ...