NC23049 华华给月月准备礼物
题目地址:
基本思路:
非常明显的二分答案,我们二分每根木棍的最大长度,然后每次check一下是否能切出大于等于K根的木棍就是了,然后就是大家二分的时候注意一下取答案的位置就好了,其他的没啥。
参考代码:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false)
#define int long long
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define pdd pair <double, double>
#define mp(a, b) make_pair(a, b)
#define INF 0x3f3f3f3f
inline int read() {
int x = 0, neg = 1; char op = getchar();
while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); }
while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); }
return neg * x;
}
inline void print(int x) {
if (x < 0) { putchar('-'); x = -x; }
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
const int maxn = 2e5 + 10;
int n,k,a[maxn];
bool C(int s) {//check一下能不能切出大于K根;
int cnt = 0;
rep(i, 1, n) {
cnt += a[i] / s;
}
return cnt >= k;
}
signed main() {
IO;
cin >> n >> k;
rep(i,1,n) cin >> a[i];
int l = 1 , r = INF; // 实际左右边界[1,1e9]就够了;
int ans = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (C(mid)) l = mid + 1, ans = mid;
else r = mid - 1;
}
cout << ans << '\n';
return 0;
}

京公网安备 11010502036488号