WA 80分求调

题目


代码

#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,a[1000001],m,l,r,ans,mid;
  cin>>n;
  for(int i=0;i<n;i++)cin>>a[i];
  while(cin>>m){
    l=-1,r=n+1,ans=2147483647;
    while(l+1<r){
      mid=l+(r-l)/2;
      if(a[mid]==m){
        ans=m;
        break;
      }if(a[mid]>m){
        if(ans>=a[mid]-m)ans=a[mid];
        r=mid;
      }else{
        if(ans>m-a[mid])ans=a[mid];
        l=mid;
      }
    }cout<<ans<<" ";
  }return 0;
}

样例,断点测试