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<iostream>
#include<vector>
#include<set>
#include<string>
#include<map>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string> 
#include<iomanip>
#include<queue>
#include<vector>
#include<set> 
#include<cstring>
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<<n<<" "<<m<<endl<<endl;
		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();
	} 
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大学生参加学科竞赛有着诸多好处,不仅有助于个人综合素质的提升,还能为未来职业发展奠定良好基础。以下是一些分析: 首先,学科竞赛是提高专业知识和技能水平的有效途径。通过参与竞赛,学生不仅能够深入学习相关专业知识,还能够接触到最新的科研成果和技术发展趋势。这有助于拓展学生的学科视野,使其对专业领域有更深刻的理解。在竞赛过程中,学生通常需要解决实际问,这锻炼了他们独立思考和解决问的能力。 其次,学科竞赛培养了学生的团队合作精神。许多竞赛项目需要团队协作来完成,这促使学生学会有效地与他人合作、协调分工。在团队合作中,学生们能够学到如何有效沟通、共同制定目标和分工合作,这对于日后进入职场具有重要意义。 此外,学科竞赛是提高学生综合能力的一种途径。竞赛项目通常会涉及到理论知识、实际操作和创新思维等多个方面,要求参赛者具备全面的素质。在竞赛过程中,学生不仅需要展现自己的专业知识,还需要具备创新意识和解决问的能力。这种全面的综合能力培养对于未来从事各类职业都具有积极作用。 此外,学科竞赛可以为学生提供展示自我、树立信心的机会。通过比赛的舞台,学生有机会展现自己在专业领域的优势,得到他人的认可和赞誉。这对于培养学生的自信心和自我价值感非常重要,有助于他们更加积极主动地投入学习和未来的职业生涯。 最后,学科竞赛对于个人职业发展具有积极的助推作用。在竞赛中脱颖而出的学生通常能够引起企业、研究机构等用人单位的关注。获得竞赛奖项不仅可以作为个人履历的亮点,还可以为进入理想的工作岗位提供有力的支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值