题目:http://codeforces.com/problemset/problem/1293/A

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.

题意:有n个楼层(从1到n),每个楼层都有餐厅能吃饭,ConneR在s层,现在有k个楼层餐厅是关门的(不超出1000),问ConneR最少要一栋多少层

分析:n很大,但是k超过1000,所以要从k下手,我刚开始理解的不太清楚用数组a【1100】来记录k个数,来一个a【k】=1,结果数组越界了(n很大),解决办法是用map<int,int>来记录,后面只需暴力从s向两边搜索即可

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
using namespace std;int t,n,s,k;
int main(){cin>>t;while(t--){map<int,int> a;cin>>n>>s>>k;a.clear();for(int i=0;i<k;i++){int g;cin>>g;a[g]=1;}int step = 0;while(1){if(s+step<=n&&a[s+step]==0)  break;if(s-step>0&&a[s-step]==0) break;step++;}cout<<step<<endl;}
}

[codeforces 1293A] ConneR and the A.R.C. Markland-N相关推荐

  1. ConneR and the A.R.C. Markland-N CodeForces - 1293A(暴力)

    Sakuzyo - Imprinting A.R.C. Markland-N is a tall building with n floors numbered from 1 to n. Betwee ...

  2. Frodo and pillows CodeForces - 760B 二分 注意l和r的选择

    以后写l和r的初始值的时候,在不影响合理性的前提下,尽量写大一点 比如下面这个代码,如果r不加以或者l==0就不行 #include <iostream> #include <cst ...

  3. 【带修莫队】CodeForces 940F Machine Learning

    S o u r c e : Source: Source: Codeforces Round #466 (Div. 2) P r o b l e m : Problem: Problem: n n n ...

  4. 基础算法 —— 模拟思维

    [概述] 模拟,是根据实际问题建立模型,模拟实际按程序走一遍,最终求出答案. 思维,则是与逻辑思维有关,其需要针对题意.数据范围等抽丝剥茧抽离出有用的信息,从而得出一个结果. 对于一般的模拟,直接根据 ...

  5. 训练 CF 1700分 题解

    C o d e F o r c e s 264 B CodeForces 264B CodeForces264B G o o d S e q u e n c e s Good Sequences Go ...

  6. 秀一下以前搜房soufun发贴机的发帖群发日志!!呵呵..

    秀一下以前搜房soufun论坛发贴机的发帖群发日志!!呵呵.. QQ 1163551688 "2009-11-28 18:00:06 562","qwerty00789& ...

  7. CodeForces 375D Tree and Queries

    传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...

  8. 【codeforces 812C】Sagheer and Nubian Market

    [题目链接]:http://codeforces.com/contest/812/problem/C [题意] 给你n个物品; 你可以选购k个物品;则 每个物品有一个基础价值; 然后还有一个附加价值; ...

  9. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  10. [题解]RGB Substring (hard version)-前缀和(codeforces 1196D2)

    题目链接:https://codeforces.com/problemset/problem/1196/D2 题意: q 个询问,每个查询将给你一个由 n 个字符组成的字符串s,每个字符都是 &quo ...

最新文章

  1. 2022-2028年中国无溶剂聚氨酯复膜胶行业市场调查研究及发展前景规划报告
  2. 写在Flutter 1.0之前
  3. SAP ME1M如何以ALV形式显示(鹦鹉学舌篇)
  4. Maven修改远程仓库配置
  5. 节省显存新思路,在PyTorch里使用2 bit激活压缩训练神经网络
  6. 总结JavaScript(Iframe、window.open、window.showModalDialog)父窗口与子窗口之间的操作
  7. mysql 如何清除sql缓存
  8. 点击一次铵钮产生一个新文本框,分别输入值,然后获取
  9. 【HDU - 4635】Strongly connected(缩点,新图性质,建图,Tarjan求强连通分量)
  10. [android]-xml解析示便-SAX
  11. BZOJ 1085(IDA*)
  12. 黑盒测试和白盒测试_黑盒测试综合策略
  13. Too many open files的四种解决办法
  14. UNIX环境高级编程——线程同步之条件变量以及属性
  15. 【转】Odoo开发之:工作流 workflow
  16. 采用状态空间法求解八数码问题
  17. 重庆大学软件工程导论总结
  18. mac怎么禁止某个应用联网?
  19. FIFO读数据异常分析
  20. JavaScript网页特效编程百宝箱pdf

热门文章

  1. 【warning】UserWarning: The parameter ‘pretrained‘ is deprecated since 0.13 and may be removed
  2. BlackBerry手机应用上网的通道列表
  3. clip gradient
  4. c语言hook函数,另类iOS上的C函数hook
  5. 联想T420S 黑苹果GPT+UEFI单系统安装
  6. oracle rac 成本及架构,Oracle Extended RAC 架构实战
  7. FFmpeg编码(YUV转H264)并改变视频分辨率示例
  8. stc12c5a60s MQ系列传感器及报警
  9. RStudio不在Plots中显示图片的一个原因
  10. html match函数,match函数 Match函数概述