Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. #define optimization std::ios_base::sync_with_stdio(0); cin.tie(0);
  3. #define _int unsigned long long int
  4.  
  5. using namespace std;
  6.  
  7. _int binarySearch(_int arr[], _int x, _int low, _int high) {
  8.     if(low <= high) {
  9.         _int mid = (high + low) / 2;
  10.         if(arr[mid] == x)
  11.             return mid;
  12.         else if(arr[mid] > x)
  13.             binarySearch(arr, x, low, mid - 1);
  14.         else
  15.             binarySearch(arr, x, mid + 1, high);
  16.     }
  17.     else
  18.         return -1;
  19. }
  20.  
  21. int main(int argc, char *argv[]) {
  22.     optimization
  23.     _int n, k, x;
  24.     cin >> n >> k;
  25.     _int *nums = new _int[n];
  26.     x = (n % 2 == 0 ? n / 2 : (n + 1) / 2);
  27.     for (_int i = 0, l = 0, j = 1; j < n + 1; j++) {
  28.         if (j % 2 == 0) {
  29.             nums[x + i] = j;
  30.             i++;
  31.         }
  32.         else {
  33.             nums[l] = j;
  34.             l++;
  35.         }
  36.     }
  37.     for (_int i = 0; i < n; i++)
  38.         cout << nums[i] << " ";
  39.     cout << "\n";
  40.     if (k )
  41.         cout << binarySearch(nums, k, x, n) << "\n";
  42.     else
  43.         cout << binarySearch(nums, k, 0, x) << "\n";
  44.     return 0;  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement