Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int n,k;
- int a[100000];
- int b[100000];
- int binarySearch(int l, int r, int x)
- {
- if (r >= l) {
- int mid = l + (r - l) / 2;
- if (a[mid] == x)
- return mid;
- if (a[mid] > x)
- return binarySearch(l, mid - 1, x);
- return binarySearch(mid + 1, r, x);
- }
- return -1;
- }
- int main() {
- n = 20;
- k = 5;
- for (int i = 0; i < n; i++) a[i] = rand() % 10;
- for (int i = 0; i < k; i++) b[i] = rand() % 15;
- cout << "Первый массив" << "\n";
- for (int i = 0; i < n; i++) cout << a[i] << " ";
- cout << "\n";
- sort(a, a + n);
- cout << "Отсортированый первый массив" << "\n";
- for (int i = 0; i < n; i++) cout << a[i] << " ";
- cout << "\n";
- cout << "Второй массив массив" << "\n";
- for (int i = 0; i < k; i++) cout << b[i] << " ";
- cout << "\n";
- for (int i = 0; i < k; i++) {
- int c = b[i];
- int p = binarySearch(0, n - 1, c);
- if (p == -1) {
- cout << "0" << "\n";
- }
- int startP = p;
- while (a[p - 1] == c && p >= 1) p--;
- cout << p + 1 << " ";
- p = startP;
- while (a[p + 1] == c && p < n - 1) p++;
- cout << p + 1 << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement