题意:给定n个可并行工作的起点的坐标,和m个终点坐标,要求用起点去碰终点,起点移动一次花费1s,问最少需要多长时间
思路:可以二分时间,记录目前最小未覆盖的点的下标,对于每个点考虑先左移动还是先右移,算出目前的起点可覆盖的最远距离后更新最小未覆盖点的下标。
注意:决策时有先往右再往左和先往左再往右两种情况如下图
#include <cstdio>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <set>
#define eps 1e-8
typedef long long ll;
const double PI = acos(-1.0);
const int maxn = 1e6;
const int INF = 0x3f3f3f;
const ll linf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
using namespace std;
int n,m;
ll a[maxn],b[maxn];
bool judge(ll x)
{
ll nxt = 1;//最左边的未覆盖的点
for(int i = 1; i<=n && nxt<=m; i++)
{
if(a[i]-b[nxt]>x)
return false;
ll s = a[i];
if(b[nxt]<=s)//左边有未覆盖的点
{
//向左再向右
s = max(s, x-(a[i]-b[nxt])+b[nxt] );
//向右再向左
s = max(s, (x-(a[i]-b[nxt]))/2+a[i] );
}
else//左边无未覆盖的点
{
s = a[i]+x;
}
while(nxt<=m && b[nxt]<=s)
nxt++;
}
return nxt>m;
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i = 1; i<=n; i++)
cin>>a[i];
for(int i = 1; i<=m; i++)
cin>>b[i];
ll l = 0,r = 1e12;
while(l<r)
{
ll mid = (l+r)>>1;
if(judge(mid))
r = mid;
else
l = mid+1;
}
cout<<l<<endl;
return 0;
}