183.Wood Cut【hard】
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.
For L=[232, 124, 456], k=7, return 114.
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。
理清题意后我们就来想想如何用算法的形式表示出来,显然在计算如2, 1, 4等分片数时我们进行了取整运算,在计算机中则可以使用下式表示:
其中 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】的更多相关文章
- hdu 6214 : Smallest Minimum Cut 【网络流】
题目链接 ISAP写法 #include <bits/stdc++.h> using namespace std; typedef long long LL; namespace Fast ...
- 【转载】用C#编写一个简单的记事本
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 【AOP】Spring AOP基础 + 实践 完整记录
Spring AOP的基础概念 ============================================================= AOP(Aspect-Oriented Pr ...
- 【转】linux中的cut/tr/join/split/xargs命令
1. cut命令 cut命令用于从文件或者标准输入中读取内容并截取每一行的特定部分并送到标准输出. 截取的方式有三种:一是按照字符位置,二是按照字节位置,三是使用一个分隔符将一行分割成多个field, ...
- POJ 2914 Minimum Cut【最小割 Stoer-Wangner】
题意:求全局最小割 不能用网络流求最小割,枚举举汇点要O(n),最短增广路最大流算法求最大流是O(n2m)复杂度,在复杂网络中O(m)=O(n2),算法总复杂度就是O(n5):就算你用其他求最大流的算 ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- LG3690 【模板】Link Cut Tree (动态树)
题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...
- AC日记——【模板】Link Cut Tree 洛谷 P3690
[模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ...
- 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 ...
随机推荐
- small test on 5.30 morning T3
经典的等价类计数问题,我们设 f(x) 为环长为 x 的时候的花环种类,那么答案显然等于 1/n * Σf( gcd (i,n) * [gcd(i,n)!=1] * [i>=0&&a ...
- 【Trie】bzoj1212 [HNOI2004]L语言
枚举每个文章里已经在Trie中被标记为可能是分割处的字符,然后再从此处跑Trie,继续向后标记.由于单词数很少,因此复杂度可以接受,O(n*m*Len). #include<cstdio> ...
- 【kmp算法】bzoj1355 [Baltic2009]Radio Transmission
引用题解:http://blog.csdn.net/wyfcyx_forever/article/details/40347425 #include<cstdio> #include< ...
- python3开发进阶-Django框架的自带认证功能auth模块和User对象的基本操作
阅读目录 auth模块 User对象 认证进阶 一.auth模块 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其 ...
- 【Python笔记】Python语言基础
Python是一种解释性(没有编译).交互式.面向对象的语言 1.安装python编译器 版本:Python2.7比较普遍,Python不是向下兼容的软件,因此Python3.x有些东西不好找资料 2 ...
- Scala实战高手****第6课 :零基础实战Scala集合操作及Spark源码解析
本课内容1.Spark中Scala集合操作鉴赏2.Scala集合操作实战 --------------------------------------------------------------- ...
- 对象数组的初始化:null reference
今天写代码的时候,发现我写的对象数组,只声明,而没有初始化,所以记录一下这个问题:null reference. Animals [] an=new Animals[5];//这只是个对象类型数组的声 ...
- java对象详解
java对象及线程详解 内存布局 普通对象布局 数组的内存布局 内部类的内存布局 对象分解 对象头-mark word(8字节) 实例数据 对齐填充(可选) java锁分析 volatile关键字 v ...
- 利用Impromptu实现duck typing的封装
Impromptu是一个动态生成代码实现接口的库,可以非常方便我们实现DuckType编程: public interface IUser { string Name { get; ...
- nobr 不换行标签
示例代码: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...