Розбір
In this problem, it is required to find a number in a sorted array, which can be done using binary search.
Let be a sorted array of length , consisting of integers. The function binary_search returns true if number is present in the array. Reading the input array takes , and handling queries takes .
The lower_bound function returns a pointer to the first position where the element can be inserted without violating the sorted order of the array.
The upper_bound function returns a pointer to the last position where the element can be inserted without violating the sorted order of the array.
If lower_boundupper_bound, then the number is present in the array. Otherwise, the number is not in the array.
The Java function Arrays.binarySearch() returns the index of the found element in the array. If the array contains multiple identical keys, it returns the index of any one of them.
If is not present in the array , the function Arrays.binarySearch returns the value , where is the index of the first element greater than . If is greater than all elements in the array , then .
The bisect function in Python is used for performing binary search in sorted sequences. It helps find the insertion point for an element in a sorted list to maintain the order of the list. This can be useful, for example, for optimizing the insertion of new elements into an ordered list or for determining where to insert an element in an array to keep it sorted.
bisect provides two main functions:
bisect.bisect_left. This function finds the insertion point for element in the sorted list . If the element is already present in the list, bisect_left will return the index of the first occurrence of . If the element is absent, the function will return the index where can be inserted without breaking the sorting order.
bisect.bisect_right. Similar to bisect_left, but it returns the index for inserting element after the existing occurrences of in the list .
Both functions accept optional arguments and , which define the search range within the list. By default, , and .
Implementation of own binary search. Suppose we need to find the element in the array within the range . Divide the segment in half, setting . If , then the element is in the range . Otherwise, continue the search in the range .
Data structures set. Insert the numbers from the input array into a . If a number appears multiple times in the array, it will be added to the set only once. The result of checking for the presence of a given element in the array remains unchanged.
An element is present in the set (and in the input array) if .
Inserting elements from the input array into the set takes O(nlog2n), and processing queries takes .
Algorithm realization —
Declare an array.
#define MAX 1000000 int m[MAX];
Read the input data.
scanf("%d %d", &n, &q); for (i = 0; i < n; i++) scanf("%d", &m[i]);
Process queries sequentially.
for (i = 0; i < q; i++) {
Read the value of .
scanf("%d", &x);
A number is present in the array if binary_search returns true.
if (binary_search(m, m + n, x) == 1) puts("YES"); else puts("NO"); }
Algorithm realization — lower_bound, upper_bound
Declare an array.
#define MAX 1000000 int m[MAX];
Read the input data.
scanf("%d %d", &n, &q); for (i = 0; i < n; i++) scanf("%d", &m[i]);
Process queries sequentially.
for (i = 0; i < q; i++) {
Read the value of .
scanf("%d", &x);
A number is present in the array if lower_boundupper_bound
if (lower_bound(m, m + n, x) != upper_bound(m, m + n, x)) puts("YES"); else puts("NO"); }
Algorithm realization — own binary search
Declare an array.
#define MAX 1000000 int m[MAX];
The my_bin_search function implements a binary search and returns , if the number is present in the array within the range .
int my_bin_search(int *m, int start, int end, int x) {
Perform a binary search on the interval .
while (start < end) { int mid = (start + end) / 2; if (x > m[mid]) start = mid + 1; else end = mid; }
Return the result.
return m[start] == x; }
The main part of the program. Read the input data.
scanf("%d %d", &n, &q); for (i = 0; i < n; i++) scanf("%d", &m[i]);
Process queries sequentially.
for (i = 0; i < q; i++) {
Read the value of .
scanf("%d", &x);
A number is present in the array if my_bin_search returns .
if (my_bin_search(m, 0, n - 1, x)) puts("YES"); else puts("NO"); }
Algorithm realization — set,
#include <cstdio> #include <algorithm> #include <set> using namespace std; int i, n, q, x; set<int> s; int main(void) { scanf("%d %d", &n, &q); for (i = 0; i < n; i++) { scanf("%d", &x); s.insert(x); } for (i = 0; i < q; i++) { scanf("%d", &x); if (s.find(x) != s.end()) puts("YES"); else puts("NO"); } return 0; }
Java realization — own binary search
import java.util.*; import java.io.*; public class Main { static int my_bin_search(int m[], int start, int end, int x) { while (start < end) { int mid = (start + end) / 2; if (x > m[mid]) start = mid + 1; else end = mid; } return (m[start] == x) ? 1 : 0; } public static void main(String[] args) { FastScanner con = new FastScanner(new InputStreamReader(System.in)); int n = con.nextInt(); int q = con.nextInt(); int m[] = new int[n]; for(int i = 0; i < n; i++) m[i] = con.nextInt(); for(int i = 0; i < q; i++) { int x = con.nextInt(); if(my_bin_search(m,0,n-1,x) == 1) System.out.println("YES"); else System.out.println("NO"); } } } class FastScanner { private BufferedReader br; private StringTokenizer st; public FastScanner(InputStreamReader reader) { br = new BufferedReader(reader); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public void close() throws Exception { br.close(); } }
Java realization — Arrays.binarySearch
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { FastScanner con = new FastScanner(new InputStreamReader(System.in)); int n = con.nextInt(); int q = con.nextInt(); int m[] = new int[n]; for(int i = 0; i < n; i++) m[i] = con.nextInt(); for(int i = 0; i < q; i++) { int x = con.nextInt(); if(Arrays.binarySearch(m, x) >= 0) System.out.println("YES"); else System.out.println("NO"); } } } class FastScanner { private BufferedReader br; private StringTokenizer st; public FastScanner(InputStreamReader reader) { br = new BufferedReader(reader); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public void close() throws Exception { br.close(); } }
Python realization
Import the bisect module.
import bisect
Read the input data.
n, q = map(int,input().split()) lst = list(map(int,input().split()))
Process queries sequentially.
for _ in range(q):
Read the value of .
x = int(input())
A number is present in the list if bisect_leftbisect_right
if bisect.bisect_left(lst, x) != bisect.bisect_right(lst, x): print("YES") else: print("NO")
Python realization — own binary search
The my_bin_search function implements a binary search:
If the number is not present in the list , the function returns .
Otherwise, it returns the position of the number in the list .
def my_bin_search(a, x): start = 0 end = len(a) – 1
Perform a binary search over the interval .
while start < end: mid = (start + end) // 2; if x > a[mid]: start = mid + 1; else: end = mid;
Return the result.
if a[start] == x: return start else: return -1;
The main part of the program. Read the input data.
n, q = map(int,input().split()) lst = list(map(int,input().split()))
Process queries sequentially.
for _ in range(q):
Read the value of .
x = int(input())
Perform a binary search. Print the corresponding result.
if my_bin_search(lst,x) != -1: print("YES") else: print("NO")