Openjudge Noi question Bank ch0111/01 find the nearest element

Source: Internet
Author: User

Total time limit:
1000ms
Memory Limit:
65536kB
Describe

In a non-descending sequence, finds the element closest to the given value.

Input
The first line contains an integer n, which is a non-descending sequence length. 1 <= n <= 100000.
The second line contains n integers, which are non-descending sequence elements. The size of all elements is between 0-1 and 000,000,000.
The third line contains an integer m, which is the number of the given value to ask. 1 <= m <= 10000.
Next m line, one integer per line, is the given value to ask the closest element. The size of all the given values is between 0-1 and 000,000,000.
Output
m rows, one integer per line, for the element value closest to the corresponding given value, preserving the input order. If more than one value satisfies the condition, the output is the smallest one.
Sample input
32 5 82105
Sample output
85

1#include <stdio.h>2#include <stdlib.h>3 #defineMAXN 1000104 intA[MAXN];5 intN;6 intcmpConst void*a,Const void*b)7 {8     return*(int*) a-* (int*) b;9 }Ten voidSearchintx) One { A     intmid,l,r,ans=-1; -mid=n/2; -L=0; theR=N; -      while(l<r-1) -     { -         if(a[mid]>x) r=mid; +         ElseL=mid; -Mid= (r+l)/2; +     } A     if(L +1==n| | x-a[l]<=a[l+1]-x) ans=A[l]; at     Elseans=a[l+1]; -printf"%d\n", ans); - } - intMain () - { -     intm,f; in     inti; -scanf"%d",&n); to      for(i=0; i<n;i++) +scanf"%d",&a[i]); -Qsort (A,n,sizeof(int), CMP); thescanf"%d",&m); *      while(m--) $     {Panax Notoginsengscanf"%d",&f); - search (f); the     } +     return 0; A}

As a basic two-part question, it seems that there is nothing to talk about, that is, while (m--) little tricks.

Openjudge Noi question Bank ch0111/01 find the nearest element

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.