#competitive-programming
CF 2222D - Permutation Construction
CF 2222D - Permutation Construction Rating: - Tags: constructive algorithms, data structures, sortings Solve time: 4m 9s Verified: no Solution Problem Understanding We are given an array $a$ of length $n$. We must construct a permutation $p$ of indices $1$ to $n$. For any pair of positions $i<j$, the pair contributes to the score only if it is an inversion in $p$, meaning $p_i>p_j$. Each such inversion contributes the interval...
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 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 2222B - Artistic Balance Tree
CF 2222B - Artistic Balance Tree Rating: - Tags: greedy, sortings Solve time: 3m 53s Verified: no Solution Problem Understanding We have an array, and before each marking operation we are allowed to reverse any odd-length segment centered at some position. After that reversal, the element currently sitting at index x_i becomes marked. The subtle detail is that marks belong to elements, not positions. If an element gets marked and...
CF 2222C - Median Partition
CF 2222C - Median Partition Rating: - Tags: dp, math Solve time: 2m 36s Verified: no Solution Setup The simulator maintains a global simulated time variable $CLOCK$ and executes each MIX instruction by dispatching to a routine that models its effect on registers, memory, and timing. The I/O instructions considered here are restricted to devices $16$ and $18$. Device $16$ represents the read-card unit, and device $18$ represents the printer...
CF 2222A - A Wonderful Contest
CF 2222A - A Wonderful Contest Rating: - Tags: brute force, dp, math Solve time: 4m 29s Verified: no Solution Problem Understanding Each problem in the contest is worth at most 100 points. If a problem has $a_i$ subtasks, then every solved subtask contributes $$\frac{100}{a_i}$$ points, and $a_i$ is guaranteed to divide 100. This means every problem contributes one of the values $$0,\ \frac{100}{a_i},\ 2\cdot\frac{100}{a_i},\ \ldots,\ 100.$$ For a contestant,...
CF 1949J - Amanda the Amoeba
CF 1949J - Amanda the Amoeba Rating: 2600 Tags: graphs, implementation, trees, two pointers Solve time: 1m 3s Verified: no Solution Problem Understanding We are asked to guide Amanda the Amoeba from an initial configuration to a target configuration on a rectangular grid. Each configuration marks Amanda's body with * , free pixels with . , and blocked pixels with X . Her body is connected and contains at least...
CF 1949K - Make Triangle
CF 1949K - Make Triangle Rating: 2800 Tags: constructive algorithms, math Solve time: 2m 29s Verified: yes Solution Problem Understanding We are given a multiset of positive integers and three required group sizes. Every number must belong to exactly one of the three groups, and each group must contain exactly the requested number of elements. After splitting the numbers, we look only at the three group sums. If those sums...
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 1949F - Dating
CF 1949F - Dating Rating: 2200 Tags: greedy, sortings, trees Solve time: 1m 57s Verified: no Solution Problem Understanding Each user can be viewed as a set of activities. We need to find two users whose sets satisfy three conditions simultaneously: They share at least one activity. The first user has at least one activity that the second user does not have. The second user has at least one activity...
CF 1949G - Scooter
CF 1949G - Scooter Rating: 2300 Tags: graphs, greedy Solve time: 3m 15s Verified: no Solution Problem Understanding Each building has two independent pieces of information. The first string describes what class is held there. A building may need a mathematics professor, a computer science professor, or no professor at all. The second string describes which professor is initially located there. A building may contain a mathematics professor, a computer...
CF 1949A - Grove
CF 1949A - Grove Rating: 3300 Tags: brute force, dfs and similar, dp, geometry, probabilities Solve time: 2m 41s Verified: yes Solution Problem Understanding Every tree is planted at an integer lattice point. Around that point we place a disk of radius r , representing the root system. Two conditions must hold. The entire disk must stay inside the square lawn. Since the lawn is the square [0,n] × [0,n]...
CF 1949E - Damage per Second
CF 1949E - Damage per Second Rating: 2900 Tags: brute force, math Solve time: 1m 7s Verified: no Solution Problem Understanding We are asked to distribute a fixed number of skill points between two attributes: damage per hit and hits per second, in order to minimize the total time to kill a sequence of monsters. Each monster has a health value $h_i$, and the time to kill it is determined...
CF 1949H - Division Avoidance
CF 1949H - Division Avoidance Rating: 3100 Tags: greedy, math Solve time: 5m 53s Verified: no Solution Problem Understanding The process starts with a single cell at (0, 0) . Whenever we divide a cell (x, y) , that cell disappears and produces (x + 1, y) and (x, y + 1) . A division is only legal if neither child is currently present. We are given a finite set...
CF 1949C - Annual Ants' Gathering
CF 1949C - Annual Ants' Gathering Rating: 1900 Tags: dfs and similar, dp, greedy, trees Solve time: 4m 48s Verified: yes Solution Problem Understanding Each vertex of the tree initially contains exactly one ant. A move chooses an edge $(u,v)$ and orders all ants currently gathered at $u$ to move to $v$. The ants obey only when the destination already contains at least as many ants as the source. If...
CF 1949D - Funny or Scary?
CF 1949D - Funny or Scary? Rating: 2600 Tags: constructive algorithms Solve time: 1m 10s Verified: no Solution Problem Understanding We are given a set of $n$ game scenarios, each of which must be played exactly once by the player. Between any two different scenarios $i$ and $j$, the game has a transition video that can be either funny (F) or scary (S). This transition is symmetric, meaning the video...
CF 241D - Numbers
CF 241D - Numbers Rating: 2900 Tags: - Solve time: 2m 21s Verified: no Solution Problem Understanding We are given a sequence of distinct integers. We may keep any subsequence, preserving the original order, and remove the rest. The remaining sequence must satisfy two conditions simultaneously. First, the xor of all remaining numbers must be zero. Second, if we concatenate the decimal representations of the remaining numbers into one huge...
CF 241E - Flights
CF 241E - Flights Rating: 2600 Tags: graphs, shortest paths Solve time: 1m 54s Verified: yes Solution Problem Understanding We are given a directed acyclic graph of cities and one-way flights. Every flight initially takes 1 hour. We may independently change any flight duration to either 1 or 2 hours. The goal is to assign durations so that every possible path from city 1 to city n has exactly the...
CF 241C - Mirror Box
CF 241C - Mirror Box Rating: 2000 Tags: geometry, implementation Solve time: 1m 27s Verified: no Solution Problem Understanding The system describes a rectangular box where a laser beam enters through one small hole on the left wall and must exit through another hole on the right wall. Inside the box, there are horizontal mirror segments placed either on the floor or on the ceiling. Each mirror covers a segment...
CF 241F - Race
CF 241F - Race Rating: 2300 Tags: brute force, implementation Solve time: 2m 26s Verified: yes Solution Problem Understanding The city is represented by a grid. Every cell is either a building, a street tile with a traversal cost from 1 to 9 , or a junction labeled by a lowercase letter. Movement rules are unusual. You can move only between adjacent cells that share an edge, but: moving from...
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...
CF 241A - Old Peykan
CF 241A - Old Peykan Rating: 1300 Tags: greedy Solve time: 1m 12s Verified: yes Solution Problem Understanding We are asked to model a journey along a straight line of cities connected by one-way roads, where a car travels at a constant speed of one kilometer per hour and consumes one liter of fuel per kilometer. Each city (except the last) has a fuel supply that replenishes every k hours....
CF 241G - Challenging Balloons
CF 241G - Challenging Balloons Rating: 1900 Tags: constructive algorithms Solve time: 1m 6s Verified: yes Solution Problem Understanding We have a row of balloons placed at increasing positions on a line. Each balloon has a pressure endurance, which limits how large its radius can grow. We inflate balloons sequentially from left to right. Each balloon expands until either it reaches its maximum radius, dictated by its pressure endurance, or...
CF 46A - Ball Game
CF 46A - Ball Game Rating: 800 Tags: brute force, implementation Solve time: 1m 23s Verified: yes Solution Problem Understanding The children stand in a circle numbered from 1 to n . Child 1 starts with the ball. The first throw moves the ball forward by 1 position, the second throw moves it forward by 2 positions, the third throw by 3 positions, and so on. After exactly n -...
CF 38B - Chess
CF 38B - Chess Rating: 1200 Tags: brute force, implementation, math Solve time: 2m Verified: no Solution Problem Understanding We are given the positions of two chess pieces on a standard 8 × 8 board, one rook and one knight. Their starting positions are guaranteed to be safe, meaning the rook does not attack the knight and the knight does not attack the rook. We must place one more knight...
CF 48H - Black and White
CF 48H - Black and White Rating: 2800 Tags: constructive algorithms Solve time: 1m 46s Verified: no Solution Problem Understanding We are asked to tile an $n \times m$ rectangular floor with three types of 2x2 square tiles: black, white, and mixed tiles that have a black and a white section in a diagonal pattern. The counts of black-only tiles, white-only tiles, and mixed tiles are given as $a$, $b$,...
CF 38G - Queue
CF 38G - Queue Rating: 2300 Tags: data structures Solve time: 1m 1s Verified: yes Solution Problem Understanding We are asked to simulate a queue of people where each person has two properties: an importance value a[i] and a patience limit c[i] . People arrive in order, and when the last person (number n ) joins, he can move forward in the queue by swapping with the person directly in...
CF 46G - Emperor's Problem
CF 46G - Emperor's Problem Rating: 2500 Tags: geometry Solve time: 1m 33s Verified: no Solution Problem Understanding We are asked to construct a convex polygon with $n$ vertices that satisfies three conditions. First, all vertices must lie on lattice points, meaning each coordinate is an integer. Second, all sides must have distinct lengths. Third, among all polygons that satisfy these two conditions, the one with the minimal possible longest...
CF 48B - Land Lot
CF 48B - Land Lot Rating: 1200 Tags: brute force, implementation Solve time: 1m 47s Verified: yes Solution Problem Understanding The garden is represented as an n × m grid. Each cell contains either 0 or 1 . A 1 means there is a tree in that square, while 0 means the square is empty. We want to place a rectangular house plot somewhere inside the grid. The rectangle must...
CF 46F - Hercule Poirot Problem
CF 46F - Hercule Poirot Problem Rating: 2300 Tags: dsu, graphs Solve time: 1m 1s Verified: yes Solution Problem Understanding We are given a house with a certain number of rooms connected by doors, and each door has a unique key. There are several residents in the house, each initially in some room with some keys. We also know the positions and key holdings of every resident at a later...
CF 38D - Vasya the Architect
CF 38D - Vasya the Architect Rating: 1900 Tags: implementation Solve time: 1m 38s Verified: yes Solution Problem Understanding We stack cubes one by one. Every cube is axis-aligned, and its projection on the ground is a square. Since the cubes are actual cubes, the side length is determined by the square base. Cube i is placed directly on top of cube i-1 . Their projections overlap with positive area,...
CF 38C - Blinds
CF 38C - Blinds Rating: 1400 Tags: brute force Solve time: 1m 7s Verified: yes Solution Problem Understanding We have a set of horizontal blind stripes of varying lengths, and our goal is to construct a rectangular blind for a window using these stripes. Each stripe can be cut into smaller pieces, but pieces cannot be shorter than a given minimum length, l . The final blind is built by...
CF 48F - Snow sellers
CF 48F - Snow sellers Rating: 2800 Tags: greedy, sortings Solve time: 1m 27s Verified: yes Solution Problem Understanding We are asked to plan snow purchases over n days from m companies, ensuring we buy exactly W cubic meters each day. Each company produces a fixed daily amount w[i] , but the cost of all snow from that company decreases linearly: c[i] on day 1, c[i] - a[i] on day...
CF 38E - Let's Go Rolling!
CF 38E - Let's Go Rolling! Rating: 1800 Tags: dp, sortings Solve time: 1m 24s Verified: yes Solution Problem Understanding We are given n marbles on a one-dimensional axis, each with a position x[i] and a pin cost c[i] . You can stick a pin in some marbles, paying the associated cost, and unpinned marbles will roll left until they hit the nearest pinned marble. If a marble has no...
CF 48E - Ivan the Fool VS Gorynych the Dragon
CF 48E - Ivan the Fool VS Gorynych the Dragon Rating: 2100 Tags: dp, games, graphs Solve time: 2m 21s Verified: yes Solution Problem Understanding The game state is completely described by two numbers: how many heads and how many tails the dragon currently has. From a state (h, t) Ivan may choose one of two move types. If he cuts i heads, where 1 ≤ i ≤ n and...
CF 46E - Comb
CF 46E - Comb Rating: 1900 Tags: data structures, dp Solve time: 1m 52s Verified: yes Solution Problem Understanding Each row of the table contains integers, and from every row we must take a positive-length prefix. If we choose c[i] cells from row i , then the selected cells in that row are exactly the first c[i] entries. The total reward is the sum of all selected numbers. The restriction...
CF 48C - The Race
CF 48C - The Race Rating: 1800 Tags: math Solve time: 1m 54s Verified: yes Solution Problem Understanding We are asked to predict the next petrol station where Vanya will stop, given the stations he has already visited. Vanya drives a car that consumes exactly 10 liters per 100 kilometers, starts with a tank filled with some unknown α liters (α ≥ 10), and stops only when the remaining petrol...
CF 46D - Parking Lot
CF 46D - Parking Lot Rating: 1800 Tags: data structures, implementation Solve time: 1m 54s Verified: yes Solution Problem Understanding We have a parking segment represented by the interval [0, L] . Cars arrive one at a time, always driving from left to right, and each driver wants to park at the earliest possible position. If a car of length x parks with its back at coordinate p , then...
CF 46B - T-shirts from Sponsor
CF 46B - T-shirts from Sponsor Rating: 1100 Tags: implementation Solve time: 1m 13s Verified: yes Solution Problem Understanding We are given a limited stock of T-shirts in five sizes: S, M, L, XL, and XXL. Each participant in the contest has a preferred size. Participants arrive in a fixed order and try to pick the T-shirt closest to their preferred size. If their preferred size is available, they take...
CF 48D - Permutations
CF 48D - Permutations Rating: 1500 Tags: greedy Solve time: 2m 3s Verified: no Solution Problem Understanding We are given a shuffled array that originally came from concatenating several permutations. Each permutation may have a different size. After concatenation, all numbers were mixed together, so the original grouping disappeared. Our task is to assign every array element to some permutation so that each group forms a valid permutation. A valid...
CF 38F - Smart Boy
CF 38F - Smart Boy Rating: 2100 Tags: dp, games, strings Solve time: 2m 49s Verified: no Solution Problem Understanding We start with an empty string. The first player picks any single letter that appears somewhere inside at least one dictionary word. After that, players alternately extend the current string by adding exactly one character either to the front or to the back. The restriction is the core of the...
CF 38H - The Great Marathon
CF 38H - The Great Marathon Rating: 2400 Tags: dp Solve time: 2m 31s Verified: no Solution Problem Understanding We have a connected weighted graph of cities. Runner i starts from city i and finishes in some other city. The finish city is chosen independently for every runner, and several runners may share the same destination. Each runner travels along a shortest path, so their finishing time is exactly the...
CF 38A - Army
CF 38A - Army Rating: 800 Tags: implementation Solve time: 1m 18s Verified: yes Solution Problem Understanding Vasya currently holds rank a in the army and wants to eventually reach rank b . Moving from rank i to rank i + 1 requires a fixed number of years, stored in the array d . The array has length n - 1 because there are exactly n - 1 transitions between...
CF 48G - Galaxy Union
CF 48G - Galaxy Union Rating: 2700 Tags: dp, trees, two pointers Solve time: 2m 23s Verified: yes Solution Problem Understanding We are given an undirected weighted graph with exactly n vertices and n edges. Since a connected graph with n vertices and n edges contains exactly one cycle, the graph is a unicyclic graph, a tree with one extra edge. For every planet u , we must compute the...
Codeforces Solutions
Codeforces Solutions Each problem comes with a full written editorial: problem analysis, approach comparison, algorithm walkthrough, Python solution with explanation, worked examples, and edge cases. The problem statement itself is not reproduced here. Follow the CF link on each problem page to read the original. 447 contests, 1823 problems, 1297 verified. Contest Name Type Rating Range Verified Time 1 Codeforces Beta Round 1 Beta 1000-2100 3/3 10m 36s 2 Codeforces...
LeetCode
LeetCode Solutions to LeetCode problems with comprehensive analysis, Python and Go implementations. Problems # Problem Difficulty Time Notes 1 Two Sum 🟢 Easy 4m The problem gives us an integer array called nums and another integ… 2 Add Two Numbers 🟡 Medium 2m 35s The problem gives us two non empty singly linked lists. Each linked… 3 Longest Substring Without Repeating Characters 🟡 Medium 1m 9s The problem gives a...
Project Euler
Project Euler Solutions to Project Euler problems. Problems # Problem Time Description 1 Problem 1 — If we list all the natural numbers below 10 that are multiples of 3 or 5, we … 2 Problem 2 — Each new term in the Fibonacci sequence is generated by adding the previous t… 3 Problem 3 — The prime factors of 13195 are 5, 7, 13 and 29. 4 Problem 4...