brain
tamnd's digital brain — notes, problems, research
41650 notes
The traceback already tells us that the current program is not failing because of the algorithm itself. It is failing because the code is reading the input incorrectly and then indexing past the end of the array.
We are given several test cases. In each test case, we start with a list of points placed on a number line. For every point we choose one special position s, and we connect s to every point xi by a segment on the integer line.
We are given two arrays, a and b, both of length n. From these arrays, we are asked to construct a directed graph with n vertices. There is an edge from vertex u to vertex v (for u != v) if the difference a[u] - a[v] is greater than or equal to b[u] - b[v].
The traceback already tells us that the current program is not failing because of the algorithm itself. It is failing because the code is reading the input incorrectly and then indexing past the end of the array.
We are given a number written as a string of digits, and we are allowed to repeatedly apply a specific rounding operation that changes digits at a chosen position and propagates carry to higher positions in a non-standard way.
We are given an array of integers and need to decide whether it can be partitioned into two non-empty groups such that the sums of each group have the same parity-both even or both odd. Each group must contain at least one element.
Petya walks past benches numbered from 1 to n. Some benches contain cookie sellers. Whenever Petya reaches a bench, he eats a cookie if one of three things is true: 1. The bench contains a seller, in which case he immediately buys and eats a cookie. 2. He has never eaten before.
We are given a binary string representing a row of trees, where each position is either an oak or a fir. The quality of a final arrangement depends only on two numbers: the longest consecutive block of zeros and the longest consecutive block of ones.
We are asked to maintain a dynamic array while processing four types of queries: append a number, remove the last $k$ numbers, roll back the last change (append or remove), and report the number of distinct numbers in the array. The array is initially empty.
We are asked to maintain an array under four types of operations: appending an element, removing the last $k$ elements, rolling back the last change (which could be either an append or a removal), and querying the number of distinct elements currently in the array.
We are asked to construct permutations of integers from 1 to $n$ in such a way that, when we compute the greatest common divisor (GCD) between consecutive elements (with wrap-around), the number of distinct GCD values is maximized.
Two players alternate turns removing objects from a shared pool. Each object is a “button” with a restriction: some buttons can only be taken by Anna, some only by Katie, and some are flexible and can be taken by either player.
A closed broken line with six segments is a hexagonal cycle whose six vertices lie on one circle.
We are given two arrays of length $n$, $a$ and $b$. For any contiguous subarray, or segment, defined by its start $l$ and end $r$, we can compute a "cost" using the formula $ The problem is subtle because segment costs depend on both ends of the segment and both arrays.
We are asked to model escape paths in a one-dimensional world where Andrey can use portals repeatedly. Each portal is a segment [li, ri] from which Andrey can enter and teleport to another contained segment [ai, bi].
We are given a tree with n cities connected by roads. Each road has a hardness value wi. The travel time along a road depends on the driver’s skill c and is calculated as ceil(wi / c).
We are given a permutation of the numbers from 1 to n. For a permutation p, define $$text{cost}(p)=sum{i=1}^{n} pi cdot i-max{i=1}^{n}(picdot i).$$ The task is to find the largest possible value of this expression among all permutations of length n.
We are given multiple arrays, each containing at least two positive integers. We are allowed to move at most one number from each array to some other array. After these moves, the “beauty” of the collection is defined as the sum of the smallest element in each array.
We are given an array of integers, and our goal is to split it into two non-empty arrays, b and c, such that no element in c divides any element in b. Each element must go into exactly one array. If this is impossible, we return -1.
We are given a string and a cursor that initially sits between two letters. The cursor can move left or right by one position, and it can also "teleport" between any two adjacent letters x and y if there exists another pair x followed by y elsewhere in the string.
We are given $2n$ tuples of the form $(a, b, c)$, where $a$ and $b$ are positive integers and $c$ is a bracket, either '(' or ')'. Exactly half of the tuples are opening brackets and the other half are closing brackets.
We are given a binary string, and we are allowed to reorder it using swaps between any two positions. The cost of each operation is one swap, and swaps are unrestricted in the sense that we can choose any pair of indices.
We are given a string s consisting only of parentheses, and we need to construct a new string t of length exactly twice that of s, such that t forms a valid bracket sequence and s does not appear anywhere inside t as a contiguous substring.
We are given a permutation and a game played on its positions. The chip starts nowhere. Alice begins by choosing any position and placing the chip there. After that, players alternate moving the chip.
We are asked to determine the minimum number of fancy coins Monocarp must use to pay exactly m burles when he has two types of coins: one worth 1 burle and one worth k burles.
In this problem, we have a card game with multiple players and a dealer. Each card belongs to one of four suits. The dealer has already partially dealt cards to players, and some cards remain in the deck.
We are asked to consider arrays of length n filled with integers from 1 to k. The "cost" of an array is defined as the maximum number of contiguous subarrays of length exactly k where each subarray contains all integers from 1 to k exactly once, and no element participates in…
We are given an array of positive integers, and our task is to transform it into a strictly increasing sequence using the fewest operations.
We are asked to check whether a given sequence of symbols could have been produced by operations on an array that starts empty.
We are given two binary strings of equal length. Both strings always start with a 0 and end with a 1. We are allowed to repeatedly apply an operation that takes a segment inside one string, provided the endpoints of that segment contain the same character, and then forces…
We start with a fixed multiset of digits from 1 to 9, each appearing exactly once, written in a row. The only operation allowed is deleting digits one by one, with the restriction that we are not allowed to delete when only two digits remain in the current sequence.
We are asked to calculate the minimum time Vika, a sorceress, needs to defeat a sequence of monsters. Each monster has a strength, and Vika has two types of magic-water and fire. In one second, she generates fixed amounts of water and fire mana.
Kolya wants to visit a movie theatre over n consecutive days, each day showing a new movie. Each movie has a raw entertainment value a[i], but the enjoyment Kolya actually gains is reduced by how long he has waited since his last visit.
We are given a sequence of fence planks, each with a height, already sorted in non-increasing order. Anya wants to know if the fence is symmetrical when viewed as a grid of unit blocks.
We are given a tree, so between any two vertices there is exactly one simple path. From this tree we want to choose several distinct vertex-sets, where each chosen set must itself be the vertex set of some simple path that contains at least two vertices.
We are dealing with a complete rooted binary tree, meaning every non-leaf node has exactly two children. Each leaf has a “hunger value” representing the number of cookies it must ultimately receive. Non-leaf nodes have a selector that decides which child a cookie moves to.
We are given an array where each position points to a value in the same range as indices. You are allowed to repeatedly pick an index i and swap the value stored at position i with the value stored at position a[i].
We are given an array of integers and a strange operation that repeatedly reduces the array by splitting it into two non-empty parts, computing the XOR of each part, and discarding the part with the smaller XOR value.
We are given a set of quests, each of which can only be completed at a specific hour within a repeating game day. Some quests depend on others, meaning they cannot be completed until certain prior quests are done.
We are given an $n times m$ board partially covered with dominoes. Each domino occupies exactly two adjacent cells, either horizontally or vertically. Some cells may remain empty.
We are given an array of distinct integers ranging from 0 up to n, inclusive. The task is to repeatedly update the array in a very specific way: for each element from left to right, replace it with the MEX (minimum excluded value) of the current array.
A channel has n subscribers. When a new post is published, exactly a subscribers are online, so those a people immediately read the post. After that, we receive a sequence of notifications. A '+' means some subscriber came online, and a '-' means some subscriber went offline.
We are given a permutation of numbers from 1 to n, and we want to transform it into the identity permutation where each value sits in its matching index position.
Let the grid cells be vertices of an $n times n$ grid graph. A final placement of the numbers $1 ldots n^2$ is valid if every vertex except the one containing $1$ has at least one adjacent vertex with a smaller number.
We start with two square boards of size $n times n$, each containing the numbers $1$ to $n^2$ exactly once. Think of each number as a labeled tile. The initial board is some arrangement of these tiles, and we want to transform it into a target arrangement.
We are asked to compute the expected number of moves to reach or exceed a target integer n starting from x = 1 when in each move you either increment x by 1 or double it, each with probability 1/2.
We are given an array of integers a of length n. For each query (l, r), we need to zero out every number in a that lies within the range [l, r] using a set of subtraction operations.
We are given a multiset of integers, and we repeatedly imagine picking two positions independently, forming an ordered pair of values $(a, b)$. Along with $a$ and $b$, both players also learn the bitwise OR $x = a mid b$.
The failure here is not algorithmic at all. The expected logic is correct: the answer is the minimum absolute value in the array. The issue is purely an implementation bug that leads to no output being printed.
We are given a square grid filled with bits, and the goal is to turn every cell into zero. The only allowed move flips values in a very specific geometric pattern: you pick a cell as a “center”, and it toggles itself and all cells strictly below it that lie within a…
The problem gives us three integers: a starting value x, an ending value y, and a length n. We need to build an array a of length n that starts at x and ends at y, is strictly increasing, and has strictly decreasing consecutive differences.
We are given a string and two kinds of moves that let us rearrange its characters in a constrained way. One move swaps characters that are two positions apart, and the other reverses a contiguous block of fixed length $k$.
We are asked to compute the expected number of moves to build a tower of height N using identical small rocks, where each placement may trigger a cascading fall of the top rocks. Every time a rock is placed at height x, the topmost rock has a probability Px/100 of falling.
We are given a weighted tree, so there is exactly one simple path between any two vertices and every edge contributes a distance equal to its weight. The diameter of this tree is the maximum distance between any pair of vertices under these edge weights.
We start with a single stack of $N$ jackets. Each jacket has a color, and the order is fixed from top to bottom. There is a second empty stack.
Each carriage initially contains some number of passengers. A passenger starting in carriage i may move left or right, but cannot cross more than Di doors.
We are given a grid with $N$ rows and $M$ columns. A game piece starts at the top-left cell $(1,1)$. From any cell, the piece can move either to the right within the same row or downward within the same column, but never left or up.
We are given a collection of $N$ sets, each set is formed from integers in the range $1$ to $K$. The sets are unordered internally, but the array of sets is ordered. After we construct these $N$ sets, we are allowed to permute them in any order.
We are maintaining a group of players, each described by two evolving attributes: an offensive value and a defensive value. Over time, both attributes can change independently through updates.
We are managing three elevators inside a tall building. Each elevator sits on a floor and can move up or down, paying a cost proportional to how far it travels.
We are given a list of integers, and we can modify any element by repeatedly incrementing or decrementing it by one. Each such unit change costs one operation.
We are given several rows of numbers, each row representing a sequence of rewards spread across time positions from 1 to M. We will perform exactly M − K + 1 actions, and each action is tied to a sliding window of K consecutive positions that moves from left to right.
We are given a directed acyclic graph where each vertex has an ordered list of outgoing edges. Each edge carries a label, either 0 or 1, and points to another vertex.
We are given two integers, but instead of being written in decimal form, they are described by their prime factorizations. One number, call it $X$, is fully determined by a list of primes and their exponents. The other number $Y$ is described the same way.
We are given an array of hidden integers and a fixed segment length $k$. We are not allowed to directly inspect the array, but we can ask queries on any contiguous block of exactly $k$ elements.
We start with a rooted tree $G$ on $n$ vertices, rooted at vertex $1$. For every vertex $v$, we look at the “subtree of $v$” defined as all vertices whose path from the root passes through $v$.
We are given a binary string and we are allowed to “modify” it using another binary string of the same length. That second string is not arbitrary in its effect, because it is constrained only by how many ones it contains.
We start with an array a of length n filled with zeros, and we want to transform it into a target array b of the same length.
We are given a hidden array of length $n$, where each element is an integer. We do not see the array directly. Instead, we are allowed to ask queries on contiguous segments of fixed length $k$.
We are given a sorted set of distinct integers. We do not control this set directly; instead, we interact with an opponent through moves that change it. Each move we either insert a new number into the set or allow Bob to remove a number under a restriction.
We are given an array a of length n. We must construct a permutation b of numbers from 1 to n such that when we subtract element by element, forming ci = ai - bi, the number of distinct values appearing in c is as large as possible.
We are given an array of integers. In one move, we are forced to pick a segment whose sum is as large as possible among all subarrays of the current array, and decrease every element in that segment by one.
The mismatch is not coming from MEX computation anymore. The segment MEX logic in the previous version is actually fine for this problem scale.
We are given an integer array, and we are allowed to split it into several contiguous blocks. Each block has a sum, and we call these block sums a new array. The constraint is not on individual blocks but on every contiguous group of blocks.
We are given only the degree of each vertex in a graph that is known to be a pseudotree. That means the final graph must be connected and contain exactly one cycle, while having exactly $n$ edges on $n$ vertices.
We are given an array of integers representing how many candies each person initially holds. The process that follows is a sequence of transfers, where each person will act exactly once as a giver and will choose a single recipient.
We are asked to construct an $n times m$ matrix where every row is a permutation of ${0, 1, dots, m-1}$. This already forces a strong structure: each row contains each value exactly once, so the matrix is composed of $n$ rearrangements of the same multiset.
We are given a group of people at a party, each initially holding some number of candies. Each person can give a certain number of candies to another person exactly once, with the amount being a power of two and not exceeding the candies they currently hold.
We are given several test cases, each describing a complete weighted travel system on a plane. Every city is a point with integer coordinates, and we are allowed to fly directly between any pair of cities.
We are working with a weighted directed graph with n vertices and m edges. Each vertex can be “highlighted” or “normal,” starting with all vertices normal.
We are given an array of integers, and we are allowed to carve it into several disjoint contiguous segments, leaving some elements unused if we want. For every chosen segment we compute its MEX, which is the smallest non-negative integer missing from that segment.
We are given an array, and we look at its prefixes one by one. For each prefix, we treat it as a multiset of integers and imagine repeatedly performing a very unusual operation: we pick any non-empty sub-multiset, remove it, compute its MEX, and insert that MEX back.
We are asked to consider the numbers from 1 to $n$ expressed in base $k$. Once expressed in base $k$, we sort these representations lexicographically, like strings. After sorting, we number the elements from 1 to $n$.
We are given a process that builds an array from left to right using prefix operations. Initially every position is zero.
We are given an array a of length n with integers between 1 and k. From a, we define a square table b of size n × n where each cell (i, j) is the minimum of a[i] and a[j]. Each integer in b represents a "color".
We are building an array of length $n$ using non-negative integers, with each element capped at $x$. The array is not arbitrary: it must have a fixed MEX equal to $k$, and among all such valid arrays, we want the one with the largest possible sum of elements.
We are given two arrays, a of length n and b of length m. We are allowed to repeatedly choose any element bj from b and update every element of a as ai = ai The key observations from the input constraints are that n and m can each be up to 200,000 but the sum across all test…
We are given a sequence of positive integers and we are allowed to perform exactly one transformation: pick a contiguous segment, compress it into a single number equal to the product of all elements in that segment, and replace the segment with that single value.
Let
We are given a set of animals, each with a cost and a "fear" relationship: every animal fears exactly one other animal, and no animal fears itself. When selling an animal, the money earned depends on whether the animal it fears has already been sold.
We are standing at room 1 of a one-dimensional corridor and want to go forward to some room k and then return back to room 1. Time increases by exactly one per step, so reaching room x for the first time takes x−1 seconds, and returning follows the same speed.
We are given a static array of integers, but the array is split dynamically into two groups depending on a binary string. Each position i contributes its value a[i] either to group 0 or group 1 depending on whether s[i] is 0 or 1. The system supports two operations.
We are given a permutation of numbers from 1 to n, and we are allowed to arrange these numbers in any order. Once the arrangement is fixed, we evaluate it using a rule that looks only at certain positions. Two sets of positions matter.
We are given multiple independent queries. Each query provides an interval $[l, r]$, and we need to construct two positive integers $a$ and $b$ such that their sum lies somewhere inside this interval and at the same time $a$ and $b$ share a common divisor greater than one.
We are given two containers holding real-valued amounts of water. One starts with a, the other with b. We also have a cup that can transfer at most c units of water in a single operation.
We are given a string composed only of the letters A and B, and we are allowed to perform two types of operations on adjacent character pairs. In the first operation, an AB pair can be turned into BC to gain a coin. In the second, a BA pair can be turned into CB to gain a coin.
We are asked to determine if one player, Valeriu, can indefinitely avoid being caught by another player, Marcel, in a city modeled as a connected graph with exactly $n$ buildings and $n$ roads. Each building is a node, and each road is an undirected edge connecting two nodes.
We are given a row of trees, each tree carrying two attributes: a height and a number of fruits. We want to choose a contiguous segment of these trees.
We are given a row of coral columns, each with some initial height. We want to build a water tank over this structure by choosing a uniform target height $h$.
We are given a binary string representing a strip of cells, where each position is either black or white. The only operation allowed is choosing any contiguous segment of fixed length $k$ and repainting all cells in that segment to white.