codingRE @ 2024-11-12 22:53:48
#include<stdio.h>
int arr[100005];
int n, k, l, left, right, mid;
int check(int mid) {
int count = 0;
if (mid == 0)
return 0;
for (int i = 0; i < n; ++i) {
count += arr[i] / mid;
}
return count >= k;
}
int main()
{
long long int sum = 0;
int sz = 0;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
sum += arr[i];
}
sz = sum / n;//最大值上界
right = sz;
while(left<=right){
mid = left + (right - left) / 2 ;
if (check(mid)) {//检查
left = mid + 1;
}
else
right = mid - 1;
}
if ((right + left) < 1)
printf("0\n");
else
printf("%d\n", (right + left) / 2);
return 0;
}
by CMiBold @ 2024-11-12 23:38:31
em,没太看出来,我把我的答案发下,你看下,我看样子可能是二分的问题
可能吧
#include<bits/stdc++.h>
using namespace std;
#define luo_xing main
const int N=1e5+7;
int n,k;
int l[N];
bool check(int x){
int w=0;
for(int i=1;i<=n;i++) w+=l[i]/x;
if(w>=k) return true;
return false;
}
int luo_xing(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>l[i];
int l=1,r=100000000,s=0;
while(l<r){
int mid=(l+r)>>1;
if(check(mid)) s=mid,l=mid+1;
else r=mid;
}
cout<<s<<endl;
return 0;
}
我的思路和你差不多,所以大同小异,你试着按我这个找找,或许可能就对了
by nbq202412904430 @ 2024-11-18 22:47:57
你试一下这个
5 5
1 1 1 1 1
1
然后还有就是那个left你还是赋值为0吧