#divide-and-conquer
CF 1250M - SmartGarden
CF 1250M - SmartGarden Rating: 2500 Tags: constructive algorithms, divide and conquer Solve time: 1m 24s Verified: yes Solution Problem Understanding The garden is an $n \times n$ grid where each cell is either a plant that must be watered or a slab that must never be touched. The layout is highly structured: all diagonal cells are slabs, and every cell strictly below the diagonal that touches the diagonal also...
CF 960E - Alternating Tree
CF 960E - Alternating Tree Rating: 2300 Tags: combinatorics, dfs and similar, divide and conquer, dp, probabilities, trees Solve time: 2m 1s Verified: no Solution Problem Understanding We are given a tree with a value attached to every node. Between any two nodes $u$ and $v$, there is exactly one simple path, and we assign a score to that directed path by taking the node values along the path and...
CF 1070M - Algoland and Berland
CF 1070M - Algoland and Berland Rating: 3000 Tags: constructive algorithms, divide and conquer, geometry Solve time: 6m 42s Verified: no Solution Problem Understanding We are given two sets of points in the plane, one set belonging to Algoland and the other to Berland. The task is to construct exactly $a + b - 1$ straight line segments, each connecting one Berland city to one Algoland city. Every segment becomes...
CF 914G - Sum the Fibonacci
CF 914G - Sum the Fibonacci Rating: 2600 Tags: bitmasks, divide and conquer, dp, fft, math Solve time: 5m 10s Verified: no Solution Problem Understanding We are given a sequence of integers, each of which can be thought of as a 17-bit mask. From this array we must form ordered selections of five positions. Each selection contributes a value computed from bitwise combinations of the chosen elements, followed by evaluating...
CF 914E - Palindromes in a Tree
CF 914E - Palindromes in a Tree Rating: 2400 Tags: bitmasks, data structures, divide and conquer, trees Solve time: 4m 42s Verified: no Solution Problem Understanding We are given a tree where each node carries a lowercase character from a limited alphabet of size 20. The task is to examine every simple path in the tree and determine whether the multiset of characters along that path can be rearranged into...
CF 1261F - Xor-Set
CF 1261F - Xor-Set Rating: 3100 Tags: bitmasks, divide and conquer, math Solve time: 6m 41s Verified: no Solution Problem Understanding We are given two very large sets of integers, but they are not listed explicitly. Instead, each set is described as a union of intervals. Every integer inside an interval belongs to the set, and different intervals may overlap, so the same number can appear multiple times in the...
CF 1773B - BinCoin
CF 1773B - BinCoin Rating: 2200 Tags: binary search, divide and conquer, hashing, implementation, probabilities, trees Solve time: 2m 27s Verified: no Solution Problem Understanding We are given a rooted binary tree with $n$ employees. Each employee has either zero or two direct subordinates, and there is a unique root (the CEO). The company runs a procedure that produces a full ordering of all employees, and this procedure is executed...
CF 1654H - Three Minimums
CF 1654H - Three Minimums Rating: 3500 Tags: combinatorics, constructive algorithms, divide and conquer, dp, fft, math Solve time: 5m 50s Verified: no Solution Problem Understanding We are asked to count permutations of the numbers from 1 to n that satisfy two independent kinds of restrictions. The first restriction is positional and local. We are given a short comparison string s of length m. It fixes whether each adjacent pair...
CF 1654F - Minimal String Xoration
CF 1654F - Minimal String Xoration Rating: 2800 Tags: bitmasks, data structures, divide and conquer, greedy, hashing, sortings, strings Solve time: 2m 42s Verified: yes Solution Problem Understanding We are given a string whose length is a power of two, indexed from 0 to $2^n - 1$. The key operation allowed is a global reindexing of the string using bitwise XOR with a fixed mask $j$. In other words, we...
CF 1553I - Stairs
CF 1553I - Stairs Rating: 3400 Tags: combinatorics, divide and conquer, dp, fft, math Solve time: 6m 11s Verified: no Solution Problem Understanding We are given an array a of length n . It is not a permutation itself but a derived “stability profile” of an unknown permutation of 1..n . For each position i in that hidden permutation, we look at all subarrays that contain i . Among those...
CF 1386C - Joker
CF 1386C - Joker Rating: 2800 Tags: *special, bitmasks, data structures, divide and conquer, dsu Solve time: 2m 24s Verified: no Solution Solution Let $N = pq$ where $p \equiv 3 \pmod 8$ and $q \equiv 7 \pmod 8$. We first prove the claimed identity involving the Jacobi symbol. Recall that the Jacobi symbol is multiplicative and satisfies $$\left(\frac{a}{N}\right) = \left(\frac{a}{p}\right) \left(\frac{a}{q}\right)$$ for any integer $a$. We compute $\left(\frac{-1}{N}\right)$ and...
CF 1425B - Blue and Red of Our Faculty!
CF 1425B - Blue and Red of Our Faculty! Rating: 2600 Tags: divide and conquer, dp Solve time: 8m 31s Verified: no Solution The error is not in the logic of computing the floor. The RuntimeError you are seeing is a **NameError: solve is not defined **. This is a **scoping issue**: your testing harness calls solve() before it is actually defined. Python cannot find a function named solve` in...
CF 1442D - Sum
CF 1442D - Sum Rating: 2800 Tags: data structures, divide and conquer, dp, greedy Solve time: 1m 31s Verified: no Solution Problem Understanding We are given several sequences, each already sorted in non-decreasing order. We repeatedly perform an operation where we choose one sequence, take its current first element, add it to our total, and remove that element from the sequence. We do this exactly k times, and the goal...
CF 1523G - Try Booking
CF 1523G - Try Booking Rating: 3200 Tags: data structures, divide and conquer Solve time: 3m 24s Verified: no Solution Problem Understanding We are given a flat available for n days and m booking requests. Each request is a segment (l_i, r_i) representing consecutive days someone wants to rent. Requests arrive in chronological order. William has a threshold x for the minimum duration he will accept: he only accepts requests...
CF 1553H - XOR and Distance
CF 1553H - XOR and Distance Rating: 2900 Tags: bitmasks, divide and conquer, trees Solve time: 2m 49s Verified: yes Solution Problem Understanding We have a set of distinct integers $a_1,\dots,a_n$, each lying in the range $[0,2^k)$. For every mask $x$, we XOR every array element with $x$, producing the set $${a_1\oplus x,\dots,a_n\oplus x}.$$ Among all pairs in that transformed set, we want the smallest absolute difference. Let that value...
CF 1575L - Longest Array Deconstruction
CF 1575L - Longest Array Deconstruction Rating: 2100 Tags: data structures, divide and conquer, dp, sortings Solve time: 2m 34s Verified: no Solution Problem Understanding We start with an array. We may repeatedly delete arbitrary elements, and after each deletion the remaining elements close up together. For any resulting array, define its score as the number of positions where the value equals its current 1-based index. We want to choose...
CF 1609F - Interesting Sections
CF 1609F - Interesting Sections Rating: 2800 Tags: data structures, divide and conquer, meet-in-the-middle, two pointers Solve time: 1m 40s Verified: no Solution Problem Understanding We are given a long array of non-negative integers. The task is to count how many contiguous subarrays have a specific property that depends on two extreme values inside the subarray: its minimum and its maximum. For any segment, we compute the smallest value and...
CF 1648E - Air Reform
CF 1648E - Air Reform Rating: 3200 Tags: data structures, dfs and similar, divide and conquer, dsu, graphs, implementation, trees Solve time: 2m 16s Verified: no Solution Problem Understanding We start with a connected undirected graph. The graph represents Berlaflot flights, and every edge has a price. The cost of traveling between two cities is unusual. A route may contain many flights, but its cost is not the sum of...
CF 1648D - Serious Business
CF 1648D - Serious Business Rating: 2800 Tags: data structures, divide and conquer, dp, implementation, shortest paths Solve time: 2m 28s Verified: no Solution This problem is rated 3500 and its solution relies on a fairly deep structural characterization of graphs whose cycle space admits a consistent cyclic orientation. Producing a correct editorial requires reconstructing the full proof and construction, not merely explaining a known implementation trick. I do not...
CF 1856D - More Wrong
CF 1856D - More Wrong Rating: 2100 Tags: divide and conquer, interactive Solve time: 1m 33s Verified: no Solution Problem Understanding We are asked to find the position of the maximum element in a hidden permutation of length $n$ by using queries that tell us the number of inversions in a subarray. Each query has a cost that grows quadratically with the subarray length, and we have a global budget...
CF 1987G2 - Spinning Round (Hard Version)
CF 1987G2 - Spinning Round (Hard Version) Rating: 3500 Tags: divide and conquer, dp, trees Solve time: 3m 28s Verified: no Solution Problem Understanding We are given a permutation of integers from 1 to $n$ and a string of instructions of length $n$, where each instruction is either L, R, or ?. Each position in the permutation can either connect to its nearest larger number to the left (L) or...
CF 1987G1 - Spinning Round (Easy Version)
CF 1987G1 - Spinning Round (Easy Version) Rating: 2900 Tags: divide and conquer, dp, trees Solve time: 3m 32s Verified: no Solution Problem Understanding We are given a permutation p of length n and a string s of the same length consisting only of ? . Each position i in the permutation defines two special indices: l_i is the last index before i where the permutation value is larger, and...
CF 1906K - Deck-Building Game
CF 1906K - Deck-Building Game Rating: 2500 Tags: divide and conquer, math Solve time: 3m 22s Verified: yes Solution Problem Understanding Each card can end up in one of three states. A card may be placed in your deck, placed in your friend's deck, or discarded entirely. Let the XOR of your deck be X and the XOR of your friend's deck be Y . We must count the number...
CF 1912B - Blueprint for Seating
CF 1912B - Blueprint for Seating Rating: 2100 Tags: combinatorics, divide and conquer, math Solve time: 2m 42s Verified: no Solution Problem Understanding We are given a row of seats split into contiguous blocks by aisles. Each block is a positive-length segment of seats, and between any two consecutive blocks there is exactly one aisle. If there are $k$ aisles, then there are $k+1$ blocks, and their sizes form a...
CF 1940C - Burenka and Pether
CF 1940C - Burenka and Pether Rating: - Tags: *special, data structures, dfs and similar, divide and conquer, dsu, graphs, sortings, trees Solve time: 1m 11s Verified: yes Solution Problem Understanding We are given a directed structure over positions 1 to n, where each position carries a value. The key restriction is that movement from one position to another is not arbitrary: you are only allowed to move forward in...
CF 2041J - Bottle Arrangement
CF 2041J - Bottle Arrangement Rating: 2700 Tags: data structures, divide and conquer, dp, dsu, greedy Solve time: 2m 2s Verified: no Solution The failure you show is not enough to diagnose the algorithm itself. Let's trace what happened: The input contains 5 test cases : 5 2 2 1 5 2 3 4 5 1 5 2 1 4 2 3 5 4 1 1 5 4 10 4...
CF 2061I - Kevin and Nivek
CF 2061I - Kevin and Nivek Rating: 3500 Tags: divide and conquer, dp Solve time: 1m 48s Verified: no Solution Problem Understanding We are asked to determine the minimum time Kevin must invest to win at least a given number of matches against Nivek, for all possible counts from 0 to $n$. Each match is either Type 1, which has a fixed time cost $a_i$ for Kevin to guarantee a...
CF 2172J - Sliding Tiles
CF 2172J - Sliding Tiles Rating: 2300 Tags: data structures, divide and conquer, dsu Solve time: 1m 46s Verified: no Solution Problem Understanding We are given an $n \times n$ grid with tiles stacked in columns and vertical bars between adjacent columns. Each column $i$ starts with $a_i$ tiles stacked from the bottom. Each bar between column $i$ and $i+1$ has a height $h_i$, which blocks movement of tiles in...
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: 1m 43s Verified: no Solution Problem Understanding We are working with a tree where each pair of vertices defines a path. For any pair of nodes $(u, v)$, we look at the unique simple path connecting them and then imagine removing all edges on that path from...
CF 2222F - Building Tree
CF 2222F - Building Tree Rating: - Tags: data structures, divide and conquer, dsu, graphs, implementation Solve time: 1m 37s Verified: no Solution Problem Understanding We start with a weighted undirected graph on n vertices. The twist is that distance between two nodes is not the usual shortest path sum. Instead, if you take any path and look at the set of edge weights used on that path, the cost...