Codeforces Round #614 (Div. 2) A( A - ConneR and the A.R.C. Markland-N)

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

题目链接:http://codeforces.com/contest/1293/problem/A

题意:一栋楼房有n(1~n)层,有个人身处s楼,现在想要到餐厅吃饭,可是现在有k个餐厅关闭的,问你该人至少爬几层楼梯才能到开放的餐厅吃饭

思路:。。。这题暴力没戏。。又是超时又是超内存。。。分两种,一个是往上找出最小的i-s即可,一个是往下找,找出最小的s-i即可,,用了数组还是超时。。用了map就过了

// 
// Created by HJYL on 2020/1/14.
//
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e4+10;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        long long n,s,fb;
        cin>>n>>s>>fb;
        map<int,int>a;
        int x;
        for(int i=0;i<fb;i++)
        {
            cin>>x;
            a[x]=1;
        }
        ll MIN=0x3ffff;
        for(ll i=s;i<=n;i++)
        {
            if(!a[i])
            {
                MIN=min(MIN,i-s);
                break;
            }
        }
        for(ll i=s-1;i>=1;i--)
        {
            if(!a[i])
            {
                MIN=min(MIN,s-i);
                break;
            }
        }
        cout<<MIN<<endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Vampire6/p/12217765.html