problems
stringlengths
691
4.89k
Yelisey has an array a of n integers. If a has length strictly greater than 1, then Yelisei can apply an operation called minimum extraction to it: 1. First, Yelisei finds the minimal number m in the array. If there are several identical minima, Yelisey can choose any of them. 2. Then the selected minimal eleme...
You are given an array of integers a of length n. The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: * either you...
The robot is located on a checkered rectangular board of size n Γ— m (n rows, m columns). The rows in the board are numbered from 1 to n from top to bottom, and the columns β€” from 1 to m from left to right. The robot is able to move from the current cell to one of the four cells adjacent by side. The sequence of comma...
The robot is located on a checkered rectangular board of size n Γ— m (n rows, m columns). The rows in the board are numbered from 1 to n from top to bottom, and the columns β€” from 1 to m from left to right. The robot is able to move from the current cell to one of the four cells adjacent by side. Each cell has one of ...
A known chef has prepared n dishes: the i-th dish consists of a_i grams of fish and b_i grams of meat. The banquet organizers estimate the balance of n dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat. Technically, the balance e...
The chef has cooked n dishes yet again: the i-th dish consists of a_i grams of fish and b_i grams of meat. Banquet organizers consider two dishes i and j equal if a_i=a_j and b_i=b_j at the same time. The banquet organizers estimate the variety of n dishes as follows. The variety of a set of dishes is equal to the n...
Given n, find any array a_1, a_2, …, a_n of integers such that all of the following conditions hold: * 1 ≀ a_i ≀ 10^9 for every i from 1 to n. * a_1 < a_2 < … <a_n * For every i from 2 to n, a_i isn't divisible by a_{i-1} It can be shown that such an array always exists under the constraints of the proble...
You are given three integers n, a, b. Determine if there exists a permutation p_1, p_2, …, p_n of integers from 1 to n, such that: * There are exactly a integers i with 2 ≀ i ≀ n-1 such that p_{i-1} < p_i > p_{i+1} (in other words, there are exactly a local maximums). * There are exactly b integers i with 2 ≀ i ≀...
n players are playing a game. There are two different maps in the game. For each player, we know his strength on each map. When two players fight on a specific map, the player with higher strength on that map always wins. No two players have the same strength on the same map. You are the game master and want to org...
You are given n dominoes. Each domino has a left and a right cell. Each cell can be colored either black or white. Some cells are already colored, while some aren't yet. The coloring is said to be valid if and only if it is possible to rearrange the dominoes in some order such that for each 1 ≀ i ≀ n the color of the ...
On an endless checkered sheet of paper, n cells are chosen and colored in three colors, where n is divisible by 3. It turns out that there are exactly n/3 marked cells of each of three colors! Find the largest such k that it's possible to choose k/3 cells of each color, remove all other marked cells, and then select ...
For an array c of nonnegative integers, MEX(c) denotes the smallest nonnegative integer that doesn't appear in it. For example, MEX([0, 1, 3]) = 2, MEX([42]) = 0. You are given integers n, k, and an array [b_1, b_2, …, b_n]. Find the number of arrays [a_1, a_2, …, a_n], for which the following conditions hold: * 0...
You are given m strings and a tree on n nodes. Each edge has some letter written on it. You have to answer q queries. Each query is described by 4 integers u, v, l and r. The answer to the query is the total number of occurrences of str(u,v) in strings with indices from l to r. str(u,v) is defined as the string that i...
Monocarp wrote down two numbers on a whiteboard. Both numbers follow a specific format: a positive integer x with p zeros appended to its end. Now Monocarp asks you to compare these two numbers. Can you help him? Input The first line contains a single integer t (1 ≀ t ≀ 10^4) β€” the number of testcases. The first li...
You are given a sequence a_1, a_2, ..., a_n consisting of n pairwise distinct positive integers. Find \left⌊ \frac n 2 \rightβŒ‹ different pairs of integers x and y such that: * x β‰  y; * x and y appear in a; * x~mod~y doesn't appear in a. Note that some x or y can belong to multiple pairs. ⌊ x βŒ‹ denotes t...
Monocarp is playing yet another computer game. In this game, his character has to kill a dragon. The battle with the dragon lasts 100^{500} seconds, during which Monocarp attacks the dragon with a poisoned dagger. The i-th attack is performed at the beginning of the a_i-th second from the battle start. The dagger itsel...
Let's call a sequence of integers x_1, x_2, ..., x_k MEX-correct if for all i (1 ≀ i ≀ k) |x_i - \operatorname{MEX}(x_1, x_2, ..., x_i)| ≀ 1 holds. Where \operatorname{MEX}(x_1, ..., x_k) is the minimum non-negative integer that doesn't belong to the set x_1, ..., x_k. For example, \operatorname{MEX}(1, 0, 1, 3) = 2 an...
There is a grid, consisting of n rows and m columns. Each cell of the grid is either free or blocked. One of the free cells contains a lab. All the cells beyond the borders of the grid are also blocked. A crazy robot has escaped from this lab. It is currently in some free cell of the grid. You can send one of the foll...
You are given a rooted tree consisting of n vertices numbered from 1 to n. The root of the tree is the vertex 1. You have to color all vertices of the tree into n colors (also numbered from 1 to n) so that there is exactly one vertex for each color. Let c_i be the color of vertex i, and p_i be the parent of vertex i i...
There are n block towers in a row, where tower i has a height of a_i. You're part of a building crew, and you want to make the buildings look as nice as possible. In a single day, you can perform the following operation: * Choose two indices i and j (1 ≀ i, j ≀ n; i β‰  j), and move a block from tower i to tower j. Th...
You are given an array consisting of all integers from [l, r] inclusive. For example, if l = 2 and r = 5, the array would be [2, 3, 4, 5]. What's the minimum number of elements you can delete to make the [bitwise AND](https://en.wikipedia.org/wiki/Bitwise_operation#AND) of the array non-zero? A bitwise AND is a binary...
There are n candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string s, where the i-th candle is lit if and only if s_i=1. <image> Initially, the candle lights are described by a string a. In an operation, you select a candle that is currentl...
'Twas the night before Christmas, and Santa's frantically setting up his new Christmas tree! There are n nodes in the tree, connected by n-1 edges. On each edge of the tree, there's a set of Christmas lights, which can be represented by an integer in binary representation. <image> He has m elves come over and admire ...
Two players, Red and Blue, are at it again, and this time they're playing with crayons! The mischievous duo is now vandalizing a rooted tree, by coloring the nodes while playing their favorite game. The game works as follows: there is a tree of size n, rooted at node 1, where each node is initially white. Red and Blue...
After getting bored by playing with crayons, you decided to switch to Legos! Today, you're working with a long strip, with height 1 and length n, some positions of which are occupied by 1 by 1 Lego pieces. In one second, you can either remove two adjacent Lego pieces from the strip (if both are present), or add two Le...
You are given an array a consisting of n non-negative integers. You have to replace each 0 in a with an integer from 1 to n (different elements equal to 0 can be replaced by different integers). The value of the array you obtain is the number of integers k from 1 to n such that the following condition holds: there ex...
There are n reindeer at the North Pole, all battling for the highest spot on the "Top Reindeer" leaderboard on the front page of CodeNorses (a popular competitive reindeer gaming website). Interestingly, the "Top Reindeer" title is just a measure of upvotes and has nothing to do with their skill level in the reindeer g...
You are given strings S and T, consisting of lowercase English letters. It is guaranteed that T is a permutation of the string abc. Find string S', the lexicographically smallest permutation of S such that T is not a subsequence of S'. String a is a permutation of string b if the number of occurrences of each distin...
Given a positive integer n. Find three distinct positive integers a, b, c such that a + b + c = n and \operatorname{gcd}(a, b) = c, where \operatorname{gcd}(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y. Input The input consists of multipl...
Paprika loves permutations. She has an array a_1, a_2, ..., a_n. She wants to make the array a permutation of integers 1 to n. In order to achieve this goal, she can perform operations on the array. In each operation she can choose two integers i (1 ≀ i ≀ n) and x (x > 0), then perform a_i := a_i mod x (that is, repla...
This is an interactive problem. The only difference between the easy and hard version is the limit on number of questions. There are n players labelled from 1 to n. It is guaranteed that n is a multiple of 3. Among them, there are k impostors and n-k crewmates. The number of impostors, k, is not given to you. It is g...
Christmas is coming, Icy has just received a box of chocolates from her grandparents! The box contains n chocolates. The i-th chocolate has a non-negative integer type a_i. Icy believes that good things come in pairs. Unfortunately, all types of chocolates are distinct (all a_i are distinct). Icy wants to make at leas...
Polycarp had an array a of 3 positive integers. He wrote out the sums of all non-empty subsequences of this array, sorted them in non-decreasing order, and got an array b of 7 integers. For example, if a = \{1, 4, 3\}, then Polycarp wrote out 1, 4, 3, 1 + 4 = 5, 1 + 3 = 4, 4 + 3 = 7, 1 + 4 + 3 = 8. After sorting, he g...
Polycarp has come up with a new game to play with you. He calls it "A missing bigram". A bigram of a word is a sequence of two adjacent letters in it. For example, word "abbaaba" contains bigrams "ab", "bb", "ba", "aa", "ab" and "ba". The game goes as follows. First, Polycarp comes up with a word, consisting only of...
You are given an array a consisting of n positive integers. You have to choose a positive integer d and paint all elements into two colors. All elements which are divisible by d will be painted red, and all other elements will be painted blue. The coloring is called beautiful if there are no pairs of adjacent elements...
You are given an array a of n integers, and another integer k such that 2k ≀ n. You have to perform exactly k operations with this array. In one operation, you have to choose two elements of the array (let them be a_i and a_j; they can be equal or different, but their positions in the array must not be the same), remo...
n towns are arranged in a circle sequentially. The towns are numbered from 1 to n in clockwise order. In the i-th town, there lives a singer with a repertoire of a_i minutes for each i ∈ [1, n]. Each singer visited all n towns in clockwise order, starting with the town he lives in, and gave exactly one concert in each...
You are given two positive integers x and y. You can perform the following operation with x: write it in its binary form without leading zeros, add 0 or 1 to the right of it, reverse the binary form and turn it into a decimal number which is assigned as the new value of x. For example: * 34 can be turned into 81 v...
Monocarp plays a computer game (yet again!). This game has a unique trading mechanics. To trade with a character, Monocarp has to choose one of the items he possesses and trade it for some item the other character possesses. Each item has an integer price. If Monocarp's chosen item has price x, then he can trade it fo...
A string is called square if it is some string written twice in a row. For example, the strings "aa", "abcabc", "abab" and "baabaa" are square. But the strings "aaa", "abaaab" and "abcdabc" are not square. For a given string s determine if it is square. Input The first line of input data contains an integer t (1 ≀ t...
Polycarp likes squares and cubes of positive integers. Here is the beginning of the sequence of numbers he likes: 1, 4, 8, 9, .... For a given number n, count the number of integers from 1 to n that Polycarp likes. In other words, find the number of such x that x is a square of a positive integer number or a cube of a...
Tanya is learning how to add numbers, but so far she is not doing it correctly. She is adding two numbers a and b using the following algorithm: 1. If one of the numbers is shorter than the other, Tanya adds leading zeros so that the numbers are the same length. 2. The numbers are processed from right to left (th...
Vlad has n friends, for each of whom he wants to buy one gift for the New Year. There are m shops in the city, in each of which he can buy a gift for any of his friends. If the j-th friend (1 ≀ j ≀ n) receives a gift bought in the shop with the number i (1 ≀ i ≀ m), then the friend receives p_{ij} units of joy. The re...
Dmitry has an array of n non-negative integers a_1, a_2, ..., a_n. In one operation, Dmitry can choose any index j (1 ≀ j ≀ n) and increase the value of the element a_j by 1. He can choose the same index j multiple times. For each i from 0 to n, determine whether Dmitry can make the MEX of the array equal to exactly ...
The Hat is a game of speedy explanation/guessing words (similar to Alias). It's fun. Try it! In this problem, we are talking about a variant of the game when the players are sitting at the table and everyone plays individually (i.e. not teams, but individual gamers play). n people gathered in a room with m tables (n β‰₯...
Polycarp is very fond of playing the game Minesweeper. Recently he found a similar game and there are such rules. There are mines on the field, for each the coordinates of its location are known (x_i, y_i). Each mine has a lifetime in seconds, after which it will explode. After the explosion, the mine also detonates a...
You are given a permutation p of n elements. A permutation of n elements is an array of length n containing each integer from 1 to n exactly once. For example, [1, 2, 3] and [4, 3, 5, 1, 2] are permutations, but [1, 2, 4] and [4, 3, 2, 1, 2] are not permutations. You should perform q queries. There are two types of qu...
You had n positive integers a_1, a_2, ..., a_n arranged in a circle. For each pair of neighboring numbers (a_1 and a_2, a_2 and a_3, ..., a_{n - 1} and a_n, and a_n and a_1), you wrote down: are the numbers in the pair equal or not. Unfortunately, you've lost a piece of paper with the array a. Moreover, you are afraid...
A rectangle with its opposite corners in (0, 0) and (w, h) and sides parallel to the axes is drawn on a plane. You are given a list of lattice points such that each point lies on a side of a rectangle but not in its corner. Also, there are at least two points on every side of a rectangle. Your task is to choose three...
You are given an integer k and a string s that consists only of characters 'a' (a lowercase Latin letter) and '*' (an asterisk). Each asterisk should be replaced with several (from 0 to k inclusive) lowercase Latin letters 'b'. Different asterisk can be replaced with different counts of letter 'b'. The result of the ...
One day, early in the morning, you decided to buy yourself a bag of chips in the nearby store. The store has chips of n different flavors. A bag of the i-th flavor costs a_i burles. The store may run out of some flavors, so you'll decide which one to buy after arriving there. But there are two major flaws in this plan...
You have an array of integers (initially empty). You have to perform q queries. Each query is of one of two types: * "1 x" β€” add the element x to the end of the array; * "2 x y" β€” replace all occurrences of x in the array with y. Find the resulting array after performing all the queries. Input The first l...
You are given a permutation p consisting of n integers 1, 2, ..., n (a permutation is an array where each element from 1 to n occurs exactly once). Let's call an array a bipartite if the following undirected graph is bipartite: * the graph consists of n vertices; * two vertices i and j are connected by an edge i...
For a sequence of strings [t_1, t_2, ..., t_m], let's define the function f([t_1, t_2, ..., t_m]) as the number of different strings (including the empty string) that are subsequences of at least one string t_i. f([]) = 0 (i. e. the number of such strings for an empty sequence is 0). You are given a sequence of string...
There are three sticks with integer lengths l_1, l_2 and l_3. You are asked to break exactly one of them into two pieces in such a way that: * both pieces have positive (strictly greater than 0) integer length; * the total length of the pieces is equal to the original length of the stick; * it's possible to ...
Berland Music is a music streaming service built specifically to support Berland local artist. Its developers are currently working on a song recommendation module. So imagine Monocarp got recommended n songs, numbered from 1 to n. The i-th song had its predicted rating equal to p_i, where 1 ≀ p_i ≀ n and every intege...
You are given an integer array a_1, a_2, ..., a_n and integer k. In one step you can * either choose some index i and decrease a_i by one (make a_i = a_i - 1); * or choose two indices i and j and set a_i equal to a_j (make a_i = a_j). What is the minimum number of steps you need to make the sum of array βˆ‘_{...
You are given a binary string (i. e. a string consisting of characters 0 and/or 1) s of length n. You can perform the following operation with the string s at most once: choose a substring (a contiguous subsequence) of s having exactly k characters 1 in it, and shuffle it (reorder the characters in the substring as you...
Petya is a math teacher. n of his students has written a test consisting of m questions. For each student, it is known which questions he has answered correctly and which he has not. If the student answers the j-th question correctly, he gets p_j points (otherwise, he gets 0 points). Moreover, the points for the quest...
Let's call a set of positive integers a_1, a_2, ..., a_k quadratic if the product of the factorials of its elements is a square of an integer, i. e. ∏_{i=1}^{k} a_i! = m^2, for some integer m. You are given a positive integer n. Your task is to find a quadratic subset of a set 1, 2, ..., n of maximum size. If there a...
A robot cleaner is placed on the floor of a rectangle room, surrounded by walls. The floor consists of n rows and m columns. The rows of the floor are numbered from 1 to n from top to bottom, and columns of the floor are numbered from 1 to m from left to right. The cell on the intersection of the r-th row and the c-th ...
Alice and Bob play the following game. Alice has a set S of disjoint ranges of integers, initially containing only one range [1, n]. In one turn, Alice picks a range [l, r] from the set S and asks Bob to pick a number in the range. Bob chooses a number d (l ≀ d ≀ r). Then Alice removes [l, r] from S and puts into the s...
There are n heaps of stone. The i-th heap has h_i stones. You want to change the number of stones in the heap by performing the following process once: * You go through the heaps from the 3-rd heap to the n-th heap, in this order. * Let i be the number of the current heap. * You can choose a number d (0 ≀ 3 β‹…...
The statement of this problem shares a lot with problem A. The differences are that in this problem, the probability is introduced, and the constraint is different. A robot cleaner is placed on the floor of a rectangle room, surrounded by walls. The floor consists of n rows and m columns. The rows of the floor are num...
A binary tree of n nodes is given. Nodes of the tree are numbered from 1 to n and the root is the node 1. Each node can have no child, only one left child, only one right child, or both children. For convenience, let's denote l_u and r_u as the left and the right child of the node u respectively, l_u = 0 if u does not ...