题意:将一块木板切成N块,长度分别为:a1,a2,……an,每次切割木板的开销为当前木板的长度。求出按照要求将木板切割完毕后的最小开销。

思路:比较奇特的贪心

每次切割都会将当前木板一分为二,可以按切割要求画出二叉树。

总开销 = 各叶子节点的值 x 该叶子节点的深度

树的深度一定,为了使总开销尽可能的小,那么比较深的叶子节点的值应尽可能的小,所以二叉树应为:

模拟出所建立的二叉树,求出根节点的值即可

为了节省时间,可以利用优先队列每次取出最小的两个值合并,并将两数之和重新加入数组,直到数组内的元素个数为一时结束,时间复杂度为O(nlogn)。

#include<stdio.h>
#include<queue>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#define INF 0x3f3f3f3f
#define LL long long
#define MOD 100000007
#define MAXSIZE 20005 using namespace std; int n,a[MAXSIZE]; LL Solve()
{
LL ans = ;
priority_queue<int,vector<int>,greater<int> >Q;
for(int i=;i<=n;i++)
Q.push(a[i]);
while(n > )
{
int minn1 = Q.top();
Q.pop();
int minn2 = Q.top();
Q.pop();
ans += (minn1 + minn2);
Q.push(minn1 + minn2);
n--;
}
return ans;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%d",&a[i]); LL ans = Solve();
printf("%lld\n",ans);
}
return ;
}

POJ 3253 Fence Repair (贪心)的更多相关文章

  1. POJ 3253 Fence Repair (贪心)

    Fence Repair Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  2. POJ 3253 Fence Repair 贪心 优先级队列

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 77001   Accepted: 25185 De ...

  3. poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...

  4. POJ 3253 Fence Repair 贪心+优先队列

    题意:农夫要将板割成n块,长度分别为L1,L2,...Ln.每次切断木板的花费为这块板的长度,问最小花费.21 分为 5 8 8三部分.   思路:思考将n部分进行n-1次两两合成最终合成L长度和题目 ...

  5. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

  6. POJ 3253 Fence Repair (优先队列)

    POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...

  7. poj 3253 Fence Repair 优先队列

    poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...

  8. POJ - 3253 Fence Repair 优先队列+贪心

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

  9. POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 53645   Accepted: 17670 De ...

  10. poj 3253 Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 42979   Accepted: 13999 De ...

随机推荐

  1. python之路入门篇

    一. Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,Guido开始写能够解释Python语言语法的解释器.Python这个名字,来 ...

  2. Gym - 101755G Underpalindromity (树状数组)

    Let us call underpalindromity of array b of length k the minimal number of times one need to increme ...

  3. POJ 1815 Friendship (Dinic)

    Friendship Time Limit: 2000MS   Memory Limit: 20000K Total Submissions: 11429   Accepted: 3173 Descr ...

  4. ASCLL、Unicode和UTF-8编码的理解

    我们已经讲过了,字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit) ...

  5. loopback(回环)

    Loopback接口是一个虚拟网络接口,在不同的领域,其含义也大不一样. 1.  TCP/IP协议栈中的loopback接口 在TCP/IP中回环设备是一个通过软件实现的虚拟网络接口,它不与任何硬件相 ...

  6. PHP下安装memcached

    tar xvf libevent-2.0.21-stable.tar.gzcd libevent./configure -prefix=/usr/local/libeventmake &&am ...

  7. OpenCV编译以及QT Creator配置

    OpenCV编译以及QT Creator配置 在进行编译前,需下载以下工具和源码: CMake ---- 用于编译: 下载地址; https://cmake.org/ 安装在D:\Program Fi ...

  8. 使用Redis模拟简单分布式锁,解决单点故障的问题

    需求描述: 最近做一个项目,项目中有一个功能,每天定时(凌晨1点)从数据库中获取需要爬虫的URL,并发送到对应的队列中,然后客户端监听对应的队列,然后执行任务.如果同时部署多个定时任务节点的话,每个节 ...

  9. 3D转换(位置)+过渡+透视

    效果如图: html代码: <div class="door"> <div class="in"><div> </di ...

  10. HDU 1015(字符运算 **)

    题意是在一段大写英文字符中找出 5 个字符通过代入公式运算得到目标值,如有多组字符满足题意,则输出字典序最大的一组,否则输出 no solution. 应该是用深搜做的,但是直接暴力也过了……(应该没 ...