题目链接

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. Window Classes in Win32

    探索Win32系统之窗口类(Window Classes in Win32) Kyle MarshMicrosoft Developer Network Technology GroupMSDN技术组 ...

  2. iOS-AFNetworking与ASIHTTPRequest的区别

    一.底层实现 1.AFN的底层实现基于OC的NSURLConnection和NSURLSession  2.ASI的底层实现基于纯C语言的CFNetwork框架  3.因为NSURLConnectio ...

  3. css3边框阴影效果

    下面来说下css3阴影的语法: box-shadow:none | <shadow> [ , <shadow> ]* <shadow> = inset? & ...

  4. 我们在删除SQL Sever某个数据库表中数据的时候,希望ID重新从1开始,而不是紧跟着最后一个ID开始需要的命令

    一.如果数据重要,请先备份数据 二.删除表中数据 SQL: Delete From ('表名')  如:Delete From abcd 三.执行新语句 SQL: dbcc checkident('表 ...

  5. 求csdn博客优良编辑方法

    看见很多大牛的csdn博客编写的非常好,阅读体验也非常强.我就纳闷了,为啥我插公式也不行,插图片也不行呢... 插图片问题:图片不能复制招贴,否则在编辑的时候可以显示但是在发表之后就无法显示了.想要显 ...

  6. springBoot @Enable*注解的工作原理

    使用注解实现异步 RunnableDemo类 package com.boot.enable.bootenable; import org.springframework.scheduling.ann ...

  7. P3986 斐波那契数列

    题目描述 定义一个数列: f(0)=a,f(1)=b,f(n)=f(n−1)+f(n−2) 其中 a,b均为正整数,n≥2 . 问有多少种 (a,b),使得 k 出现在这个数列里,且不是前两项. 由于 ...

  8. P2420 让我们异或吧

    题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中…xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor( B是否是男生)=A和B是否能 ...

  9. CentOS 服务ftp(vsftpd)

    1.检查是否已经安装vsftpd yum list installed | grep vsftpd 2.安装vsftpd yum install -y vsftpd 3.检查vsftpd system ...

  10. BZOJ2298:[HAOI2011]problem a——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=2298 https://www.luogu.org/problemnew/show/P2519 一次 ...