文章

26

粉丝

0

获赞

138

访问

4.3k

头像
Aggressive cows 题解:二分答案查找+贪心思想
P1638
发布于2026年3月11日 11:09
阅读数 108

通过83原因:题目是多组数据输入,但题目上没说

#include<bits/stdc++.h>
using namespace std;
long long int x[100005]= {0};
long long int n,c;//n个棚,c个牛
bool judge(long long int d)
{
    long int cnt=1;//表示已经有几头牛放进去了
    long long int last=x[0];//x[0]是一定要取到的
    for(long long int i=1; i<n; i++)
    {
        if(x[i]-last>=d)
        {
            last=x[i];
            cnt++;
            if(cnt>=c) return true;
        }
    }
    return false;
}
void solve()
{
    long long int l=1,r=x[n-1]-x[0];//答案,最小是0,最大是x[n-1]-x[0];
    long long int mid;
    while(l<r)
    {
        mid=(l+r+1)/2;
        if(judge(mid)) l=mid;
        else r=mid-1;
    }
...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发