Codeforces Round #477.A

题的地址:http://codeforces.com/contest/966/problem/A

题的解法很好想,就是求离y1最近的左右两座电梯和两个楼梯,但是在使用lower_bound
上出现问题,以及没有找到计算花费时间的通用公式,其实这样就可以
MIN = min(MIN,abs(x2 - x1) + abs(y2 - a[ans]) + abs(y1 - a[ans]));
lower_bound如果从0开始的话,大于等于第一个会返回0,数组里值如果都大于,也返回0
所以从1开始,这样就返回0,代表都大于,返回1,就代表大于等于第一个元素

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN = 100005;
const int inf = 0x3f3f3f3f;
int a[MAXN];
int b[MAXN];
int main()
{
    int n,m,cl,ce,v;
    scanf("%d %d %d %d %d",&n,&m,&cl,&ce,&v);
    for(int i = 1;i <= cl;++i)
    {
        scanf("%d",&a[i]);
    }
    for(int i = 1;i <= ce;++i)
    {
        scanf("%d",&b[i]);
    }
    int q;
    scanf("%d",&q);
    for(int i = 0;i < q;++i)
    {
        int x1,y1,x2,y2;
        scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
        if(x1 == x2){
            printf("%d\n",abs(y2 - y1));
            continue;
        }
        int ans = lower_bound(a + 1,a + cl + 1,y1) - a - 1;
        int tmp = lower_bound(b + 1,b + ce + 1,y1) - b - 1;
        int MIN = inf;
        if(cl != 0){
        if(ans > 0){
            MIN = min(MIN,abs(x2 - x1) + abs(y2 - a[ans]) + abs(y1 - a[ans]));
        }
        if(ans <= cl){
            MIN = min(MIN,abs(x2 - x1) + abs(y2 - a[ans + 1]) + abs(y1 - a[ans + 1]));
        }
        }
        if(ce != 0){
        if(tmp > 0){
            MIN = min(MIN,(abs(x2 - x1) + v - 1) / v + abs(y2 - b[tmp]) + abs(y1 - b[tmp]));
        }
        if(tmp <= ce){
            MIN = min(MIN,(abs(x2 - x1) + v - 1) / v + abs(y2 - b[tmp + 1]) + abs(y1 - b[tmp + 1]));
        }
        }
        printf("%d\n",MIN);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值