Fence Repair (POJ 3253)
农夫约翰为了修理栅栏,要将一块很长的木板切割成N块。准备切成的木板长度为L1、L2、L3、、、LN,未切割前的木板长度恰好为切割后木板长度的总和。每次切断木板时,需要的开销为这块木板的长度。例如长度为21的木板要切成长度为5,8,8的三块木板。长度为21的木板切成长度为13和8的板时,开销为21。再将长度为5和8的板时,开销为13。于是合计开销是34。请求按照目标要求将木板切割完最小的开销是多少。
#include "iostream"
using namespace std; typedef long long ll;
const int MAX_N = 20000;
int N = 3, L[MAX_N] = {8,5,8}; void solve() {
ll ans = 0;
//直到计算到木板为1块时为止
while (N>1)
{
//求出最短的木板mii1和次短的木板mii2
int mii1 = 0, mii2 = 1;
if (L[mii1] > L[mii2]) swap(mii1,mii2);
//找出最短板与次短板的坐标
for (int i = 2; i < N;i++) {
if (L[i]<L[mii1])
{
mii2 = mii1;
mii1 = i;
}
else if (L[i]<L[mii2]) {
mii2 = i;
}
}
//将两块板拼合
int t = L[mii1] + L[mii2];
ans += t;
/*
最短板恰好为最后一个板,则交换最短板与次短板坐标,目的让
合成板放在较前位置覆盖掉,次短板交换最后一个板子,并且N-1
如果次短板是最后一个板子则直接N-1被撇除。
*/
if (mii1 == N - 1) swap(mii1,mii2);
L[mii1] = t;
L[mii2] = L[N-1];
N--;
}
cout << ans << endl;
}
int main() {
solve();
system("pause");
return 0;
}
Fence Repair (POJ 3253)的更多相关文章
- Fence Repair POJ - 3253 (贪心)
Farmer John wants to repair a small length of the fence around the pasture. He measures the fence an ...
- R - Fence Repair POJ - 3253
Farmer John wants to repair a small length of the fence around the pasture. He measures the fence an ...
- 贪心算法——Fence Repair(POJ 3253)
题目描述 农夫约翰为了修理栅栏,要将一块很长的木板切割成N块.准备切成的木板长度为L1,L2,L3--LN,未切割前木板的长度恰好为切割后木板长度的总和.每次切断木板时,需要的开销为这块木板的长度.请 ...
- Fence Repair POJ - 3253 哈夫曼思想 优先队列
题意:给出一段无限长的棍子,切一刀需要的代价是棍子的总长,例如21切一刀 变成什么长度 都是代价21 列如7切成5 和2 也是代价7题解:可以利用霍夫曼编码的思想 短的棍子就放在底层 长的尽量切少一次 ...
- 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:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
- poj 3253 Fence Repair 优先队列
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- POJ 3253 Fence Repair 贪心 优先级队列
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 77001 Accepted: 25185 De ...
随机推荐
- Java随机数生产算法
java提供了Math.random()函数,返回一个double类型的随机数,也有util包里的Random类,可以生成double,int,float,long,bytes等随机数. 但有些业务需 ...
- hibernate generator class="" id详解
“assigned” 主键由外部程序负责生成,在 save() 之前指定一个. “hilo” 通过hi/lo 算法实现的主键生成机制,需要额外的数据库表或字段提供高 ...
- [React] React Router: Nested Routes
Since react-router routes are components, creating nested routes is as simple as making one route a ...
- c++11 : Variadic Macros(变长参宏)
Variadic macros are function-like macros that contain a variable number of arguments. Remarks To u ...
- 内容提供者 DocumentProvider Uri工具类
Activity /**详见http://blog.csdn.net/coder_pig/article/details/47905881 Calendar Provider:日历提供者,就是针对针对 ...
- Menu( 菜单)
一. 加载方式菜单组件通常用于快捷菜单,在加载方式上,通过 class 或 JS 进行设置为菜单组件.然后,再通过 JS 事件部分再响应.//class 加载方式<div id="bo ...
- 关于html页面图片自动撑开的问题
如下列代码: <div id="divnr" style="text-align: center; margin: 10px; width:600px;" ...
- C++ 知识点 2
基本类型常量 const int a; int const a; const int *a; int * const a; int const * a const; 之间的区别? const int ...
- XML预定义的实体
在TSQL读取XML中一些特定的字符时出错,在XML中有些自付要用实体: 下面是五个在XML文档中预定义好的实体: < < 小于号 > > 大于号 & & 和 ...
- Linux修改时间时区并在Tomcat中生效
Linux查看当前时间时区linux:~ # datelinux:~ # date –Rlinux:~ # zdump -v /usr/share/zoneinfo/Asia/Beijing ---- ...