CodeForces - 343C 二分

题目链接

题意:给定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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值