P2440 木材加工(二分+贪心)

时间:2023-03-09 13:41:12
P2440 木材加工(二分+贪心)

思路:这里就要看往那边贪心了,因为解决的是最大值最小化,最小值最大化。也就是说当满足大于等于c时,l=mid+1这样的二分得到的就是在所有满足条件函数下的最右端.

#include<iostream>
#include<algorithm>
using namespace std; #define ll long long
const int maxn = 1e5 + ;
int a[maxn], n, maxx, ans;
ll c, mid; bool check(ll x){
ll sum = ;
for (int i = ; i <= n; ++i)
sum += a[i] / x;
return sum >= c;
} void half(){
ll l = , r = maxx;
while (l <= r){
mid = (r + l) >> ;
if (check(mid)){ l = mid + ; }
else r = mid - ;
}
ans = r;
} int main(){
cin >> n >> c;
for (int i = ; i <= n; ++i)
{
cin >> a[i]; maxx = max(maxx, a[i]);
}
half(); //二分
cout << ans << endl;
}