NC23049

华华给月月准备礼物

https://ac.nowcoder.com/acm/problem/23049

题意

你有段小木棍,对每段小木棍切割,要求得到段长度相等为(正整数)的小木棍,求最大为多少?


思路

答案具有单调性,故考虑二分,时间复杂度

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  maxn = 1e6 + 5;
const int N = 2e5 + 5;

int n,k,a[N];

bool check(int x){
    int ans=0;
    for(int i=1;i<=n;i++){
        ans+=a[i]/x;
        if(ans>=k) return true;
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    sort(a+1,a+1+n);
    int L=1,R=a[n]+1,ans=0;
    while(L<R){
        int mid=L+(R-L)/2;
        if(check(mid)) L=mid+1,ans=mid;
        else R=mid;
    }
    cout<<ans<<'\n';
    return 0;
}
全部评论

相关推荐

程序员小白条:你是沟通了900个,不是投了900份简历,你能投900份,意味着对面都要回复你900次,你早就找到实习了,没亮点就是这样的,别局限地区,时间投的也要早,现在都要7月了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务