Problem Statement
There are $N$ mochi (rice cakes), arranged in ascending order of size. The size of the $i$-th mochi $(1\leq i\leq N)$ is $A_i$.
Given two mochi $A$ and $B$, with sizes $a$ and $b$ respectively, you can make one kagamimochi (a stacked rice cake) by placing mochi $A$ on top of mochi $B$ if and only if $a$ is at most half of $b$.
Find how many kagamimochi can be made simultaneously.
More precisely, find the maximum non-negative integer $K$ for which the following is possible:
- From the $N$ mochi, choose $2K$ of them to form $K$ pairs. For each pair, place one mochi on top of the other, to make $K$ kagamimochi.
Constraints
- $2 \leq N \leq 5 \times 10^5$
- $1 \leq A_i \leq 10^9 \ (1 \leq i \leq N)$
- $A_i \leq A_{i+1} \ (1 \leq i < N)$
- All input values are integers.
Input
The input is given from Standard Input in the following format:
$N$ $A_1$ $A_2$ $\dotsc$ $A_N$
Output
Print the maximum $K$ such that $K$ kagamimochi can be made simultaneously.
|
|
使用
while(l + 1!= r)形式的二分查找时,通常确实需要额外添加一步判断来确定最终答案。这种二分查找写法在循环结束后,只是把答案范围缩小到了两个相邻的值,即
l和r,但并没有明确指出哪个值才是真正满足条件的结果。因此,为了得到准确答案,需要依据具体的判定条件(例如本题中的check函数),对l和r分别或其中之一进行再次验证。不过,要是能确定在循环结束时,其中某个指针必然指向正确结果,那就无需额外判断。但多数情况下,很难提前做出这样的保证,所以添加额外判断是一种稳妥、通用的做法,能确保代码逻辑的正确性。