brain
tamnd's digital brain — notes, problems, research
41650 notes
We are given a set of points on a line, each point representing the center of a disk we must draw. Every disk has a positive radius, and no two disks can overlap, though touching at the edges is allowed.
We are asked to take an integer $n$ and split it into exactly $k$ non-negative integers whose bitwise XOR equals $n$. Among all such splits, we want one that maximizes the sum of the numbers. Each number in the split must be between 0 and $n$ inclusive.
We are given a circular prize wheel with l numbered sections from 0 to l-1. The wheel pointer starts at a section a. Each spin moves the pointer forward by exactly b positions modulo l.
We are given a sequence of strings, and we must insert them one by one into an initially empty string. Each new string can be placed either at the left end or the right end of the current result. After all insertions, we obtain a single final string.
We are given two binary strings, which we can think of as observed measurements from a medical test. The first string represents the recorded presence or absence of reactions to several allergens.
We are given several token sequences, each sequence representing a text where some positions are produced by a language model and others are written directly by a user. Only the positions marked as generated contribute to the training loss.
The canvas is a rectangular grid of cells, but the real structure lives on its grid graph: vertices are grid intersection points and edges connect adjacent intersections horizontally or vertically. Initially, some collection of simple cycles is drawn along these edges.
We have two sets of points on a Cartesian plane: pumps scattered across a valley and reservoirs positioned on surrounding hills.
We are given a casino machine whose behaviour is determined by one hidden probability value. There is an array of probabilities, and the casino owner secretly picks one index uniformly at random at the start and fixes it forever.
We are given a rectangular cake with dimensions $w times h times d$. The goal is to divide this solid into exactly $n$ smaller rectangular pieces, all identical in size. The only allowed operations are cuts that are parallel to the faces of the cake.
Each round produces a choice of an integer at every position on a cycle. After the numbers are chosen, we look at each adjacent pair on the cycle and award one point to both endpoints whenever the left endpoint is strictly larger than the right endpoint.
We have a skyscraper with floors numbered from 1 up to very large numbers, and several people waiting for an elevator on different floors. Each person has a distinct starting floor and a distinct destination floor, and no two floors overlap.
We are asked to analyze a variation of the classical game of Nim. In the original Nim, players take turns picking a pile and removing any number of stones. The player who takes the last stone wins.
Each layer of the doorway is a one-dimensional segment between two fixed walls. Inside that segment, there are several rigid door blocks placed in a line, but each block is allowed to slide left or right as long as it does not overlap other blocks or cross the walls.
We are asked to calculate how many full sets of street signs can be made when one street's order is missing. Each street in Alphabet City has a name made from capital letters, and each street has an order for m identical signs.
We are asked to simulate a turn-based game between Alice and Bob. Each player starts with an array of positive integers. On their turn, a player picks any element from their own array and applies it against the maximal element in the opponent's array.
We are dealing with a hidden connected graph that has a very restricted structure: it is a cactus, meaning every edge participates in at most one simple cycle.
We are given a rooted tree with vertex 1 fixed as the root. A sequence of n “placements” must be assigned, one for each vertex of the tree, but the twist is that the placement order is not arbitrary. The first placement is forced onto the root.
We have a dynamic herd of reindeer, each with strength $2^{ci}$, and we need to answer three types of queries: adding a reindeer, removing a reindeer, or counting how many subsets of the herd meet a certain carrying capacity threshold.
Monocarp wants to make as many friends happy as possible with a limited budget. He has a number of friends and a set of boxes.
We maintain a multiset of reindeer. Each reindeer is identified by a power level, but the real value that matters is a power of two, $2^{ci}$.
We are given several independent scenarios. In each scenario, there are $n$ people and $n+1$ boxes of decorations. Box $0$ is special, and each person $i$ has their own personal box $i$. Each box starts with some number of decorations.
We are given a string composed of the characters 0, 2, 5, and 6. A string is considered a New Year string if it either contains the substring 2026 somewhere or does not contain the substring 2025 at all.
We are asked to help Monocarp bake a layered cake under two constraints: the size of each layer grows exponentially and the chocolate covering each layer must alternate between white and dark. The top layer always has size 1.
We are given three circular sequences of equal length, each representing sizes of snowballs on a conveyor belt. From each conveyor we choose a starting position, and then we simultaneously walk forward step by step around all three cycles.
We are given a binary string where each position contains either a 0 or a 1. We are allowed a fixed number of operations, exactly one for each value from 1 up to half the length of the string.
We are working with a binary string where each position holds either a 0 or a 1, and we are allowed to repeatedly apply a very specific kind of operation. Each operation is indexed from 1 upward, and on operation number x we may choose a starting position l.
The interactive version allows us to ask up to three carefully chosen instructions and observe the surviving snakes. In the hacked version, the speeds are already given in the input, and our job is to reproduce the answer that an optimal interactive solution would output.
We are given an array of integers and asked to divide it into exactly $k$ non-empty subsequences. For any subsequence $b$, its cost is defined as the product of its length and the sum of its elements. Our goal is to minimize the total cost across all $k$ subsequences.
We are given a rooted tree with each node labeled by a lowercase letter. For each node $i$, we are asked to analyze all strings that can be formed by starting at any node in the subtree of $i$ and repeatedly jumping to a proper descendant, concatenating the letters along the…
We are given a sequence of length $n$ where some positions are fixed numbers between $1$ and $m$, and some positions are zero placeholders that must be filled. After filling all zeros with values in $[1, m]$, the resulting sequence must satisfy two conditions at the same time.
We are given a rooted tree, where all nodes are initially white, and we need to color all nodes black in the minimum number of operations.
We are given a rooted tree with n vertices, where the root is vertex 1, and initially all vertices are white. The distance di of a vertex i is the number of edges on the path from the root to i.
We are given an array of non-negative integers and a number $k$. The task is to repeatedly remove elements from the array, always choosing a window of length $k$ whose MEX is maximal, and deleting any element within that window.
We have a linear arrangement of bases numbered from 1 to n. Our home base is at position k, and we start with exactly one soldier there. Every day, we can select a base and move any number of soldiers from it either left or right by exactly one position.
We are given a binary array that evolves through a merging game. Two players, Alice and Bob, alternate turns. On each turn, a player selects a contiguous segment of length at least two, removes it, and replaces it with a single value derived from that segment.
We have a rooted tree whose leaves each contain exactly one cherry. A shake performed at vertex v makes the cherries fall from every leaf inside the subtree of v. Once a cherry has fallen, it must never be affected by another shake.
We are given an array of integers placed on a line, and we must support two operations: point updates and range queries. The interesting part is not the update itself, but how a special property behaves over a segment.
Working
We are given a permutation of length $n$, which is simply an array of integers from $1$ to $n$ in some order without repeats.
Vadim has an hourglass that measures exactly s minutes. He flips it initially, and after every k minutes, he flips it again, regardless of whether all the sand has fallen. If the sand finishes before the next flip, he waits until the scheduled flip time.
We have $n$ people. They must be partitioned into teams, where every team has either 2 or 3 members. After the teams are formed, each team independently chooses one of two civilizations.
Andrei starts with a single pile containing $n$ apples. He can split any pile of size $x$ into two smaller piles in exactly one minute, creating one pile of $lfloor x/2 rfloor$ apples and another of $lceil x/2 rceil$ apples.
The tournament has a surprisingly simple structure once we stop thinking about individual matches. Whenever a cow survives a match, its new skill becomes the sum of the two participants.
We are given multiple arrays and are allowed to perform a single operation: choose an element from one array, move it to the end of another array, and then compute the sum of MEXes of all arrays after this move. The task is to sum the values of all possible such operations.
The tournament can be viewed as a complete binary tree over the array of cows. Each leaf is a single cow. Every internal node represents a contiguous segment of cows. The skill of a node is the XOR of all values inside that segment.
Every robot receives exactly the same sequence of moves. After the first instruction, all robots shift by the same displacement. After the second instruction, all robots shift by another common displacement, and so on.
We start with an array and a sequence of update operations. Each operation adds some value to one position. The unusual part is the crash rule. After every update, if any element of the current array becomes larger than h, the computer immediately crashes.
We are given an array of integers, which may include negative numbers. The task allows us to pick a single integer shift x and add it to every element of the array. After performing this shift, we want to maximize the MEX of the array.
We are asked to generate a sequence of distinct "perfect roots" for each test case. A perfect root is simply an integer $x$ such that $x^2$ is also an integer, which is trivially true for all positive integers.
We are given an array. For every position, we look at the prefix ending there and take the maximum value seen so far. The score of the array is the sum of all those prefix maxima. We may swap any two elements at most once, or choose not to swap at all.
We are asked to reconstruct a permutation $q$ of length $n$ from a special encoding of Cartesian trees derived from a series of arrays.
We are given two DFS preorder traversals of the same set of vertices {0,1,...,n}. The tree itself is unknown. The only thing we know is that in both traversals the root 0 appears first, and for every vertex the order of its children during DFS is arbitrary.
I can write the editorial, but only if I can first reconstruct the solution completely and verify the algorithm. For this particular problem, the official editorial for F2 is unusually terse and omits many crucial details.
We have a sequence of $n+1$ rooms connected by $n$ doors. Each door $i$ has a number $ai$ indicating the exact second it will automatically open, if untouched. Some rooms contain keys at the start according to a binary string $s$, where a 1 means a key exists in that room.
We are given an array of integers, and we are asked to determine the largest integer $k$ such that we can sort the array in non-descending order by swapping any two elements whose difference is at least $k$.
We are asked to analyze a two-player game on a directed graph with $n$ vertices. The graph is almost a chain: for every vertex $u$ from 1 to $n-1$, there is an edge $u to u+1$.
We are given a row of seats represented as a binary string, where 1 indicates a student is sitting there and 0 indicates an empty seat. No two students are adjacent initially.
We have a tree. Vertex i initially contains ai nuts. There are two kinds of electricity costs. The first cost comes from a special redistribution operation. We choose a vertex v. Then every other vertex is processed from larger distance to smaller distance from v.
We are not asked to count permutations directly. The real task is to understand which binary strings $w$ are possible, what value $f(w)$ they produce, and then optimize over all replacements of the question marks.
We are given a binary string s of length n. Our goal is to transform this string into a single character "1" using a special operation: we can take any contiguous substring, and replace it with the character that occurs at least as many times as the other character in that…
We are given a string of length $n$ consisting of 0s and 1s. For a permutation $p$ of $[0, 1, dots, n-1]$, the function $f(w)$ counts the number of permutations such that, for each position $i$, the minimum excluded value (MEX) condition is satisfied: if $wi = 1$, there exists…
We have an array of numbers. We may select any even number of elements from it and partition them into ordered pairs. A pair (x, y) contributes 1 to the table only if row x and column y exist.
We need to construct a permutation of the numbers from 1 to n. For every position i with 2 ≤ i ≤ n-1, there must exist some position j satisfying i ≤ j ≤ n such that $$pi = pj oplus i.$$ Since we are free to choose the entire permutation, this is a constructive problem.
We are asked to help a frog move along an infinite number line from position 0 to a target position $x$. The frog has $n$ types of magical jumps.
I can't reliably write a complete accepted editorial and proof for Codeforces 2190G from the problem statement alone.
We start with a forest. We may add edges between its connected components until the whole graph becomes a single tree. For every resulting tree, run the standard Prüfer-code deletion process: repeatedly remove the smallest numbered leaf until only two vertices remain.
Instead of thinking about medians directly, it is much more useful to think about the permutation being revealed in increasing value order. Suppose we process the values of a permutation from 1 to n. When value x is processed, its position becomes "active".
The official editorial for this problem is exceptionally long and derives a fairly intricate bit-DP from several structural lemmas about XOR products of intervals.
We are asked to interact with a hidden permutation p of size n and produce a permutation q that is lexicographically just larger than p and also satisfies the property that reversing q gives a permutation larger than the reverse of p. The catch is that we never see p directly.
We are asked to compute a sum over all non-empty subsequences of a given bracket sequence. Each subsequence has a “score” defined as the length of its longest regular bracket subsequence that is strictly “better” than itself.
We are given a regular bracket sequence s. A regular bracket sequence (RBS) is one where every prefix contains at least as many '(' as ')', and the total numbers of opening and closing brackets are equal.
We are asked to analyze a two-player game on a binary string, where Alice goes first and Bob follows, taking turns. On their turn, a player may select any strictly non-increasing subsequence of the string and rearrange it to be non-decreasing.
We are asked to color a sequence of distinct integers arranged in a row using two colors so that two conditions hold simultaneously. First, any two adjacent numbers in the original sequence must have different colors.
Each position i contains a pair of values (ai, bi). For that position we have exactly two choices. If we do nothing, the value ai stays in array a and bi stays in array b. If we swap that position, the value bi moves to array a and ai moves to array b.
We are given a tree rooted at node 1, where each node carries a positive integer weight. The cost of the tree, relative to a chosen root, is the sum over all nodes of the product of the node's weight and its distance from that root.
I can write a full editorial only if I first reconstruct the actual solution in detail. For this problem, the official editorial contains a fairly intricate two-phase DP with interval states, probability propagation, prefix/suffix aggregation, and a second DP once the two…
We are given a binary string, which is a sequence of 0s and 1s, and we can perform a very specific operation: pick one index i, and flip every bit in the string except the bit at index i. Flipping means changing 0 to 1 and 1 to 0.
We are given a shooter game scenario where we have a gun with a magazine of n bullets. Each bullet has a fixed damage ai, and bullets are fired in sequence, one per second.
We are given a string consisting of lowercase letters and asked to determine how to rotate it to maximize the number of contiguous blocks of identical letters. A block is defined as a maximal substring of consecutive identical characters.
We are given a tree, which is a connected acyclic graph with $n$ vertices, and each vertex has an associated integer value. The operation we can perform repeatedly is to remove a vertex if the sum of the values of its remaining neighbors differs in parity from its own value.
We are given a tree, which is an undirected, connected, acyclic graph of $n$ vertices. Two hidden vertices, $x$ and $y$, define a unique path because trees have exactly one simple path between any pair of vertices.
The courier starts at $(Ax, Ay)$, must visit every house, and finally reach $(Bx, By)$. The movement rules are unusual. He may move one unit to the right, one unit up, or one unit down. He can never decrease his $x$-coordinate.
We are given an array whose values lie between 1 and n. An element may be reused any number of times, so the only thing that matters is which values are present in the array, not how many times they occur.
We are given a sequence of swords, each having a strength value, and a sequence of monsters, each requiring a certain number of strikes to be defeated. Each sword can be used at most once, because after striking a monster it breaks immediately.
We start with two arrays of the same length. For any position, we may directly overwrite a[i] with b[i]. We may also copy values from right to left by replacing a[i] with the current value of a[i+1]. Each query asks for the largest possible sum on a subarray a[l..
We are given a permutation of numbers from 1 to n, which means every number in this range appears exactly once. We are allowed to choose a single continuous segment of this permutation and reverse it.
We are given an array of integers and a fixed increment value. In one move, we can pick any position in the array and increase that single element by exactly the same amount each time. We can repeat this operation as many times as we want on any indices.
We are asked to count certain ways to cut a tree into connected components with a specific property. The tree has n vertices, each with a value av. We are also given a small set of numbers b1, ..., bk.
We have a rectangular grid. Every cell contains an integer value, positive or negative. Michelangelo chooses a path from the top-left corner to the bottom-right corner. He may only move right or down.
We are given a grid of size $n times m$ filled with zeros and ones. We need to make a cut from the top-left corner to the bottom-right corner, moving only right or down.
We are given a tree with n vertices, where each vertex has a non-negative integer av written on it. We are also given a set of k distinct non-negative integers b1, ..., bk.
We are given several independent scenarios. In each scenario, there are multiple bank accounts, each holding some amount of money.
We are given multiple strips of paper, each of the same length, containing lowercase letters. Each column across the strips represents a choice: the letter at that column in the decrypted message must come from one of the letters in that column of the strips.
We are given a fence made of $n$ unit-width boards arranged in a line. We are allowed to remove any subset of these boards, but doing so creates gaps in the fence. The only constraint is that the removed boards must not form a continuous block of length $w$ or more.
We are given a square grid of points with coordinates from 1 to 3n along both axes, forming a total of $3n times 3n$ points. The task is to select the largest possible set of triangles such that each triangle uses three distinct points from the grid, has an area of exactly 0.
We are given a rooted binary tree where every vertex either has exactly two children or is a leaf. The root is vertex 0, and all other vertices are numbered from 1 to n. A process starts from some vertex v and moves step by step according to a very specific stateful rule.
We are given a set of quadratic functions, each defined by coefficients $ai$, $bi$, and $ci$. Two functions are called independent if they never intersect, which algebraically means their difference is never zero.
We are given a rooted binary tree where vertex 0 is the root and every other vertex is either a leaf or has exactly two children, left and right. The vertices are numbered, and the structure is fully specified by child pointers.
We are given a hidden sequence of integers $a1, a2, dots, an$ and, instead of the sequence itself, we are provided with a function evaluated at each index: $f(x) = sum{i=1}^n ai cdot Given that $n$ can be as large as 300,000 and the total sum over all test cases is also…