#P8487. Optimally Lucky Binary Search

    ID: 21662 Type: Default 1000ms 256MiB

Optimally Lucky Binary Search

In this problem, you are given an ascending sequence of distinct integers from 0 to n-1. A modified version of binary search is used to find a target number whose value is exactly its index (i.e. the element at index x is x). However, instead of choosing the middle index in a deterministic way, the code randomly chooses one of two alternatives for the mid index in each iteration. The pseudocode of the algorithm is given below in both iterative and recursive forms:

Iterative form:

int find(int *num, int x, int len) {
    int l = 0, r = len - 1, mid, cnt = 0, w;
    while(l < r) {
        cnt++;
        w = rand() % 2;
        mid = (l + r + w) / 2;
        if(num[mid] - w < x) l = mid + (!w);
        else r = mid - w;
    }
    return mid;
}

Recursive form:

int cnt;
int get(int *num, int x, int l, int r) {
    if(l == r) return l;
    cnt++;
    int w = rand() % 2;
    int mid = (l + r + w) / 2;
    if(num[mid] - w < x) return get(num, x, mid + (!w), r);
    else return get(num, x, l, mid - w);
}

int find(int *num, int x, int len) {
    cnt = 0;
    return get(num, x, 0, len - 1);
}

Note that the two versions are equivalent. The only twist is that at every iteration (or recursive call) we can choose one of the two methods by selecting a parameter (w) from ({0,1}). In other words, you are allowed to take the more "lucky" branch in each iteration so that the total number of iterations (i.e. the value of (cnt)) until the search narrows the interval to a single element is minimized.

Formally, let the sequence be indexed from 0 to n-1. You are given two integers (n) (the length of the sequence) and (x) (the target index). Assuming you can choose the branch (i.e. choose (w=0) or (w=1)) in each iteration optimally, compute the minimum possible number of iterations required to finish the search.

In each iteration, if the current search interval is ([l, r]) (with (n = r - l + 1)), then you have two choices:

  1. Choose (w = 0): ( \text{Let } mid_0 = \left\lfloor \frac{l+r}{2} \right\rfloor. \quad \text{If } mid_0 \ge x, \text{ then the next interval becomes } [l, mid_0]. \quad \text{Otherwise, if } mid_0 < x, \text{ the interval becomes } [mid_0 + 1, r]. )

  2. Choose (w = 1): ( \text{Let } mid_1 = \left\lfloor \frac{l+r+1}{2} \right\rfloor \quad(=\lceil \frac{l+r}{2} \rceil).\quad \text{If } mid_1 - 1 \ge x, \text{ then the interval becomes } [l, mid_1 - 1]. \quad \text{Otherwise, if } mid_1 - 1 < x, \text{ it becomes } [mid_1, r]. )

Your task is to compute the minimum number of iterations needed so that the interval reduces to a single element (which will necessarily be (x) if you always choose the correct branch).

inputFormat

The input consists of a single line containing two space-separated integers (n) and (x), where (1 \le n \le 10^9) and (0 \le x < n). (n) is the length of the ascending sequence (with elements from 0 to n-1) and (x) is the given target index.

outputFormat

Output a single integer, the minimum number of iterations (i.e. the minimum possible value of (cnt)) required to locate the target if you can choose the optimal branch at every iteration.

sample

2 0
1