题目链接:http://poj.org/problem?id=3253

Fence Repair
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 61005   Accepted: 20119

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).
题目大意:将一块很长的木板切割成n块,每块的长度为a[i]  未切割前木板的长度恰好为切割后木板长度的总和。 每次切割木板时,需要开销为这块木板的长度。 例如长度为21的木板切割成
长度为5 8 8的三块木板。长度为21的木板切割成13 8的两块,需要开销21 ,长度为13的木板切割成5 8的两块木板,需要开销为13 ,所以总共要34。    问你切割木板所需要的最小开销是多少?
思路:由于切割木板的顺序不确定,自由度很高,这个题目貌似很难入手。  其实呢, 只要每次都找最小的两块木板,把新生成的木板加入进去就行了,一直循环此过程,就是最优解
那么为什么呢??   其实就是一颗二叉树,因为不管你怎么选叶子节点的个数都是一样的(就是n块木板) 该木板所需的花费刚好为该节点的值*该节点的深度,那么是不是越小的放在越下面
花费金额越少呢??   就是这样了,看代码
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=2e4+10;
const int maxk=1e4+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int n;
int a[maxn];
void solve()
{ll ans=0;while(n>1){int mi1=0,mi2=1;if(a[mi1]>a[mi2]) swap(mi1,mi2);for(int i=2;i<n;i++){if(a[i]<a[mi1]){mi2=mi1;mi1=i;}else if(a[i]<a[mi2]){mi2=i;}}int l=a[mi1]+a[mi2];ans+=l;if(a[mi1]==n-1) swap(mi1,mi2);a[mi1]=l;a[mi2]=a[n-1];n--;}cout<<ans<<endl;
}
int main()
{cin>>n;for(int i=0;i<n;i++){cin>>a[i];}sort(a,a+n);solve();return 0;
}

上面的算法复杂度是n*n,其实还有一种更快的方法,原理跟上面的一样,但是下面的算法用到优先队列,所以把复杂度降到nlogn

看代码

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=2e4+10;
const int maxk=1e4+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int n;
int a[maxn];
priority_queue<int,vector<int>,greater<int> >que;//声明一个从小到大取出数值的优先队列
void solve()
{ll ans=0;for(int i=0;i<n;i++)que.push(a[i]);//全部存入队列while(n>1){int l1,l2;l1=que.top();que.pop();l2=que.top();que.pop();int t=l1+l2;ans+=t;que.push(t);n--;}cout<<ans<<endl;
}
int main()
{cin>>n;for(int i=0;i<n;i++){cin>>a[i];}// sort(a,a+n);
    solve();return 0;
}

转载于:https://www.cnblogs.com/caijiaming/p/9417819.html

Fence Repair (二叉树求解)(优先队列,先取出小的)相关推荐

  1. Fence Repair(不会优先队列的看过来)

    Fence Repair(不会优先队列的看过来) Farmer John wants to repair a small length of the fence around the pasture. ...

  2. P1090 合并果子 / [USACO06NOV] Fence Repair G(贪心+优先队列) 洛谷

    题目描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和.可 ...

  3. POJ3253 Fence Repair【哈夫曼树+优先队列】

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 48026   Accepted: 15784 De ...

  4. Fence Repair(优先队列)

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

  5. SDUT 2423 Fence Repair(优先队列)

    Fence Repair Time Limit: 2000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description Farmer ...

  6. POJ 3253 -- Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 55661   Accepted: 18331 De ...

  7. bzoj1724【Usaco2006 Nov】Fence Repair 切割木板

    1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 920  Solved: 4 ...

  8. [BZOJ1724][Usaco2006 Nov]Fence Repair 切割木板

    1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1272  Solved: ...

  9. 老BOJ 07 Fence Repair

    Fence Repair Accept:199     Submit:784 Time Limit:1000MS     Memory Limit:65536KB Description Farmer ...

最新文章

  1. 阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:7. 服务调用
  2. auxprop mysql_Postfix 反垃圾过滤
  3. 《应用时间序列分析:R软件陪同》——导读
  4. [推荐]大量 Blazor 学习资源(三)
  5. c语言蛮力法实现背包问题
  6. ESD-PCIe 402-1 CAN卡驱动安装及测试
  7. centos7 thinkpad trackpoint 中键滚动 关闭粘贴 配置
  8. 【实战】Udacity弹窗测试—ABtest
  9. (二十三)用几何布朗运动模拟股价走势
  10. [蓝桥杯单片机 八位共阳数码管](4)
  11. 关于Allele(等位基因)的理解
  12. 【公告】淘宝 npm 域名即将切换 npmmirror 重构升级
  13. js 匹配正确手机号
  14. 【基金学习】小白基金学习记录(一)
  15. 做游戏与web的区别 - 服务器篇【1】
  16. C++之路进阶——codevs3566(紧急疏散)
  17. Unity 3D 面试 数据结构与算法简述
  18. 蚂蚁金服若IPO 信贷业务或将得到长远发展
  19. 成为职业游戏建模师该如何学习?
  20. 5.2 前端开发日报

热门文章

  1. Flex RIA的ArcIMS WebGIS之路(一)--胸中的那棵竹
  2. 2021年双十一大复盘:众人唱衰双十一,我们却发现了这些机会
  3. 【免费下载】2021年8月热门报告盘点(附热门报告列表及下载链接)
  4. 原生xgboost与sklearn里的xgboost
  5. 操作系统中的互斥锁与条件变量
  6. 国际顶会加持,算法大赛等你共探技术新可能
  7. MoSE: 多任务混合序列专家模型
  8. python支付宝自动支付_python-支付宝支付示例
  9. 机载激光雷达测量技术及工程应用实践_倾斜摄影与激光雷达技术在实景三维测量应用中的比较...
  10. 挑战Tiktok,跨境卖家的新红利流量渠道?