Fence Repair (二叉树求解)(优先队列,先取出小的)
题目链接:http://poj.org/problem?id=3253
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
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<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 (二叉树求解)(优先队列,先取出小的)相关推荐
- Fence Repair(不会优先队列的看过来)
Fence Repair(不会优先队列的看过来) Farmer John wants to repair a small length of the fence around the pasture. ...
- P1090 合并果子 / [USACO06NOV] Fence Repair G(贪心+优先队列) 洛谷
题目描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和.可 ...
- POJ3253 Fence Repair【哈夫曼树+优先队列】
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 48026 Accepted: 15784 De ...
- Fence Repair(优先队列)
Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- SDUT 2423 Fence Repair(优先队列)
Fence Repair Time Limit: 2000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description Farmer ...
- POJ 3253 -- Fence Repair
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 55661 Accepted: 18331 De ...
- bzoj1724【Usaco2006 Nov】Fence Repair 切割木板
1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 920 Solved: 4 ...
- [BZOJ1724][Usaco2006 Nov]Fence Repair 切割木板
1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 1272 Solved: ...
- 老BOJ 07 Fence Repair
Fence Repair Accept:199 Submit:784 Time Limit:1000MS Memory Limit:65536KB Description Farmer ...
最新文章
- 阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:7. 服务调用
- auxprop mysql_Postfix 反垃圾过滤
- 《应用时间序列分析:R软件陪同》——导读
- [推荐]大量 Blazor 学习资源(三)
- c语言蛮力法实现背包问题
- ESD-PCIe 402-1 CAN卡驱动安装及测试
- centos7 thinkpad trackpoint 中键滚动 关闭粘贴 配置
- 【实战】Udacity弹窗测试—ABtest
- (二十三)用几何布朗运动模拟股价走势
- [蓝桥杯单片机 八位共阳数码管](4)
- 关于Allele(等位基因)的理解
- 【公告】淘宝 npm 域名即将切换 npmmirror 重构升级
- js 匹配正确手机号
- 【基金学习】小白基金学习记录(一)
- 做游戏与web的区别 - 服务器篇【1】
- C++之路进阶——codevs3566(紧急疏散)
- Unity 3D 面试 数据结构与算法简述
- 蚂蚁金服若IPO 信贷业务或将得到长远发展
- 成为职业游戏建模师该如何学习?
- 5.2 前端开发日报
热门文章
- Flex RIA的ArcIMS WebGIS之路(一)--胸中的那棵竹
- 2021年双十一大复盘:众人唱衰双十一,我们却发现了这些机会
- 【免费下载】2021年8月热门报告盘点(附热门报告列表及下载链接)
- 原生xgboost与sklearn里的xgboost
- 操作系统中的互斥锁与条件变量
- 国际顶会加持,算法大赛等你共探技术新可能
- MoSE: 多任务混合序列专家模型
- python支付宝自动支付_python-支付宝支付示例
- 机载激光雷达测量技术及工程应用实践_倾斜摄影与激光雷达技术在实景三维测量应用中的比较...
- 挑战Tiktok,跨境卖家的新红利流量渠道?