农夫约翰为了修理栅栏,要将一块很长的木板切割成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. javaweb笔记之get和post的不同

    1 GET方式 1)提交的参数数据会放在请求信息的URL后面.以?开头,多个参数数据以 & 分 割. 2)浏览器的地址会发生改变 3)参数数据的容量不能超过1KB. 4)不适合敏感数据的提交 ...

  2. Linux 常用命令记录

    1.查看磁盘空间使用情况 df -[a i m] 或更多 df -lh 2.查看目录文件占用大小 du -sh * du --max-depth=1 -lh 3.内存使用qingkuang free ...

  3. 安装 Android 运行环境

    如果你恰好有一些旧的. 过时的 Android SDK 版本,请务必把所需的包更新至下面提到的版本并安装所有缺少的部分. 安装和配置 SDK 安装最新的 JDK. 使用 brew install an ...

  4. hdu 1253 胜利大逃亡 (三维简单bfs+剪枝)

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  5. 最新的四款国外VPN,免费稳定,可以看国外网站的

    tyle="margin-top:20px; margin-right:0px; margin-bottom:0px; margin-left:0px; font-family:Arial; ...

  6. CentOS6.7 常用操作命令

    centos 安装py环境 1.安装wget工具: yum install wget 2.安装Python-2.7.8: wget --no-check-certificate https://www ...

  7. ssh免密码登陆远程服务器

    ssh免密码登陆远程服务器 在使用windows下的cygwin或者在linux下使用Terminal进行远程服务器登陆测试的时候总是会要求输入账号密码,对于此我们可以使用ssh将公钥放在服务器上的方 ...

  8. AngularJs练习Demo15自定义服务

    @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...

  9. mac版sublime text2包管理器安装步骤

    第一步: control+-打开命令执行窗口. 第二步: 将包管理器的代码复制到命令执行窗口: import urllib2,os,hashlib; h = '2915d1851351e5ee549c ...

  10. php之类,对象(二)继承性,static静态的,const常量

    三大特性 之二 继承性: 1.概念:如果一个类有子类,那么该子类会继承父类的一切东西,但私有成员访问不到. 2.在定义子类时需要加关键字:extends class Text extends Info ...