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)的算法。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <vector>
  5. #include <cstring>
  6. #include <string>
  7. #include <algorithm>
  8. #include <string>
  9. #include <set>
  10. #include <functional>
  11. #include <numeric>
  12. #include <sstream>
  13. #include <stack>
  14. #include <map>
  15. #include <queue>
  16.  
  17. #define CL(arr, val) memset(arr, val, sizeof(arr))
  18.  
  19. #define ll long long
  20. #define inf 0x7f7f7f7f
  21. #define lc l,m,rt<<1
  22. #define rc m + 1,r,rt<<1|1
  23. #define pi acos(-1.0)
  24.  
  25. #define L(x) (x) << 1
  26. #define R(x) (x) << 1 | 1
  27. #define MID(l, r) (l + r) >> 1
  28. #define Min(x, y) (x) < (y) ? (x) : (y)
  29. #define Max(x, y) (x) < (y) ? (y) : (x)
  30. #define E(x) (1 << (x))
  31. #define iabs(x) (x) < 0 ? -(x) : (x)
  32. #define OUT(x) printf("%I64d\n", x)
  33. #define lowbit(x) (x)&(-x)
  34. #define Read() freopen("a.txt", "r", stdin)
  35. #define Write() freopen("b.txt", "w", stdout);
  36. #define maxn 1000000000
  37. #define N 50005
  38. using namespace std;
  39.  
  40. int L[N];
  41. int main()
  42. {
  43. // Read();
  44. int n;
  45. long long ans=;
  46. scanf("%d",&n);
  47. for(int i=;i<n;i++) scanf("%d",&L[i]);
  48. while(n>)
  49. {
  50. int m1=,m2=;
  51. if(L[m1]>L[m2]) swap(m1,m2);
  52. for(int i=;i<n;i++)
  53. {
  54. if(L[m1]>L[i])
  55. {
  56. m2=m1;
  57. m1=i;
  58. }
  59. else if(L[m2]>L[i])
  60. {
  61. m2=i;
  62. }
  63. }
  64. //printf("%d %d\n",L[m1],L[m2]);
  65. int t=L[m1]+L[m2];
  66. ans+=t;
  67. if(m1==n-) swap(m1,m2);
  68. L[m1]=t;
  69. L[m2]=L[n-];
  70. n--;
  71. }
  72. printf("%lld\n",ans);
  73. return ;
  74. }

优化后的代码:每次只需要选择长度最小的两块木板,则用优先队列从小到大排序,每次把取出来的两块木板之和压进队列即可。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <vector>
  5. #include <cstring>
  6. #include <string>
  7. #include <algorithm>
  8. #include <string>
  9. #include <set>
  10. #include <functional>
  11. #include <numeric>
  12. #include <sstream>
  13. #include <stack>
  14. //#include <map>
  15. #include <queue>
  16.  
  17. #define CL(arr, val) memset(arr, val, sizeof(arr))
  18.  
  19. #define ll long long
  20. #define inf 0x7f7f7f7f
  21. #define lc l,m,rt<<1
  22. #define rc m + 1,r,rt<<1|1
  23. #define pi acos(-1.0)
  24.  
  25. #define L(x) (x) << 1
  26. #define R(x) (x) << 1 | 1
  27. #define MID(l, r) (l + r) >> 1
  28. #define Min(x, y) (x) < (y) ? (x) : (y)
  29. #define Max(x, y) (x) < (y) ? (y) : (x)
  30. #define E(x) (1 << (x))
  31. #define iabs(x) (x) < 0 ? -(x) : (x)
  32. #define OUT(x) printf("%I64d\n", x)
  33. #define lowbit(x) (x)&(-x)
  34. #define Read() freopen("a.txt", "r", stdin)
  35. #define Write() freopen("b.txt", "w", stdout);
  36. #define maxn 1000000000
  37. #define N 50005
  38. using namespace std;
  39.  
  40. int L[N];
  41. priority_queue<int, vector<int>, greater<int> >que;
  42. int main()
  43. {
  44. //Read();
  45. int n;
  46. scanf("%d",&n);
  47. for(int i=;i<n;i++)
  48. {
  49. scanf("%d",&L[i]);
  50. que.push(L[i]);
  51. }
  52. ll ans=;
  53. while(que.size()>)
  54. {
  55. int l1,l2;
  56. l1=que.top();
  57. que.pop();
  58. l2=que.top();
  59. que.pop();
  60. ans+=l1+l2;
  61. que.push(l1+l2);
  62. }
  63. printf("%lld\n",ans);
  64. return ;
  65. }

51nod 1117 聪明的木匠 (贪心)的更多相关文章

  1. 51NOD 1117 聪明的木匠

    来源:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1117 挑战原题吧  大概 每次挑选最小的两个,合起来 #inclu ...

  2. 51nod 1117 聪明的木匠 (哈夫曼树)

    题目:传送门. 题意:中文题. 题解:就是构造一颗哈夫曼树,数据结构里的知识. #include <iostream> #include <cstdio> #include & ...

  3. POJ 3253 Fence Repair C++ STL multiset 可解 (同51nod 1117 聪明的木匠)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 53106   Accepted: 17508 De ...

  4. 51nod 1117 贪心

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1117 1117 聪明的木匠 题目来源: 河北大学算法艺术协会 基准时间限 ...

  5. 51nod1117 聪明的木匠【贪心+优先队列】

    一位老木匠需要将一根长的木棒切成N段.每段的长度分别为L1,L2,......,LN(1 <= L1,L2,-,LN <= 1000,且均为整数)个长度单位.我们认为切割时仅在整数点处切且 ...

  6. 51nod 1163 最高的奖励(贪心+优先队列)

    题目链接:51nod 1163 最高的奖励 看着这题我立马就想到昨天也做了一道贪心加优先队列的题了奥. 按任务最晚结束时间从小到大排序,依次选择任务,如果该任务最晚结束时间比当前时间点晚,则将该任务的 ...

  7. 51Nod 1091 线段的重叠(贪心+区间相关,板子题)

    1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5         难度:1级算法题 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...

  8. 51nod 1672 区间交(贪心)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1672 题意: 思路:其实这就是一个经典的区间贪心问题,只需要按照左端点排 ...

  9. 51nod 1351 吃点心(贪心)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1351 题意: 思路: 要么先选low值大的,要么先选high值大的,分两 ...

随机推荐

  1. 【BZOJ】【4011】【HNOI2015】落忆枫音

    拓扑排序+DP 题解:http://blog.csdn.net/PoPoQQQ/article/details/45194103 http://www.cnblogs.com/mmlz/p/44487 ...

  2. java中的匿名内部类

    匿名内部类在java编码中不是很常见,它可一让类实现多继承的特性(多个父类~1个子类) java中的匿名内部类总结http://www.cnblogs.com/nerxious/archive/201 ...

  3. Nodejs Express 4.X 中文API 2--- Request篇

    相关阅读: Express 4.X API 翻译[一] --  Application篇 Express4.XApi 翻译[二] --  Request篇 Express4.XApi 翻译[三] -- ...

  4. [Js/Jquery]天气接口简单使用

    写在前面 今天在群里有朋友使用一个天气api,觉得挺实用的,就记录一下.省的以后再花费功夫去找. 地址:http://www.k780.com/api,在这个网站提供了实用的几种接口,比如查询ip,天 ...

  5. css 之优先策略

    <html> <head> <title>testCSS</title> <style type="text/css"> ...

  6. Ruby中的语句中断和返回

    李哲 - APRIL 28, 2015 return,break,next 这几个关键字的使用都涉及到跳出作用域的问题,而他们的不同 则在于不同的关键字跳出去的目的作用域的不同,因为有代码块则导致有一 ...

  7. css display visibility

    当visibility被设置为"hidden"的时候,元素虽然被隐藏了,但它仍然占据它原来所在的位置.注意,当元素被隐藏之后,就不能再接收到其它事件了. display属性就有一点 ...

  8. SharePoint Server 2007 简体中文下载

    SharePoint Server 2007 简体中文下载 2010-12-16 10:56 正式版key SN: Tkjcb-3wkhk-2ty2t-qymk2-9xm2y 这个版本也是通过Key来 ...

  9. Android安卓开发环境搭建详细教程

    安装目录:步骤1 安装JDK步骤2 安装 Android SDK ----http://www.androiddevtools.cn/ 步骤3 安装Tomcat步骤4 安装Ant步骤5 安装Eclip ...

  10. hdu 3537 Daizhenyang's Coin (翻硬币游戏)

    #include<stdio.h> #include<algorithm> #include<string.h> using namespace std; ]; i ...