POJ 3258 River Hopscotch

本文详细解析了POJ3258 RiverHopscotch问题的解决思路,采用二分查找法确定青蛙跳跃的最小距离。通过预处理数据、排序和检查函数实现了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

POJ 3258 River Hopscotch

思路:二分最小距离。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))

const int N=5e4+5;
const int INF=0x3f3f3f3f;
int a[N]={0};
int l,n,m;

bool check(ll v)
{
    int cnt=0,pre=0;
    for(int i=1;i<=n+1;)
    {
        if(a[i]-a[pre]>=v)
        {
            pre=i;
            i++;
        }
        else
        {
            cnt++;
            i++;
        }
    }
    
    if(cnt>m)return false;
    else return true; 
}

ll binasrch(ll l,ll r)
{
    ll mid=(l+r)>>1;
    while(l<r-1)
    {
        if(check(mid))l=mid;
        else r=mid;
        mid=(l+r)>>1;
        //cout<<l<<' '<<r<<endl;
    }
    return mid;
}


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    cin>>l>>n>>m;
    for(int i=1;i<=n;i++)cin>>a[i];
    sort(a+1,a+n+1);
    a[n+1]=l;
    
    int ans=binasrch(0,2*l);
    cout<<ans<<endl;
    return 0;
} 

 

转载于:https://www.cnblogs.com/widsom/p/7620106.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值