Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define optimization std::ios_base::sync_with_stdio(0); cin.tie(0);
- #define _int unsigned long long int
- using namespace std;
- _int binarySearch(_int arr[], _int x, _int low, _int high) {
- if(low <= high) {
- _int mid = (high + low) / 2;
- if(arr[mid] == x)
- return mid;
- else if(arr[mid] > x)
- binarySearch(arr, x, low, mid - 1);
- else
- binarySearch(arr, x, mid + 1, high);
- }
- else
- return -1;
- }
- int main(int argc, char *argv[]) {
- optimization
- _int n, k, x;
- cin >> n >> k;
- _int *nums = new _int[n];
- x = (n % 2 == 0 ? n / 2 : (n + 1) / 2);
- for (_int i = 0, l = 0, j = 1; j < n + 1; j++) {
- if (j % 2 == 0) {
- nums[x + i] = j;
- i++;
- }
- else {
- nums[l] = j;
- l++;
- }
- }
- for (_int i = 0; i < n; i++)
- cout << nums[i] << " ";
- cout << "\n";
- if (k )
- cout << binarySearch(nums, k, x, n) << "\n";
- else
- cout << binarySearch(nums, k, 0, x) << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement