题的地址: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;
}