WA,60分
1465:练56.2 查找最接近的元素
内存限制:64MB
时间限制:1.000S
题目描述
在一个非降序列中,查找与给定值最接近的元素。
输入
第一行包含一个整数n,为非降序列长度。
1≤n≤100000
第二行包含n个整数,为非降序列各元素。所有元素的大小均在0∼1,000,000,0000之间。
第三行包含一个整数m,为要询问的给定值个数。
1≤m≤10000
接下来m行,每行一个整数,为要询问最接近元素的给定值。所有给定值的大小均在0∼1,000,000,000之间。
输出
m 行,每行一个整数,为最接近相应给定值的元素值,保持输入顺序。若有多个值满足条件,输出最小的一个。
样例输入
3
2 5 8
2
10
5
样例输出
8
5
#include<bits/stdc++.h>
using namespace std;
int a[100005];
int n,x,t,pos;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
sort(a+1,a+n+1);
cin>>t;
while(t--){
cin>>x;
int pos=lower_bound(a+1,a+n+1,x)-a;
if(pos==n+1||x-a[pos-1]<a[pos]-x)cout<<a[pos-1]<<"\n";
else cout<<a[pos]<<"\n";
}
return 0;
}