Solution
分析一下题目, 这道题我们首先分析能不能二分
二分的条件是满足单调性 和 check答案比较简单
这道题目我们可以二分长度, 显然是满足单调性的
每次二分木棍的长度mid, 然后对每个a[i] 都能得到 a[i] / mid 个需要的木棍
我们check一下能不能得到超过k个所需木棍
直接统计满足条件的最大长度即可
注意答案会出现0, ans 初始值我一开始用的 -1
wa了一发orz
Code
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long LL;
const ll mod = 1e9 + 7;
const int DEG = 20;
const double PI = acos(-1.0);
const double eps = 1e-10;
const ll inf = 1e18;
const int N = 2e5 + 5;
static auto faster_iostream = []() {
std::ios::sync_with_stdio(false); // turn off sync
std::cin.tie(nullptr); // untie in/out streams
return 0;
}();
int a[N];
int main(){
int n, k;
cin >> n >> k;
int left = 1, right = 1e9;
int ans = 0; // 注意是 0
for(int i = 1; i <= n; i++) cin >> a[i];
while(left <= right){
int mid = (left + right) >> 1;
ll cnt = 0;
for(int i = 1; i <= n; i++){
cnt += a[i] / mid;
}
if(cnt < k){
right = mid - 1;
}
else {
ans = mid; // 统计答案
left = mid + 1;
}
}
cout << ans << "\n";
return 0;
} 
京公网安备 11010502036488号