C. Lazy Narek(二分)
题目来源:https://codeforces.com/contest/2005/problem/C
//
题意:n长的数轴,给出m个老师的位置,每一次老师可以停留到当前位置,或者左右移动一个单元。给出q个同学的位置,每个同学每次可以和老师一样是否选择移动,问每个同学,老师抓到该同学的最小次数。
//
思路:一眼二分,注意的是用upper_bound(),如果容器全是小于等于查找元素,返回a.end(),end()不指向任何元素。如果全是大于查找的元素,返回a.begin(),指向第一个元素。
//
代码:
点击查看代码
// Problem: B2. The Strict Teacher (Hard Version)
// Contest: Codeforces - Codeforces Round 972 (Div. 2)
// URL: https://codeforces.com/contest/2005/problem/B2
// Memory Limit: 256 MB
// Time Limit: 1500 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
#define int long long
using namespace std;
void solve(){
int n,m,q;
cin>>n>>m>>q;
vector<int>a(m);
for(auto &i:a){cin>>i;}
sort(a.begin(),a.end());
while(q--){
int p;cin>>p;
auto it=upper_bound(a.begin(),a.end(),p);
if(it==a.begin()){//在最左边的位置
cout<<*it-1<<endl;
}
else if(it==a.end()){//在最右边的位置
cout<<n-(*--it)<<endl;
}
else{//在中间
cout<<(*it-(*(--it)))/2<<endl;
}
}
}
signed main()
{
int t=1;
cin>>t;
while(t--){
solve();
}
}
浙公网安备 33010602011771号