P2678 跳石头

时间:2023-03-09 00:37:11
P2678 跳石头

传送门

思路:

  二分跳跃的最短距离 mid 。暴力判断如果有两个石头直接的距离小于 mid ,就把这个石头拿走。如果拿走的石头数目 cnt ≤ m,说明二分的答案可行,ans = mid,接着二分更短的跳跃距离。

Code:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<stack>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
using namespace std;
#define lck_max(a,b) ((a)>(b)?(a):(b))
#define lck_min(a,b) ((a)<(b)?(a):(b))
typedef long long LL;
const int maxn=5e5+;
LL d,n,m;
LL t[maxn];
inline LL read()
{
LL kr=,xs=;
char ls;
ls=getchar();
while(!isdigit(ls))
{
if(!(ls^))
kr=-;
ls=getchar();
}
while(isdigit(ls))
{
xs=(xs<<)+(xs<<)+(ls^);
ls=getchar();
}
return xs*kr;
}
inline void out(LL xs)
{
if(!xs) {putchar(); return;}
if(xs<) putchar('-'),xs=-xs;
int kr[],ls=;
while(xs) kr[++ls]=xs%,xs/=;
while(ls) putchar(kr[ls]+),ls--;
}
LL cnt,i,now;
inline bool check(LL u)
{
cnt=,i=,now=;
while(i<=n)
{
i++;
if(t[i]-t[now]<u)
cnt++;
else
now=i;
}
if(cnt>m)
return false;
else
return true;
}
LL ans;
inline void query(LL l,LL r)
{
while(l<=r)
{
LL mid=l+r>>;
if(check(mid))
ans=mid,l=mid+;
else
r=mid-;
}
}
int main()
{
d=read();n=read();m=read();
for(LL i=;i<=n;i++)
t[i]=read();
t[n+]=d;//终点到起点的距离。
query(,d);
out(ans),putchar('\n');
return ;
}