当前位置:网站首页>洛谷 P2440 木材加工
洛谷 P2440 木材加工
2022-08-01 19:40:00 【君义_noip】
【题目链接】
【题目考点】
1. 二份答案
【解题思路】
该题要求的是小段木头的长度l满足条件的最大值。
小段木头长度l需要满足的条件为:假设每小段长度为l,切出的小段数量要大于等于k。
设函数bool check(int l);,判读如果每小段长度为l,切出的小段数量是否大于等于k。
假设第i个原木的长度为 a i a_i ai,那么这段原木以长度l切小段,能切出 ⌊ a i l ⌋ \lfloor \frac{a_i}{l} \rfloor ⌊lai⌋个小段。把每个原木能切出的小段数量加和,即为小段总数量。判断小段总数量是否大于等于k,把结果返回。
用二分答案的解题方法,求满足该条件的最大值。
【题解代码】
解法1:二分答案
#include <bits/stdc++.h>
using namespace std;
#define N 100005
int a[N], n, k;//a[i]:第i个原木的长度
bool check(int l)//切成小段l,是否满足切出的小段数量大于等于k
{
long long s = 0;//小段数量
for(int i = 1; i <= n; ++i)
s += a[i] / l;
return s >= k;
}
int main()
{
cin >> n >> k;
for(int i = 1; i <= n; ++i)
cin >> a[i];
if(check(1) == false)//如果切成1cm的小段,数量也达不到k个
{
cout << 0;
return 0;
}
int l = 1, r = 100000000;
while(l < r)
{
int mid = (l+r+1)/2;
if(check(mid))
l = mid;
else
r = mid-1;
}
cout << l;
return 0;
}
边栏推荐
猜你喜欢

ThreadLocal讲义

安装win32gui失败,解决问题

Creo5.0 rough hexagon is how to draw

为什么限制了Oracle的SGA和PGA,OS仍然会用到SWAP?

使用常见问题解答软件的好处有哪些?

八百客、销售易、纷享销客各行其道

即时通讯开发移动端弱网络优化方法总结

Win11怎么安装语音包?Win11语音包安装教程

57:第五章:开发admin管理服务:10:开发【从MongoDB的GridFS中,获取文件,接口】;(从GridFS中,获取文件的SOP)(不使用MongoDB的服务,可以排除其自动加载类)
![57: Chapter 5: Develop admin management services: 10: Develop [get files from MongoDB's GridFS, interface]; (from GridFS, get the SOP of files) (Do not use MongoDB's service, you can exclude its autom](/img/e1/2fa8dcc9c246bbbc2494326a83cda1.png)
57: Chapter 5: Develop admin management services: 10: Develop [get files from MongoDB's GridFS, interface]; (from GridFS, get the SOP of files) (Do not use MongoDB's service, you can exclude its autom
随机推荐
Mobile Zero of Likou Brush Questions
第56章 业务逻辑之物流/配送实体定义
58:第五章:开发admin管理服务:11:开发【管理员人脸登录,接口】;(未实测)(使用了阿里AI人脸识别)(演示了,使用RestTemplate实现接口调用接口;)
XSS靶场中级绕过
力扣刷题之移动零
Combining two ordered arrays
nacos安装与配置
In the background of the GBase 8c database, what command is used to perform the master-slave switchover operation for the gtm and dn nodes?
图文详述Eureka的缓存机制/三级缓存
To drive efficient upstream and downstream collaboration, how can cross-border B2B e-commerce platforms release the core value of the LED industry supply chain?
17. Load balancing
datax - 艰难debug路
deploy zabbix
57: Chapter 5: Develop admin management services: 10: Develop [get files from MongoDB's GridFS, interface]; (from GridFS, get the SOP of files) (Do not use MongoDB's service, you can exclude its autom
【蓝桥杯选拔赛真题47】Scratch潜艇游戏 少儿编程scratch蓝桥杯选拔赛真题讲解
Oracle排序某个字段, 如果这个varchar2类型的字段有数字也有文字 , 怎么按照数字大小排序?
The solution to the vtk volume rendering code error (the code can run in vtk7, 8, 9), and the VTK dataset website
重保特辑|筑牢第一道防线,云防火墙攻防演练最佳实践
数值矩阵的图形表示
有点奇怪!访问目的网址,主机能容器却不行