Greedy:Fence Repair(POJ 3252)

问题大意:农夫约翰为了修理栅栏,要将一块很长的木块切割成N块,准备切成的木板的长度为L1,L2...LN,未切割前的木板的长度恰好为切割后木板的长度的总和,每次切断木板的时候,需要的开销为这块木板的长度,例如长度为21的木板需要切成长度为5,8,8的三块木板,长为21的木板切成长为13和8的板时,开销为21,。再将长度为13的板切成长度为5和8的板时,开销是13,于是合计开销是34,然后要你求切割完的最小开销是什么。
乍眼看这一道题好像有很多种切割方案,似乎是很难解决的,但是我们这样想,就是当我们切一块木板的时候,我们所用的开销就是切割这一块木板所要用到的长度,我们得到了两块木板,这两块木板可以继续切的话,他们的总开销就是这两块木板的和,也就是一块长的木板。那么我们想到最后,就会发现其实就是我们得到了一个这样的东西,就是最小的板会所要用到的开销会占N次(视通过多少次分割得到这块板来定),如果我们要画一幅图来展示这个过程的话。。。

上面最大的开销是34=8*2+5*2+8(也就是二叉树的树叶对应节点大小乘以高度)
那么为了得到最小的开销,我们很容易就发现,我们只要把相对较小的节点放在比较深的地方就可以了,这是一种比较常用的贪婪的思想,证明我就省略了
那么现在我们只用不断地把最小的节点结合起来,最后就会形成最小的开销,那么不断找最小嘛,很容易就想到用堆,堆呢你可以自己写(熟练的话会很快),也可以直接用std(会比较简洁)
#include<stdio.h>
#include<stdlib.h> typedef int Position, *Heap; int Delete_Min(Heap, int *const);
void Push(Heap, int, int *const); int main(void)
{
int N, i, size = ;
int L1, L2, tmp;
long long ans = ; while (~scanf("%d",&N))
{
Heap pque = (Heap)malloc(sizeof(int)*N);
for (i = ; i < N; i++)
{
scanf("%d", &tmp);
Push(pque, tmp, &size);
}
while (size > )
{
L1 = Delete_Min(pque, &size);
L2 = Delete_Min(pque, &size); ans += (long long)L1 + L2;
Push(pque, L1 + L2, &size);
}
free(pque);
printf("%lld\n", ans);
}
} int Delete_Min(Heap heap,int *const size)
{
Position pr = , s1, s2, s;
int out = heap[], tmp = heap[(*size)--];
for (; pr <= *size;)
{
s1 = pr << ; s2 = s1 + ;
if (s2 <= *size)
{
if (heap[s1] < heap[s2]){ heap[pr] = heap[s1]; pr = s1; }
else{ heap[pr] = heap[s2]; pr = s2; }
}
else
{
if (s1 <= *size){ heap[pr] = heap[s1]; pr = s1;break; }
else break;
}
}
for (s = pr; s > ;)
{
if (s % == )
{
pr = s >> ;
if (heap[pr] > tmp) { heap[s] = heap[pr];s = pr;}
else break;
}
else
{
pr = (s - ) >> ;
if (heap[pr] > tmp){ heap[s] = heap[pr];s = pr; }
else break;
}
}
heap[s] = tmp;
return out;
} void Push(Heap heap, int item, int *const size)
{
Position s = ++(*size), pr;
for (; s > ;)
{
if (s % == )
{
pr = s >> ;
if (heap[pr] > item) { heap[s] = heap[pr]; s = pr; }
else break;
}
else
{
pr = (s - ) >> ;
if (heap[pr] > item){ heap[s] = heap[pr]; s = pr; }
else break;
}
}
heap[s] = item;
}
这是直接写堆的代码
#include<iostream>
#include<queue>
#include<functional>
#include<cstdio> using namespace std; int main(void)
{
int N, i;
int L1, L2, tmp;
long long ans = ; while (~scanf("%d", &N))
{
priority_queue<int, vector<int>, greater<int> >pque;
for (i = ; i < N; i++)
{
scanf("%d", &tmp);
pque.push(tmp);
}
while (pque.size() > )
{
L1 = pque.top();
pque.pop();
L2 = pque.top();
pque.pop(); ans += (long long)L1 + L2;
pque.push(L1 + L2);
};
printf("%lld\n", ans);
}
}
这是用的std,事实上呢用std是挺方便的,虽然稍微损失了一下性能,我直接用堆是472K,32ms,用std是920k,47ms,相差不大
另外这个思想还可以用来做著名的Huffman编码,其实Huffman以前写了一个,以后会贴出来看看
Greedy:Fence Repair(POJ 3252)的更多相关文章
- Fence Repair(poj 3253)
题意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个要求的小木板的长度,及小木板的个数n,求最小费用 提示: 以 3 5 8 5 ...
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- poj 3253:Fence Repair(堆排序应用)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23913 Accepted: 7595 Des ...
- POJ 30253 Fence Repair (二叉树+优先队列)
题目链接 Description Farmer John wants to repair a small length of the fence around the pasture. He meas ...
- Fence Repair (二叉树求解)(优先队列,先取出小的)
题目链接:http://poj.org/problem?id=3253 Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- poj 3253 Fence Repair (STL优先队列)
版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/34805369 转载请注明出 ...
- Round Numbers(poj 3252)
题意:算出区间内二进制中0的个数大于等于1的个数的数字有多少个 /* 本来以为用数位DP搞,但是组合数更简单. 我们设n的二进制长度为len. ①:先考虑长度小于len的数字. 这里以数字22为例,二 ...
- poj-3253 fence repair(贪心题)
题目描述: Farmer John wants to repair a small length of the fence around the pasture. He measures the fe ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
随机推荐
- 【kAri OJ】621. 廖神的树
时间限制 3000 ms 内存限制 65536 KB 题目描述 廖神和男神在植树节的时候准备玩一个奇怪的游戏,他们现在有一个被分割成n*n个格子的矩形土地,他们现在准备往这个地里种树,但这个种树游戏必 ...
- RPD资料库创建(1)
BI创建(数据)分析.仪表盘.报表前,都需要对数据进行建模,在oracle biee里称为创建“资料档案库”-该文件后缀为RPD,所以一般也称为创建RPD文件. 步骤: 1.从windows开始菜单里 ...
- Oracle创建主外键
-创建表格语法: create table 表名( 字段名1 字段类型(长度) 是否为空, 字段名2 字段类型 是否为空); -增加主键 alt ...
- opencv笔记6:角点检测
time:2015年10月09日 星期五 23时11分58秒 # opencv笔记6:角点检测 update:从角点检测,学习图像的特征,这是后续图像跟踪.图像匹配的基础. 角点检测是什么鬼?前面一篇 ...
- TCP/IP详解 学习三
网际协议 ip Ip 是不可靠和无连接的 ip首部 4个字节的 32 bit值以下面的次序传输:首先是 0-7 bit,其次 8-15 bit,然后 1 6-23 bit,最后是 24~31 bit. ...
- 为什么mvc里面的ModelState.IsValid一只都是true
http://zhidao.baidu.com/link?url=H69JQBpF8vbJEOUUc1RCjRZZ05gSGn6PiPL740aGgR3qIfFTT__pt4KgEg7O47lReYR ...
- 代码注册广播接收者&利用广播调用服务的方法服务声命周期(混合开启)
1)说明文档: 2)效果演示: 3)代码演示:
- Spring+Struts2+Mybatis框架搭建时的常见典型问题
搭建SSM框架时,总是遇到这样那样的问题,有的一眼就能看出来,有的需要经验的积累.现将自己搭建SSM框架时遇到的典型问题总结如下: 一.Struts2框架下的action中无法使用@Autowired ...
- ActionBar右边菜单按钮的添加
在res目录下新建文件夹menu,存放men.xml文件 menu.xml <menu xmlns:android="http://schemas.android.com/apk/re ...
- Memcache和Redis