CodeForces-967C(二分查找最近值)

链接:CodeForces-967C

题意:n*m的楼房,cl个楼梯,ce个电梯,除了电梯的最大速度是v外,其他速度都是1。给出q次询问,回答(x1, y1)到(x2, y2),最少需要多少时间?

题解:同层直接求距离,不同层二分查找最近的楼梯与电梯,选最快的。

#include <bits/stdc++.h>
using namespace std;

const double EPS = 1e-8;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
int n, m, cl, ce, v, q, a, b, c, d;
int l[maxn], e[maxn];

int main()
{
    scanf("%d%d%d%d%d", &n, &m, &cl, &ce, &v);
    for(int i = 1; i <= cl; i++) scanf("%d", &l[i]);
    for(int i = 1; i <= ce; i++) scanf("%d", &e[i]);

    sort(l + 1, l + cl + 1);
    sort(e + 1, e + ce + 1);

    scanf("%d", &q);
    while(q--){
        scanf("%d%d%d%d",&a, &b, &c, &d);

        if(a == c) printf("%d\n", abs(b - d));
        else{
            int ans = INT_MAX;

            int u = lower_bound(l + 1, l + cl + 1, b) - l;
            if(u <= cl) ans = min(ans, abs(l[u] - b) + abs(l[u] - d) + abs(a - c));
            if(u > 1) ans = min(ans, abs(l[u-1] - b) + abs(l[u-1] - d) + abs(a - c));

            u = lower_bound(e + 1, e + ce + 1, b) - e;
            if(u <= ce) ans = min(ans, abs(e[u] - b) + abs(e[u] - d) + (abs(a - c) + v - 1) / v);
            if(u > 1) ans = min(ans, abs(e[u-1] - b) + abs(e[u-1] - d) + (abs(a - c) + v - 1) / v);

            printf("%d\n", ans);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值