农夫约翰为了修理栅栏,要将一块很长的木板切割成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)的更多相关文章

  1. Fence Repair POJ - 3253 (贪心)

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

  2. R - Fence Repair POJ - 3253

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

  3. 贪心算法——Fence Repair(POJ 3253)

    题目描述 农夫约翰为了修理栅栏,要将一块很长的木板切割成N块.准备切成的木板长度为L1,L2,L3--LN,未切割前木板的长度恰好为切割后木板长度的总和.每次切断木板时,需要的开销为这块木板的长度.请 ...

  4. Fence Repair POJ - 3253 哈夫曼思想 优先队列

    题意:给出一段无限长的棍子,切一刀需要的代价是棍子的总长,例如21切一刀 变成什么长度 都是代价21 列如7切成5 和2 也是代价7题解:可以利用霍夫曼编码的思想 短的棍子就放在底层 长的尽量切少一次 ...

  5. POJ 3253 Fence Repair(修篱笆)

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

  6. POJ 3253 Fence Repair (贪心)

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

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

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

  8. poj 3253 Fence Repair 优先队列

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

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

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

随机推荐

  1. java实现的23种设计模式 (个人推荐)

    http://zz563143188.iteye.com/blog/1847029 mark下,个人用,大家会也可以看看写的不错.

  2. Hive 7、Hive 的内表、外表、分区(22)

    Hive 7.Hive 的内表.外表.分区   1.Hive的内表 Hive 的内表,就是正常创建的表,在 http://www.cnblogs.com/raphael5200/p/5208437.h ...

  3. 关于url路径的定义方式

    一.概述 无论是做网页,还是WEB系统,我们都会用到链接,图片,文件的地方,这些地方都涉及到路径的问题,例如:background-image:url();这一CSS样式,而url()的定义方式有两种 ...

  4. 《Linux Device Drivers》第十二章 PCI司机——note

    一个简短的引论 它给这一章总线架构的高级概述 集中访问讨论Peripheral Component Interconnect(PCI,外围组件互连)外设内核函数 PCI公交车是最好的支持的内核总线 本 ...

  5. POJ 2455Secret Milking Machine(二分+网络流之最大流)

    题目地址:POJ2455 手残真浪费时间啊..又拖到了今天才找出了错误..每晚两道题不知不觉又变回了每晚一道题...sad.. 第一次在isap中忘记调用bfs,第二次则是遍历的时候竟然是从1開始遍历 ...

  6. mybatis之mapper.xml分析

    select: id:方法名,在同一个mapper.xml中,要保持唯一 parameterType:指定输入的参数类型,不是必须的,如果不指定,mybatis会自动识别(推荐指定). resultT ...

  7. C#。4.1数组的应用

    数组的应用 (一).冒泡排序.1.冒泡排序是用双层循环解决.外层循环的是趟数,里层循环的是次数.2.趟数=n-1:次数=n-趟数.3.里层循环使用if比较相临的两个数的大小,进行数值交换. 代码 in ...

  8. Chrome Browser

    set default search engine as follow for force encrypted searching: https://encrypted.google.com/sear ...

  9. 自定义悬浮按钮:FloatingButton

    floating_button_layout.xml <?xml version="1.0" encoding="utf-8"?> <Rela ...

  10. ListView自定义滑动条

    /** * 修改默认滑动条 */ private void SetSliderIcon() { try { Field f = AbsListView.class.getDeclaredField(& ...