有n根木棍,第i根木棍长度为a[i],每次操作可以选一根木棍将其锯成两段,要求总操作次数不超过k。问最终所有木棍最大长度的最小值是多少?
1<=n<=2e5; 0<=k<=1e9; 1<=a[i]<=1e9
最小化最大值,或者反过来最大化最小值,优先考虑二分答案,对于某个特定的长度x,考虑将其锯成最长x一段需要的次数,如果总次数不超过k,那x就在可行域里。
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=b;i>=a;i--)
const int N = 200005;
int k, n, A[N];
int check(int x) {
int cnt = 0;
rep(i,1,n) {
if (A[i] % x == 0)
cnt += A[i] / x - 1;
else
cnt += A[i] / x;
}
return cnt <= k;
}
void solve() {
cin >> n >> k;
rep(i,1,n) cin >> A[i];
int lo = 0, hi = 1e10, mid;
while (lo + 1 < hi) {
mid = (lo + hi) / 2;
if (check(mid))
hi = mid;
else
lo = mid;
}
cout << hi << "\n";
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
while (t--) solve();
return 0;
}
标签:cnt,int,lo,abc174E,mid,hi,木棍,最小化
From: https://www.cnblogs.com/chenfy27/p/18077451