题解:CF2005B2 The Strict Teacher (Hard Version)

· · 题解

前言

6 篇题解。

分析

我们可以分类讨论,明显发现,只有 David 两侧的老师或者墙有作用。同时,我们要先排序。

接下来我们开始分类讨论。

好东西

你可以不用手写二分,因为 C++ 里面自带了两个二分函数 lower_bound 和 upper_bound。

注意

一定要搞清楚 n,m 代表者什么,不要搞混 n,m就因为这个我调了半小时。

代码

//By xiaozhou001
#include<bits/stdc++.h>
using namespace std;
int t,n,m,q,b,a[100007];
int main()
{
    cin>>t;
    while(t--){
        cin>>n>>m>>q;
        for(int i=1;i<=m;i++){
            cin>>a[i];
        }
        sort(a+1,a+1+m);
        for(int i=1;i<=q;i++){
            cin>>b;
            if(b<a[1]){
                cout<<a[1]-1<<endl;
            }else if(b>a[m]){
                cout<<n-a[m]<<endl;
            }else{
                int l=lower_bound(a+1,a+1+m,b)-a;
                int r=upper_bound(a+1,a+1+m,b)-a-1;
                cout<<abs(a[r]-a[l])/2<<endl;
            }
        }
    }
    return 0;
}