Half Exercises find explanations elements

Title Description

Now tell you a length \ (n \) of an ordered array of \ (A_1, A_2, ..., A_N \) , and \ (q \) once asked Always ask will give you a number \ (x \ ) , for Always ask, you need to determine whether there is a certain element in the array \ (the X-a_i = \) .

Input Format

Comprising a first line of input integer \ (n-(. 1 \ n-Le \ Le 100000) \) , for the number of elements in the array representation.
The second row contains the input \ (\ n-) integers, there is a space between each two of the elements in the array used to represent \ (a_1, a_2, ..., a_n (1 \ le a_i \ le 10 ^ 9 and A_1 \ Le A_2 \ Le ... \ Le A_N) \) .
The third line of the input contains an integer \ (Q (. 1 \ Le Q \ 100000 Le) \) , for indicating the number of interrogation.
Next \ (Q \) rows, each row contains an integer \ (X (. 1 \ Le X \ ^. 9 Le 10) \) , indicates the number to be interrogated.

Output Format

For each query \ (X \) , if there are elements in the array is equal to \ (X \) , output "YES"; otherwise, the output "NO". Each output on separate lines.

Sample input

5
1 3 5 7 9
3
1
2
3

Sample Output

YES
NO
YES

Topic analysis

This question relates to the algorithm: Two points.
This question tells us that because array elements are monotonically non-decreasing order of the input (i.e. \ (a_i \ A_ Le +. 1 {I} \) ), we make the argument coordinates \ (I \) , for the strain array elements \ (a_i \) divided by two.
We will write a program bool solve(int x)function, it is determined whether the present value of the array \ (X \) elements. Logic that determines:
a start setting \ (L =. 1 \) (coordinates of the left edge of the array), provided \ (R & lt = \ n-) (coordinates of the right boundary of the array), then as long as \ (L \ le R \) condition, so I \ (MID = (L + R & lt) / 2 \) (i.e. \ (L \) and \ (R & lt \) median), it is judged \ (a [mid] \) is equal to \ (the X-\) , there will be three cases:

  • If \ (A [MID] X = \) , found, return true;
  • If \ (A [MID] \ lt X \) , then \ (a [mid] \) , and its left-side range (i.e. \ ([L, mid] \ ) of all elements within the range) are \ (\ lt x \ ) , so that \ (MID = L + \). 1 , the right half region into the search is continued;
  • If \ (A [MID] \ gt X \) , then \ (a [mid] \) , and its right of the range (i.e., \ ([mid, R] \ ) range) in all the elements \ (\ gt x \ ) , so that \ (R & lt MID = -. 1 \) , into the left half of the area of the search is continued.

The above cycle of operation when the exit (i.e., has been satisfied \ (L \ le R \) conditions), if we have not found equal to \ (X \) elements, then in the array \ (A \) is not present in equal \ (x \) elements.

Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, a[maxn], q, x;

bool solve(int x) {
    int L = 1, R = n;
    while (L <= R) {
        int mid = (L + R) / 2;
        if (a[mid] == x) return true;
        else if (a[mid] > x)
            R = mid - 1;
        else // a[mid] < x
            L = mid + 1;
    }
    return false;
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    cin >> q;
    while (q --) {
        cin >> x;
        puts( solve(x) ? "YES" : "NO" );
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/zifeiynoip/p/11450618.html