A
2026-06-06 11:49:20
发布于:四川
8阅读
0回复
0点赞
看出来这道题就是一道二分答案的题
那直接去二分问题问的的值,也就是直接去二分答案短跳跃距离的最大值
套模板就不说了
最难写的就是check函数了(英文里check的意思是检查,检验的意思
bool ck(int x){
int tot=0,i=0,now=0;
while (i<n+1){
i++;
if (a[i]-a[now]<x){
tot++;
}
else{
now=i;
}
}
if (tot>m) return false;
else return true;
}
这里我的check函数检查的是最大的跳跃距离为mid时是否过关
若过关,则向后寻找更大的最大跳跃距离
若未过关,则向前寻找能过关的最大跳跃距离
AC代码
#include<bits/stdc++.h>
#define N (50000+10)
using namespace std;
int a[N];
int d,n,m,ans;
bool ck(int x){
int tot=0,i=0,now=0;
while (i<n+1){
i++;
if(a[i]-a[now]<x){
tot++;
}
else{
now=i;
}
}
if (tot>m) return false;
else return true;
}
int main(){
scanf("%d%d%d",&d,&n,&m);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
a[n+1]=d;
int l=1,r=d,mid;
while (l<=r){
mid=(l+r)/2;
if (ck(mid)){
ans=mid;
l=mid+1;
}
else r=mid-1;
}
printf("%d",ans);
}
其实我是来水题解的,看到的点个赞吧

这里空空如也





有帮助,赞一个