#binary-search
CF 2222G - Statistics on Tree
CF 2222G - Statistics on Tree Rating: - Tags: binary search, brute force, dfs and similar, divide and conquer, graphs, trees Solve time: 6m 33s Verified: no Solution Problem Understanding We are given a tree. For a pair of vertices $(u,v)$, we remove every edge belonging to the unique simple path between them. After those edges are deleted, the tree breaks into several connected components. The value of the pair...
CF 2222E - Seek the Truth
CF 2222E - Seek the Truth Rating: - Tags: binary search, bitmasks, constructive algorithms, interactive Solve time: 1m 59s Verified: no Solution Problem Understanding The original problem is interactive, but the version used for judging after the contest is the hacked format. Instead of interacting with a judge, each test case directly gives us the hidden values n , k , and c . The interactive story is useful because...
CF 1949B - Charming Meals
CF 1949B - Charming Meals Rating: 1500 Tags: binary search, brute force, greedy, sortings Solve time: 1m 44s Verified: yes Solution Problem Understanding We have two arrays of size n . The array a contains the spiciness values of the appetizers, and the array b contains the spiciness values of the main dishes. Every appetizer must be paired with exactly one main dish, and every main dish must be used...
CF 241B - Friends
CF 241B - Friends Rating: 2700 Tags: binary search, bitmasks, data structures, math Solve time: 2m 23s Verified: no Solution Problem Understanding We have an array of friend attractiveness values. Every unordered pair of distinct friends produces one possible picture, and the value of that picture is the xor of the two attractiveness values. Among all possible pairs, we want to choose exactly m distinct pairs whose xor values have...
Interpolation Search with Fallback
Interpolation Search with Fallback Interpolation search with fallback combines the speed of interpolation search on well distributed numeric keys with the reliability of binary search. It estimates where the target should be by value, but falls back to ordinary binary search when the estimate becomes poor, unstable, or unsafe. This gives good performance on near uniform data while preserving the worst case behavior of binary search. Problem Given a sorted...
Branchless Lower Bound
Branchless Lower Bound Branchless lower bound computes the first position where a value is greater than or equal to a target, without using unpredictable branches. It follows the same logical structure as binary search but replaces control flow with arithmetic updates or conditional moves. The result is the same as standard lower bound. The benefit is improved performance on modern CPUs when branch misprediction is expensive. Problem Given a sorted...
Eytzinger Layout Search
Eytzinger Layout Search Eytzinger layout search reorganizes a sorted array into a breadth first tree layout, also known as heap order. The goal is to improve cache locality and make memory access patterns more predictable. Instead of storing data in sorted order, the array is rearranged so that binary search follows a sequential memory pattern. This reduces cache misses and improves throughput on modern CPUs. Problem Given a sorted array...
Cache Aware Binary Search
Cache Aware Binary Search Cache aware binary search modifies ordinary binary search to account for the memory hierarchy. The algorithm still uses ordering and comparisons, but the data layout or search strategy is chosen with explicit knowledge of cache line size, block size, or page size. The goal is to reduce cache misses. On modern hardware, a comparison is cheap, while an unpredictable memory load can be expensive. Problem Given...
Van Emde Boas Layout Search
Van Emde Boas Layout Search Van Emde Boas layout search stores a binary search tree in recursive memory order. The layout is cache oblivious, which means it does not need to know the cache line size or memory block size in advance. The main idea is to split the tree into a top subtree and several bottom subtrees, then store each part recursively. Nodes that are close in the search...
Branchless Binary Search
Branchless Binary Search Branchless binary search is a binary search variant designed to reduce branch mispredictions. A normal binary search repeatedly branches on whether the middle value is less than the target. On modern CPUs, this branch can be hard to predict because the search direction depends on the data. A branchless version keeps the same comparison logic but updates the search position using conditional moves, masks, or arithmetic expressions....
SIMD Binary Search
SIMD Binary Search SIMD binary search accelerates binary search by evaluating multiple candidate comparisons in parallel. Instead of checking a single midpoint per step, the algorithm probes several positions using vector instructions and narrows the search range based on the combined results. The structure still follows binary search, but each iteration performs more comparisons per memory access. Problem Given a sorted array $A$ of length $n$ and a target value...