当前位置: 代码迷 >> 综合 >> 洛谷 P2440 木材加工
  详细解决方案

洛谷 P2440 木材加工

热度:58   发布时间:2023-12-28 05:05:29.0

题目:https://www.luogu.com.cn/problem/P2440

这道题的思路跟P1873砍树这道题的思路一样。

P1873题目链接:https://www.luogu.com.cn/problem/P1873

代码:

#include <bits/stdc++.h>
#define MAXN 100050
#define ll long long
ll arr[MAXN];
using namespace std;int main()
{ll n,k,i,longest=0,r=0;cin>>n>>k;for(i=0; i<n; i++){cin>>arr[i];if(arr[i]>longest){longest=arr[i];r += arr[i];}}if(r<k){cout<<"0"<<endl;return 0;}ll mid,left=0,right=longest,a=0;while(left<=right){mid = (right-left)/2 + left;a=0;for(i=0; i<n; i++){a += arr[i]/mid;}if(a>=k){left = mid + 1;}else{right = mid - 1;}}mid = left-1;cout<<mid<<endl;return 0;
}