ababababababababa @ 2024-08-14 09:05:28
#include<bits/stdc++.h>
using namespace std;
long long n,m,a[1000010],ans;
bool check(long long x)
{
long long ans=0;
for (int i=1;i<=n;i++)
ans+=a[i]/x;
return ans>=m;
}
int main()
{
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
sort(a+1,a+n+1);
int l=0,r=100000001;
while(l<=r)
{
int mid=(l+r)/2;
if(check(mid))
l=mid+1;
else
r=mid-1;
}
printf("%d",r);
return 0;
}
88分......劳烦各位大佬帮忙看看是不是细节问题
by guanzisheng2 @ 2024-08-14 09:08:04
#include<bits/stdc++.h>
using namespace std;
int n,m,mid,a[100005],l,r;
bool f(int x);
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
l = 0, r = 100000000;
while(l<r){
mid=((l+r)+1)/2;
if(f(mid)){
l=mid;
}
else{
r=mid-1;
}
}
cout << l;
}
bool f(int x){
int sum = 0;
for(int i = 1; i <= n; i++) {
sum += a[i] / x;
}
return sum >= m;
}
by ababababababababa @ 2024-08-14 10:04:47
谢谢 此帖结
by LVFUyang1 @ 2024-08-22 20:02:40
#include<bits/stdc++.h>
using namespace std;
int n,k,l[100005];
bool check(int x){
int cnt=0;
for (int i=1;i<=n;i++)
cnt+=l[i]/x;
if (cnt>=k) return 1;
else return 0;
}
int find(int l,int r){
while(l<=r){
int mid=(l+r)/2;
if(check(mid)){
l=mid+1;
}
else{
r=mid-1;
}
}
return l;
}
int main(){
cin>>n>>k;
int ma=-1;
for(int i=1;i<=n;i++){
cin>>l[i];
ma=max(ma,l[i]);
}
cout<<find(1,ma)-1;
}