ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

ConneR and the A.R.C. Markland-N

2020-01-20 13:41:14  阅读:430  来源: 互联网

标签:floor int restaurant test ConneR Markland include


ConneR and the A.R.C. Markland-N

A.R.C. Markland-N is a tall building with n floors numbered from 1 to n. 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 s of the building. On each floor (including floor s, of course), there is a restaurant offering meals. However, due to renovations being in progress, k 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 t (1≤t≤1000) — the number of test cases in the test. Then the descriptions of t test cases follow.

The first line of a test case contains three integers n, s and k (2≤n≤1e9, 1≤s≤n, 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 k distinct integers a1,a2,…,ak (1≤ai≤n) — the floor numbers of the currently closed restaurants.

It is guaranteed that the sum of k over all test cases does not exceed 1000.

Output

For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor s 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 4.

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 6-th floor.

方法一:vector

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> a;
int main()
{
	int n,s,k,t;
	cin>>t;
	while(t--){
		cin>>n>>s>>k;
		a.clear();
		a.resize(k);
		for(int i=0;i<k;i++){
			int z;
			cin>>z;
			a.push_back(z);
		}
		for(int i=0;i<=k;i++){
			if(s-i>=1&&find(a.begin(),a.end(),s-i)==a.end()){
				cout<<i<<endl;
				break;
			}
			if(s+i<=n&&find(a.begin(),a.end(),s+i)==a.end()){
				cout<<i<<endl;
				break;
			}
		}
	}
	return 0;
}

方法二:map

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
map<int,bool> a;
const int maxn=0x7ffffff;
int main()
{
	int n,s,k,t,l,r;
	cin>>t;
	while(t--){
		cin>>n>>s>>k;
		a.clear();
		for(int i=1;i<=k;i++){
			int z;
			cin>>z;
			a[z]=1;
		}
		r=s;
		l=s;
		while(a[r]){
			r++;
		}
		while(a[l]){
			l--;
		}
		if(r==n+1){
			r=maxn;
		}
		if(l==0){
			l=maxn;
		}
		cout<<min(abs(s-r),abs(s-l))<<endl;
	}
	return 0;
}
linjiayina 发布了70 篇原创文章 · 获赞 2 · 访问量 2652 私信 关注

标签:floor,int,restaurant,test,ConneR,Markland,include
来源: https://blog.csdn.net/linjiayina/article/details/104050478

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有