brain

tamnd's digital brain — notes, problems, research

41650 notes

CF 1184C1 - Heidi and the Turing Test (Easy)

We are given a small set of points on a grid, where almost all points lie exactly on the border of an axis-aligned square. Only one point is an exception and lies strictly inside the square. The task is to identify that single interior point. The square is not given explicitly.

codeforcescompetitive-programmingimplementation
CF 1184B1 - The Doctor Meets Vader (Easy)

Each spaceship has an attack power. Each empire base has a defense value and a gold amount. A spaceship can destroy every base whose defense is not greater than the spaceship's attack power.

codeforcescompetitive-programmingbinary-searchsortings
CF 1184B3 - The Doctor Meets Vader (Hard)

We are working on a weighted selection problem over two interacting layers. First, there is a small fixed graph of planets, where distances are measured as the shortest number of wormholes between nodes. Then there is a large set of spaceships and bases placed on these planets.

codeforcescompetitive-programmingflowsshortest-paths
CF 1183H - Subsequences (hard version)

We are given a string and we want to construct a collection of distinct strings, where each string must be a subsequence of the original.

codeforcescompetitive-programmingdpstrings
CF 1183G - Candy Box (hard version)

Each candy belongs to a type. For every type, we know how many candies of that type exist, and among those candies how many have f = 1. We want to build a gift by selecting some candies.

codeforcescompetitive-programminggreedyimplementationsortings
CF 1183B - Equalize Prices

We are given several independent scenarios. In each one, there is a list of product prices, and we are allowed to adjust each price by at most k in either direction, but we are not allowed to adjust it more than once.

codeforcescompetitive-programmingmath
CF 1183C - Computer Game

We are simulating a sequence of exactly $n$ game turns. At any moment Vova has a battery charge $k$, and each turn he must choose one of two actions that reduce the charge. One action is cheaper in terms of battery loss but can only be used when the battery is sufficiently high.

codeforcescompetitive-programmingbinary-searchmath
CF 1183F - Topforces Strikes Back

We are given several independent datasets. In each dataset there is a multiset of positive integers, where each integer represents the “value” of a problem.

codeforcescompetitive-programmingbrute-forcemathsortings
CF 1183D - Candy Box (easy version)

We are given several independent candy collections. Each collection is just a multiset of integers, where each integer represents a candy type. From each collection, we want to build a single “gift” by selecting some candies.

codeforcescompetitive-programminggreedysortings
CF 1182B - Plus from Picture

We are given a grid of characters representing a picture, where each cell is either empty or filled. The task is to decide whether the filled cells form exactly one plus-shaped figure. A valid plus shape has a single central filled cell.

codeforcescompetitive-programmingdfs-and-similarimplementationstrings
CF 1182F - Maximum Sine

We are given a function over integers that depends on a scaled angle of a sine wave. For any integer position $x$, we compute an angle proportional to $x$, specifically $frac{p}{q}pi x$, and evaluate the absolute value of its sine.

codeforcescompetitive-programmingbinary-searchdata-structuresnumber-theory
CF 1182E - Product Oriented Recurrence

We are given a sequence defined by a multiplicative recurrence. The first three values are known, and every later value is built from the previous three by multiplying them together and then scaling by a power of a constant.

codeforcescompetitive-programmingdpmathmatricesnumber-theory
CF 1182C - Beautiful Lyrics

We are given a collection of words, and we want to group them into as many valid “lyrics” as possible. Each lyric uses four words arranged as two lines of two words each.

codeforcescompetitive-programmingdata-structuresgreedystrings
CF 1181A - Chunga-Changa

Two people hold some amounts of a currency and can optionally transfer integer amounts between each other before spending. Each unit of currency buys one item in chunks of size z, meaning each person independently converts their money into floor(money / z) items.

codeforcescompetitive-programminggreedymath
CF 1181E2 - A Story of One Country (Hard)

We are given a set of axis-aligned rectangles that represent “castles”. These rectangles do not overlap in area, although they may share boundaries or corners.

codeforcescompetitive-programmingbrute-forcegreedysortings
CF 1181D - Irrigation

We are given a sequence of past assignments where each of $m$ cities has hosted an event some number of times. After these first $n$ years, a deterministic rule takes over: in every future year, the city that has hosted the fewest events so far is chosen, and if multiple…

codeforcescompetitive-programmingbinary-searchdata-structuresimplementationsortingstreestwo-pointers
CF 1181C - Flag

We are given a rectangular grid with n rows and m columns, where each cell already has one of three colors. The goal is to repaint some cells so that the final grid looks like a “flag” pattern composed of three horizontal stripes.

codeforcescompetitive-programmingbrute-forcecombinatoricsdpimplementation
CF 1180B - Nick and Array

We are given an array of integers, and for each position we are allowed to repeatedly apply a transformation that replaces a value $x$ with $-x-1$.

codeforcescompetitive-programminggreedyimplementation
CF 1179E - Alesya and Discrete Math

We are dealing with several functions that behave like monotone step counters on a huge integer line. Each function starts at value zero at position zero, ends at a fixed value L at position 10^18, and can only change in a very restricted way: as we move from x−1 to x, the…

codeforcescompetitive-programmingdivide-and-conquerinteractive
CF 1179D - Fedor Runs for President

We are given a tree with $n$ vertices. Every pair of vertices has exactly one simple path between them. We are allowed to add exactly one extra edge between any two distinct vertices, turning the structure into a graph with one cycle.

codeforcescompetitive-programmingdata-structuresdptrees
CF 1179B - Tolik and His Uncle

We are given an $n times m$ grid, and we start from the top-left cell $(1,1)$. The goal is to produce a complete ordering of all grid cells such that we visit every cell exactly once, starting from $(1,1)$.

codeforcescompetitive-programmingconstructive-algorithms
CF 1178H - Stock Exchange

We are given a collection of $2n$ stocks. Each stock $i$ has a price that evolves over time in a very simple deterministic way: at integer time $t$, its price is $ai cdot t + bi$. Time starts at $t = 0$, and prices only change at integer moments.

codeforcescompetitive-programmingbinary-searchflowsgraphs
CF 1178G - The Awesomest Vertex

We are working with a rooted tree where every node contributes two values, ai and bi. For any node v, consider the path from the root down to v. Along this path we accumulate the sums of a values and b values separately.

codeforcescompetitive-programmingdata-structuresdfs-and-similar
CF 1178F2 - Long Colorful Strip

We are given a long strip of length $m$, initially all painted with color $0$. Then colors $1$ through $n$ are applied one by one. Each color $i$ is applied by choosing a segment $[ai, bi)$ that is currently monochromatic, and repainting it with color $i$.

codeforcescompetitive-programmingdp
CF 1178C - Tiles

We are tiling a rectangular grid of size $w times h$, where each cell must contain one square tile. Each tile is a square split along a diagonal into a black and a white triangular half.

codeforcescompetitive-programmingcombinatoricsgreedymath
CF 1178D - Prime Graph

We are asked to construct a simple undirected graph on exactly $n$ vertices. We are free to choose any edges as long as we avoid self-loops and duplicate edges. The graph must satisfy two number-theoretic constraints at the same time.

codeforcescompetitive-programmingconstructive-algorithmsgreedymathnumber-theory
CF 1178E - Archaeology

We are given a long string formed only from the letters a, b, and c, with the restriction that no two adjacent characters are equal.

codeforcescompetitive-programmingbrute-forceconstructive-algorithmsgreedystrings
CF 1177A - Digits Sequence (Easy Edition)

We are given a single positive integer $k$, and we imagine writing all positive integers in order, starting from 1, directly next to each other without spaces or separators. This produces one continuous digit stream like 12345678910111213... and so on.

codeforcescompetitive-programmingimplementation
CF 1176E - Cover it!

We are given an undirected connected graph for each query, and we must select a subset of vertices such that every vertex we do not select has at least one selected neighbor. In other words, every unchosen vertex must be “covered” by an adjacent chosen vertex.

codeforcescompetitive-programmingdfs-and-similardsugraphsshortest-pathstrees
CF 1176D - Recover it!

We are given an unknown array of integers. Each original value is between 2 and 200000. From this hidden array, a second array is produced by expanding every element into two values and then shuffling everything.

codeforcescompetitive-programmingdfs-and-similargraphsgreedynumber-theorysortings
CF 1176C - Lose it!

We are given a long sequence where every element is guaranteed to be one of six fixed values: 4, 8, 15, 16, 23, 42. The task is not to reorder or modify values, only to delete elements so that what remains can be partitioned into complete ordered chains of length six.

codeforcescompetitive-programmingdpgreedyimplementation
CF 1176B - Merge it!

We are given several independent arrays. In each array, we are allowed to repeatedly take any two elements, remove them, and insert their sum back into the array. Each operation reduces the number of elements by one while preserving the total sum.

codeforcescompetitive-programmingmath
CF 1175D - Array Splitting

We are given a sequence of numbers and we must cut it into exactly $k$ consecutive parts, where each element belongs to exactly one part and parts preserve order. Once the partition is fixed, every element gets a label equal to the index of the segment it lands in.

codeforcescompetitive-programminggreedysortings
CF 1174F - Ehab and the Big Finale

We are given a tree with nodes numbered from 1 to n, rooted at node 1. Inside this tree, there is a hidden node x.

codeforcescompetitive-programmingconstructive-algorithmsdivide-and-conquergraphsimplementationinteractivetrees
CF 1174D - Ehab and the Expected XOR Problem

We are asked to build a sequence of integers, each lying in the range from 1 up to $2^n - 1$, with a very specific restriction on its contiguous segments.

codeforcescompetitive-programmingbitmasksconstructive-algorithms
CF 1174A - Ehab Fails to Be Thanos

We are given a multiset of integers of size exactly twice some number n. The task is to rearrange these values into a new order such that if we split the reordered array into two consecutive halves of length n, the sum of the left half is different from the sum of the right half.

codeforcescompetitive-programmingconstructive-algorithmsgreedysortings
CF 1174C - Ehab and a Special Coloring Problem

We are asked to assign a label to every integer from 2 up to n. These labels are positive integers, and they must satisfy a strong interaction rule: whenever two numbers share no common divisor greater than 1, meaning they are coprime, their assigned labels must be different.

codeforcescompetitive-programmingconstructive-algorithmsnumber-theory
CF 1173B - Nauuo and Chess

We are asked to place numbered pieces from 1 to n onto an m by m grid. Each piece i is placed at a cell with integer coordinates.

codeforcescompetitive-programmingconstructive-algorithmsgreedy
CF 1172C1 - Nauuo and Pictures (easy version)

We are working with a system of pictures where each picture has a weight that controls how likely it is to be shown when we visit a website. At any moment, the probability of seeing picture $i$ is its weight divided by the total weight of all pictures.

codeforcescompetitive-programmingdpprobabilities
CF 1169A - Circle Metro

Two trains move on a circular line with stations labeled from 1 to n. One train moves clockwise, increasing station numbers modulo n, while the other moves counterclockwise, decreasing station numbers modulo n.

codeforcescompetitive-programmingimplementationmath
CF 1168E - Xor Permutations

We are given a multiset-like array of length $2^k$, where every value lies in the same range as indices of permutations of size $2^k$.

codeforcescompetitive-programmingconstructive-algorithmsmath
CF 1168B - Good Triple

We are given a binary string and we look at all possible contiguous substrings. For each substring, we want to know whether it contains a pattern of three equally spaced positions where all three characters are identical.

codeforcescompetitive-programmingbrute-forcetwo-pointers
CF 1168A - Increasing by Modulo

We are given a circular-valued array where each element lies in the range from zero up to some fixed modulus minus one. One operation consists of choosing any subset of positions, and incrementing all chosen values by one with wraparound at the modulus boundary.

codeforcescompetitive-programmingbinary-searchgreedy
CF 1167D - Bicolored RBS

We are given a valid parentheses string. Think of it as a walk that starts at height 0, where every opening bracket increases the height by 1 and every closing bracket decreases it by 1, and the walk never goes negative and ends at 0.

codeforcescompetitive-programmingconstructive-algorithmsgreedy
CF 1167A - Telephone Number

We are given a string of digits and we are allowed to delete characters freely, without changing the relative order of the remaining digits. The task is to decide whether we can extract a subsequence of length exactly 11 that forms a valid telephone number.

codeforcescompetitive-programmingbrute-forcegreedystrings
CF 1166F - Vicky's Delivery Service

The task describes a growing undirected graph where each edge also carries a color. Edges only ever get added, never removed. Interleaved with these updates are connectivity queries, but connectivity is not defined in the usual sense of arbitrary walks.

codeforcescompetitive-programmingdata-structuresdsugraphshashing
CF 1166D - Cute Sequences

We are given a starting value a, an ending value b, and a parameter m. We need to decide whether we can construct a sequence of positive integers that begins at a, ends at b, and grows in a very specific cumulative way.

codeforcescompetitive-programmingbinary-searchbrute-forcegreedymath
CF 1166A - Silent Classroom

We are given a list of student names and we are allowed to split these students into two classrooms. Every student must go to exactly one of the two rooms, and either room may end up empty.

codeforcescompetitive-programmingcombinatoricsgreedy
CF 1165F2 - Microtransactions (hard version)

Ivan needs to buy a fixed multiset of items, split into types, where each type requires a certain number of copies. Each day he earns exactly one unit of currency, and he can spend it immediately to buy items in the evening of the same day or save it for later.

codeforcescompetitive-programmingbinary-searchgreedyimplementation
CF 1165D - Almost All Divisors

We are given a multiset that is supposed to represent almost all divisors of some unknown integer $x$. “Almost all” here has a precise meaning: the list contains every divisor of $x$ except $1$ and $x$ itself.

codeforcescompetitive-programmingmathnumber-theory
CF 1165F1 - Microtransactions (easy version)

Ivan needs to acquire a collection of items, where each item belongs to one of several types. For each type, he must buy an exact required quantity.

codeforcescompetitive-programmingbinary-searchgreedy
CF 1165B - Polycarp Training

We are given a list of contests, each with a certain number of problems. Polycarp trains day by day, and on day $k$, he must pick exactly one unused contest and solve exactly $k$ problems from it.

codeforcescompetitive-programmingdata-structuresgreedysortings
CF 1163F - Indecisive Taxi Fee

We are given a weighted undirected graph with up to 200,000 vertices and edges. Each edge has a fixed weight, and we care about the shortest path from node 1 to node n using the sum of edge weights. The twist is that we are not solving just one shortest path problem.

codeforcescompetitive-programmingdata-structuresgraphsshortest-paths
CF 1163A - Eating Soup

We start with a circle of $n$ cats sitting in order, forming a single continuous ring of occupied positions. Then $m$ of these cats leave. After each departure, the circle is no longer fully connected, because empty positions break adjacency.

codeforcescompetitive-programminggreedymath
CF 1163C1 - Power Transmission (Easy Edition)

We are given a set of points on a 2D plane. Each point represents a power pole, and every pair of poles defines a straight infinite line, which we interpret as a “wire”.

codeforcescompetitive-programmingbrute-forcegeometry
CF 1162B - Double Matrix

We are given two grids of the same size. Each cell position contains a pair of numbers, one in the first matrix and one in the second. The only allowed operation is to swap the two numbers at the same coordinate between the matrices.

codeforcescompetitive-programmingbrute-forcegreedy
CF 1159A - A pile of stones

We are given a sequence of operations applied to a single pile of stones. Each operation is either adding one stone or removing one stone.

codeforcescompetitive-programmingimplementationmath
CF 1106D - Lunar New Year and a Wander

We are given an undirected connected graph with $n$ nodes. Bob starts at node 1 and performs a walk along edges. Every time he enters a node that has never been seen before, he writes it down.

codeforcescompetitive-programmingdata-structuresdfs-and-similargraphsgreedyshortest-paths
CF 1106A - Lunar New Year and Cross Counting

We are given a square grid filled with two possible characters, X and .. The task is to scan every interior cell of this grid and decide whether it is the center of a “cross” pattern formed by diagonals.

codeforcescompetitive-programmingimplementation
CF 1105B - Zuhair and Strings

We are given a string and a fixed window size $k$. The task is to repeatedly carve out several disjoint blocks of length $k$ from the string. Each chosen block must consist of the same character repeated $k$ times, and all chosen blocks must use the same character.

codeforcescompetitive-programmingbrute-forceimplementationstrings
CF 1105D - Kilani and the Game

We are given a rectangular grid where each cell is either blocked, empty, or already owned by one of up to nine players.

codeforcescompetitive-programmingdfs-and-similargraphsimplementationshortest-paths
CF 1103A - Grid game

We are working on a fixed 4 by 4 board that starts empty. A stream of dominoes arrives one by one, and each domino must be placed immediately when it comes. Each domino is either vertical, covering two cells in the same column, or horizontal, covering two cells in the same row.

codeforcescompetitive-programmingconstructive-algorithmsimplementation
CF 1102E - Monotonic Renumeration

We are given a sequence of integers, and we want to assign another sequence of integers of the same length, starting from zero, with very specific structure rules.

codeforcescompetitive-programmingcombinatoricssortings
CF 1102B - Array K-Coloring

We are given a sequence of integers and asked to assign each position one of k labels, which we will call colors. Every position must receive exactly one color. At the same time, every color from 1 to k must appear at least once among all positions.

codeforcescompetitive-programminggreedysortings
CF 1102D - Balanced Ternary String

We are given a string of length $n$, where each position holds one of three symbols: 0, 1, or 2. We are allowed to modify characters, but each modification counts as a replacement cost of 1 per position changed.

codeforcescompetitive-programminggreedystrings
CF 1102C - Doors Breaking and Repairing

We are given a collection of doors, each with an initial durability value. You and an opponent alternate moves for an extremely large number of turns, so the process effectively runs until both players’ optimal strategies stabilize.

codeforcescompetitive-programminggames
CF 1101G - (Zero XOR Subset)-less

We are given a sequence of integers, and we want to cut it into consecutive chunks. Every element must belong to exactly one chunk, and every chunk must be non-empty. After splitting, we look at all possible non-empty collections of these chunks.

codeforcescompetitive-programmingmathmatrices
CF 1101E - Polycarp's New Job

We are processing a growing collection of rectangular banknotes. Each time a note is added, it stays forever. Later we receive queries asking whether all notes seen so far can be placed inside a given rectangular wallet.

codeforcescompetitive-programmingimplementation
CF 1101C - Division and Union

We are given several independent test cases. Each test case provides a collection of closed segments on a number line. The task is to assign every segment to one of two groups such that no segment in one group overlaps with any segment in the other group.

codeforcescompetitive-programmingsortings
CF 1101A - Minimum Integer

Each query describes a forbidden interval on the number line and a fixed step size. We are asked to find the smallest positive integer that is a multiple of a given number $d$, but lies strictly outside the interval $[l, r]$.

codeforcescompetitive-programmingmath
CF 1101D - GCD Counting

We are given a tree where each node carries an integer label. For any two vertices, consider the unique simple path between them. Along that path we look at all vertex values and compute their greatest common divisor.

codeforcescompetitive-programmingdata-structuresdfs-and-similardpnumber-theorytrees
CF 1100A - Roman and Browser

We are given a row of browser tabs indexed from 1 to n. Each tab is either useful for an exam or represents a social media site. This is encoded as an array of length n where each value is either 1 or -1. Roman chooses a step size k. After that, he selects a starting tab b.

codeforcescompetitive-programmingimplementation
CF 1100F - Ivan and Burgers

We are given an array of values on a line, where each position represents a burger shop and the value at that position is the cost of the most expensive burger available there.

codeforcescompetitive-programmingdata-structuresdivide-and-conquergreedymath
binary searchmath
data structuresimplementationmathtwo pointers
constructive algorithmsdata structuresgraphsgreedyimplementationmathtwo pointers
greedy
dfs and similardsugraphs
greedytwo pointers
chinese remainder theoremmath
implementation
constructive algorithmsflowsgraph matchings
dpgraphsmathmatricesmeet-in-the-middle
dpgreedysortings
greedy
CF 1097D - Makoto and a Blackboard

We start with a single integer placed on a board. At each step, this number is replaced by one of its divisors, chosen uniformly at random.

codeforcescompetitive-programmingdpmathnumber-theoryprobabilities
CF 1097B - Petr and a Combination Lock

We are given a circular lock that behaves like a 360-degree dial. Starting from zero, we perform a sequence of rotations, each rotation having a fixed magnitude, but we are free to choose its direction: clockwise or counterclockwise.

codeforcescompetitive-programmingbitmasksbrute-forcedp
CF 1096G - Lucky Tickets

We are building digit strings of fixed even length using a restricted alphabet of digits. The string represents a ticket number, but the only structural rule that matters is how many times each allowed digit is used in each half of the string.

codeforcescompetitive-programmingdivide-and-conquerdpfft
CF 1096E - The Top Scorer

We are given a final score distribution across $p$ players whose scores are non-negative integers summing to $s$. Hasan is player 1, and his score is only partially constrained: it must be at least $r$, but otherwise unknown.

codeforcescompetitive-programmingcombinatoricsdpmathprobabilities
CF 1096D - Easy Problem

We are given a string and an integer weight attached to every character position. We are allowed to delete characters, and each deletion costs the weight of the deleted position as it originally appears in the string.

codeforcescompetitive-programmingdp
CF 1096C - Polygon for the Angle

We are working with a regular polygon, meaning all sides and angles are symmetric. The input gives an angle value, and we must decide whether there exists some regular n-gon such that we can pick three vertices $a, b, c$ (not necessarily consecutive) and the angle formed at…

codeforcescompetitive-programmingbrute-forcegeometry
CF 1095A - Repeating Cipher

We are given a string that was produced by a very specific “stretched repetition” rule applied to some hidden original string. The original string is short, with length at most 10, but we do not know it.

codeforcescompetitive-programmingimplementation
CF 1095E - Almost Regular Bracket Sequence

We are given a binary string of parentheses. Each character is either an opening bracket or a closing bracket. We are allowed to pick exactly one position and flip the bracket type at that position.

codeforcescompetitive-programmingimplementation
CF 1095C - Powers Of Two

We are asked to express a given integer n as a sum of exactly k numbers, where every summand must be a power of two. Each chosen number is therefore of the form 1, 2, 4, 8, ..., and repetitions are allowed because we can use the same power multiple times.

codeforcescompetitive-programmingbitmasksgreedy
CF 1095D - Circular Dance

We are given a hidden circular ordering of the numbers from 1 to n. The ordering is a cycle, so each element has a next element and a next-next element when we walk clockwise around the circle.

codeforcescompetitive-programmingimplementation
CF 1093F - Vasya and Array

We are given a partially specified array of length $n$. Each position either already contains a fixed value between $1$ and $k$, or is unknown and marked as $-1$. We must replace every unknown position with a value from $1$ to $k$, producing a fully filled array.

codeforcescompetitive-programmingdp
CF 1093D - Beautiful Graph

We are given an undirected graph and asked to assign each vertex one value from the set {1, 2, 3}. The assignment is valid only if every edge connects two vertices whose values sum to an odd number. The task is to count how many such assignments exist, taken modulo 998244353.

codeforcescompetitive-programmingdfs-and-similargraphs
CF 1092F - Tree with Maximum Cost

We are given a weighted tree, where each node carries a positive value. We are allowed to choose any node as a reference point, and for that choice we compute a score defined as the sum over all nodes of their value multiplied by their distance to the chosen node.

codeforcescompetitive-programmingdfs-and-similardptrees
CF 1092E - Minimal Diameter Forest

We are given a graph that is already a forest, meaning it consists of several disconnected trees. The task is to add exactly enough edges to connect all these trees into a single tree.

codeforcescompetitive-programmingconstructive-algorithmsdfs-and-similargreedytrees
CF 1092A - Uniform String

We are asked to construct a string for each query such that the string has a fixed length n and uses only the first k lowercase Latin letters, from 'a' up to the k-th letter. Every one of these k letters must appear at least once in the final string.

codeforcescompetitive-programmingimplementation