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 聪明的木匠 (贪心)的更多相关文章

  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. vasprintf的实现

    有些系统,例如AIX,没有vasprintf和asprintf ,如果是做porting,有时需要写一个简单的代替. 代码如下: #if !defined(HAVE_VASPRINTF) #if de ...

  2. 【POJ】【1635】Subway Tree Systems

    树的最小表示法 给定两个有根树的dfs序,问这两棵树是否同构 题解:http://blog.sina.com.cn/s/blog_a4c6b95201017tlz.html 题目要求判断两棵树是否是同 ...

  3. tcp 多线程与多进程调用close

    http://blog.csdn.net/russell_tao/article/details/13092727 大家知道,所谓线程其实就是“轻量级”的进程.创建进程只能是一个进程(父进程)创建另一 ...

  4. NGUI无法按住鼠标按住时无法监听OnHover事件

    UICamera.cs 修改前: if ((!isPressed) && highlightChanged) { currentScheme = ControlScheme.Mouse ...

  5. SGU 105

    There is sequence 1, 12, 123, 1234, ..., 12345678910, ... . Given first N elements of that sequence. ...

  6. Android的事件处理机制详解(二)-----基于监听的事件处理机制

    基于监听的事件处理机制 前言: 我们开发的app更多的时候是需要与用户的交互----即对用户的操作进行响应 这就涉及到了android的事件处理机制; android给我们提供了两套功能强大的处理机制 ...

  7. C#修改注册表设置默认浏览器

    项目中用到VPN技术登录来访问内部网络的应用系统,VPN客户端连接后会自动以默认浏览器来打开站点,由于应用系统使用的前端框架对IE浏览器版本要求较高,而用户大多数的电脑里安装的IE的版本都较低,于是想 ...

  8. Microsoft SDK 中Sample案例之Amcap項目 的运行方法(转)

    http://blog.csdn.net/erick08/article/details/7194575 Microsoft  SDK 中Sample之Amcap 的运行方法      写这篇文章的由 ...

  9. 传说中的WCF(7):“单向”&“双向”

    在WCF中,服务器与客户端的通讯有单向(单工)和双向(双工)之分.要说有什么形式上的表现,那就是单向与双向生成的SOAP不同,咱们先放下代码不说.但通常情况下,我们也不太需要去研究生成的SOAP是啥样 ...

  10. poj 1733(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...