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

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≤109, 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.
代码:

#include<iostream>
#include<stdio.h>
#include<string>
#include<cstring>
#include<set>
typedef long long ll;
using namespace std; 
ll tem;
bool f,f1;
int main()
{
	int T;
    int maxs;
    cin>>T;
    ll n,s,k;
   while(T--)
   {
       f=false; f1=false;
       set<long long> st;
       cin>>n>>s>>k;
       for(int i=0;i<k;i++)
       {
           cin>>tem;
           st.insert(tem);
       }
       if(!st.count(s))
       {
           cout<<0<<endl;
           continue;
       }
       long long mas=s,mins=s;//这里要格外注意千万千万不要写mas=s+1,mins=s-1;如果s==n的时候mas
       //就会直接跳出循环,s==1也同理,我真是傻了。这题用map做代码会少很多,我这里用的是set做的,
       //结果我看了一下用map还是用set时间和空间都差不多。map的代码我就不放了
       for(long long i=0;i<n;i++)
       {
           if(!st.count(mas) && f==false)
           {
               break;
           }
           if(!st.count(mins) && f1==false)
           {
               break;
           }
           if(f==false)  mas++;
           if(f1==false) mins--;
           if(mas>n)
           { f=true; mas=0x3f3f3f3f;}
           if(mins<=0)
           {f1=true;mins=0x3f3f3f3f;}
       }
       cout<<min(abs(mas-s),abs(s-mins))<<endl;
   }
    return 0;
}
发布了12 篇原创文章 · 获赞 0 · 访问量 225

猜你喜欢

转载自blog.csdn.net/weixin_43305294/article/details/104529897