183.Wood Cut【hard】

Given n pieces of wood with length L[i] (integer array). Cut them into small pieces to guarantee you could have equal or more than k pieces with the same length. What is the longest length you can get from the n pieces of wood? Given L & k, return the maximum length of the small pieces.

Notice

You couldn't cut wood into float length.

If you couldn't get >= k pieces, return 0.

Example

For L=[232, 124, 456]k=7, return 114.

Challenge

O(n log Len), where Len is the longest length of the wood.

这个题一上来一点思路没有,参考:https://algorithm.yuanbin.me/zh-hans/binary_search/wood_cut.html里面的思路

这道题要直接想到二分搜素其实不容易,但是看到题中 Challenge 的提示后你大概就能想到往二分搜索上靠了。首先来分析下题意,题目意思是说给出 n 段木材L[i], 将这 n 段木材切分为至少 k 段,这 k 段等长,求能从 n 段原材料中获得的最长单段木材长度。以 k=7 为例,要将 L 中的原材料分为7段,能得到的最大单段长度为114, 232/114 = 2, 124/114 = 1, 456/114 = 4, 2 + 1 + 4 = 7。

理清题意后我们就来想想如何用算法的形式表示出来,显然在计算如214等分片数时我们进行了取整运算,在计算机中则可以使用下式表示:

其中 l 为单段最大长度,显然有 1 ≤ l ≤ max(L[i]). 单段长度最小为1,最大不可能超过给定原材料中的最大木材长度。

Warning 注意求和与取整的顺序,是先求 L[i]/l的单个值,而不是先对L[i]求和。

分析到这里就和题 sqrt(x) 差不多一样了,要求的是 l 的最大可能取值,同时 l 可以看做是从有序序列[1, max(L[i])]的一个元素,典型的二分搜素!

代码参考了:http://www.jiuzhang.com/solution/wood-cut/

解法一:

 public class Solution {
/**
*@param L: Given n pieces of wood with length L[i]
*@param k: An integer
*return: The maximum length of the small pieces.
*/
public int woodCut(int[] L, int k) {
int max = ;
for (int i = ; i < L.length; i++) {
max = Math.max(max, L[i]);
} // find the largest length that can cut more than k pieces of wood.
int start = , end = max;
while (start + < end) {
int mid = start + (end - start) / ;
if (count(L, mid) >= k) {
start = mid;
} else {
end = mid;
}
} if (count(L, end) >= k) {
return end;
}
if (count(L, start) >= k) {
return start;
}
return ;
} private int count(int[] L, int length) {
int sum = ;
for (int i = ; i < L.length; i++) {
sum += L[i] / length;
}
return sum;
}
}

对于上面发现还有可以优化的地方,那就是我们二分找长度的时候只需要找所有木块里面最短的即可,就是所谓的木桶原理,那么end上界又可以进一步减少。

解法二:

 class Solution {
public:
/*
* @param L: Given n pieces of wood with length L[i]
* @param k: An integer
* @return: The maximum length of the small pieces
*/
int woodCut(vector<int> &L, int k) {
if (L.empty() || k <= ) {
return ;
}
//get min
int min = INT_MIN;
for (int i = ; i < L.size(); ++i) {
min = (min < L[i] ? L[i] : min);
} int start = ;
int end = min; while (start + < end) {
int mid = start + (end - start) / ; if (cal(L, mid) >= k) {
start = mid;
}
else {
end = mid;
}
} if (cal(L, end) >= k) {
return end;
}
else if (cal(L, start) >= k) {
return start;
}
else {
return ;
}
} int cal(vector<int> & L, int len) {
int sum = ;
for (int i = ; i < L.size(); ++i) {
sum += L[i] / len;
} return sum;
}
};

183.Wood Cut【hard】的更多相关文章

  1. hdu 6214 : Smallest Minimum Cut 【网络流】

    题目链接 ISAP写法 #include <bits/stdc++.h> using namespace std; typedef long long LL; namespace Fast ...

  2. 【转载】用C#编写一个简单的记事本

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. 【AOP】Spring AOP基础 + 实践 完整记录

    Spring AOP的基础概念 ============================================================= AOP(Aspect-Oriented Pr ...

  4. 【转】linux中的cut/tr/join/split/xargs命令

    1. cut命令 cut命令用于从文件或者标准输入中读取内容并截取每一行的特定部分并送到标准输出. 截取的方式有三种:一是按照字符位置,二是按照字节位置,三是使用一个分隔符将一行分割成多个field, ...

  5. POJ 2914 Minimum Cut【最小割 Stoer-Wangner】

    题意:求全局最小割 不能用网络流求最小割,枚举举汇点要O(n),最短增广路最大流算法求最大流是O(n2m)复杂度,在复杂网络中O(m)=O(n2),算法总复杂度就是O(n5):就算你用其他求最大流的算 ...

  6. P3690 【模板】Link Cut Tree (动态树)

    P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...

  7. LG3690 【模板】Link Cut Tree (动态树)

    题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...

  8. AC日记——【模板】Link Cut Tree 洛谷 P3690

    [模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ...

  9. HDU 6214 Smallest Minimum Cut 【网络流最小割+ 二种方法只能一种有效+hdu 3987原题】

    Problem Description Consider a network G=(V,E) with source s and sink t . An s-t cut is a partition ...

随机推荐

  1. 【二分答案】bzoj1639 [Usaco2007 Mar]Monthly Expense 月度开支

    #include<cstdio> using namespace std; #define N 100001 int n,m,a[N]; bool check(int x) { int n ...

  2. Mysql-库的基本操作

    一 .系统数据库 二 .创建数据库 三 .数据库相关操作 一. 系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信 ...

  3. 6.6(java学习笔记)文件分割(IO综合例子)

    基本思路: 文件分割:将一个文件分割成若干个独立的文件.    设置分割后小文件文件的字节数,然后读取被分割文件, 将对应的字节数写入分割后的小文件中.     使用seek定位下一次读取位置. 文件 ...

  4. [Interview]读懂面试问题,在面试官面前变被动为主动

    面试是供需双方心理的较量,作为求职者来说,了解对方问题的内涵,做到“明明白白他的心”,就能变被动为主动.因此,读懂面试问题,掌握面试考官的提问的目的,有准备.有针对性地回答,对提高应聘的成功率是有很大 ...

  5. Postman Json测试接口

    当传递Json数据时: 1.必须添加http头:content-type:application/json,否则会报错(后台取不到相对应的值) 注意:如果服务端只支持UTF-8,但程序未对提交数据进行 ...

  6. React Native Navigator组件回调

    在push的时候定义回调函数: this.props.navigator.push({ component: nextVC, title: titleName, passProps: { //回调 g ...

  7. MOD_EXPIRES安装和配置 提高网站速率

    MOD_EXPIRES安装和配置 提高网站速率   实施这一方法将节省你难以置信数额的带宽,极大地加快你的网站为你的网站访客.基本上,对于图片,CSS , JavaScript以及其他文件可以通过优化 ...

  8. 用Qemu模拟vexpress-a9 (五) --- u-boot引导kernel,device tree的使用

    环境介绍 Win7 64 + Vmware 11 + ubuntu14.04 32 u-boot 版本:u-boot-2015-04 Linux kernel版本:linux-3.16.y busyb ...

  9. 如何使用 vimdiff 来 git diff

    git config --global diff.tool vimdiffgit config --global difftool.prompt falsegit config --global al ...

  10. 用友u8数据库表结构

    用友数据库表名参照表1 Accessaries 成套件表2 AccInformation 帐套参数表3 AdjustPVouch4 AdjustPVouchs5 Ap_AlarmSet 单位报警分类设 ...