51nod 1117 聪明的木匠 (贪心)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1117
跟挑战程序书上例题一样,将要切割的n断木板,分别对应二叉树树的叶子节点,则切割的总开销为木板的长度×节点的深度,可以画图理解,那么最短的木板(L1)应当是深度最大的叶子节点之一,次短的板(L2)和它是兄弟节点,由一块长度是(L1+L2) 的木板切割而来,这样可以变成(n-1)块木板,然后递归求解到n==1。
书上贪心部分用的是O(n×n) 的算法 在这里会超时,需要2.4节优先队列O(NlogN)的算法。
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <vector>
- #include <cstring>
- #include <string>
- #include <algorithm>
- #include <string>
- #include <set>
- #include <functional>
- #include <numeric>
- #include <sstream>
- #include <stack>
- #include <map>
- #include <queue>
- #define CL(arr, val) memset(arr, val, sizeof(arr))
- #define ll long long
- #define inf 0x7f7f7f7f
- #define lc l,m,rt<<1
- #define rc m + 1,r,rt<<1|1
- #define pi acos(-1.0)
- #define L(x) (x) << 1
- #define R(x) (x) << 1 | 1
- #define MID(l, r) (l + r) >> 1
- #define Min(x, y) (x) < (y) ? (x) : (y)
- #define Max(x, y) (x) < (y) ? (y) : (x)
- #define E(x) (1 << (x))
- #define iabs(x) (x) < 0 ? -(x) : (x)
- #define OUT(x) printf("%I64d\n", x)
- #define lowbit(x) (x)&(-x)
- #define Read() freopen("a.txt", "r", stdin)
- #define Write() freopen("b.txt", "w", stdout);
- #define maxn 1000000000
- #define N 50005
- using namespace std;
- int L[N];
- int main()
- {
- // Read();
- int n;
- long long ans=;
- scanf("%d",&n);
- for(int i=;i<n;i++) scanf("%d",&L[i]);
- while(n>)
- {
- int m1=,m2=;
- if(L[m1]>L[m2]) swap(m1,m2);
- for(int i=;i<n;i++)
- {
- if(L[m1]>L[i])
- {
- m2=m1;
- m1=i;
- }
- else if(L[m2]>L[i])
- {
- m2=i;
- }
- }
- //printf("%d %d\n",L[m1],L[m2]);
- int t=L[m1]+L[m2];
- ans+=t;
- if(m1==n-) swap(m1,m2);
- L[m1]=t;
- L[m2]=L[n-];
- n--;
- }
- printf("%lld\n",ans);
- return ;
- }
优化后的代码:每次只需要选择长度最小的两块木板,则用优先队列从小到大排序,每次把取出来的两块木板之和压进队列即可。
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <vector>
- #include <cstring>
- #include <string>
- #include <algorithm>
- #include <string>
- #include <set>
- #include <functional>
- #include <numeric>
- #include <sstream>
- #include <stack>
- //#include <map>
- #include <queue>
- #define CL(arr, val) memset(arr, val, sizeof(arr))
- #define ll long long
- #define inf 0x7f7f7f7f
- #define lc l,m,rt<<1
- #define rc m + 1,r,rt<<1|1
- #define pi acos(-1.0)
- #define L(x) (x) << 1
- #define R(x) (x) << 1 | 1
- #define MID(l, r) (l + r) >> 1
- #define Min(x, y) (x) < (y) ? (x) : (y)
- #define Max(x, y) (x) < (y) ? (y) : (x)
- #define E(x) (1 << (x))
- #define iabs(x) (x) < 0 ? -(x) : (x)
- #define OUT(x) printf("%I64d\n", x)
- #define lowbit(x) (x)&(-x)
- #define Read() freopen("a.txt", "r", stdin)
- #define Write() freopen("b.txt", "w", stdout);
- #define maxn 1000000000
- #define N 50005
- using namespace std;
- int L[N];
- priority_queue<int, vector<int>, greater<int> >que;
- int main()
- {
- //Read();
- int n;
- scanf("%d",&n);
- for(int i=;i<n;i++)
- {
- scanf("%d",&L[i]);
- que.push(L[i]);
- }
- ll ans=;
- while(que.size()>)
- {
- int l1,l2;
- l1=que.top();
- que.pop();
- l2=que.top();
- que.pop();
- ans+=l1+l2;
- que.push(l1+l2);
- }
- printf("%lld\n",ans);
- return ;
- }
51nod 1117 聪明的木匠 (贪心)的更多相关文章
- 51NOD 1117 聪明的木匠
来源:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1117 挑战原题吧 大概 每次挑选最小的两个,合起来 #inclu ...
- 51nod 1117 聪明的木匠 (哈夫曼树)
题目:传送门. 题意:中文题. 题解:就是构造一颗哈夫曼树,数据结构里的知识. #include <iostream> #include <cstdio> #include & ...
- POJ 3253 Fence Repair C++ STL multiset 可解 (同51nod 1117 聪明的木匠)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53106 Accepted: 17508 De ...
- 51nod 1117 贪心
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1117 1117 聪明的木匠 题目来源: 河北大学算法艺术协会 基准时间限 ...
- 51nod1117 聪明的木匠【贪心+优先队列】
一位老木匠需要将一根长的木棒切成N段.每段的长度分别为L1,L2,......,LN(1 <= L1,L2,-,LN <= 1000,且均为整数)个长度单位.我们认为切割时仅在整数点处切且 ...
- 51nod 1163 最高的奖励(贪心+优先队列)
题目链接:51nod 1163 最高的奖励 看着这题我立马就想到昨天也做了一道贪心加优先队列的题了奥. 按任务最晚结束时间从小到大排序,依次选择任务,如果该任务最晚结束时间比当前时间点晚,则将该任务的 ...
- 51Nod 1091 线段的重叠(贪心+区间相关,板子题)
1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...
- 51nod 1672 区间交(贪心)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1672 题意: 思路:其实这就是一个经典的区间贪心问题,只需要按照左端点排 ...
- 51nod 1351 吃点心(贪心)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1351 题意: 思路: 要么先选low值大的,要么先选high值大的,分两 ...
随机推荐
- 【BZOJ】【4011】【HNOI2015】落忆枫音
拓扑排序+DP 题解:http://blog.csdn.net/PoPoQQQ/article/details/45194103 http://www.cnblogs.com/mmlz/p/44487 ...
- java中的匿名内部类
匿名内部类在java编码中不是很常见,它可一让类实现多继承的特性(多个父类~1个子类) java中的匿名内部类总结http://www.cnblogs.com/nerxious/archive/201 ...
- Nodejs Express 4.X 中文API 2--- Request篇
相关阅读: Express 4.X API 翻译[一] -- Application篇 Express4.XApi 翻译[二] -- Request篇 Express4.XApi 翻译[三] -- ...
- [Js/Jquery]天气接口简单使用
写在前面 今天在群里有朋友使用一个天气api,觉得挺实用的,就记录一下.省的以后再花费功夫去找. 地址:http://www.k780.com/api,在这个网站提供了实用的几种接口,比如查询ip,天 ...
- css 之优先策略
<html> <head> <title>testCSS</title> <style type="text/css"> ...
- Ruby中的语句中断和返回
李哲 - APRIL 28, 2015 return,break,next 这几个关键字的使用都涉及到跳出作用域的问题,而他们的不同 则在于不同的关键字跳出去的目的作用域的不同,因为有代码块则导致有一 ...
- css display visibility
当visibility被设置为"hidden"的时候,元素虽然被隐藏了,但它仍然占据它原来所在的位置.注意,当元素被隐藏之后,就不能再接收到其它事件了. display属性就有一点 ...
- SharePoint Server 2007 简体中文下载
SharePoint Server 2007 简体中文下载 2010-12-16 10:56 正式版key SN: Tkjcb-3wkhk-2ty2t-qymk2-9xm2y 这个版本也是通过Key来 ...
- Android安卓开发环境搭建详细教程
安装目录:步骤1 安装JDK步骤2 安装 Android SDK ----http://www.androiddevtools.cn/ 步骤3 安装Tomcat步骤4 安装Ant步骤5 安装Eclip ...
- hdu 3537 Daizhenyang's Coin (翻硬币游戏)
#include<stdio.h> #include<algorithm> #include<string.h> using namespace std; ]; i ...