52分求调,玄关

P2440 木材加工

liouyulin @ 2024-11-30 08:51:57

52分求调

#include<iostream>
using namespace std;
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
int n,k,a[100010];
int check(int x){
    int ans=0;
    for(int i=0;i<n;i++)
        ans+=a[i]/x;
    return ans>=k;
}
int main(){
    IOS;
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>a[i];
    int l=1,r=100000;
    while(l<r){
        int mid=(l+r+1)>>1;
        if(check(mid))
        l=mid;
        else
        r=mid-1;
    }
    cout<<l;
}

by PengRuiXuan @ 2024-11-30 08:56:28

#include <bits/stdc++.h>
using namespace std;
const int N=100010;
int n,k,a[N];
bool cheak(int x)
{
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        cnt+=a[i]/x;
    }
    return cnt>=k;
}
int bsearch(int l,int r)
{
    while(l<r)
    {
        int mid=(l+r+1)>>1;
        if(cheak(mid))
        {
            l=mid;
        }
        else
        {
            r=mid-1;
        }
    }
    return l;
}

int main()
{
    cin>>n>>k;
    int l=1,r=0;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        r=max(r,a[i]);
    }
    int ans=bsearch(l,r);
    if(cheak(ans))
      cout<<ans<<endl;
    else
      cout<<"0"<<endl;
    return 0;
}

|