Codeforces Round #614 (Div. 2) A题ConneR and the A.R.C. Markland-N

原题:
A.R.C. Markland-N is a tall building with nn floors numbered from 11 to nn. Between each two adjacent floors in the building, there is a staircase connecting them.

It’s lunchtime for our sensei Colin “ConneR” Neumann Jr, and he’s planning for a location to enjoy his meal.

ConneR’s office is at floor ss of the building. On each floor (including floor ss, of course), there is a restaurant offering meals. However, due to renovations being in progress, kk of the restaurants are currently closed, and as a result, ConneR can’t enjoy his lunch there.

CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.

Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns’ way!

Input
The first line contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases in the test. Then the descriptions of tt test cases follow.

The first line of a test case contains three integers nn, ss and kk (2≤n≤1092≤n≤109, 1≤s≤n1≤s≤n, 1≤k≤min(n−1,1000)1≤k≤min(n−1,1000)) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants.

The second line of a test case contains kk distinct integers a1,a2,…,aka1,a2,…,ak (1≤ai≤n1≤ai≤n) — the floor numbers of the currently closed restaurants.

It is guaranteed that the sum of kk over all test cases does not exceed 10001000.

Output
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor ss to a floor with an open restaurant.

Example
input
5
5 2 3
1 2 3
4 3 3
4 1 2
10 2 6
1 2 3 4 5 7
2 1 1
2
100 76 8
76 75 36 67 41 74 10 77
output
2
0
4
0
2
Note
In the first example test case, the nearest floor with an open restaurant would be the floor 44.

In the second example test case, the floor with ConneR’s office still has an open restaurant, so Sensei won’t have to go anywhere.

In the third example test case, the closest open restaurant is on the 66-th floor.
题目链接
题目大意:人要去吃饭,他在一幢楼里面,每层楼都有餐厅,但有些楼层不开(会告诉你),让你到找最近的可以吃饭的楼层要走几步。(往上走往下走)
做法:水题,可以用map做,也可以用set搞掉。但不要妄图开个超大的数组,那不现实,这边提供set做法。

#include
#include
#include
#include
#include
#include
#include
#include
#include 
#include
#include
#include
#include 
#include
using namespace std;
int main()
{
	int t,k,t1,n1,a,b,c,d,e,f,n,m;
	cin>>t;
	while(t--){
		set<int>s1;
		set<int>::iterator it;
		f=0;int flag=0;n=199999999;m=199999999;
		cin>>a>>b>>c;
		for(int i=0;i<c;i++){//因为楼层可能数量太大,所以干脆把不能的楼层序号存一下,不会超过1000 
			cin>>k;
			s1.insert(k);
		}
		d=b;
		while(1){
			int ff=0;
			for(it=s1.begin();it!=s1.end();it++){//每次寻找在不在里面就行,想要再提速还可以用二分查找,不用也能过 
				if(*it==d){
					ff=1;break;
				}
			}
			if(!ff){
				break;
			}
			else if(d==1){
				flag=2;
				break;
			}
			else{
				d--;f++;
			}
		}
		if(flag!=2)
		{
			n=f;
		}
		f=0;d=b;
		while(1){
			int ff=0;
			for(it=s1.begin();it!=s1.end();it++){
				if(*it==d){
					ff=1;break;
				}
			}
			if(!ff){
				break;
			}
			else if(d==a){
				flag=3;
				break;
			}
			 
			else{
				d++;f++;
			}
		}
		if(flag!=3){
			m=f;
		}
	//	cout<
		if(n==199999999&&m==199999999)cout<<0<<endl;
		else if(n==199999999)
		cout<<m<<endl;
		else if(m==199999999){
		cout<<n<<endl;
		}
		else{
			cout<<min(n,m)<<endl;
		}
		s1.clear();
	} 
}

备注:很多地方其实是可以改进的,但鉴于是水题也就懒得改了。

你可能感兴趣的:(Codeforces Round #614 (Div. 2) A题ConneR and the A.R.C. Markland-N)