【Codeforces】967C Stairs and Elevators (二分)。

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CSDN___CSDN/article/details/87484158

http://codeforces.com/contest/967/problem/C

从一个地方到另一个地方,可以选择爬楼梯或者坐电梯 ,前提是楼梯或者电梯存在。

n 楼层数

m 每一层的房间数

s 楼梯的数量

e 电梯的数量

v 电梯的速度

第2行是s个楼梯的位置

第3行是e个电梯的位置

第4行就是测试样例的个数

下面就是测试样例

纵向移动一格或者横向移动一隔花费一个单位的时间,坐电梯按照电梯速度来算,有小数的话向上取整

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>

using namespace std;

int cs[100000+5];
int ce[100000+5];
int n,m,v,s,e,T;
int x2,y2,x3,y3;


int main ()
{
	int i,k;
	cin >> n >> m >> s >> e >> v;//s stair e elevators
	for(i=0;i<s;i++)
	{
		cin >> cs[i];
	}
	for(i=0;i<e;i++)
	{
		cin >> ce[i];
	}		
	cin >> T;
	int time;
	while(T--)
	{
		cin >> x2 >> y2 >> x3 >> y3;
		if(x2==x3)
		{
			cout << abs(y3-y2) << endl;
		}
		else
		{
			time = 1000000000;
			k = lower_bound(ce,ce+e,y2)-ce;
			if(k<e)
			{
				time = min(time,abs(ce[k]-y2)+abs(ce[k]-y3));
			}
			if(k>0)
			{
				time = min(time,abs(y2-ce[k-1])+abs(y3-ce[k-1]));
			}
			time += (abs(x2-x3)/v); 
	 		if(abs(x2-x3)%v!=0)
				time++;				
			k = lower_bound(cs,cs+s,y2)-cs;
			if(k<s)
			{
				time = min(time,abs(cs[k]-y2)+abs(cs[k]-y3)+abs(x2-x3));
			}
			if(k>0)
			{
				time = min(time,abs(y2-cs[k-1])+abs(y3-cs[k-1])+abs(x2-x3));
			}
			cout << time << endl;
		}
	}
	return 0;
}

注意判断条件if(k>0)。

猜你喜欢

转载自blog.csdn.net/CSDN___CSDN/article/details/87484158