P2440 木材加工(二分+贪心)
思路:这里就要看往那边贪心了,因为解决的是最大值最小化,最小值最大化。也就是说当满足大于等于c时,l=mid+1这样的二分得到的就是在所有满足条件函数下的最右端.
#include<iostream> #include<algorithm> using namespace std; #define ll long long const int maxn = 1e5 + 10; int a[maxn], n, maxx, ans; ll c, mid; bool check(ll x){ ll sum = 0; for (int i = 1; i <= n; ++i) sum += a[i] / x; return sum >= c; } void half(){ ll l = 1, r = maxx; while (l <= r){ mid = (r + l) >> 1; if (check(mid)){ l = mid + 1; } else r = mid - 1; } ans = r; } int main(){ cin >> n >> c; for (int i = 1; i <= n; ++i) { cin >> a[i]; maxx = max(maxx, a[i]); } half(); //二分 cout << ans << endl; }