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 ...
随机推荐
- Exercise02_07
import javax.swing.JOptionPane; public class Years { public static void main(String[] args){ String ...
- 【OpenJudge9267】【递推】核电站
核电站 总时间限制: 5000ms 单个测试点时间限制: 1000ms 内存限制: 131072kB [描述] 一个核电站有N个放核物质的坑,坑排列在一条直线上.如果连续M个坑中放入核物质,则会发生爆 ...
- 上传ipa文件时报错 Your account already has a valid iOS distribution certificate
这个问题是因为你本机的生产证书是在别人的电脑上创建的,所以才会提示你已经有一个有效的生产证书,但是没有安装到本地:
- hdu4565之矩阵快速幂
So Easy! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- PostgreSQL配置文件--资源使用(除WAL外)
2 资源使用(除WAL外) RESOURCE USAGE (except for WAL) 2.1 内存 Memory 2.1.1 shared_buffers 数字型 默认: shared_buff ...
- 64位的centos6.9的vnc-sever的安装及桌面环境安装
1.VNC (Virtual Network Computer)是虚拟网络计算机的缩写.VNC 是在基于 UNIX 和 Linux 操作系统的免费的开源软件,远程控制能力强大,高效实用,其性能可以和 ...
- LNMP第二部分nginx、php配置
内容概要:一. nginx.confvim /usr/local/nginx/conf/nginx.conf //清空原来的配置,加入如下内容:user nobody nobody;worker_pr ...
- 腾讯云linux服务器分区方案
刚刚在腾讯云买了一台服务器,刚买的服务器的数据盘都是需要自己来分区的,下面就记录一下操作. 通过命令fdisk-l查看硬盘信息 可以看到有两块硬盘/dev/vda和/dev/vdb,启动vda是系统盘 ...
- yii2 URL重写 nginx的配置
Url的重写 nginx的配置文件 [root@localhost protected]# vim /etc/nginx/conf.d/default.conf server { listen ...
- python合并多个csv文件并去重
#coding=utf-8 import os import pandas as pd import glob def hebing(): csv_list = glob.glob('*.csv') ...