cf343c 二分答案+模拟

时间:2021-07-13 05:16:17
/*
怎么判断能否在时间k内完成扫描
贪心:每次取出最靠左边的磁头去扫描最左边的,然后再往右扫描即可
如果当前点无法扫到最左侧点,那么后继点一样无法扫到
*/
#include<bits/stdc++.h>
#define maxn 100005
#define ll long long
using namespace std; int n,m;
ll h[maxn],p[maxn]; int judge(ll x){
ll time1,time2;
int index=;
for(int i=;i<=n;i++){
if(h[i]-p[index]>x) return ;
if(p[index]>=h[i]){//直接往右扫描
while(index<=m && p[index]<=x+h[i])
index++;
if(index>m) return ;//
}
else {
time1=(x-(h[i]-p[index]))/;
time2=x-(h[i]-p[index])*;
time1=max(time1,time2); //先往左在往右或者先往右再往左的最长右走时间
while(index<=m && p[index]<=time1+h[i])
index++;
if(index>m) return ;
}
}
return ;
} int main(){
while(scanf("%d%d",&n,&m)==){
for(int i=;i<=n;i++) scanf("%lld",&h[i]);
for(int i=;i<=m;i++) scanf("%lld",&p[i]);
ll l=,r=max(h[n],p[m])*,ans=;
while(l<=r){
ll mid=l+r>>;
if(judge(mid))
ans=mid,r=mid-;
else l=mid+;
}
printf("%I64d\n",ans);
}
}