A. ConneR and the A.R.C. Markland-N
题目链接A. ConneR and the A.R.C. Markland-N
题目大意
t 组数据,每组有一栋 n 层高且每层都有餐厅的大楼,现在有一个人在 s 楼要去吃饭,给出了 k 个餐厅关门的层数,问在 s 楼的这个人,最少走几层才吃到饭
解题思路
k 的范围是从 1 到 min(n-1,1000),也就是说最多有1000层餐厅关门,最坏的情况就是从当前层s到s-1000或s+1000范围内餐厅都关门,二分查找再求最优解即可
当然我也看到网上很多大佬用set和map做,要是觉得二分太麻烦,下面甩给你们链接自己去康吧
嘻
set做法
map做法
附上代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int INF=0x3f3f3f;
int a[1010],k;
int f(int y){
int l=1,r=k;
while(r>=l){
int mid=(r+l)/2;
if(a[mid]==y)
return 0;
if(a[mid]>y)
r=mid-1;
if(a[mid]<y)
l=mid+1;
}
return 1;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int t;
cin>>t;
while(t--){
int ans=INF;
int n,s;
cin>>n>>s>>k;
for(int i=1;i<=k;i++)
cin>>a[i];
sort(a+1,a+k+1);
for(int i=max(s-1000,1);i<=min(n,s+1000);i++){
if(f(i))
ans=min(ans,abs(s-i));
}
cout<<ans<<endl;
}
return 0;
}