题面:
Sakuzyo - Imprinting
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.
题意:
一栋n层的楼每一层都有餐馆,其中k层的餐馆是关闭的,一个人在s层,问他至少要上或下几层才能进入没有关门的餐馆
题解:
小贪心一下
将关闭的k层放入数组a排序
判断,若s层不属于k层直接输出0
否则循环判断a[i+1]-a[i]是否大于1
因为s层处于数组内,所以最佳楼层一定是相邻于某个元素的,只判断a[i]+1和a[i+1]-1两个楼层即可
AC代码:

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int a[1050];
int n,s,k,d,i,p,ans;
int main(){int T;cin>>T;while(T--){cin>>n>>s>>k;for(i=1;i<=k;i++) cin>>a[i];sort(a+1,a+1+k);a[0]=0;a[k+1]=n+1;ans=10000;bool flag=true;for(i=0;i<=k;i++){if(a[i]==s||a[i+1]==s) flag=false;if(a[i+1]-a[i]>1){ans=min(ans,abs(s-a[i+1]+1));ans=min(ans,abs(s-a[i]-1));}}if(flag) ans=0;cout<<ans<<endl;}return 0;
}

A - 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. 基础算法 —— 模拟思维

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

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

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

  4. 硬盘的S.M.A.R.T.是什么?如何看懂它

    说道硬盘的S.M.A.R.T.这个既常用然而又陌生的家伙,说真的如果直接看的话有多少个人能看懂这份表?现在我们就要简单的讲解一下NVMe SSD的S.M.A.R.T.信息. 说道硬盘的S.M.A.R. ...

  5. S.M.A.R.T.

    S.M.A.R.T.,全称为"Self-Monitoring Analysis and Reporting Technology",即"自我监测.分析及报告技术" ...

  6. 给定一个 n X n 的矩阵 和 R,求旋转 90 X R 度以后的矩阵

    1. 问题 [例题]循环输入,每组数据先输入一个 n(n<=10),然后 n 行 n 列数据代表一个矩阵, 每个数据为一个整数,然后再给出一个数字 R,输出这个矩阵按照顺时针旋转 90 X R ...

  7. python二进制打开(rb)和文本格式打开(r)什么区别?

    使用 open() 函数以文本格式打开文件和以二进制格式打开文件,唯一的区别是对文件中换行符的处理不同. 在 Windows 系统中,文件中用 "\r\n" 作为行末标识符(即换行 ...

  8. \r \r\n \t的区别

    \n 软回车:       在Windows 中表示换行且回到下一行的最开始位置.相当于Mac OS 里的 \r 的效果.       在Linux.unix 中只表示换行,但不会回到下一行的开始位置 ...

  9. adb install -r ,-d 等的解释

    adb install -r 替换已存在的应用程序,也就是说强制安装 adb install -l 锁定该应用程序 adb install -t 允许测试包 adb install -s 把应用程序安 ...

  10. Android Studio – Cannot resolve symbol ‘R’

    Android Studio – Cannot resolve symbol 'R' 解决方法: Build -> Clean Project Tools -> Android -> ...

最新文章

  1. Openstack数据库管理工具alembic更新Enum类型
  2. IC/FPGA笔试/面试题分析(十一)基础概念(三态门等)
  3. 如何快速学习产品?实践才是王道!同为小白的人儿加油!
  4. Apache JMeter 记一次使用HTTP工具POST提交JSON数据进行送积分高并发压测(二)
  5. POJ 1804 逆序数 解题(归并排序)
  6. 微信公众平台开发(75) 语音识别
  7. 使用Zookeeper实现负载均衡原理
  8. python数据分析库pandas官方教程
  9. 微信跳转浏览器html5,微信跳转浏览器或提示手机端打开HTML代码 最新
  10. disabled spring cloud bus interation with spring cloud sleuth zipkin
  11. python any_python的any
  12. 谁说前端不能搞红黑树,用这55张图拿JS一起手撕红黑树
  13. Flume 海量日志收集利器
  14. 训练赛一:bfs广搜题目 CF115B Lawnmower
  15. 【PIC】单片机基本概述
  16. 最简单dophinscheduler 集成datax步骤
  17. 【深度学习小知识】K-means聚类
  18. 为啥人类从1970年1月1日开始计时?
  19. 区块链--拜占庭将军问题--区块链
  20. fofa资产收集-cert证书

热门文章

  1. mysql 正则表达式 包含中文_MYSQL 中文检索匹配与正则表达式
  2. 显示模块模式 — Revealing Module Pattern
  3. 光明勇士iOS服务器维护,裕际网络科技《光明勇士》3月11日 iOSamp;安卓部分区服数据互通公告 - 热门手游公告-TK游戏...
  4. SLAM综述阅读笔记一:Past, Present, and Future of Simultaneous Localization And Mapping(2016)
  5. 数字转换成英语的程序(c++实现)
  6. outlook分组邮件提醒
  7. 天天淘宝,你却不知道个性化推荐技术...
  8. 设计模式-手机生产-抽象工厂模式
  9. 无法获取目标服务器证书的SSL指纹--VMware VCSA
  10. 大型网站的架构技术-学习自李智慧的书