problem_id
stringlengths
5
6
url
stringlengths
48
49
title
stringlengths
2
46
rating
int64
800
3.5k
tags
listlengths
1
11
div
stringclasses
16 values
time_limit_ms
int64
1k
13k
memory_limit_mb
int64
32
1.02k
description
stringlengths
67
2.52k
input
stringlengths
99
1.93k
βŒ€
output
stringlengths
47
1.35k
βŒ€
examples
listlengths
1
5
note
stringlengths
0
1.68k
prompt
stringlengths
505
6.96k
2006A
https://codeforces.com/problemset/problem/2006/A
Iris and Game on the Tree
1,700
[ "constructive algorithms", "dfs and similar", "games", "graphs", "greedy", "trees" ]
Div. 1
2,000
256
Iris has a tree rooted at vertex $1$. Each vertex has a value of $\mathtt 0$ or $\mathtt 1$. Let's consider a leaf of the tree (the vertex $1$ is never considered a leaf) and define its weight. Construct a string formed by the values of the vertices on the path starting at the root and ending in this leaf. Then the we...
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 5\cdot 10^4$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$) β€” the number of vertices in the tree. The f...
For each test case, output a single integer β€” the final score of the tree.
[ [ "6\n4\n1 2\n1 3\n4 1\n0101\n4\n1 2\n3 2\n2 4\n???0\n5\n1 2\n1 3\n2 4\n2 5\n?1?01\n6\n1 2\n2 3\n3 4\n5 3\n3 6\n?0????\n5\n1 2\n1 3\n1 4\n1 5\n11?1?\n2\n2 1\n??", "2\n1\n1\n2\n1\n0" ] ]
In the first test case, all the values of the vertices have been determined. There are three different paths from the root to a leaf: * From vertex $1$ to vertex $2$. The string formed by the path is $\mathtt{01}$, so the weight of the leaf is $0-1=-1$. * From vertex $1$ to vertex $3$. The string formed by the pa...
Title: Iris and Game on the Tree time_limit_ms: 2000 memory_limit_mb: 256 Description: Iris has a tree rooted at vertex $1$. Each vertex has a value of $\mathtt 0$ or $\mathtt 1$. Let's consider a leaf of the tree (the vertex $1$ is never considered a leaf) and define its weight. Construct a string formed by the value...
2006B
https://codeforces.com/problemset/problem/2006/B
Iris and the Tree
1,800
[ "brute force", "data structures", "dfs and similar", "dsu", "math", "trees" ]
Div. 1
3,000
256
Given a rooted tree with the root at vertex $1$. For any vertex $i$ ($1 < i \leq n$) in the tree, there is an edge connecting vertices $i$ and $p_i$ ($1 \leq p_i < i$), with a weight equal to $t_i$. Iris does not know the values of $t_i$, but she knows that $\displaystyle\sum_{i=2}^n t_i = w$ and each of the $t_i$ is ...
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$ and $w$ ($2 \le n \le 2 \cdot 10^5$, $0 \leq w \leq 10^{12}$) β€” the number of ...
For each test case, output one line containing $n-1$ integers, each representing the answer after each event.
[ [ "4\n2 1000000000000\n1\n2 1000000000000\n4 9\n1 1 1\n2 2\n4 4\n3 3\n6 100\n1 2 3 2 1\n6 17\n3 32\n2 4\n4 26\n5 21\n10 511\n1 2 2 4 2 1 1 8 8\n3 2\n6 16\n10 256\n9 128\n2 1\n5 8\n8 64\n4 4\n7 32", "2000000000000\n25 18 18\n449 302 247 200 200\n4585 4473 2681 1567 1454 1322 1094 1022 1022" ] ]
In the first test case, $\operatorname{dist}(1, 2) = \operatorname{dist}(2, 1) = t_2 = w = 10^{12}$, so $\operatorname{dist}(1, 2) + \operatorname{dist}(2, 1) = 2 \cdot 10^{12}$. In the second test case, the tree after Iris found out all $t_x$ is shown below: ![](CDN_BASE_URL/2f461ea7db7bb0cda4536a2a78190c2f) $\oper...
Title: Iris and the Tree time_limit_ms: 3000 memory_limit_mb: 256 Description: Given a rooted tree with the root at vertex $1$. For any vertex $i$ ($1 < i \leq n$) in the tree, there is an edge connecting vertices $i$ and $p_i$ ($1 \leq p_i < i$), with a weight equal to $t_i$. Iris does not know the values of $t_i$, b...
2006C
https://codeforces.com/problemset/problem/2006/C
Eri and Expanded Sets
2,300
[ "data structures", "divide and conquer", "math", "number theory", "two pointers" ]
Div. 1
3,000
512
Let there be a set that contains distinct positive integers. To expand the set to contain as many integers as possible, Eri can choose two integers $x\neq y$ from the set such that their average $\frac{x+y}2$ is still a positive integer and isn't contained in the set, and add it to the set. The integers $x$ and $y$ rem...
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 4 \cdot 10^5$) β€” length of the array $a$. The second line...
For each test case, output a single integer β€” the number of brilliant subarrays.
[ [ "6\n2\n2 2\n6\n1 3 6 10 15 21\n5\n6 30 18 36 9\n1\n1000000000\n6\n1 1 4 5 1 4\n12\n70 130 90 90 90 108 612 500 451 171 193 193", "3\n18\n5\n1\n18\n53" ] ]
In the first test case, the array $a = [2, 2]$ has $3$ subarrays: $[2]$, $[2]$, and $[2, 2]$. For all of them, the set only contains a single integer $2$, therefore it's always consecutive. All these subarrays are brilliant, so the answer is $3$. In the second test case, let's consider the subarray $[3, 6, 10]$. We ca...
Title: Eri and Expanded Sets time_limit_ms: 3000 memory_limit_mb: 512 Description: Let there be a set that contains distinct positive integers. To expand the set to contain as many integers as possible, Eri can choose two integers $x\neq y$ from the set such that their average $\frac{x+y}2$ is still a positive integer ...
2018A
https://codeforces.com/problemset/problem/2018/A
Cards Partition
1,600
[ "2-sat", "brute force", "greedy", "implementation", "math" ]
Div. 1
2,000
256
[DJ Genki vs Gram - Einherjar Joker](https://soundcloud.com/leon- hwang-368077289/einherjar-joker-dj-genki-vs-gram) β € You have some cards. An integer between $1$ and $n$ is written on each card: specifically, for each $i$ from $1$ to $n$, you have $a_i$ cards which have the number $i$ written on them. There is also ...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \leq n \leq 2 \cdot 10^5$, $0 \leq k \leq 10^{16}$) β€” the number of distinct types of cards a...
For each test case, output a single integer: the maximum possible size of a deck if you operate optimally.
[ [ "9\n3 1\n3 2 2\n5 4\n2 6 1 2 4\n2 100\n1410065408 10000000000\n10 8\n7 4 6 6 9 3 10 2 8 7\n2 12\n2 2\n2 70\n0 1\n1 0\n1\n3 0\n2 1 2\n3 1\n0 3 3", "2\n3\n1\n7\n2\n2\n1\n1\n2" ] ]
In the first test case, you can buy one card with the number $1$, and your cards become $[1, 1, 1, 1, 2, 2, 3, 3]$. You can partition them into the decks $[1, 2], [1, 2], [1, 3], [1, 3]$: they all have size $2$, and they all contain distinct values. You can show that you cannot get a partition with decks of size greate...
Title: Cards Partition time_limit_ms: 2000 memory_limit_mb: 256 Description: [DJ Genki vs Gram - Einherjar Joker](https://soundcloud.com/leon- hwang-368077289/einherjar-joker-dj-genki-vs-gram) β € You have some cards. An integer between $1$ and $n$ is written on each card: specifically, for each $i$ from $1$ to $n$, yo...
2018B
https://codeforces.com/problemset/problem/2018/B
Speedbreaker
1,900
[ "binary search", "data structures", "dp", "greedy", "implementation", "two pointers" ]
Div. 1
2,000
256
[Djjaner - Speedbreaker](https://soundcloud.com/luciano- ferrari-151560131/speedbreaker) β € There are $n$ cities in a row, numbered $1, 2, \ldots, n$ left to right. * At time $1$, you conquer exactly one city, called the starting city. * At time $2, 3, \ldots, n$, you can choose a city adjacent to the ones conqu...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of cities. The second line of each test case contains $...
For each test case, output a single integer: the number of starting cities that allow you to win.
[ [ "3\n6\n6 3 3 3 5 5\n6\n5 6 4 1 4 5\n9\n8 6 4 2 1 3 5 7 9", "3\n0\n1" ] ]
In the first test case, cities $2$, $3$, and $4$ are good starting cities. In the second test case, there are no good starting cities. In the third test case, the only good starting city is city $5$.
Title: Speedbreaker time_limit_ms: 2000 memory_limit_mb: 256 Description: [Djjaner - Speedbreaker](https://soundcloud.com/luciano- ferrari-151560131/speedbreaker) β € There are $n$ cities in a row, numbered $1, 2, \ldots, n$ left to right. * At time $1$, you conquer exactly one city, called the starting city. * A...
2018C
https://codeforces.com/problemset/problem/2018/C
Tree Pruning
1,700
[ "brute force", "dfs and similar", "greedy", "sortings", "trees" ]
Div. 1
3,000
256
[t+pazolite, ginkiha, Hommarju - Paved Garden](https://soundcloud.com/fractalex-gd/ginkiha-paved-garden-little) β € You are given a tree with $n$ nodes, rooted at node $1$. In this problem, a leaf is a non-root node with degree $1$. In one operation, you can remove a leaf and the edge adjacent to it (possibly, new lea...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($3 \leq n \leq 5 \cdot 10^5$) β€” the number of nodes. Each of the next $n-1$ lines contains two i...
For each test case, output a single integer: the minimum number of operations needed to achieve your goal.
[ [ "3\n7\n1 2\n1 3\n2 4\n2 5\n4 6\n4 7\n7\n1 2\n1 3\n1 4\n2 5\n3 6\n5 7\n15\n12 9\n1 6\n6 14\n9 11\n8 7\n3 5\n13 5\n6 10\n13 15\n13 6\n14 12\n7 2\n8 1\n1 4", "2\n2\n5" ] ]
In the first two examples, the tree is as follows: ![](CDN_BASE_URL/754511fbdb88995aaa49733a1fe75dfc) In the first example, by removing edges $(1, 3)$ and $(2, 5)$, the resulting tree has all leaves (nodes $6$ and $7$) at the same distance from the root (node $1$), which is $3$. The answer is $2$, as it is the minimu...
Title: Tree Pruning time_limit_ms: 3000 memory_limit_mb: 256 Description: [t+pazolite, ginkiha, Hommarju - Paved Garden](https://soundcloud.com/fractalex-gd/ginkiha-paved-garden-little) β € You are given a tree with $n$ nodes, rooted at node $1$. In this problem, a leaf is a non-root node with degree $1$. In one opera...
2018D
https://codeforces.com/problemset/problem/2018/D
Max Plus Min Plus Size
2,200
[ "data structures", "dp", "dsu", "greedy", "implementation", "matrices", "sortings" ]
Div. 1
2,000
256
[EnV - The Dusty Dragon Tavern](https://soundcloud.com/envyofficial/env-the- dusty-dragon-tavern) β € You are given an array $a_1, a_2, \ldots, a_n$ of positive integers. You can color some elements of the array red, but there cannot be two adjacent red elements (i.e., for $1 \leq i \leq n-1$, at least one of $a_i$ an...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the length of the array. The second line of each test case contain...
For each test case, output a single integer: the maximum possible score you can get after coloring some elements red according to the statement.
[ [ "4\n3\n5 4 5\n3\n4 5 4\n10\n3 3 3 3 4 1 2 3 5 4\n10\n17 89 92 42 29 41 92 14 70 45", "12\n11\n12\n186" ] ]
In the first test case, you can color the array as follows: $[\color{red}{5}, 4, \color{red}{5}]$. Your score is $\max([5, 5]) + \min([5, 5]) + \text{size}([5, 5]) = 5+5+2 = 12$. This is the maximum score you can get. In the second test case, you can color the array as follows: $[4, \color{red}{5}, 4]$. Your score is ...
Title: Max Plus Min Plus Size time_limit_ms: 2000 memory_limit_mb: 256 Description: [EnV - The Dusty Dragon Tavern](https://soundcloud.com/envyofficial/env-the- dusty-dragon-tavern) β € You are given an array $a_1, a_2, \ldots, a_n$ of positive integers. You can color some elements of the array red, but there cannot b...
2023B
https://codeforces.com/problemset/problem/2023/B
Skipping
1,700
[ "binary search", "dp", "graphs", "shortest paths" ]
Div. 1
2,000
256
It is already the year $3024$, ideas for problems have long run out, and the olympiad now takes place in a modified individual format. The olympiad consists of $n$ problems, numbered from $1$ to $n$. The $i$-th problem has its own score $a_i$ and a certain parameter $b_i$ ($1 \le b_i \le n$). Initially, the testing sy...
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 4 \cdot 10^5$) β€” the number of problems in the olympiad. ...
For each test case, output a single integer β€” the maximum number of points that Prokhor can achieve.
[ [ "4\n2\n15 16\n2 1\n5\n10 10 100 100 1000\n3 4 1 1 1\n3\n100 49 50\n3 2 2\n4\n100 200 300 1000\n2 3 4 1", "16\n200\n100\n1000" ] ]
In the first test case, Prokhor can skip the first problem; then he will receive the problem with index $b_1 = 2$. Prokhor can submit it and receive $a_2 = 16$ points. After that, the competition will end because Prokhor has already received all problems. Note that if Prokhor submits the first problem, he will receive ...
Title: Skipping time_limit_ms: 2000 memory_limit_mb: 256 Description: It is already the year $3024$, ideas for problems have long run out, and the olympiad now takes place in a modified individual format. The olympiad consists of $n$ problems, numbered from $1$ to $n$. The $i$-th problem has its own score $a_i$ and a c...
2023C
https://codeforces.com/problemset/problem/2023/C
C+K+S
2,400
[ "constructive algorithms", "dfs and similar", "graphs", "greedy", "hashing", "implementation", "strings" ]
Div. 1
3,000
256
You are given two strongly connected$^{\dagger}$ directed graphs, each with exactly $n$ vertices, but possibly different numbers of edges. Upon closer inspection, you noticed an important feature β€” the length of any cycle in these graphs is divisible by $k$. Each of the $2n$ vertices belongs to exactly one of two type...
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($2 \le k \le n \le 2 \cdot 10^5$) β€” the number of vertices in each grap...
For each test case, output "YES" (without quotes) if it is possible to draw $n$ new edges such that all conditions are met, and "NO" (without quotes) otherwise. You may output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
[ [ "3\n4 2\n1 0 0 1\n4\n1 2\n2 3\n3 4\n4 1\n1 0 0 1\n4\n1 3\n3 2\n2 4\n4 1\n3 3\n0 0 0\n3\n1 2\n2 3\n3 1\n1 1 0\n3\n1 2\n2 3\n3 1\n4 2\n1 1 1 1\n4\n1 2\n2 3\n3 4\n4 1\n0 0 0 0\n6\n1 2\n2 1\n1 3\n3 1\n1 4\n4 1", "YES\nNO\nYES" ] ]
In the first test case, it is possible to draw edges from the first graph to the second graph as $(1, 3)$ and $(4, 2)$ (the first number in the pair is the vertex number in the first graph, and the second number in the pair is the vertex number in the second graph), and from the second graph to the first graph as $(1, ...
Title: C+K+S time_limit_ms: 3000 memory_limit_mb: 256 Description: You are given two strongly connected$^{\dagger}$ directed graphs, each with exactly $n$ vertices, but possibly different numbers of edges. Upon closer inspection, you noticed an important feature β€” the length of any cycle in these graphs is divisible by...
1993A
https://codeforces.com/problemset/problem/1993/A
Question Marks
800
[ "greedy", "implementation" ]
Div. 2
1,000
256
Tim is doing a test consisting of $4n$ questions; each question has $4$ options: 'A', 'B', 'C', and 'D'. For each option, there are exactly $n$ correct answers corresponding to that option β€” meaning there are $n$ questions with the answer 'A', $n$ questions with the answer 'B', $n$ questions with the answer 'C', and $n...
The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains an integer $n$ ($1 \le n \le 100$). The second line of each test case contains a string $s$ of $4n$ characters ($s_i \in \\{\texttt{A}, \texttt{B}, \texttt{C}, \texttt{D}, \texttt{?}...
For each test case, print a single integer β€” the maximum score that Tim can achieve.
[ [ "6\n1\nABCD\n2\nAAAAAAAA\n2\nAAAABBBB\n2\n????????\n3\nABCABCABCABC\n5\nACADC??ACAC?DCAABC?C", "4\n2\n4\n0\n9\n13" ] ]
In the first test case, there is exactly one question with each answer 'A', 'B', 'C', and 'D'; so it's possible that Tim gets all his answers correct. In the second test case, there are only two correct answers 'A' which makes him get exactly $2$ points in any case. In the third test case, Tim can get at most $2$ cor...
Title: Question Marks time_limit_ms: 1000 memory_limit_mb: 256 Description: Tim is doing a test consisting of $4n$ questions; each question has $4$ options: 'A', 'B', 'C', and 'D'. For each option, there are exactly $n$ correct answers corresponding to that option β€” meaning there are $n$ questions with the answer 'A', ...
1984C1
https://codeforces.com/problemset/problem/1984/C1
Magnitude (Easy Version)
1,300
[ "dp", "greedy", "math" ]
Div. 1 + 2
2,000
256
The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved. You are given an array $a$ of length $n$. Start with $c = 0$. Then, for each $i$ from $1$ to $n$ (in increasing order) do exactly one of the following: * Option $1$: set $c$ to $c ...
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$). The second line of each case contains $n$ integers $a_1$, $a_2$, $a_3$, $\ldots$, $a_n$ ($-10^9 \leq a_i \leq 10^9$). The sum ...
For each test case, output a single integer β€” the value of $k$.
[ [ "5\n4\n10 -9 -3 4\n8\n1 4 3 4 1 4 3 4\n3\n-1 -2 -3\n4\n-1000000000 1000000000 1000000000 1000000000\n4\n1 9 8 4", "6\n24\n6\n4000000000\n22" ] ]
In the first test case, if we set $c$ to its absolute value every time we add to it, we end up with $6$. It can be shown that this is the maximum result. In the second test case, taking the absolute value will never change anything, so we can just sum the array without doing anything to get $24$. In the third test ca...
Title: Magnitude (Easy Version) time_limit_ms: 2000 memory_limit_mb: 256 Description: The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved. You are given an array $a$ of length $n$. Start with $c = 0$. Then, for each $i$ from $1$ to $n$ ...
1973B
https://codeforces.com/problemset/problem/1973/B
Cat, Fox and the Lonely Array
1,300
[ "binary search", "bitmasks", "data structures", "greedy", "math", "two pointers" ]
Div. 2
2,000
256
Today, Cat and Fox found an array $a$ consisting of $n$ non-negative integers. Define the loneliness of $a$ as the smallest positive integer $k$ ($1 \le k \le n$) such that for any two positive integers $i$ and $j$ ($1 \leq i, j \leq n - k +1$), the following holds: $$a_i | a_{i+1} | \ldots | a_{i+k-1} = a_j | a_{j+1}...
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4 $) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $n$ ($1 \leq n \leq 10^5$) β€” the length of the array $a$. The second line of each...
For each test case, print one integer β€” the loneliness of the given array.
[ [ "7\n1\n0\n3\n2 2 2\n3\n1 0 2\n5\n3 0 1 4 2\n5\n2 0 4 0 2\n7\n0 0 0 0 1 2 4\n8\n0 1 3 2 2 1 0 3", "1\n1\n3\n4\n4\n7\n3" ] ]
In the first example, the loneliness of an array with a single element is always $1$, so the answer is $1$. In the second example, the OR of each subarray of length $k = 1$ is $2$, so the loneliness of the whole array is $1$. In the seventh example, it's true that $(0 | 1 | 3) = (1 | 3 | 2) = (3 | 2 | 2) = (2 | 2 | 1...
Title: Cat, Fox and the Lonely Array time_limit_ms: 2000 memory_limit_mb: 256 Description: Today, Cat and Fox found an array $a$ consisting of $n$ non-negative integers. Define the loneliness of $a$ as the smallest positive integer $k$ ($1 \le k \le n$) such that for any two positive integers $i$ and $j$ ($1 \leq i, j...
2027D2
https://codeforces.com/problemset/problem/2027/D2
The Endspeaker (Hard Version)
2,200
[ "binary search", "data structures", "dp", "greedy", "implementation", "two pointers" ]
Div. 2
2,000
256
This is the hard version of this problem. The only difference is that you need to also output the number of optimal sequences in this version. You must solve both versions to be able to hack. You're given an array $a$ of length $n$, and an array $b$ of length $m$ ($b_i > b_{i+1}$ for all $1 \le i < m$). Initially, the...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 3 \cdot 10^5$, $\boldsymbol{1 \le n \cdot m \le 3 \cdot 10^5}$). The second ...
For each test case, if it's possible to make $a$ empty, then output two integers. The first should be the minimum total cost of the operations, and the second should be the number of sequences of operations which achieve this minimum cost, modulo $10^9 + 7$. If there is no possible sequence of operations which makes $...
[ [ "5\n4 2\n9 3 4 3\n11 7\n1 2\n20\n19 18\n10 2\n2 5 2 1 10 3 2 9 9 6\n17 9\n10 11\n2 2 2 2 2 2 2 2 2 2\n20 18 16 14 12 10 8 6 4 2 1\n1 6\n10\n32 16 8 4 2 1", "1 3\n-1\n2 11\n10 42\n4 1" ] ]
In the first test case, there are $3$ optimal sequences of operations which yield a total cost of $1$: * All $3$ sequences begin with a type $2$ operation, removing the prefix $[9]$ to make $a = [3, 4, 3]$, incurring a cost of $1$. Then, we perform a type $1$ operation to increase the value of $k$ by $1$. All subseq...
Title: The Endspeaker (Hard Version) time_limit_ms: 2000 memory_limit_mb: 256 Description: This is the hard version of this problem. The only difference is that you need to also output the number of optimal sequences in this version. You must solve both versions to be able to hack. You're given an array $a$ of length ...
1993F1
https://codeforces.com/problemset/problem/1993/F1
Dyn-scripted Robot (Easy Version)
2,400
[ "brute force", "chinese remainder theorem", "constructive algorithms", "math", "number theory" ]
Div. 2
3,000
256
This is the easy version of the problem. The only difference is that in this version $k \le n$. You can make hacks only if both versions of the problem are solved. Given a $w \times h$ rectangle on the $Oxy$ plane, with points $(0, 0)$ at the bottom-left and $(w, h)$ at the top-right of the rectangle. You also ha...
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains four integers $n$, $k$, $w$, and $h$ ($1 \le n, w, h \le 10^6$; $1 \le k \le n$). The second line contains a single string $s$ of size $n$ ($s_i \in \\{\texttt{L}, \texttt{R}, \textt...
For each test case, print a single integer β€” the number of times the robot reaches $(0, 0)$ when executing script $s$ for $k$ times continuously.
[ [ "5\n2 2 2 2\nUR\n4 2 1 1\nLLDD\n6 3 3 1\nRLRRRL\n5 5 3 3\nRUURD\n7 5 3 4\nRRDLUUU", "0\n4\n3\n0\n1" ] ]
In the first test case, the robot only moves up and right. In the end, it occupies the position $(2, 2)$ but never visits $(0, 0)$. So the answer is $0$. In the second test case, each time executing the script the robot visits the origin twice. And since $k=2$, it visits the origin $2 \cdot 2 = 4$ times overall. ![](...
Title: Dyn-scripted Robot (Easy Version) time_limit_ms: 3000 memory_limit_mb: 256 Description: This is the easy version of the problem. The only difference is that in this version $k \le n$. You can make hacks only if both versions of the problem are solved. Given a $w \times h$ rectangle on the $Oxy$ plane, with ...
1990C
https://codeforces.com/problemset/problem/1990/C
Mad MAD Sum
1,500
[ "brute force", "greedy", "math" ]
Div. 2
2,000
256
We define the $\operatorname{MAD}$ (Maximum Appearing Duplicate) in an array as the largest number that appears at least twice in the array. Specifically, if there is no number that appears at least twice, the $\operatorname{MAD}$ value is $0$. For example, $\operatorname{MAD}([1, 2, 1]) = 1$, $\operatorname{MAD}([2, ...
The first line contains an integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) β€” the number of test cases. For each test case: * The first line contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β€” the size of the array $a$; * The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) β€” the el...
For each test case, output the value of $sum$ in a new line.
[ [ "4\n1\n1\n3\n2 2 3\n4\n2 1 1 2\n4\n4 4 4 4", "1\n13\n9\n40" ] ]
In the first test case, $a=[1]$ initially. In the first loop: 1. Set $sum := sum + a_1 = 0+1=1$; 2. Set $b_1 :=\ \operatorname{MAD}([a_1])=\ \operatorname{MAD}([1])=0$, and then set $a_1 := b_1$. After the first loop, $a=[0]$ and the process ends. The value of $sum$ after the process is $1$. In the second tes...
Title: Mad MAD Sum time_limit_ms: 2000 memory_limit_mb: 256 Description: We define the $\operatorname{MAD}$ (Maximum Appearing Duplicate) in an array as the largest number that appears at least twice in the array. Specifically, if there is no number that appears at least twice, the $\operatorname{MAD}$ value is $0$. F...
1971A
https://codeforces.com/problemset/problem/1971/A
My First Sorting Problem
800
[ "implementation", "sortings" ]
Div. 4
1,000
256
You are given two integers $x$ and $y$. Output two integers: the minimum of $x$ and $y$, followed by the maximum of $x$ and $y$.
The first line contains a single integer $t$ ($1 \leq t \leq 100$) β€” the number of test cases. The only line of each test case contains two space-separated integers $x$ and $y$ ($0 \leq x, y \leq 9$).
For each test case, output two integers: the minimum of $x$ and $y$, followed by the maximum of $x$ and $y$.
[ [ "10\n1 9\n8 4\n1 4\n3 4\n2 0\n2 4\n6 9\n3 3\n0 0\n9 9", "1 9\n4 8\n1 4\n3 4\n0 2\n2 4\n6 9\n3 3\n0 0\n9 9" ] ]
Title: My First Sorting Problem time_limit_ms: 1000 memory_limit_mb: 256 Description: You are given two integers $x$ and $y$. Output two integers: the minimum of $x$ and $y$, followed by the maximum of $x$ and $y$. Input: The first line contains a single integer $t$ ($1 \leq t \leq 100$) β€” the number of test cases. ...
1994C
https://codeforces.com/problemset/problem/1994/C
Hungry Games
1,600
[ "binary search", "dp", "two pointers" ]
Div. 1 + 2
2,000
256
Yaroslav is playing a computer game, and at one of the levels, he encountered $n$ mushrooms arranged in a row. Each mushroom has its own level of toxicity; the $i$-th mushroom from the beginning has a toxicity level of $a_i$. Yaroslav can choose two integers $1 \le l \le r \le n$, and then his character will take turns...
Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) β€” the number of test cases. Then follows the description of the test cases. The first line of each test case contains two integers $n$, $x$ ($1 \leq n \leq 2 \cdot 10^5, 1 \le x \le 10^9$) β€” the number of mushrooms...
For each test case, output a single number β€” the number of subsegments such that the final value of $g$ will not be zero.
[ [ "5\n4 2\n1 1 1 1\n3 2\n1 2 3\n1 6\n10\n6 3\n1 2 1 4 3 8\n5 999999999\n999999999 999999998 1000000000 1000000000 500000000", "8\n2\n0\n10\n7" ] ]
In the first test case, the subsegments $(1, 1)$, $(1, 2)$, $(1, 4)$, $(2, 2)$, $(2, 3)$, $(3, 3)$, $(3, 4)$ and $(4, 4)$ are suitable. In the second test case, non-zero $g$ will remain only on the subsegments $(1, 1)$ and $(2, 2)$. In the third test case, on the only possible subsegment, $g$ will be zero.
Title: Hungry Games time_limit_ms: 2000 memory_limit_mb: 256 Description: Yaroslav is playing a computer game, and at one of the levels, he encountered $n$ mushrooms arranged in a row. Each mushroom has its own level of toxicity; the $i$-th mushroom from the beginning has a toxicity level of $a_i$. Yaroslav can choose ...
2023E
https://codeforces.com/problemset/problem/2023/E
Tree of Life
3,300
[ "dp", "greedy", "trees" ]
Div. 1
2,000
512
In the heart of an ancient kingdom grows the legendary Tree of Life β€” the only one of its kind and the source of magical power for the entire world. The tree consists of $n$ nodes. Each node of this tree is a magical source, connected to other such sources through magical channels (edges). In total, there are $n-1$ cha...
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 4 \cdot 10^4$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \leq n \leq 5 \cdot 10^5$) β€” the number of nodes in the Tree o...
For each test case, output a single integer β€” the minimum number of paths that the sages need to select to prevent a catastrophe.
[ [ "5\n4\n1 2\n2 3\n3 4\n2\n1 2\n4\n1 2\n1 3\n1 4\n8\n3 7\n2 4\n1 2\n2 5\n3 6\n1 3\n3 8\n6\n2 3\n1 2\n3 6\n1 5\n1 4", "1\n0\n3\n7\n3" ] ]
In the first test case, there are two pairs of channels emanating from a single node: $(1, 2)$ and $(2, 3)$, $(2, 3)$ and $(3, 4)$. It is sufficient to perform the ritual along the path $1-2-3-4$. Thus, the answer is $1$. In the second test case, there are no pairs of channels emanating from a single node, so the answ...
Title: Tree of Life time_limit_ms: 2000 memory_limit_mb: 512 Description: In the heart of an ancient kingdom grows the legendary Tree of Life β€” the only one of its kind and the source of magical power for the entire world. The tree consists of $n$ nodes. Each node of this tree is a magical source, connected to other su...
1971D
https://codeforces.com/problemset/problem/1971/D
Binary Cut
1,100
[ "dp", "greedy", "implementation", "sortings", "strings" ]
Div. 4
2,000
256
You are given a binary string$^{\dagger}$. Please find the minimum number of pieces you need to cut it into, so that the resulting pieces can be rearranged into a sorted binary string. ![](CDN_BASE_URL/f49d41e3510eec9591123a65ee9389ab) Note that: * each character must lie in exactly one of the pieces; * the pie...
The first line contains a single integer $t$ ($1 \leq t \leq 500$) β€” the number of test cases. The only line of each test case contains a single string $s$ ($1 \leq |s| \leq 500$) consisting of characters $\texttt{0}$ and $\texttt{1}$, where $|s|$ denotes the length of the string $s$.
For each test case, output a single integer β€” the minimum number of pieces needed to be able to rearrange the string into a sorted binary string.
[ [ "6\n11010\n00000000\n1\n10\n0001111\n0110", "3\n1\n1\n2\n1\n2" ] ]
The first test case is pictured in the statement. It can be proven that you can't use fewer than $3$ pieces. In the second and third test cases, the binary string is already sorted, so only $1$ piece is needed. In the fourth test case, you need to make a single cut between the two characters and rearrange them to mak...
Title: Binary Cut time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given a binary string$^{\dagger}$. Please find the minimum number of pieces you need to cut it into, so that the resulting pieces can be rearranged into a sorted binary string. ![](CDN_BASE_URL/f49d41e3510eec9591123a65ee9389ab) Note that:...
1972C
https://codeforces.com/problemset/problem/1972/C
Permutation Counting
1,400
[ "binary search", "constructive algorithms", "greedy", "implementation", "math", "sortings" ]
Div. 2
2,000
256
You have some cards. An integer between $1$ and $n$ is written on each card: specifically, for each $i$ from $1$ to $n$, you have $a_i$ cards which have the number $i$ written on them. There is also a shop which contains unlimited cards of each type. You have $k$ coins, so you can buy $k$ new cards in total, and the c...
Each test contains multiple test cases. The first line contains the number of test cases $t\ (1\le t\le 100)$. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1\le n \le 2 \cdot 10^5$, $0\le k \le 10^{12}$) β€” the number of distinct types of cards and the num...
For each test case, output a single line containing an integer: the maximum score you can get.
[ [ "8\n1 10\n1\n2 4\n8 4\n3 4\n6 1 8\n3 9\n7 6 2\n5 3\n6 6 7 4 6\n9 7\n7 6 1 7 6 2 4 3 3\n10 10\n1 3 1 2 1 9 3 5 7 5\n9 8\n5 8 7 5 1 3 2 9 8", "11\n15\n15\n22\n28\n32\n28\n36" ] ]
In the first test case, the final (and only) array we can get is $[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]$ (including $11$ single $1$s), which contains $11$ subarrays consisting of a permutation of $[1]$. In the second test case, we can buy $0$ cards of type $1$ and $4$ cards of type $2$, and then we rearrange the cards as ...
Title: Permutation Counting time_limit_ms: 2000 memory_limit_mb: 256 Description: You have some cards. An integer between $1$ and $n$ is written on each card: specifically, for each $i$ from $1$ to $n$, you have $a_i$ cards which have the number $i$ written on them. There is also a shop which contains unlimited cards ...
1978B
https://codeforces.com/problemset/problem/1978/B
New Bakery
800
[ "binary search", "greedy", "math", "ternary search" ]
Div. 2
1,000
256
Bob decided to open a bakery. On the opening day, he baked $n$ buns that he can sell. The usual price of a bun is $a$ coins, but to attract customers, Bob organized the following promotion: * Bob chooses some integer $k$ ($0 \le k \le \min(n, b)$). * Bob sells the first $k$ buns at a modified price. In this case, ...
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The description of the test cases follows. The only line of each test case contains three integers $n$, $a$, and $b$ ($1 \le n, a, b \le 10^9$) β€” the number of buns, the usual price ...
For each test case, output a single integer β€” the maximum profit that Bob can obtain.
[ [ "7\n4 4 5\n5 5 9\n10 10 5\n5 5 11\n1000000000 1000000000 1000000000\n1000000000 1000000000 1\n1000 1 1000", "17\n35\n100\n45\n1000000000000000000\n1000000000000000000\n500500" ] ]
In the first test case, it is optimal for Bob to choose $k = 1$. Then he will sell one bun for $5$ coins, and three buns at the usual price for $4$ coins each. Then the profit will be $5 + 4 + 4 + 4 = 17$ coins. In the second test case, it is optimal for Bob to choose $k = 5$. Then he will sell all the buns at the mod...
Title: New Bakery time_limit_ms: 1000 memory_limit_mb: 256 Description: Bob decided to open a bakery. On the opening day, he baked $n$ buns that he can sell. The usual price of a bun is $a$ coins, but to attract customers, Bob organized the following promotion: * Bob chooses some integer $k$ ($0 \le k \le \min(n, b)...
1985D
https://codeforces.com/problemset/problem/1985/D
Manhattan Circle
900
[ "implementation", "math" ]
Div. 4
2,000
256
Given a $n$ by $m$ grid consisting of '.' and '#' characters, there exists a whole manhattan circle on the grid. The top left corner of the grid has coordinates $(1,1)$, and the bottom right corner has coordinates $(n, m)$. Point ($a, b$) belongs to the manhattan circle centered at ($h, k$) if $|h - a| + |k - b| < r$,...
The first line contains $t$ ($1 \leq t \leq 1000$) β€” the number of test cases. The first line of each test case contains $n$ and $m$ ($1 \leq n \cdot m \leq 2 \cdot 10^5$) β€” the height and width of the grid, respectively. The next $n$ lines contains $m$ characters '.' or '#'. If the character is '#', then the point ...
For each test case, output the two integers, the coordinates of the center of the circle.
[ [ "6\n5 5\n.....\n.....\n..#..\n.....\n.....\n5 5\n..#..\n.###.\n#####\n.###.\n..#..\n5 6\n......\n......\n.#....\n###...\n.#....\n1 1\n#\n5 6\n...#..\n..###.\n.#####\n..###.\n...#..\n2 10\n..........\n...#......", "3 3\n3 3\n4 2\n1 1\n3 4\n2 4" ] ]
Title: Manhattan Circle time_limit_ms: 2000 memory_limit_mb: 256 Description: Given a $n$ by $m$ grid consisting of '.' and '#' characters, there exists a whole manhattan circle on the grid. The top left corner of the grid has coordinates $(1,1)$, and the bottom right corner has coordinates $(n, m)$. Point ($a, b$) be...
1987D
https://codeforces.com/problemset/problem/1987/D
World is Mine
1,800
[ "dp", "games" ]
Div. 1 + 2
2,000
256
Alice and Bob are playing a game. Initially, there are $n$ cakes, with the $i$-th cake having a tastiness value of $a_i$. Alice and Bob take turns eating them, with Alice starting first: * In her turn, Alice chooses and eats any remaining cake whose tastiness is strictly greater than the maximum tastiness of any of...
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 500$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$) β€” the number of cakes. The second line of each te...
For each test case, output a single integer β€” the number of cakes Alice will eat if both players play optimally.
[ [ "9\n4\n1 4 2 3\n3\n1 1 1\n5\n1 4 2 3 4\n4\n3 4 1 4\n1\n1\n8\n4 3 2 5 6 8 3 4\n7\n6 1 1 3 5 3 1\n11\n6 11 6 8 7 5 3 11 2 3 5\n17\n2 6 5 3 9 1 6 2 5 6 3 2 3 9 6 1 6", "2\n1\n3\n2\n1\n3\n2\n4\n4" ] ]
In the first test case, one possible sequence of turns is: 1. Alice eats a cake with a tastiness value of $1$. The remaining cakes are $[4, 2, 3]$. 2. Bob eats a cake with a tastiness value of $2$. The remaining cakes are $[4, 3]$. 3. Alice eats a cake with a tastiness of $3$. The remaining cakes are $[4]$. ...
Title: World is Mine time_limit_ms: 2000 memory_limit_mb: 256 Description: Alice and Bob are playing a game. Initially, there are $n$ cakes, with the $i$-th cake having a tastiness value of $a_i$. Alice and Bob take turns eating them, with Alice starting first: * In her turn, Alice chooses and eats any remaining ca...
1980A
https://codeforces.com/problemset/problem/1980/A
Problem Generator
800
[ "math" ]
Div. 3
1,000
256
Vlad is planning to hold $m$ rounds next month. Each round should contain one problem of difficulty levels 'A', 'B', 'C', 'D', 'E', 'F', and 'G'. Vlad already has a bank of $n$ problems, where the $i$-th problem has a difficulty level of $a_i$. There may not be enough of these problems, so he may have to come up with ...
The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 50$, $1 \le m \le 5$) β€” the number of problems in the bank and the number of upcoming rounds, respectively. The second line of each test case c...
For each test case, output a single integer β€” the minimum number of problems that need to come up with to hold $m$ rounds.
[ [ "3\n10 1\nBGECDCBDED\n10 2\nBGECDCBDED\n9 1\nBBCDEFFGG", "2\n5\n1" ] ]
Title: Problem Generator time_limit_ms: 1000 memory_limit_mb: 256 Description: Vlad is planning to hold $m$ rounds next month. Each round should contain one problem of difficulty levels 'A', 'B', 'C', 'D', 'E', 'F', and 'G'. Vlad already has a bank of $n$ problems, where the $i$-th problem has a difficulty level of $a...
1984C2
https://codeforces.com/problemset/problem/1984/C2
Magnitude (Hard Version)
1,700
[ "combinatorics", "dp", "greedy", "math" ]
Div. 1 + 2
2,000
256
The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved. You are given an array $a$ of length $n$. Start with $c = 0$. Then, for each $i$ from $1$ to $n$ (in increasing order) do exactly one of the following: * Option $1$: set $c$ to $c ...
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$). The sum of $n$ o...
For each test case, output a single integer β€” the number of unique procedures that result in $c = k$, modulo $998\,244\,353$.
[ [ "5\n4\n2 -5 3 -3\n8\n1 4 3 4 1 4 3 4\n3\n-1 -2 -3\n4\n-1000000000 1000000000 1000000000 1000000000\n4\n1 9 8 4", "12\n256\n1\n8\n16" ] ]
In the first test case, it can be shown that our maximal final value of $c$ is $3$. There are $12$ ways to achieve this because in order to get $3$, we have to take absolute value at indices $2$ or $4$, or both, resulting in $3$ ways. For the other two indices, it doesn't change the value whether we take absolute value...
Title: Magnitude (Hard Version) time_limit_ms: 2000 memory_limit_mb: 256 Description: The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved. You are given an array $a$ of length $n$. Start with $c = 0$. Then, for each $i$ from $1$ to $n$ ...
1976D
https://codeforces.com/problemset/problem/1976/D
Invertible Bracket Sequences
2,000
[ "binary search", "combinatorics", "data structures", "divide and conquer", "implementation", "two pointers" ]
Div. 2
2,000
256
A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example: * bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)");...
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The only line of each test case contains a non-empty regular bracket sequence; it consists only of characters '(' and/or ')'. Additional constraint on the input: the total length of the regular bracket sequences over all tes...
For each test case, print a single integer β€” the number of pairs $(l,r)$ meeting the conditions from the statement.
[ [ "4\n(())\n()\n()()()\n(()())(())", "1\n0\n3\n13" ] ]
In the first example, there is only one pair: * $(2, 3)$: (()) $\rightarrow$ ()(). In the second example, there are no pairs. In the third example, there are three pairs: * $(2, 3)$: ()()() $\rightarrow$ (())(); * $(4, 5)$: ()()() $\rightarrow$ ()(()); * $(2, 5)$: ()()() $\rightarrow$ (()());
Title: Invertible Bracket Sequences time_limit_ms: 2000 memory_limit_mb: 256 Description: A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example: * bracket sequences...
2002F1
https://codeforces.com/problemset/problem/2002/F1
Court Blue (Easy Version)
2,600
[ "brute force", "dfs and similar", "dp", "math", "number theory" ]
Div. 1 + 2
3,000
512
This is the easy version of the problem. In this version, $n=m$ and the time limit is lower. You can make hacks only if both versions of the problem are solved. In the court of the Blue King, Lelle and Flamm are having a performance match. The match consists of several rounds. In each round, either Lelle or Flamm wins...
The first line contains an integer $t$ ($1\leq t \leq 10^3$) β€” the number of test cases. The only line of each test case contains four integers $n$, $m$, $l$, $f$ ($2\leq n\leq m \leq 2\cdot 10^7$, $1\leq l,f \leq 10^9$, $\bf{n=m}$): $n$, $m$ gives the upper bound on the number of Lelle and Flamm's wins, $l$ and $f$ d...
For each test case, output a single integer β€” the maximum total score of a successful performance.
[ [ "8\n3 3 2 5\n4 4 1 4\n6 6 2 2\n7 7 2 3\n9 9 9 1\n2 2 1 4\n5 5 1 4\n8 8 6 7", "19\n17\n18\n33\n86\n9\n24\n86" ], [ "1\n20000000 20000000 1341 331", "33439999007" ], [ "2\n1984 1984 19 84\n9982 9982 44 35", "204143\n788403" ] ]
In the first test case, a possible performance is as follows: * Flamm wins, $\gcd(0,1)=1$. * Lelle wins, $\gcd(1,1)=1$. * Flamm wins, $\gcd(1,2)=1$. * Flamm wins, $\gcd(1,3)=1$. * Lelle wins, $\gcd(2,3)=1$. * Lelle and Flamm agree to stop the match. The final score is $2\cdot2+3\cdot5=19$. In the t...
Title: Court Blue (Easy Version) time_limit_ms: 3000 memory_limit_mb: 512 Description: This is the easy version of the problem. In this version, $n=m$ and the time limit is lower. You can make hacks only if both versions of the problem are solved. In the court of the Blue King, Lelle and Flamm are having a performance...
2027B
https://codeforces.com/problemset/problem/2027/B
Stalin Sort
1,100
[ "brute force", "greedy" ]
Div. 2
1,000
256
Stalin Sort is a humorous sorting algorithm designed to eliminate elements which are out of place instead of bothering to sort them properly, lending itself to an $\mathcal{O}(n)$ time complexity. It goes as follows: starting from the second element in the array, if it is strictly smaller than the previous element (ig...
Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 500$) β€” the number of test cases. This is followed by descriptions of the test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2000$) β€” the size of the array. The second line of eac...
For each test case, output a single integer β€” the minimum number of integers which must be removed from the array to make it vulnerable.
[ [ "6\n7\n3 6 4 9 2 5 2\n5\n5 4 4 2 2\n8\n2 2 4 4 6 6 10 10\n1\n1000\n9\n6 8 9 10 12 9 7 5 4\n7\n300000000 600000000 400000000 900000000 200000000 400000000 200000000", "2\n0\n6\n0\n4\n2" ] ]
In the first test case, the optimal answer is to remove the numbers $3$ and $9$. Then we are left with $a = [6, 4, 2, 5, 2]$. To show this array is vulnerable, we can first apply a Stalin Sort on the subarray $[4, 2, 5]$ to get $a = [6, 4, 5, 2]$ and then apply a Stalin Sort on the subarray $[6, 4, 5]$ to get $a = [6, ...
Title: Stalin Sort time_limit_ms: 1000 memory_limit_mb: 256 Description: Stalin Sort is a humorous sorting algorithm designed to eliminate elements which are out of place instead of bothering to sort them properly, lending itself to an $\mathcal{O}(n)$ time complexity. It goes as follows: starting from the second elem...
1982F
https://codeforces.com/problemset/problem/1982/F
Sorting Problem Again
2,600
[ "binary search", "data structures", "sortings" ]
Div. 2
2,500
256
You have an array $a$ of $n$ elements. There are also $q$ modifications of the array. Before the first modification and after each modification, you would like to know the following: What is the minimum length subarray that needs to be sorted in non-decreasing order in order for the array $a$ to be completely sorted i...
Each test consists of several test cases. The first line contains an integer $t$ ($1 \le t \le 10$) β€” the number of test cases. Then follows the description of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 5 \cdot 10^{5}$). The second line of each test case contains $n$ inte...
For each test case, output $q + 1$ lines. Each line should contain $2$ integers $l, r$ β€” the boundaries of the minimum subarray, such that sorting it will make the array $a$ completely sorted. If $a$ is already sorted, then output $l = -1$, $r = -1$.
[ [ "2\n5\n2 2 3 4 5\n3\n2 1\n4 1\n1 1\n5\n1 2 3 4 5\n9\n1 4\n2 3\n5 2\n3 1\n1 1\n5 1\n4 1\n3 1\n2 1", "-1 -1\n1 2\n1 4\n3 4\n-1 -1\n1 3\n1 3\n1 5\n1 5\n2 5\n2 5\n2 5\n2 5\n-1 -1" ] ]
Let's consider the first test case: * Initially, the array is sorted in non-decreasing order: $[2, 2, 3, 4, 5]$ * After the first query, the array looks like this: $[\color{red}{2}, \color{red}{1}, 3, 4, 5]$. * After the second query, the array looks like this: $[\color{red}{2}, \color{red}{1}, \color{red}{3},...
Title: Sorting Problem Again time_limit_ms: 2500 memory_limit_mb: 256 Description: You have an array $a$ of $n$ elements. There are also $q$ modifications of the array. Before the first modification and after each modification, you would like to know the following: What is the minimum length subarray that needs to be ...
1992B
https://codeforces.com/problemset/problem/1992/B
Angry Monk
800
[ "greedy", "math", "sortings" ]
Div. 3
2,000
256
To celebrate his recovery, k1o0n has baked an enormous $n$ metres long potato casserole. Turns out, Noobish_Monk just can't stand potatoes, so he decided to ruin k1o0n's meal. He has cut it into $k$ pieces, of lengths $a_1, a_2, \dots, a_k$ meters. k1o0n wasn't keen on that. Luckily, everything can be fixed. In order...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of each test case consists of two lines. The first line contains two integers $n$ and $k$ ($2 \le n \le 10^9$, $2 \le k \le 10^5$) β€” length of casserole and the number of pieces. The second l...
For each test case, output the minimum number of operations K1o0n needs to restore his pie after the terror of Noobish_Monk.
[ [ "4\n5 3\n3 1 1\n5 2\n3 2\n11 4\n2 3 1 5\n16 6\n1 6 1 1 1 6", "2\n3\n9\n15" ] ]
Title: Angry Monk time_limit_ms: 2000 memory_limit_mb: 256 Description: To celebrate his recovery, k1o0n has baked an enormous $n$ metres long potato casserole. Turns out, Noobish_Monk just can't stand potatoes, so he decided to ruin k1o0n's meal. He has cut it into $k$ pieces, of lengths $a_1, a_2, \dots, a_k$ meters...
1974C
https://codeforces.com/problemset/problem/1974/C
Beautiful Triple Pairs
1,400
[ "combinatorics", "data structures" ]
Div. 3
4,000
256
Polycarp was given an array $a$ of $n$ integers. He really likes triples of numbers, so for each $j$ ($1 \le j \le n - 2$) he wrote down a triple of elements $[a_j, a_{j + 1}, a_{j + 2}]$. Polycarp considers a pair of triples $b$ and $c$ beautiful if they differ in exactly one position, that is, one of the following c...
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($3 \le n \le 2 \cdot 10^5$) β€” the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$)...
For each test case, output a single integer β€” the number of beautiful pairs of triples among the pairs of the form $[a_j, a_{j + 1}, a_{j + 2}]$. Note that the answer may not fit into 32-bit data types.
[ [ "8\n5\n3 2 2 2 3\n5\n1 2 1 2 1\n8\n1 2 3 2 2 3 4 2\n4\n2 1 1 1\n8\n2 1 1 2 1 1 1 1\n7\n2 1 1 1 1 1 1\n6\n2 1 1 1 1 1\n5\n2 1 1 1 1", "2\n0\n3\n1\n8\n4\n3\n2" ] ]
In the first example, $a = [3, 2, 2, 2, 3]$, Polycarp will write the following triples: 1. $[3, 2, 2]$; 2. $[2, 2, 2]$; 3. $[2, 2, 3]$. The beautiful pairs are triple $1$ with triple $2$ and triple $2$ with triple $3$. In the third example, $a = [1, 2, 3, 2, 2, 3, 4, 2]$, Polycarp will write the following t...
Title: Beautiful Triple Pairs time_limit_ms: 4000 memory_limit_mb: 256 Description: Polycarp was given an array $a$ of $n$ integers. He really likes triples of numbers, so for each $j$ ($1 \le j \le n - 2$) he wrote down a triple of elements $[a_j, a_{j + 1}, a_{j + 2}]$. Polycarp considers a pair of triples $b$ and $...
2021B
https://codeforces.com/problemset/problem/2021/B
Maximize Mex
1,200
[ "brute force", "greedy", "math", "number theory" ]
Div. 2
1,000
256
You are given an array $a$ of $n$ positive integers and an integer $x$. You can do the following two-step operation any (possibly zero) number of times: 1. Choose an index $i$ ($1 \leq i \leq n$). 2. Increase $a_i$ by $x$, in other words $a_i := a_i + x$. Find the maximum value of the $\operatorname{MEX}$ of $a...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows. The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 2 \cdot 10^5$; $1 \le x \le 10^9$) β€” the length of the array and the integer to ...
For each test case, output a single integer: the maximum $\operatorname{MEX}$ of $a$ if you perform the operations optimally.
[ [ "3\n6 3\n0 3 2 1 5 2\n6 2\n1 3 4 1 0 2\n4 5\n2 5 10 3", "4\n6\n0" ] ]
In the first test case, the $\operatorname{MEX}$ of $a$ is $4$ without performing any operations, which is the maximum. In the second test case, the $\operatorname{MEX}$ of $a$ is $5$ without performing any operations. If we perform two operations both with $i=1$, we will have the array $a=[5,3,4,1,0,2]$. Then, the $\...
Title: Maximize Mex time_limit_ms: 1000 memory_limit_mb: 256 Description: You are given an array $a$ of $n$ positive integers and an integer $x$. You can do the following two-step operation any (possibly zero) number of times: 1. Choose an index $i$ ($1 \leq i \leq n$). 2. Increase $a_i$ by $x$, in other words $a...
1973F
https://codeforces.com/problemset/problem/1973/F
Maximum GCD Sum Queries
3,100
[ "bitmasks", "brute force", "dp", "implementation", "number theory" ]
Div. 2
5,000
512
For $k$ positive integers $x_1, x_2, \ldots, x_k$, the value $\gcd(x_1, x_2, \ldots, x_k)$ is the greatest common divisor of the integers $x_1, x_2, \ldots, x_k$ β€” the largest integer $z$ such that all the integers $x_1, x_2, \ldots, x_k$ are divisible by $z$. You are given three arrays $a_1, a_2, \ldots, a_n$, $b_1, ...
There are two integers on the first line β€” the numbers $n$ and $q$ ($1 \leq n \leq 5 \cdot 10^5$, $1 \leq q \leq 5 \cdot 10^5$). On the second line, there are $n$ integers β€” the numbers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^8$). On the third line, there are $n$ integers β€” the numbers $b_1, b_2, \ldots, b_n$ ($...
Print $q$ integers β€” the maximum value you can get for each of the $q$ possible values $d$.
[ [ "3 4\n1 2 3\n4 5 6\n1 1 1\n0 1 2 3", "2 3 3 3" ], [ "5 5\n3 4 6 8 4\n8 3 4 9 3\n10 20 30 40 50\n5 55 13 1000 113", "2 7 3 7 7" ], [ "1 1\n3\n4\n5\n0", "7" ] ]
In the first query of the first example, we are not allowed to do any swaps at all, so the answer is $\gcd(1, 2, 3) + \gcd(4, 5, 6) = 2$. In the second query, one of the ways to achieve the optimal value is to swap $a_2$ and $b_2$, then the answer is $\gcd(1, 5, 3) + \gcd(4, 2, 6) = 3$. In the second query of the seco...
Title: Maximum GCD Sum Queries time_limit_ms: 5000 memory_limit_mb: 512 Description: For $k$ positive integers $x_1, x_2, \ldots, x_k$, the value $\gcd(x_1, x_2, \ldots, x_k)$ is the greatest common divisor of the integers $x_1, x_2, \ldots, x_k$ β€” the largest integer $z$ such that all the integers $x_1, x_2, \ldots, x...
1985F
https://codeforces.com/problemset/problem/1985/F
Final Boss
1,500
[ "binary search", "data structures" ]
Div. 4
2,000
256
You are facing the final boss in your favorite video game. The boss enemy has $h$ health. Your character has $n$ attacks. The $i$'th attack deals $a_i$ damage to the boss but has a cooldown of $c_i$ turns, meaning the next time you can use this attack is turn $x + c_i$ if your current turn is $x$. Each turn, you can us...
The first line contains $t$ ($1 \leq t \leq 10^4$) – the number of test cases. The first line of each test case contains two integers $h$ and $n$ ($1 \leq h, n \leq 2 \cdot 10^5$) – the health of the boss and the number of attacks you have. The following line of each test case contains $n$ integers $a_1, a_2, ..., a...
For each test case, output an integer, the minimum number of turns required to beat the boss.
[ [ "8\n3 2\n2 1\n2 1\n5 2\n2 1\n2 1\n50 3\n5 6 7\n5 6 7\n50 3\n2 2 2\n3 3 3\n90000 2\n200000 200000\n1 1\n100000 1\n1\n200000\n6 7\n3 2 3 2 3 1 2\n6 5 9 5 10 7 7\n21 6\n1 1 1 1 1 1\n5 5 8 10 7 6", "1\n3\n15\n25\n1\n19999800001\n1\n21" ] ]
For the first test case, you can use attacks $1$ and $2$ on the first turn, dealing $3$ damage in total, and slaying the boss. For the second case, you can beat the boss in $3$ turns by using the following attacks: Turn $1$: Use attacks $1$ and $2$, dealing $3$ damage to the boss. The boss now has $2$ health left. T...
Title: Final Boss time_limit_ms: 2000 memory_limit_mb: 256 Description: You are facing the final boss in your favorite video game. The boss enemy has $h$ health. Your character has $n$ attacks. The $i$'th attack deals $a_i$ damage to the boss but has a cooldown of $c_i$ turns, meaning the next time you can use this att...
2028F
https://codeforces.com/problemset/problem/2028/F
Alice's Adventures in Addition
2,700
[ "bitmasks", "brute force", "dp", "implementation" ]
Div. 2
3,000
32
Note that the memory limit is unusual. The Cheshire Cat has a riddle for Alice: given $n$ integers $a_1, a_2, \ldots, a_n$ and a target $m$, is there a way to insert $+$ and $\times$ into the circles of the expression $$a_1 \circ a_2 \circ \cdots \circ a_n = m$$ to make it true? We follow the usual order of operations...
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n, m$ ($1\le n\le 2\cdot 10^5$; $1\le m\le 10^4$) β€” the number of integers an...
For each test case, output "YES" without quotes if it is possible to get the target by inserting $+$ or $\times$ and "NO" otherwise. You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
[ [ "6\n5 4\n2 1 1 1 2\n5 5\n2 1 1 1 2\n5 6\n2 1 1 1 2\n5 7\n2 1 1 1 2\n5 8\n2 1 1 1 2\n5 6\n2 0 2 2 3", "YES\nYES\nYES\nYES\nNO\nYES" ] ]
Possible solutions for the first four test cases are shown below. $$\begin{align*} 2 \times 1 + 1 \times 1 \times 2 &= 4 \\\ 2 \times 1 + 1 + 1 \times 2 &= 5 \\\ 2 \times 1 + 1 + 1 \times 2 &= 6 \\\ 2 + 1 + 1 + 1 + 2 &= 7 \\\ \end{align*}$$ It is impossible to get a result of $8$ in the fifth test case.
Title: Alice's Adventures in Addition time_limit_ms: 3000 memory_limit_mb: 32 Description: Note that the memory limit is unusual. The Cheshire Cat has a riddle for Alice: given $n$ integers $a_1, a_2, \ldots, a_n$ and a target $m$, is there a way to insert $+$ and $\times$ into the circles of the expression $$a_1 \cir...
1986C
https://codeforces.com/problemset/problem/1986/C
Update Queries
1,100
[ "data structures", "greedy", "sortings" ]
Div. 3
2,000
256
Let's consider the following simple problem. You are given a string $s$ of length $n$, consisting of lowercase Latin letters, as well as an array of indices $ind$ of length $m$ ($1 \leq ind_i \leq n$) and a string $c$ of length $m$, consisting of lowercase Latin letters. Then, in order, you perform the update operation...
Each test consists of several sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of sets of input data. Then follows their description. The first line of each set of input data contains two integers $n$ and $m$ ($1 \leq n, m \leq 10^5$) β€” the length of the string $s$ a...
For each set of input data, output the lexicographically smallest string $s$ that can be obtained by rearranging the indices in the array $ind$ and the letters in the string $c$ as you like.
[ [ "4\n1 2\na\n1 1\ncb\n4 4\nmeow\n1 2 1 4\nzcwz\n7 4\nabacaba\n1 3 5 7\ndamn\n7 10\ntraktor\n7 6 5 4 3 2 1 6 4 2\ncodeforces", "b\ncwoz\nabdcmbn\nccdeefo" ] ]
In the first set of input data, you can leave the array $ind$ and the string $c$ unchanged and simply perform all operations in that order. In the second set of input data, you can set the array $ind = [1, 1, 4, 2]$ and $c =$ "zczw". Then the string $s$ will change as follows: $meow \rightarrow zeow \rightarrow ceow \...
Title: Update Queries time_limit_ms: 2000 memory_limit_mb: 256 Description: Let's consider the following simple problem. You are given a string $s$ of length $n$, consisting of lowercase Latin letters, as well as an array of indices $ind$ of length $m$ ($1 \leq ind_i \leq n$) and a string $c$ of length $m$, consisting ...
2025E
https://codeforces.com/problemset/problem/2025/E
Card Game
2,200
[ "combinatorics", "dp", "fft", "greedy", "math" ]
Div. 2
2,000
512
In the most popular card game in Berland, a deck of $n \times m$ cards is used. Each card has two parameters: suit and rank. Suits in the game are numbered from $1$ to $n$, and ranks are numbered from $1$ to $m$. There is exactly one card in the deck for each combination of suit and rank. A card with suit $a$ and rank...
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 500$). Additional constraint on the input: $m$ is even.
Print a single integer β€” the number of ways to distribute the cards so that the first player wins, taken modulo $998244353$.
[ [ "1 4", "2" ], [ "2 2", "2" ], [ "3 6", "1690" ], [ "5 4", "568" ], [ "500 500", "84693741" ] ]
Title: Card Game time_limit_ms: 2000 memory_limit_mb: 512 Description: In the most popular card game in Berland, a deck of $n \times m$ cards is used. Each card has two parameters: suit and rank. Suits in the game are numbered from $1$ to $n$, and ranks are numbered from $1$ to $m$. There is exactly one card in the dec...
1989B
https://codeforces.com/problemset/problem/1989/B
Substring and Subsequence
1,200
[ "brute force", "greedy", "strings" ]
Div. 2
2,000
256
You are given two strings $a$ and $b$, both consisting of lowercase Latin letters. A subsequence of a string is a string which can be obtained by removing several (possibly zero) characters from the original string. A substring of a string is a contiguous subsequence of that string. For example, consider the string a...
The first line contains a single integer $t$ ($1 \le t \le 10^3$) β€” the number of test cases. The first line of each test case contains a string $a$ ($1 \le |a| \le 100$), consisting of lowercase Latin letters. The second line of each test case contains a string $b$ ($1 \le |b| \le 100$), consisting of lowercase Lati...
For each test case, print a single integer β€” the minimum possible length of the string that contains $a$ as a substring and $b$ as a subsequence.
[ [ "5\naba\ncb\ner\ncf\nmmm\nmmm\ncontest\ntest\ncde\nabcefg", "4\n4\n3\n7\n7" ] ]
In the examples below, the characters that correspond to the subsequence equal to $b$ are bolded. In the first example, one of the possible answers is caba. In the second example, one of the possible answers is ercf. In the third example, one of the possible answers is mmm. In the fourth example, one of the possibl...
Title: Substring and Subsequence time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given two strings $a$ and $b$, both consisting of lowercase Latin letters. A subsequence of a string is a string which can be obtained by removing several (possibly zero) characters from the original string. A substring of a...
1986F
https://codeforces.com/problemset/problem/1986/F
Non-academic Problem
1,900
[ "dfs and similar", "graphs", "trees" ]
Div. 3
2,000
256
You are given a connected undirected graph, the vertices of which are numbered with integers from $1$ to $n$. Your task is to minimize the number of pairs of vertices $1 \leq u < v \leq n$ between which there exists a path in this graph. To achieve this, you can remove exactly one edge from the graph. Find the smalles...
Each test consists of several sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of sets of input data. Then follows their description. The first line of each set of input data contains two integers $n$ and $m$ ($2 \leq n \leq 10^5$, $n - 1 \leq m \leq \min(10^5, \frac...
For each set of input data, output the smallest number of pairs of reachable vertices, if exactly one edge can be removed.
[ [ "6\n2 1\n1 2\n3 3\n1 2\n2 3\n1 3\n5 5\n1 2\n1 3\n3 4\n4 5\n5 3\n6 7\n1 2\n1 3\n2 3\n3 4\n4 5\n4 6\n5 6\n5 5\n1 2\n1 3\n2 3\n2 4\n3 5\n10 12\n1 2\n1 3\n2 3\n2 4\n4 5\n5 6\n6 7\n7 4\n3 8\n8 9\n9 10\n10 8", "0\n3\n4\n6\n6\n21" ] ]
In the first set of input data, we will remove the single edge $(1, 2)$ and the only pair of vertices $(1, 2)$ will become unreachable from each other. In the second set of input data, no matter which edge we remove, all vertices will be reachable from each other. In the fourth set of input data, the graph looks like...
Title: Non-academic Problem time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given a connected undirected graph, the vertices of which are numbered with integers from $1$ to $n$. Your task is to minimize the number of pairs of vertices $1 \leq u < v \leq n$ between which there exists a path in this graph. ...
1974E
https://codeforces.com/problemset/problem/1974/E
Money Buys Happiness
1,800
[ "dp" ]
Div. 3
3,000
256
Being a physicist, Charlie likes to plan his life in simple and precise terms. For the next $m$ months, starting with no money, Charlie will work hard and earn $x$ pounds per month. For the $i$-th month $(1 \le i \le m)$, there'll be a single opportunity of paying cost $c_i$ pounds to obtain happiness $h_i$. Borrowin...
The first line of input contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers, $m$ and $x$ ($1 \le m \le 50$, $1 \le x \le 10^8$) β€” the total number of months and the monthly salary. The $i$-th of the following $m$ lines contains two inte...
For each test case, print a single integer, the maximum sum of happiness Charlie could obtain.
[ [ "7\n1 10\n1 5\n2 80\n0 10\n200 100\n3 100\n70 100\n100 200\n150 150\n5 8\n3 1\n5 3\n3 4\n1 5\n5 3\n2 5\n1 5\n2 1\n5 3\n2 5\n2 4\n4 1\n5 1\n3 4\n5 2\n2 1\n1 2\n3 5\n3 2\n3 2", "0\n10\n200\n15\n1\n9\n9" ] ]
In the first test case, Charlie only gets paid at the end of the month, so is unable to afford anything. In the second test case, Charlie obtains the free happiness in the first month. In the third test case, it's optimal for Charlie to buy happiness in the second month. Even with money left at the end, Charlie could...
Title: Money Buys Happiness time_limit_ms: 3000 memory_limit_mb: 256 Description: Being a physicist, Charlie likes to plan his life in simple and precise terms. For the next $m$ months, starting with no money, Charlie will work hard and earn $x$ pounds per month. For the $i$-th month $(1 \le i \le m)$, there'll be a s...
1989E
https://codeforces.com/problemset/problem/1989/E
Distance to Different
2,300
[ "combinatorics", "dp", "math" ]
Div. 2
2,000
512
Consider an array $a$ of $n$ integers, where every element is from $1$ to $k$, and every integer from $1$ to $k$ appears at least once. Let the array $b$ be constructed as follows: for the $i$-th element of $a$, $b_i$ is the distance to the closest element in $a$ which is not equal to $a_i$. In other words, $b_i = \mi...
The only line of the input contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le \min(n, 10)$).
Print one integer β€” the number of different arrays $b$ you can obtain, taken modulo $998244353$.
[ [ "2 2", "1" ], [ "4 3", "3" ], [ "6 2", "20" ], [ "6 5", "3" ], [ "133 7", "336975971" ] ]
Title: Distance to Different time_limit_ms: 2000 memory_limit_mb: 512 Description: Consider an array $a$ of $n$ integers, where every element is from $1$ to $k$, and every integer from $1$ to $k$ appears at least once. Let the array $b$ be constructed as follows: for the $i$-th element of $a$, $b_i$ is the distance to...
2033F
https://codeforces.com/problemset/problem/2033/F
Kosuke's Sloth
1,800
[ "brute force", "math", "number theory" ]
Div. 3
1,000
256
Kosuke is too lazy. He will not give you any legend, just the task: Fibonacci numbers are defined as follows: * $f(1)=f(2)=1$. * $f(n)=f(n-1)+f(n-2)$ $(3\le n)$ We denote $G(n,k)$ as an index of the $n$-th Fibonacci number that is divisible by $k$. For given $n$ and $k$, compute $G(n,k)$. As this number can b...
The first line of the input data contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first and only line contains two integers $n$ and $k$ ($1 \le n \le 10^{18}$, $1 \le k \le 10^5$). It is guaranteed that the sum of $k$ across all test cases does not exceed $10^6$.
For each test case, output the only number: the value $G(n,k)$ taken by modulo $10^9+7$.
[ [ "3\n3 2\n100 1\n1000000000000 1377", "9\n100\n999244007" ] ]
Title: Kosuke's Sloth time_limit_ms: 1000 memory_limit_mb: 256 Description: Kosuke is too lazy. He will not give you any legend, just the task: Fibonacci numbers are defined as follows: * $f(1)=f(2)=1$. * $f(n)=f(n-1)+f(n-2)$ $(3\le n)$ We denote $G(n,k)$ as an index of the $n$-th Fibonacci number that is divi...
2005E1
https://codeforces.com/problemset/problem/2005/E1
Subtangle Game (Easy Version)
2,100
[ "dp", "games", "greedy", "implementation" ]
Div. 2
2,000
256
This is the easy version of the problem. The differences between the two versions are the constraints on all the variables. You can make hacks only if both versions of the problem are solved. Tsovak and Narek are playing a game. They have an array $a$ and a matrix $b$ of integers with $n$ rows and $m$ columns, numbere...
The first line of the input contains $t$ ($1 \le t \le 300$) – the number of test cases. The first line of each test case contains three integers $l$, $n$, and $m$ ($1 \le l, n, m \le 300$) – the size of the array and the sizes of the matrix. The second line contains $l$ integers $a_1, a_2, a_3, \ldots a_l$ ($1 \le a...
You should output $t$ lines, the $i$-th of them containing a character representing the answer of the $i$-th test case: "T" if Tsovak wins or "N", otherwise (without quotes).
[ [ "3\n2 2 3\n1 2\n1 3 5\n4 5 2\n2 2 4\n1 2\n1 1 3 2\n4 2 5 1\n2 4 2\n1 2\n3 4\n5 5\n5 5\n5 5", "N\nT\nN" ] ]
In the first example, Tsovak starts by looking for $1$. There is only one occurrence of $1$ at $(1,1)$, so he chooses it. Then Narek needs to look for $2$ in the submatrix of $(2, 2)$, which consists of just the last two elements: $5$ and $2$. He chooses $2$, and then Tsovak loses since the array has ended. In the sec...
Title: Subtangle Game (Easy Version) time_limit_ms: 2000 memory_limit_mb: 256 Description: This is the easy version of the problem. The differences between the two versions are the constraints on all the variables. You can make hacks only if both versions of the problem are solved. Tsovak and Narek are playing a game....
1981F
https://codeforces.com/problemset/problem/1981/F
Turtle and Paths on a Tree
3,000
[ "data structures", "dp", "trees" ]
Div. 2
4,000
1,024
Note the unusual definition of $\text{MEX}$ in this problem. Piggy gave Turtle a binary tree$^{\dagger}$ with $n$ vertices and a sequence $a_1, a_2, \ldots, a_n$ on his birthday. The binary tree is rooted at vertex $1$. If a set of paths $P = \\{(x_i, y_i)\\}$ in the tree covers each edge exactly once, then Turtle wi...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \le n \le 2.5 \cdot 10^4$) β€” the number of vertices in the tree. The second line of each test...
For each test case, output a single integer β€” the minimum value over all good sets of paths.
[ [ "5\n5\n3 2 2 1 1\n1 1 2 2\n5\n3 2 1 1 1\n1 1 2 2\n6\n1 2 1 2 1 3\n1 2 3 3 4\n7\n2 1 2 3 1 2 1\n1 1 2 2 3 3\n10\n1 2 2 1 4 2 3 1 2 1\n1 1 2 2 3 3 4 5 5", "4\n6\n6\n6\n7" ] ]
In the first test case, the tree is as follows. The number in brackets denotes the weight of the vertex: ![](CDN_BASE_URL/45a1635df0fcab1b711d2165163f0dfb) The good set of paths with the minimum value is $\\{(2, 3), (4, 5)\\}$. Note that in this test case $\\{(4, 5)\\}$ and $\\{(3, 4), (4, 5)\\}$ are not good sets o...
Title: Turtle and Paths on a Tree time_limit_ms: 4000 memory_limit_mb: 1024 Description: Note the unusual definition of $\text{MEX}$ in this problem. Piggy gave Turtle a binary tree$^{\dagger}$ with $n$ vertices and a sequence $a_1, a_2, \ldots, a_n$ on his birthday. The binary tree is rooted at vertex $1$. If a set ...
2035B
https://codeforces.com/problemset/problem/2035/B
Everyone Loves Tres
900
[ "constructive algorithms", "greedy", "math", "number theory" ]
Div. 1 + 2
1,000
256
There are 3 heroes and 3 villains, so 6 people in total. Given a positive integer $n$. Find the smallest integer whose decimal representation has length $n$ and consists only of $3$s and $6$s such that it is divisible by both $33$ and $66$. If no such integer exists, print $-1$.
The first line contains a single integer $t$ ($1\le t\le 500$) β€” the number of test cases. The only line of each test case contains a single integer $n$ ($1\le n\le 500$) β€” the length of the decimal representation.
For each test case, output the smallest required integer if such an integer exists and $-1$ otherwise.
[ [ "6\n1\n2\n3\n4\n5\n7", "-1\n66\n-1\n3366\n36366\n3336366" ] ]
For $n=1$, no such integer exists as neither $3$ nor $6$ is divisible by $33$. For $n=2$, $66$ consists only of $6$s and it is divisible by both $33$ and $66$. For $n=3$, no such integer exists. Only $363$ is divisible by $33$, but it is not divisible by $66$. For $n=4$, $3366$ and $6666$ are divisible by both $33$ ...
Title: Everyone Loves Tres time_limit_ms: 1000 memory_limit_mb: 256 Description: There are 3 heroes and 3 villains, so 6 people in total. Given a positive integer $n$. Find the smallest integer whose decimal representation has length $n$ and consists only of $3$s and $6$s such that it is divisible by both $33$ and $66...
1973E
https://codeforces.com/problemset/problem/1973/E
Cat, Fox and Swaps
2,500
[ "graphs", "math", "sortings" ]
Div. 2
2,000
256
Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her β€” he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constrai...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers: the ...
For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints.
[ [ "7\n2\n2 1\n3\n3 1 2\n4\n3 2 1 4\n5\n5 3 1 2 4\n5\n1 2 3 4 5\n6\n3 2 1 5 4 6\n6\n1 3 2 4 5 6", "6\n11\n23\n29\n55\n46\n58" ] ]
In the first example, we need to be able to swap $1$ and $2$, so we must be able to swap numbers with sum $3$. There are exactly $6$ pairs satisfying the condition: $(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$ and $(3, 4)$, so the answer is $6$. In the second example, the $11$ pairs satisfying the condition are $(1, 4), (...
Title: Cat, Fox and Swaps time_limit_ms: 2000 memory_limit_mb: 256 Description: Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her β€” he is able to swap any two numbers $x$...
2029G
https://codeforces.com/problemset/problem/2029/G
Balanced Problem
3,000
[ "data structures", "dp" ]
Div. 1 + 2
3,000
1,024
There is an array $a$ consisting of $n$ integers. Initially, all elements of $a$ are equal to $0$. Kevin can perform several operations on the array. Each operation is one of the following two types: * Prefix addition β€” Kevin first selects an index $x$ ($1\le x\le n$), and then for each $1\le j\le x$, increases $a_...
Each test contains multiple test cases. The first line of the input contains a single integer $t$ ($1\le t\le 1000$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains three integers $n$, $m$, and $V$ ($1\le n, m\le 2\cdot 10^5$, $1\le V\le 2000$) β€” the length ...
For each test case, output $V$ integers in a single line, the $i$-th integer denoting the maximum possible beauty after Kevin performs some new operations when $v=i$.
[ [ "5\n3 3 2\n1 2 4\nL 3\nR 3\nL 1\n3 3 2\n5 1 4\nL 3\nR 3\nL 1\n5 4 5\n1 1 1 1 1\nL 3\nR 2\nL 5\nL 4\n10 12 9\n10 9 8 7 6 5 4 3 2 1\nL 2\nL 4\nR 4\nR 4\nL 6\nR 8\nL 3\nL 2\nR 1\nR 10\nL 8\nL 1\n1 1 4\n1000000000\nL 1", "2 6\n1 9\n0 1 3 5 5\n0 0 0 6 25 32 35 44 51\n1000000000 1000000000 1000000000 1000000000...
In the first test case, the array $a$ changes as follows for the initial operations: $[0, 0, 0] \xrightarrow{\mathtt{L}\ 3} [1, 1, 1] \xrightarrow{\mathtt{R}\ 3} [1, 1, 2] \xrightarrow{\mathtt{L}\ 1} [2, 1, 2]$. * For $v=1$, it is optimal to not perform any new operations, and the beauty is $b=c_2=2$; * For $v=2$...
Title: Balanced Problem time_limit_ms: 3000 memory_limit_mb: 1024 Description: There is an array $a$ consisting of $n$ integers. Initially, all elements of $a$ are equal to $0$. Kevin can perform several operations on the array. Each operation is one of the following two types: * Prefix addition β€” Kevin first selec...
1982B
https://codeforces.com/problemset/problem/1982/B
Collatz Conjecture
1,200
[ "brute force", "implementation", "math", "number theory" ]
Div. 2
1,000
256
Recently, the first-year student Maxim learned about the Collatz conjecture, but he didn't pay much attention during the lecture, so he believes that the following process is mentioned in the conjecture: There is a variable $x$ and a constant $y$. The following operation is performed $k$ times: * increase $x$ by $1...
Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) β€” the number of test cases. Then follows the description of the test cases. The only line of each test case contains three integers $x$, $y$, and $k$ ($1 \le x, k \le 10^{9}$, $2 \le y \le 10^{9}$) β€” the initial va...
For each test case, output a single integer β€” the number obtained after applying $k$ operations.
[ [ "13\n1 3 1\n2 3 1\n24 5 5\n16 3 2\n2 2 1\n1337 18 1\n1 2 144133\n12345678 3 10\n998244353 2 998244353\n998244353 123456789 998244352\n998244354 998241111 998244352\n998244355 2 9982443\n1000000000 1000000000 1000000000", "2\n1\n1\n2\n3\n1338\n1\n16936\n1\n21180097\n6486\n1\n2" ] ]
In the first test case, there is only one operation applied to $x = 1$, resulting in $x$ becoming $2$. In the second test case, for $x = 2$, within one operation, one is added to $x$ and it's divided by $y = 3$, resulting in $x$ becoming $1$. In the third test case, $x$ changes as follows: * After the first operat...
Title: Collatz Conjecture time_limit_ms: 1000 memory_limit_mb: 256 Description: Recently, the first-year student Maxim learned about the Collatz conjecture, but he didn't pay much attention during the lecture, so he believes that the following process is mentioned in the conjecture: There is a variable $x$ and a const...
2032C
https://codeforces.com/problemset/problem/2032/C
Trinity
1,400
[ "binary search", "math", "sortings", "two pointers" ]
Div. 2
2,000
256
You are given an array $a$ of $n$ elements $a_1, a_2, \ldots, a_n$. You can perform the following operation any number (possibly $0$) of times: * Choose two integers $i$ and $j$, where $1 \le i, j \le n$, and assign $a_i := a_j$. Find the minimum number of operations required to make the array $a$ satisfy the con...
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($3 \le n \le 2 \cdot 10^5$) β€” the number of elements in the array $a$. The...
For each test case, output a single integer β€” the minimum number of operations required.
[ [ "4\n7\n1 2 3 4 5 6 7\n3\n1 3 2\n3\n4 5 3\n15\n9 3 8 1 6 5 3 8 2 1 4 2 9 4 7", "3\n1\n0\n8" ] ]
In the first test case, one of the possible series of operations would be: * Assign $a_1 := a_4 = 4$. The array will become $[4, 2, 3, 4, 5, 6, 7]$. * Assign $a_2 := a_5 = 5$. The array will become $[4, 5, 3, 4, 5, 6, 7]$. * Assign $a_7 := a_1 = 4$. The array will become $[4, 5, 3, 4, 5, 6, 4]$. It can be pr...
Title: Trinity time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given an array $a$ of $n$ elements $a_1, a_2, \ldots, a_n$. You can perform the following operation any number (possibly $0$) of times: * Choose two integers $i$ and $j$, where $1 \le i, j \le n$, and assign $a_i := a_j$. Find the minimu...
1992F
https://codeforces.com/problemset/problem/1992/F
Valuable Cards
1,900
[ "brute force", "dp", "greedy", "number theory", "two pointers" ]
Div. 3
4,000
512
In his favorite cafe Kmes once again wanted to try the herring under a fur coat. Previously, it would not have been difficult for him to do this, but the cafe recently introduced a new purchasing policy. Now, in order to make a purchase, Kmes needs to solve the following problem: $n$ cards with prices for different po...
The first line contains a single integer $t$ ($1 \le t \le 10^3$) β€” the number of test cases. The first line of each set of input data gives you $2$ integers $n$ and $x$ ($1 \le n \le 10^5, 2 \le x \le 10^5$) β€” the number of cards and the integer, respectively. The second line of each set of input data contains $n$ i...
For each set of input data, output the minimum number of bad segments.
[ [ "8\n6 4\n2 3 6 2 1 2\n9 100000\n50000 25000 12500 6250 3125 2 4 8 16\n5 2\n1 1 1 1 1\n8 6\n4 3 4 3 4 3 4 3\n7 12\n6 11 1 3 11 10 2\n10 5\n2 4 4 2 4 4 4 3 1 1\n7 8\n4 6 5 1 2 4 1\n8 27\n3 9 17 26 2 20 9 3", "3\n2\n1\n1\n2\n1\n3\n3" ] ]
Title: Valuable Cards time_limit_ms: 4000 memory_limit_mb: 512 Description: In his favorite cafe Kmes once again wanted to try the herring under a fur coat. Previously, it would not have been difficult for him to do this, but the cafe recently introduced a new purchasing policy. Now, in order to make a purchase, Kmes ...
2004G
https://codeforces.com/problemset/problem/2004/G
Substring Compression
3,200
[ "data structures", "dp", "matrices" ]
Div. 2
2,000
1,024
Let's define the operation of compressing a string $t$, consisting of at least $2$ digits from $1$ to $9$, as follows: * split it into an even number of non-empty substrings β€” let these substrings be $t_1, t_2, \dots, t_m$ (so, $t = t_1 + t_2 + \dots + t_m$, where $+$ is the concatenation operation); * write the ...
The first line contains two integers $n$ and $k$ ($2 \le k \le n \le 2 \cdot 10^5$). The second line contains the string $s$ ($|s| = n$), consisting only of digits from $1$ to $9$.
Output $n - k + 1$ integers β€” $f(s_{1,k}), f(s_{2,k+1}), \dots, f(s_{n - k + 1, n})$.
[ [ "4 4\n5999", "14" ], [ "10 3\n1111111111", "2 2 2 2 2 2 2 2" ], [ "11 4\n49998641312", "12 18 17 15 12 7 7 2" ] ]
Title: Substring Compression time_limit_ms: 2000 memory_limit_mb: 1024 Description: Let's define the operation of compressing a string $t$, consisting of at least $2$ digits from $1$ to $9$, as follows: * split it into an even number of non-empty substrings β€” let these substrings be $t_1, t_2, \dots, t_m$ (so, $t = ...
2005C
https://codeforces.com/problemset/problem/2005/C
Lazy Narek
1,800
[ "dp", "implementation", "strings" ]
Div. 2
2,000
256
Narek is too lazy to create the third problem of this contest. His friend Artur suggests that he should use ChatGPT. ChatGPT creates $n$ problems, each consisting of $m$ letters, so Narek has $n$ strings. To make the problem harder, he combines the problems by selecting some of the $n$ strings possibly none and concate...
In the first line of the input, you're given a single integer $t$ ($1 \le t \le 10^5$), the number of test cases. Then the description of each test case follows. In the first line of each test case, you're given two integers $n, m$ ($1 \le n, m \le 10^3$), the number of strings and the length of each string. In the n...
For each test case, output a single integer: the maximal possible value of $score_n - score_c$.
[ [ "4\n5 2\nnn\naa\nrr\nee\nkk\n1 5\nnarek\n1 4\nnare\n5 7\nnrrarek\nnrnekan\nuuuuuuu\nppppppp\nnkarekz", "0\n5\n0\n7" ] ]
In the first test case, one of the optimal answers is when Narek doesn't choose any of the strings, so the answer is $0$. He can alternatively choose all the strings. In this case, the full string becomes "nnaarreekk". Narek can choose the first appearances of all letters and add $5$ to the score. His opponent will add...
Title: Lazy Narek time_limit_ms: 2000 memory_limit_mb: 256 Description: Narek is too lazy to create the third problem of this contest. His friend Artur suggests that he should use ChatGPT. ChatGPT creates $n$ problems, each consisting of $m$ letters, so Narek has $n$ strings. To make the problem harder, he combines the...
1978F
https://codeforces.com/problemset/problem/1978/F
Large Graph
2,400
[ "data structures", "dfs and similar", "dsu", "graphs", "number theory", "two pointers" ]
Div. 2
4,000
512
Given an array $a$ of length $n$. Let's construct a square matrix $b$ of size $n \times n$, in which the $i$-th row contains the array $a$ cyclically shifted to the right by $(i - 1)$. For example, for the array $a = [3, 4, 5]$, the obtained matrix is $$b = \begin{bmatrix} 3 & 4 & 5 \\\ 5 & 3 & 4 \\\ 4 & 5 & 3 \end{bm...
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^6$, $2 \le k \le 2 \cdot 10^6$) β€” the length of the a...
For each test case, output a single integer β€” the number of connected components in the obtained graph.
[ [ "6\n3 3\n3 4 5\n3 3\n3 4 9\n3 2\n3 4 9\n2 2\n2 8\n5 3\n8 27 5 4 3\n4 10\n2 2 2 2", "3\n2\n3\n1\n4\n1" ] ]
In the first test case, the matrix $b$ is given in the statement. The first connected component contains the vertices $(1, 1)$, $(2, 2)$, and $(3, 3)$. The second connected component contains the vertices $(1, 2)$, $(2, 3)$, and $(3, 1)$. The third connected component contains the vertices $(1, 3)$, $(2, 1)$, and $(3, ...
Title: Large Graph time_limit_ms: 4000 memory_limit_mb: 512 Description: Given an array $a$ of length $n$. Let's construct a square matrix $b$ of size $n \times n$, in which the $i$-th row contains the array $a$ cyclically shifted to the right by $(i - 1)$. For example, for the array $a = [3, 4, 5]$, the obtained matri...
2002F2
https://codeforces.com/problemset/problem/2002/F2
Court Blue (Hard Version)
2,800
[ "brute force", "dp", "math", "number theory" ]
Div. 1 + 2
4,000
512
This is the hard version of the problem. In this version, it is not guaranteed that $n=m$, and the time limit is higher. You can make hacks only if both versions of the problem are solved. In the court of the Blue King, Lelle and Flamm are having a performance match. The match consists of several rounds. In each round...
The first line contains an integer $t$ ($1\leq t \leq 10^3$) β€” the number of test cases. The only line of each test case contains four integers $n$, $m$, $l$, $f$ ($2\leq n\leq m \leq 2\cdot 10^7$, $1\leq l,f \leq 10^9$): $n$, $m$ give the upper bound on the number of Lelle and Flamm's wins, $l$ and $f$ determine the ...
For each test case, output a single integer β€” the maximum total score of a successful performance.
[ [ "8\n3 4 2 5\n4 4 1 4\n6 6 2 2\n7 9 2 3\n8 9 9 1\n2 7 1 4\n5 9 1 4\n5 6 6 7", "22\n17\n18\n37\n77\n30\n41\n59" ], [ "2\n3082823 20000000 1341 331\n20000000 20000000 3 5", "10754065643\n159999991" ], [ "1\n139 1293 193 412", "559543" ] ]
In the first test case, a possible performance is as follows: * Flamm wins, $\gcd(0,1)=1$. * Lelle wins, $\gcd(1,1)=1$. * Flamm wins, $\gcd(1,2)=1$. * Flamm wins, $\gcd(1,3)=1$. * Flamm wins, $\gcd(1,4)=1$. * Lelle and Flamm agree to stop the match. The final score is $1\cdot2+4\cdot5=22$.
Title: Court Blue (Hard Version) time_limit_ms: 4000 memory_limit_mb: 512 Description: This is the hard version of the problem. In this version, it is not guaranteed that $n=m$, and the time limit is higher. You can make hacks only if both versions of the problem are solved. In the court of the Blue King, Lelle and Fl...
2036C
https://codeforces.com/problemset/problem/2036/C
Anya and 1100
1,100
[ "brute force", "implementation" ]
Div. 3
3,000
256
While rummaging through things in a distant drawer, Anya found a beautiful string $s$ consisting only of zeros and ones. Now she wants to make it even more beautiful by performing $q$ operations on it. Each operation is described by two integers $i$ ($1 \le i \le |s|$) and $v$ ($v \in \\{0, 1\\}$) and means that the ...
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first line of the test case contains the string $s$ ($1 \leq |s| \leq 2 \cdot 10^5$), consisting only of the characters "0" and "1". Here $|s|$ denotes the length of the string $s$. The next line contains an integer $q$ ($1...
For each query, output "YES", if "1100" is present in Anya's string; otherwise, output "NO". You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
[ [ "4\n100\n4\n1 1\n2 0\n2 0\n3 1\n1100000\n3\n6 1\n7 1\n4 1\n111010\n4\n1 1\n5 0\n4 1\n5 0\n0100\n4\n3 1\n1 1\n2 0\n2 1", "NO\nNO\nNO\nNO\nYES\nYES\nNO\nNO\nYES\nYES\nYES\nNO\nNO\nNO\nNO" ] ]
Title: Anya and 1100 time_limit_ms: 3000 memory_limit_mb: 256 Description: While rummaging through things in a distant drawer, Anya found a beautiful string $s$ consisting only of zeros and ones. Now she wants to make it even more beautiful by performing $q$ operations on it. Each operation is described by two intege...
2028C
https://codeforces.com/problemset/problem/2028/C
Alice's Adventures in Cutting Cake
1,600
[ "binary search", "dp", "greedy", "two pointers" ]
Div. 2
2,000
256
Alice is at the Mad Hatter's tea party! There is a long sheet cake made up of $n$ sections with tastiness values $a_1, a_2, \ldots, a_n$. There are $m$ creatures at the tea party, excluding Alice. Alice will cut the cake into $m + 1$ pieces. Formally, she will partition the cake into $m + 1$ subarrays, where each suba...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first line of each test case contains three integers $n, m, v$ ($1\le m\le n\le 2\cdot 10^5$; $1\le v\le 10^9$) β€” the number of sections, the number of creat...
For each test case, output the maximum tastiness Alice can achieve for her piece, or $-1$ if there is no way to make sure every creature is happy.
[ [ "7\n6 2 1\n1 1 10 1 1 10\n6 2 2\n1 1 10 1 1 10\n6 2 3\n1 1 10 1 1 10\n6 2 10\n1 1 10 1 1 10\n6 2 11\n1 1 10 1 1 10\n6 2 12\n1 1 10 1 1 10\n6 2 12\n1 1 1 1 10 10", "22\n12\n2\n2\n2\n0\n-1" ] ]
For the first test case, Alice can give the first and second section as their own pieces, and then take the remaining $10 + 1 + 1 + 10 = 22$ tastiness for herself. We can show that she cannot do any better. For the second test case, Alice could give the first and second section as one piece, and the sixth section as o...
Title: Alice's Adventures in Cutting Cake time_limit_ms: 2000 memory_limit_mb: 256 Description: Alice is at the Mad Hatter's tea party! There is a long sheet cake made up of $n$ sections with tastiness values $a_1, a_2, \ldots, a_n$. There are $m$ creatures at the tea party, excluding Alice. Alice will cut the cake in...
2014A
https://codeforces.com/problemset/problem/2014/A
Robin Helps
800
[ "greedy", "implementation" ]
Div. 3
1,000
256
There is a little bit of the outlaw in everyone, and a little bit of the hero too. The heroic outlaw Robin Hood is famous for taking from the rich and giving to the poor. Robin encounters $n$ people starting from the $1$-st and ending with the $n$-th. The $i$-th person has $a_i$ gold. If $a_i \ge k$, Robin will take ...
The first line of the input contains a single integer $t$ ($1\leq t \leq 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 50, 1 \le k \le 100$) β€” the number of people and the threshold at which Robin Hood takes the gold. The second line of each test case...
For each test case, output a single integer, the number of people that will get gold from Robin Hood.
[ [ "4\n2 2\n2 0\n3 2\n3 0 0\n6 2\n0 3 0 0 0 0\n2 5\n5 4", "1\n2\n3\n0" ] ]
In the first test case, Robin takes $2$ gold from the first person and gives a gold to the second person. In the second test case, Robin takes $3$ gold and gives $1$ gold to each of the next $2$ people. In the third test case, Robin takes $3$ gold and so only gives gold to $3$ other people.
Title: Robin Helps time_limit_ms: 1000 memory_limit_mb: 256 Description: There is a little bit of the outlaw in everyone, and a little bit of the hero too. The heroic outlaw Robin Hood is famous for taking from the rich and giving to the poor. Robin encounters $n$ people starting from the $1$-st and ending with the $...
1998D
https://codeforces.com/problemset/problem/1998/D
Determine Winning Islands in Race
2,100
[ "data structures", "dp", "graphs", "greedy", "shortest paths" ]
Div. 2
2,000
256
MOOOOOOOOOOOOOOOOO β€” Bessie the Cow, The Art of Racing on Islands Two of Farmer John's cows, Bessie and Elsie, are planning to race on $n$ islands. There are $n - 1$ main bridges, connecting island $i$ to island $i + 1$ for all $1 \leq i \leq n - 1$. Additionally, there are $m$ alternative bridges. Elsie can use both...
The first line contains $t$ ($1 \leq t \leq 10^4$) – the number of test cases. The first line of each test case contains $n$ and $m$ ($2 \leq n \leq 2 \cdot 10^5$, $0 \leq m \leq 2 \cdot 10^5$) – the number of islands and the number of alternative bridges. The next $m$ lines of each test case contain $u$ and $v$ ($1 ...
For each test case, output a binary string of length $n - 1$ on a new line. The $i$'th character is $1$ if it is possible for Bessie to win if she starts on island $i$. Otherwise, it is $0$.
[ [ "5\n6 0\n6 1\n2 6\n6 1\n1 5\n10 4\n1 3\n1 6\n2 7\n3 8\n15 3\n2 8\n4 9\n8 15", "11111\n11011\n10011\n100001111\n11000111000111" ] ]
In the first test case, there are no alternative bridges for Elsie to overtake Bessie and reach island $n$ first, so Bessie will win on all islands because she always moves first. In the second case, Bessie will lose if she starts on island $3$ because: * Bessie's Turn: Take a main bridge from island $3$ to island ...
Title: Determine Winning Islands in Race time_limit_ms: 2000 memory_limit_mb: 256 Description: MOOOOOOOOOOOOOOOOO β€” Bessie the Cow, The Art of Racing on Islands Two of Farmer John's cows, Bessie and Elsie, are planning to race on $n$ islands. There are $n - 1$ main bridges, connecting island $i$ to island $i + 1$ for...
1986B
https://codeforces.com/problemset/problem/1986/B
Matrix Stabilization
1,000
[ "brute force", "data structures", "greedy", "sortings" ]
Div. 3
2,000
256
You are given a matrix of size $n \times m$, where the rows are numbered from $1$ to $n$ from top to bottom, and the columns are numbered from $1$ to $m$ from left to right. The element at the intersection of the $i$-th row and the $j$-th column is denoted by $a_{ij}$. Consider the algorithm for stabilizing matrix $a$...
Each test consists of multiple sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of sets of input data. This is followed by their description. The first line of each set of input data contains two integers $n$ and $m$ ($1 \leq n, m \leq 100, n \cdot m > 1$) β€” the numb...
For each set of input data, output $n$ lines with $m$ numbers in each line β€” the values of the cells of matrix $a$ after the stabilization algorithm.
[ [ "6\n1 2\n3 1\n2 1\n1\n1\n2 2\n1 2\n3 4\n2 3\n7 4 5\n1 8 10\n5 4\n92 74 31 74\n74 92 17 7\n31 17 92 3\n74 7 3 92\n7 31 1 1\n3 3\n1000000000 1 1000000000\n1 1000000000 1\n1000000000 1 1000000000", "1 1 \n1 \n1 \n1 2 \n3 3 \n4 4 5 \n1 8 8 \n74 74 31 31 \n74 74 17 7 \n31 17 17 3 \n31 7 3 3 \n7 7 1 1 \n1 1 1 \...
In the first set of input data, the algorithm will select the cell $(1, 1)$ twice in a row and then terminate. ![](CDN_BASE_URL/ce7b57ad25f59927de6d90880ce5a2ba) In the second set of input data, there is no cell whose value is strictly greater than the values of all neighboring cells. In the third set of input data,...
Title: Matrix Stabilization time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given a matrix of size $n \times m$, where the rows are numbered from $1$ to $n$ from top to bottom, and the columns are numbered from $1$ to $m$ from left to right. The element at the intersection of the $i$-th row and the $j$-th...
1994E
https://codeforces.com/problemset/problem/1994/E
Wooden Game
2,000
[ "bitmasks", "greedy", "math", "trees" ]
Div. 1 + 2
2,000
256
You are given a forest of $k$ rooted trees$^{\text{βˆ—}}$. Lumberjack Timofey wants to cut down the entire forest by applying the following operation: * Select a subtree$^{\text{†}}$ of any vertex of one of the trees and remove it from the tree. Timofey loves bitwise operations, so he wants the [bitwise OR](https://...
Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. Then follows the description of the test cases. The first line of each test case contains a single integer $k$ ($1 \leq k \leq 10^6$) β€” the number of trees in the forest. This is follow...
For each test case, output a single integer β€” the maximum result that can be obtained.
[ [ "3\n1\n1\n\n\n2\n4\n1 2 2\n6\n1 1 3 1 3\n1\n10\n1 2 2 1 1 5 7 6 4", "1\n7\n10" ] ]
In the second test case, the trees look like this: ![](CDN_BASE_URL/149fbfb0b4a0bcf29a8a6b8b997b79b2) The first operation removes the entire second tree. ![](CDN_BASE_URL/3a1651525e7c5087a7d88d51824c16d6) The second operation removes vertex $4$ from the first tree. ![](CDN_BASE_URL/c5e219384aa686ba75274781503be59f...
Title: Wooden Game time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given a forest of $k$ rooted trees$^{\text{βˆ—}}$. Lumberjack Timofey wants to cut down the entire forest by applying the following operation: * Select a subtree$^{\text{†}}$ of any vertex of one of the trees and remove it from the tree. ...
2008D
https://codeforces.com/problemset/problem/2008/D
Sakurako's Hobby
1,100
[ "dp", "dsu", "graphs", "math" ]
Div. 3
2,000
256
For a certain permutation $p$$^{\text{βˆ—}}$ Sakurako calls an integer $j$ reachable from an integer $i$ if it is possible to make $i$ equal to $j$ by assigning $i=p_i$ a certain number of times. If $p=[3,5,6,1,2,4]$, then, for example, $4$ is reachable from $1$, because: $i=1$ $\rightarrow$ $i=p_1=3$ $\rightarrow$ $i=p...
The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1\le n\le 2\cdot 10^5$) β€” the number of elements in the array. The second line of each test case contains $n$ integers $p_1, p_2, \dots, p_n$ ($1\le p_i\le n$)...
For each test case, output $n$ integers $F(1), F(2), \dots, F(n)$.
[ [ "5\n1\n1\n0\n5\n1 2 4 5 3\n10101\n5\n5 4 1 3 2\n10011\n6\n3 5 6 1 2 4\n010000\n6\n1 2 3 4 5 6\n100110", "1 \n0 1 1 1 1 \n2 2 2 2 2 \n4 1 4 4 1 4 \n0 1 1 0 0 1" ] ]
Title: Sakurako's Hobby time_limit_ms: 2000 memory_limit_mb: 256 Description: For a certain permutation $p$$^{\text{βˆ—}}$ Sakurako calls an integer $j$ reachable from an integer $i$ if it is possible to make $i$ equal to $j$ by assigning $i=p_i$ a certain number of times. If $p=[3,5,6,1,2,4]$, then, for example, $4$ is...
2024B
https://codeforces.com/problemset/problem/2024/B
Buying Lemonade
1,100
[ "binary search", "constructive algorithms", "sortings" ]
Div. 2
1,000
256
There is a vending machine that sells lemonade. The machine has a total of $n$ slots. You know that initially, the $i$-th slot contains $a_i$ cans of lemonade. There are also $n$ buttons on the machine, each button corresponds to a slot, with exactly one button corresponding to each slot. Unfortunately, the labels on t...
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \leq k \leq 10^9$) β€” the number of slots...
For each test case, output a single integer β€” the minimum number of button presses needed to guarantee that you receive at least $k$ cans of lemonade.
[ [ "5\n2 1\n1 1\n2 2\n1 2\n3 4\n2 1 3\n10 50\n1 1 3 8 8 9 12 13 27 27\n2 1000000000\n1000000000 500000000", "1\n2\n5\n53\n1000000000" ] ]
In the first test case, we can simply press the first button and receive one can of lemonade. In the second test case, we can press each button once and guarantee that we receive $2$ cans of lemonade. Note that if we simply press one button twice, we might not be lucky, and that button could correspond to the first sl...
Title: Buying Lemonade time_limit_ms: 1000 memory_limit_mb: 256 Description: There is a vending machine that sells lemonade. The machine has a total of $n$ slots. You know that initially, the $i$-th slot contains $a_i$ cans of lemonade. There are also $n$ buttons on the machine, each button corresponds to a slot, with ...
1974A
https://codeforces.com/problemset/problem/1974/A
Phone Desktop
800
[ "greedy", "math" ]
Div. 3
1,000
256
Little Rosie has a phone with a desktop (or launcher, as it is also called). The desktop can consist of several screens. Each screen is represented as a grid of size $5 \times 3$, i.e., five rows and three columns. There are $x$ applications with an icon size of $1 \times 1$ cells; such an icon occupies only one cell ...
The first line of the input contains $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first and only line of each test case contains two integers $x$ and $y$ ($0 \leq x, y \leq 99$) β€” the number of applications with a $1 \times 1$ icon and the number of applications with a $2 \times 2$ icon, respectively.
For each test case, output the minimal number of required screens on a separate line.
[ [ "11\n1 1\n7 2\n12 4\n0 3\n1 0\n8 1\n0 0\n2 0\n15 0\n8 2\n0 9", "1\n1\n2\n2\n1\n1\n0\n1\n1\n2\n5" ] ]
The solution for the first test case can look as follows: ![](CDN_BASE_URL/c1f8e47d264264fb303b101863bef14b) Blue squares represent empty spaces for icons, green squares represent $1 \times 1$ icons, red squares represent $2 \times 2$ icons The solution for the third test case can look as follows: ![](CDN_BASE_URL/0...
Title: Phone Desktop time_limit_ms: 1000 memory_limit_mb: 256 Description: Little Rosie has a phone with a desktop (or launcher, as it is also called). The desktop can consist of several screens. Each screen is represented as a grid of size $5 \times 3$, i.e., five rows and three columns. There are $x$ applications wi...
1982A
https://codeforces.com/problemset/problem/1982/A
Soccer
800
[ "greedy", "implementation", "math", "sortings" ]
Div. 2
1,000
256
Dima loves watching soccer. In such a game, the score on the scoreboard is represented as $x$ : $y$, where $x$ is the number of goals of the first team, and $y$ is the number of goals of the second team. At any given time, only one team can score a goal, so the score $x$ : $y$ can change to either $(x + 1)$ : $y$, or $...
Each test consists of several test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) β€” the number of test cases. Then follows the description of the test cases. The first line of each test case contains two integers $x_{1}, y_{1}$ ($0 \le x_{1}, y_{1} \le 10^{9}$, $x_{1} \neq y_{1}$) β€” the score bef...
For each test case, output "YES" without quotes if it is possible, that the teams never had a tie while Dima was away, otherwise output "NO" without quotes. You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
[ [ "6\n1 0\n5 0\n1 2\n3 2\n1 2\n4 5\n1 2\n4 3\n1 2\n1 2\n998244353 0\n1000000000 999999999", "YES\nNO\nYES\nNO\nYES\nYES" ] ]
In the first test case, the score before Dima left was $1$ : $0$. When he leaves, the first team scores several goals in a row until the score becomes $5$ : $0$, so the answer is YES. In the second test case, the score could only change as follows: * $1$ : $2$ * $2$ : $2$ * $3$ : $2$ In this scenario, there...
Title: Soccer time_limit_ms: 1000 memory_limit_mb: 256 Description: Dima loves watching soccer. In such a game, the score on the scoreboard is represented as $x$ : $y$, where $x$ is the number of goals of the first team, and $y$ is the number of goals of the second team. At any given time, only one team can score a goa...
2033C
https://codeforces.com/problemset/problem/2033/C
Sakurako's Field Trip
1,400
[ "dp", "greedy", "two pointers" ]
Div. 3
2,000
256
Even in university, students need to relax. That is why Sakurakos teacher decided to go on a field trip. It is known that all of the students will be walking in one line. The student with index $i$ has some topic of interest which is described as $a_i$. As a teacher, you want to minimise the disturbance of the line of ...
The first line contains one integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Each test case is described by two lines. * The first line contains one integer $n$ ($2 \le n \le 10^5$) β€” the length of the line of students. * The second line contains $n$ integers $a_i$ ($1\le a_i\le n$) β€” the topics of int...
For each test case, output the minimal possible disturbance of the line that you can achieve.
[ [ "9\n5\n1 1 1 2 3\n6\n2 1 2 2 1 1\n4\n1 2 1 1\n6\n2 1 1 2 2 4\n4\n2 1 2 3\n6\n1 2 2 1 2 1\n5\n4 5 5 1 5\n7\n1 4 3 5 1 1 3\n7\n3 1 3 2 2 3 3", "1\n2\n1\n0\n0\n1\n1\n0\n2" ] ]
In the first example, it is necessary to apply the operation to $i=2$, thus the array will become $[1, \textbf{2}, 1, \textbf{1}, 3]$, with the bold elements indicating those that have swapped places. The disturbance of this array is equal to $1$. In the fourth example, it is sufficient to apply the operation to $i=3$...
Title: Sakurako's Field Trip time_limit_ms: 2000 memory_limit_mb: 256 Description: Even in university, students need to relax. That is why Sakurakos teacher decided to go on a field trip. It is known that all of the students will be walking in one line. The student with index $i$ has some topic of interest which is des...
2013E
https://codeforces.com/problemset/problem/2013/E
Prefix GCD
2,200
[ "brute force", "dp", "greedy", "math", "number theory" ]
Div. 2
2,000
256
Since Mansur is tired of making legends, there will be no legends for this task. You are given an array of positive integer numbers $a_1, a_2, \ldots, a_n$. The elements of the array can be rearranged in any order. You need to find the smallest possible value of the expression $$\gcd(a_1) + \gcd(a_1, a_2) + \ldots + \...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first line of each test case contains a single number $n$ ($1 \le n \le 10^5$) β€” the size of the array. The second line of each test case contains $n$ numbe...
For each test case, output a single number on a separate line β€” the answer to the problem.
[ [ "5\n3\n4 2 2\n2\n6 3\n3\n10 15 6\n5\n6 42 12 52 20\n4\n42 154 231 66", "6\n6\n9\n14\n51" ] ]
In the first test case, the elements can be rearranged as follows: $[2, 4, 2]$. Then the answer will be $\gcd(2) + \gcd(2, 4) + \gcd(2, 4, 2) = 2 + 2 + 2 = 6$. In the third test case, the elements can be rearranged as follows: $[6, 10, 15]$. Then the answer will be $\gcd(6) + \gcd(6, 10) + \gcd(6, 10, 15) = 6 + 2 + 1 ...
Title: Prefix GCD time_limit_ms: 2000 memory_limit_mb: 256 Description: Since Mansur is tired of making legends, there will be no legends for this task. You are given an array of positive integer numbers $a_1, a_2, \ldots, a_n$. The elements of the array can be rearranged in any order. You need to find the smallest po...
2014G
https://codeforces.com/problemset/problem/2014/G
Milky Days
2,200
[ "brute force", "data structures", "greedy", "implementation" ]
Div. 3
2,000
256
What is done is done, and the spoilt milk cannot be helped. Little John is as little as night is day β€” he was known to be a giant, at possibly $2.1$ metres tall. It has everything to do with his love for milk. His dairy diary has $n$ entries, showing that he acquired $a_i$ pints of fresh milk on day $d_i$. Milk decli...
The first line of the input contains a single integer $t$ ($1\leq t \leq 10^4$), the number of test cases. The first line of each test case consists of three integers $n$, $m$, $k$ ($1\le n$, $m$, $k \le 10^5$), the number of diary entries, the maximum pints needed for a milk satisfaction day, and the duration of milk...
For each test case, output a single integer, the number of milk satisfaction days.
[ [ "6\n1 1 3\n1 5\n2 3 3\n1 5\n2 7\n4 5 2\n1 9\n2 6\n4 9\n5 6\n5 2 4\n4 7\n5 3\n7 1\n11 2\n12 1\n4 1 3\n5 10\n9 4\n14 8\n15 3\n5 5 5\n8 9\n10 7\n16 10\n21 5\n28 9", "3\n3\n4\n5\n10\n6" ] ]
In the first test case, $5$ pints of milk are good for $3$ days before spoiling. In the second test case, the following will happen: * On day $1$, he will receive $5$ pints of milk and drink $3$ of them (leaving $2$ pints from day $1$); * On day $2$, he will receive $7$ pints of milk and drink $3$ of them (leavi...
Title: Milky Days time_limit_ms: 2000 memory_limit_mb: 256 Description: What is done is done, and the spoilt milk cannot be helped. Little John is as little as night is day β€” he was known to be a giant, at possibly $2.1$ metres tall. It has everything to do with his love for milk. His dairy diary has $n$ entries, sho...
1975C
https://codeforces.com/problemset/problem/1975/C
Chamo and Mocha's Array
1,200
[ "binary search", "brute force", "greedy" ]
Div. 1 + 2
2,000
256
Mocha likes arrays, so before her departure, Chamo gave her an array $a$ consisting of $n$ positive integers as a gift. Mocha doesn't like arrays containing different numbers, so Mocha decides to use magic to change the array. Mocha can perform the following three-step operation some (possibly, zero) times: 1. Choo...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\leq t\leq 500$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2\leq n\leq 10^5$) β€” the length of the array $a$. The second line of each test case contains $n$...
For each test case, output the maximum value of the number.
[ [ "2\n2\n1 2\n5\n1 2 3 4 5", "1\n4" ] ]
In the first test case, $a=[1,2]$. Mocha can only choose the interval $(l,r)=(1,2)$. The array will be changed to $a=[1,1]$. Therefore, the answer is $1$. In the second test case, Mocha can perform the following operations: * Choose the interval $(l,r)=(4,5)$, then $a=[1,2,3,4,4]$. * Choose the interval $(l,r)=(...
Title: Chamo and Mocha's Array time_limit_ms: 2000 memory_limit_mb: 256 Description: Mocha likes arrays, so before her departure, Chamo gave her an array $a$ consisting of $n$ positive integers as a gift. Mocha doesn't like arrays containing different numbers, so Mocha decides to use magic to change the array. Mocha c...
2008C
https://codeforces.com/problemset/problem/2008/C
Longest Good Array
800
[ "binary search", "brute force", "math" ]
Div. 3
2,000
256
Today, Sakurako was studying arrays. An array $a$ of length $n$ is considered good if and only if: * the array $a$ is increasing, meaning $a_{i - 1} < a_i$ for all $2 \le i \le n$; * the differences between adjacent elements are increasing, meaning $a_i - a_{i-1} < a_{i+1} - a_i$ for all $2 \le i < n$. Sakurako...
The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The only line of each test case contains two integers $l$ and $r$ ($1\le l\le r\le 10^9$).
For each test case, output a single integer β€” the length of the longest good array Sakurako can form given $l$ and $r$.
[ [ "5\n1 2\n1 5\n2 2\n10 20\n1 1000000000", "2\n3\n1\n5\n44721" ] ]
For $l=1$ and $r=5$, one possible array could be $(1,2,5)$. It can be proven that an array of length $4$ does not exist for the given $l$ and $r$. For $l=2$ and $r=2$, the only possible array is $(2)$. For $l=10$ and $r=20$, the only possible array is $(10,11,13,16,20)$.
Title: Longest Good Array time_limit_ms: 2000 memory_limit_mb: 256 Description: Today, Sakurako was studying arrays. An array $a$ of length $n$ is considered good if and only if: * the array $a$ is increasing, meaning $a_{i - 1} < a_i$ for all $2 \le i \le n$; * the differences between adjacent elements are incre...
1999B
https://codeforces.com/problemset/problem/1999/B
Card Game
1,000
[ "brute force", "constructive algorithms", "implementation" ]
Div. 4
2,000
256
Suneet and Slavic play a card game. The rules of the game are as follows: * Each card has an integer value between $1$ and $10$. * Each player receives $2$ cards which are face-down (so a player doesn't know their cards). * The game is turn-based and consists exactly of two turns. In a round, both players pick...
The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first and only line of each test case contains $4$ integers $a_1$, $a_2$, $b_1$, $b_2$ ($1 \leq a_1, a_2, b_1, b_2 \leq 10$) where $a_1$ and $a_2$ represent the cards Suneet has, and $b_1$ and $b_2$ represent the cards Slavic...
For each test case, output a single integer β€” the number of games Suneet would win considering all possible games.
[ [ "5\n3 8 2 6\n1 1 1 1\n10 10 2 2\n1 1 10 10\n3 8 7 2", "2\n0\n4\n0\n2" ] ]
Consider the first test case when Slavic starts with the cards that have the values $2$ and $6$, and Suneet starts with cards that have the values $3$ and $8$. The game could happen in $4$ different ways: * Suneet flips $3$ and Slavic flips $2$. Suneet wins the first round. Then, Suneet flips $8$ and Slavic flips $6...
Title: Card Game time_limit_ms: 2000 memory_limit_mb: 256 Description: Suneet and Slavic play a card game. The rules of the game are as follows: * Each card has an integer value between $1$ and $10$. * Each player receives $2$ cards which are face-down (so a player doesn't know their cards). * The game is turn...
2021E2
https://codeforces.com/problemset/problem/2021/E2
Digital Village (Hard Version)
2,500
[ "data structures", "dp", "dsu", "graphs", "math", "trees" ]
Div. 2
2,000
256
This is the hard version of the problem. In the three versions, the constraints on $n$ and $m$ are different. You can make hacks only if all the versions of the problem are solved. Pak Chanek is setting up internet connections for the village of Khuntien. The village can be represented as a connected simple graph with...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2000$). The description of the test cases follows. The first line of each test case contains three integers $n$, $m$, $p$ ($2 \le n \le 5000$; $n-1 \le m \le 5000$; $1 \le p \le n$) β€” the number of houses, the nu...
For each test case, output $n$ integers: the minimum total latency that can be achieved for all the houses requiring internet for each $k = 1,2,\ldots,n$.
[ [ "2\n9 8 5\n2 5 6 8 9\n1 2 1\n1 3 2\n3 4 10\n4 5 3\n4 6 5\n1 7 10\n7 8 4\n7 9 2\n3 3 2\n3 1\n1 2 1\n2 3 3\n1 3 2", "34 19 9 4 0 0 0 0 0\n2 0 0" ] ]
In the first test case for $k=3$, a possible optimal solution is to install servers at vertices $2$, $6$ and $8$ and obtain the following latency: * $\text{latency}(2) = 0$ * $\text{latency}(5) = \max(3, 5) = 5$ * $\text{latency}(6) = 0$ * $\text{latency}(8) = 0$ * $\text{latency}(9) = \max(2, 4) = 4$ ...
Title: Digital Village (Hard Version) time_limit_ms: 2000 memory_limit_mb: 256 Description: This is the hard version of the problem. In the three versions, the constraints on $n$ and $m$ are different. You can make hacks only if all the versions of the problem are solved. Pak Chanek is setting up internet connections ...
1984H
https://codeforces.com/problemset/problem/1984/H
Tower Capturing
3,300
[ "combinatorics", "dp", "geometry" ]
Div. 1 + 2
2,000
256
There are $n$ towers at $n$ distinct points $(x_1, y_1), (x_2, y_2), \ldots, (x_n, y_n)$, such that no three are collinear and no four are concyclic. Initially, you own towers $(x_1, y_1)$ and $(x_2, y_2)$, and you want to capture all of them. To do this, you can do the following operation any number of times: * Pic...
The first line contains a single integer $t$ ($1 \leq t \leq 250$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($4 \leq n \leq 100$) β€” the number of towers. The $i$-th of the next $n$ lines contains two integers $x_i$ and $y_i$ ($-10^4 \leq x_i, y_i \leq 10^4$) β€” the loc...
For each test case, output a single integer β€” the number of attack plans of minimal length after which you capture all towers, modulo $998\,244\,353$.
[ [ "3\n5\n1 1\n2 5\n3 3\n4 2\n5 4\n6\n1 1\n3 3\n1 2\n2 1\n3 10000\n19 84\n7\n2 7\n-4 -3\n-3 6\n3 1\n-5 2\n1 -4\n-1 7", "1\n0\n10" ] ]
In the first test case, there is only one possible attack plan of shortest length, shown below. ![](CDN_BASE_URL/fa2c55e855095283a306e017f03d06af) * Use the operation with $P =$ tower $1$, $Q =$ tower $2$, and $R =$ tower $5$. The circle through these three towers contains all towers inside of it, and as a result t...
Title: Tower Capturing time_limit_ms: 2000 memory_limit_mb: 256 Description: There are $n$ towers at $n$ distinct points $(x_1, y_1), (x_2, y_2), \ldots, (x_n, y_n)$, such that no three are collinear and no four are concyclic. Initially, you own towers $(x_1, y_1)$ and $(x_2, y_2)$, and you want to capture all of them....
2021E3
https://codeforces.com/problemset/problem/2021/E3
Digital Village (Extreme Version)
2,800
[ "data structures", "dfs and similar", "dp", "dsu", "graphs", "greedy", "math", "trees" ]
Div. 2
2,000
256
This is the extreme version of the problem. In the three versions, the constraints on $n$ and $m$ are different. You can make hacks only if all the versions of the problem are solved. Pak Chanek is setting up internet connections for the village of Khuntien. The village can be represented as a connected simple graph w...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first line of each test case contains 3 integers $n$, $m$, $p$ ($2 \le n \le 2 \cdot 10^5$; $n-1 \le m \le 2 \cdot 10^5$; $1 \le p \le n$) β€” the number of ho...
For each test case, output $n$ integers: the minimum total latency that can be achieved for all the houses requiring internet for each $k = 1,2,\ldots,n$.
[ [ "2\n9 8 5\n2 5 6 8 9\n1 2 1\n1 3 2\n3 4 10\n4 5 3\n4 6 5\n1 7 10\n7 8 4\n7 9 2\n3 3 2\n3 1\n1 2 1\n2 3 3\n1 3 2", "34 19 9 4 0 0 0 0 0\n2 0 0" ] ]
In the first test case for $k=3$, a possible optimal solution is to install servers at vertices $2$, $6$ and $8$ and obtain the following latency: * $\text{latency}(2) = 0$ * $\text{latency}(5) = \max(3, 5) = 5$ * $\text{latency}(6) = 0$ * $\text{latency}(8) = 0$ * $\text{latency}(9) = \max(2, 4) = 4$ ...
Title: Digital Village (Extreme Version) time_limit_ms: 2000 memory_limit_mb: 256 Description: This is the extreme version of the problem. In the three versions, the constraints on $n$ and $m$ are different. You can make hacks only if all the versions of the problem are solved. Pak Chanek is setting up internet connec...
1975A
https://codeforces.com/problemset/problem/1975/A
Bazoka and Mocha's Array
800
[ "brute force", "greedy", "implementation", "sortings" ]
Div. 1 + 2
1,000
256
Mocha likes arrays, so before her departure, Bazoka gave her an array $a$ consisting of $n$ positive integers as a gift. Now Mocha wants to know whether array $a$ could become sorted in non- decreasing order after performing the following operation some (possibly, zero) times: * Split the array into two parts β€” a p...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\leq t\leq 1000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2\leq n\leq 50$) β€” the length of the array $a$. The second line of each test case contains $n$ ...
For each test case, output "Yes" if $a$ could become non-decreasing after performing the operation any number of times, and output "No" if not. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
[ [ "3\n6\n1 1 4 5 1 4\n5\n7 9 2 2 3\n3\n1 2 3", "No\nYes\nYes" ] ]
In the first test case, it can be proven that $a$ cannot become non- decreasing after performing the operation any number of times. In the second test case, we can perform the following operations to make $a$ sorted in non-decreasing order: * Split the array into two parts: $x=[7]$ and $y=[9,2,2,3]$, then swap thes...
Title: Bazoka and Mocha's Array time_limit_ms: 1000 memory_limit_mb: 256 Description: Mocha likes arrays, so before her departure, Bazoka gave her an array $a$ consisting of $n$ positive integers as a gift. Now Mocha wants to know whether array $a$ could become sorted in non- decreasing order after performing the foll...
1981B
https://codeforces.com/problemset/problem/1981/B
Turtle and an Infinite Sequence
1,300
[ "bitmasks", "math" ]
Div. 2
1,000
256
There is a sequence $a_0, a_1, a_2, \ldots$ of infinite length. Initially $a_i = i$ for every non-negative integer $i$. After every second, each element of the sequence will simultaneously change. $a_i$ will change to $a_{i - 1} \mid a_i \mid a_{i + 1}$ for every positive integer $i$. $a_0$ will change to $a_0 \mid a_...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first line of each test case contains two integers $n, m$ ($0 \le n, m \le 10^9$).
For each test case, output a single integer β€” the value of $a_n$ after $m$ seconds.
[ [ "9\n0 0\n0 1\n0 2\n1 0\n5 2\n10 1\n20 3\n1145 14\n19198 10", "0\n1\n3\n1\n7\n11\n23\n1279\n19455" ] ]
After $1$ second, $[a_0, a_1, a_2, a_3, a_4, a_5]$ will become $[1, 3, 3, 7, 7, 7]$. After $2$ seconds, $[a_0, a_1, a_2, a_3, a_4, a_5]$ will become $[3, 3, 7, 7, 7, 7]$.
Title: Turtle and an Infinite Sequence time_limit_ms: 1000 memory_limit_mb: 256 Description: There is a sequence $a_0, a_1, a_2, \ldots$ of infinite length. Initially $a_i = i$ for every non-negative integer $i$. After every second, each element of the sequence will simultaneously change. $a_i$ will change to $a_{i - ...
2005E2
https://codeforces.com/problemset/problem/2005/E2
Subtangle Game (Hard Version)
2,500
[ "data structures", "dp", "games", "greedy", "implementation" ]
Div. 2
2,000
256
This is the hard version of the problem. The differences between the two versions are the constraints on all the variables. You can make hacks only if both versions of the problem are solved. Tsovak and Narek are playing a game. They have an array $a$ and a matrix $b$ of integers with $n$ rows and $m$ columns, numbere...
The first line of the input contains $t$ ($1 \le t \le 1500$) – the number of test cases. The first line of each test case contains three integers $l$, $n$, and $m$ ($1 \le l, n, m \le 1500$) – the size of the array and the sizes of the matrix. The second line contains $l$ integers $a_1, a_2, a_3, \ldots a_l$ ($1 \le...
You should output $t$ lines, the $i$-th of them containing a character representing the answer of the $i$-th test case: "T" if Tsovak wins or "N", otherwise (without quotes).
[ [ "3\n2 2 3\n1 2\n1 3 6\n4 6 2\n2 2 4\n1 2\n1 1 3 2\n4 2 5 1\n2 4 2\n1 2\n3 4\n5 6\n7 8\n8 8", "N\nT\nN" ] ]
In the first example, Tsovak starts by looking for $1$. There is only one occurrence of $1$ at $(1,1)$, so he chooses it. Then Narek needs to look for $2$ in the submatrix of $(2, 2)$, which consists of just the last two elements: $6$ and $2$. He chooses $2$, and then Tsovak loses since the array has ended. In the sec...
Title: Subtangle Game (Hard Version) time_limit_ms: 2000 memory_limit_mb: 256 Description: This is the hard version of the problem. The differences between the two versions are the constraints on all the variables. You can make hacks only if both versions of the problem are solved. Tsovak and Narek are playing a game....
1981E
https://codeforces.com/problemset/problem/1981/E
Turtle and Intersected Segments
2,600
[ "data structures", "dsu", "graphs", "greedy" ]
Div. 2
5,000
512
Turtle just received $n$ segments and a sequence $a_1, a_2, \ldots, a_n$. The $i$-th segment is $[l_i, r_i]$. Turtle will create an undirected graph $G$. If segment $i$ and segment $j$ intersect, then Turtle will add an undirected edge between $i$ and $j$ with a weight of $|a_i - a_j|$, for every $i \ne j$. Turtle wa...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \le n \le 5 \cdot 10^5$) β€” the number of segments. The $i$-th of the following $n$ lines cont...
For each test case, output a single integer β€” the sum of the weights of the edges of the minimum spanning tree of the graph $G$. If the graph $G$ has no spanning tree, output $-1$.
[ [ "4\n5\n1 7 3\n2 4 6\n3 5 5\n6 7 9\n3 4 4\n5\n2 7 3\n1 3 6\n4 5 5\n6 7 9\n1 1 4\n4\n1 4 3\n1 2 1\n3 4 5\n1 4 4\n3\n1 3 1\n2 3 3\n4 5 8", "9\n13\n4\n-1" ] ]
In the first test case, the graph $G$ is as follows: ![](CDN_BASE_URL/7b0a32f8f1d54a5dbbaffee1efd8ece1) One of the minimum spanning trees of $G$ is as follows: ![](CDN_BASE_URL/7a035af2d5227648781d0c24b81c076e) The sum of the weights of the edges of the minimum spanning tree is $9$. In the second test case, the gr...
Title: Turtle and Intersected Segments time_limit_ms: 5000 memory_limit_mb: 512 Description: Turtle just received $n$ segments and a sequence $a_1, a_2, \ldots, a_n$. The $i$-th segment is $[l_i, r_i]$. Turtle will create an undirected graph $G$. If segment $i$ and segment $j$ intersect, then Turtle will add an undire...
1993D
https://codeforces.com/problemset/problem/1993/D
Med-imize
2,200
[ "binary search", "dp", "greedy" ]
Div. 2
2,000
256
Given two positive integers $n$ and $k$, and another array $a$ of $n$ integers. In one operation, you can select any subarray of size $k$ of $a$, then remove it from the array without changing the order of other elements. More formally, let $(l, r)$ be an operation on subarray $a_l, a_{l+1}, \ldots, a_r$ such that $r-...
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 5 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the array $a$. It is guaranteed that...
For each test case, print a single integer β€” the largest median possible after performing the operations.
[ [ "5\n4 3\n3 9 9 2\n5 3\n3 2 5 6 4\n7 1\n5 9 2 6 5 4 6\n8 2\n7 1 2 6 8 3 4 5\n4 5\n3 4 5 6", "3\n4\n9\n6\n4" ] ]
In the first test case, you can select a subarray $(l, r)$ which can be either $(1, 3)$ or $(2, 4)$. Thus, two obtainable final arrays are $[3]$ and $[2]$. The former one has the larger median ($3 > 2$) so the answer is $3$. In the second test case, three obtainable final arrays are $[6, 4]$, $[3, 4]$, and $[3, 2]$. T...
Title: Med-imize time_limit_ms: 2000 memory_limit_mb: 256 Description: Given two positive integers $n$ and $k$, and another array $a$ of $n$ integers. In one operation, you can select any subarray of size $k$ of $a$, then remove it from the array without changing the order of other elements. More formally, let $(l, r)...
1984E
https://codeforces.com/problemset/problem/1984/E
Shuffle
2,400
[ "dp", "greedy", "trees" ]
Div. 1 + 2
2,000
256
Two hungry red pandas, Oscar and Lura, have a tree $T$ with $n$ nodes. They are willing to perform the following shuffle procedure on the whole tree $T$ exactly once. With this shuffle procedure, they will create a new tree out of the nodes of the old tree. 1. Choose any node $V$ from the original tree $T$. Create a...
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first line of every test case contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the number of nodes within the original tree $T$. The next $n - 1$ lines each contain two integers $u$ and $v$ ($1 \leq u, v ...
For each test case, output a single integer β€” the maximum number of leaves achievable with exactly one shuffle procedure on the whole tree.
[ [ "4\n5\n1 2\n1 3\n2 4\n2 5\n5\n1 2\n2 3\n3 4\n4 5\n6\n1 2\n1 3\n1 4\n1 5\n1 6\n10\n9 3\n8 1\n10 6\n8 5\n7 8\n4 6\n1 3\n10 1\n2 7", "4\n3\n5\n6" ] ]
In the first test case, it can be shown that the maximum number of leaves is $4$. To accomplish this, we can start our shuffle with selecting node $3$ as the new root. ![](CDN_BASE_URL/038cb83999c75e319cd6897cdfe03b7b) Next, we are left only with one subtree, in which we can select node $2$ to be the new root of that ...
Title: Shuffle time_limit_ms: 2000 memory_limit_mb: 256 Description: Two hungry red pandas, Oscar and Lura, have a tree $T$ with $n$ nodes. They are willing to perform the following shuffle procedure on the whole tree $T$ exactly once. With this shuffle procedure, they will create a new tree out of the nodes of the old...
2004A
https://codeforces.com/problemset/problem/2004/A
Closest Point
800
[ "implementation", "math" ]
Div. 2
2,000
512
Consider a set of points on a line. The distance between two points $i$ and $j$ is $|i - j|$. The point $i$ from the set is the closest to the point $j$ from the set, if there is no other point $k$ in the set such that the distance from $j$ to $k$ is strictly less than the distance from $j$ to $i$. In other words, all...
The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. Each test case consists of two lines: * the first line contains one integer $n$ ($2 \le n \le 40$) β€” the number of points in the set; * the second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_1 < x_2 < \dots <...
For each test case, print YES if it is possible to add a new point according to the conditions from the statement. Otherwise, print NO.
[ [ "3\n2\n3 8\n2\n5 6\n6\n1 2 3 4 5 10", "YES\nNO\nNO" ] ]
In the first example, the point $7$ will be the closest to both $3$ and $8$. In the second example, it is impossible to add an integer point so that it becomes the closest to both $5$ and $6$, and is different from both of them.
Title: Closest Point time_limit_ms: 2000 memory_limit_mb: 512 Description: Consider a set of points on a line. The distance between two points $i$ and $j$ is $|i - j|$. The point $i$ from the set is the closest to the point $j$ from the set, if there is no other point $k$ in the set such that the distance from $j$ to ...
1980F2
https://codeforces.com/problemset/problem/1980/F2
Field Division (hard version)
2,400
[ "math", "sortings" ]
Div. 3
3,000
256
This is a hard version of the problem; it differs from the easy version only by the question. The easy version only needs you to print whether some values are non-zero or not. The hard version needs you to print the exact values. Alice and Bob are dividing the field. The field is a rectangle of size $n \times m$ ($2 \...
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains three integers $n$, $m$, and $k$ ($2 \le n, m \le 10^9$, $2 \le k \le 2 \cdot 10^5$) β€” the field sizes and the number of fountains, respectively. Then follow $k$ lines, each containi...
For each test case, first output $\alpha$ β€” the maximum size of the plot that can belong to Alice if Bob does not give her any of the fountains. Then output $k$ non-negative integers $a_1, a_2, \dots, a_k$, where $a_i$ is a value such that after Bob gives Alice the $i$-th fountain, the maximum size of her plot will be ...
[ [ "5\n2 2 3\n1 1\n1 2\n2 2\n5 5 4\n1 2\n2 2\n3 4\n4 3\n2 5 9\n1 2\n1 5\n1 1\n2 2\n2 4\n2 5\n1 4\n2 3\n1 3\n6 4 4\n6 2\n1 3\n1 4\n1 2\n3 4 5\n2 1\n3 2\n1 4\n1 3\n2 4", "1\n1 0 1 \n11\n0 1 0 4 \n1\n0 0 1 1 0 0 0 0 0 \n6\n15 0 0 0 \n1\n2 3 0 0 0" ] ]
Below are the images for the second example: ![](CDN_BASE_URL/0e3fee1fe74ac4348d869fc256da57a3) The indices of the fountains are labeled in green. The cells belonging to Alice are marked in blue. Note that if Bob gives Alice fountain $1$ or fountain $3$, then that fountain cannot be on Alice's plot.
Title: Field Division (hard version) time_limit_ms: 3000 memory_limit_mb: 256 Description: This is a hard version of the problem; it differs from the easy version only by the question. The easy version only needs you to print whether some values are non-zero or not. The hard version needs you to print the exact values....
2008B
https://codeforces.com/problemset/problem/2008/B
Square or Not
800
[ "brute force", "math", "strings" ]
Div. 3
2,000
256
A beautiful binary matrix is a matrix that has ones on its edges and zeros inside. ![](CDN_BASE_URL/5df31946486165887b31c82158ed558d) Examples of four beautiful binary matrices. Today, Sakurako was playing with a beautiful binary matrix of size $r \times c$ and created a binary string $s$ by writing down all the rows...
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the length of the string. The second line of each test case contains the string $s$ of length $n$. The string is always the result...
Print "Yes", if the original matrix could have been square, and "No" otherwise.
[ [ "5\n2\n11\n4\n1111\n9\n111101111\n9\n111111111\n12\n111110011111", "No\nYes\nYes\nNo\nNo" ] ]
For the second test case, string 1111 can be obtained from the matrix: $1$| $1$ ---|--- $1$| $1$ For the third test case, string 111101111 can be obtained from the matrix: $1$| $1$| $1$ ---|---|--- $1$| $0$| $1$ $1$| $1$| $1$ There is no square matrix in the fourth case, such that the string can b...
Title: Square or Not time_limit_ms: 2000 memory_limit_mb: 256 Description: A beautiful binary matrix is a matrix that has ones on its edges and zeros inside. ![](CDN_BASE_URL/5df31946486165887b31c82158ed558d) Examples of four beautiful binary matrices. Today, Sakurako was playing with a beautiful binary matrix of siz...
1976C
https://codeforces.com/problemset/problem/1976/C
Job Interview
1,600
[ "binary search", "dp", "greedy", "implementation", "two pointers" ]
Div. 2
2,000
256
Monocarp is opening his own IT company. He wants to hire $n$ programmers and $m$ testers. There are $n+m+1$ candidates, numbered from $1$ to $n+m+1$ in chronological order of their arriving time. The $i$-th candidate has programming skill $a_i$ and testing skill $b_i$ (a person's programming skill is different from th...
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Each test case consists of three lines: * the first line contains two integers $n$ and $m$ ($0 \le n, m \le 2 \cdot 10^5$; $2 \le n + m + 1 \le 2 \cdot 10^5$) β€” the number of programmers and the number of testers Monocarp ...
For each test case, print $n + m + 1$ integers, where the $i$-th integer should be equal to the skill of the team if everyone except the $i$-th candidate comes to interview.
[ [ "4\n1 0\n2 1\n1 2\n0 2\n4 5 5\n5 4 1\n1 2\n2 1 5 4\n5 2 3 1\n3 1\n4 3 3 4 1\n5 5 4 5 2", "1 2 \n5 6 9 \n8 11 11 12 \n13 13 13 12 15" ] ]
Let's consider the third test case of the example: * if the $1$-st candidate does not arrive, the $2$-nd candidate gets hired as a tester, the $3$-rd candidate gets hired as a programmer, the $4$-th candidate gets hired as a tester. The total skill of the team will be $2 + 5 + 1 = 8$; * if the $2$-nd candidate do...
Title: Job Interview time_limit_ms: 2000 memory_limit_mb: 256 Description: Monocarp is opening his own IT company. He wants to hire $n$ programmers and $m$ testers. There are $n+m+1$ candidates, numbered from $1$ to $n+m+1$ in chronological order of their arriving time. The $i$-th candidate has programming skill $a_i$...
2033A
https://codeforces.com/problemset/problem/2033/A
Sakurako and Kosuke
800
[ "constructive algorithms", "implementation", "math" ]
Div. 3
1,000
256
Sakurako and Kosuke decided to play some games with a dot on a coordinate line. The dot is currently located in position $x=0$. They will be taking turns, and Sakurako will be the one to start. On the $i$-th move, the current player will move the dot in some direction by $2\cdot i-1$ units. Sakurako will always be mov...
The first line contains one integer $t$ ($1\le t\le 100$) β€” the number of games that Sakurako and Kosuke played. Each game is described by one number $n$ ($1 \le n\le 100$) β€” the number that defines the condition when the game ends.
For each of the $t$ games, output a line with the result of that game. If Sakurako makes the last turn, output "Sakurako" (without quotes); else output "Kosuke".
[ [ "4\n1\n6\n3\n98", "Kosuke\nSakurako\nKosuke\nSakurako" ] ]
Title: Sakurako and Kosuke time_limit_ms: 1000 memory_limit_mb: 256 Description: Sakurako and Kosuke decided to play some games with a dot on a coordinate line. The dot is currently located in position $x=0$. They will be taking turns, and Sakurako will be the one to start. On the $i$-th move, the current player will ...
1998E2
https://codeforces.com/problemset/problem/1998/E2
Eliminating Balls With Merging (Hard Version)
2,500
[ "binary search", "brute force", "data structures", "divide and conquer", "greedy", "implementation" ]
Div. 2
4,000
512
Drink water. β€” Sun Tzu, The Art of Becoming a Healthy Programmer This is the hard version of the problem. The only difference is that $x=1$ in this version. You must solve both versions to be able to hack. You are given two integers $n$ and $x$ ($x=1$). There are $n$ balls lined up in a row, numbered from $1$ to $n$...
The first line contains $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $x$ ($1 \leq n \leq 2 \cdot 10^5; x = 1$) β€” the number of balls and the smallest index $i$ for which you need to find $f(i)$. The second line of each test case contains $a_1, a...
For each test case, output $n-x+1$ space separated integers on a new line, where the $j$-th integer should represent $f(x+j-1)$.
[ [ "3\n5 1\n1 2 3 2 1\n7 1\n4 5 1 2 1 4 5\n11 1\n1 2 3 1 1 9 3 2 4 1 3", "1 1 2 2 3\n1 1 1 1 1 3 4\n1 1 2 2 2 1 1 1 3 3 4" ] ]
In the first test case, below are the possible values of $j$ for each $f(i)$ from $1$ to $n$. * For $f(1)$, the only possible value of $j$ is $1$. * For $f(2)$, the only possible value of $j$ is $2$. * For $f(3)$, the possible values of $j$ are $2$ and $3$. * For $f(4)$, the possible values of $j$ are $2$ a...
Title: Eliminating Balls With Merging (Hard Version) time_limit_ms: 4000 memory_limit_mb: 512 Description: Drink water. β€” Sun Tzu, The Art of Becoming a Healthy Programmer This is the hard version of the problem. The only difference is that $x=1$ in this version. You must solve both versions to be able to hack. You ...
2036E
https://codeforces.com/problemset/problem/2036/E
Reverse the Rivers
1,600
[ "binary search", "constructive algorithms", "data structures", "greedy" ]
Div. 3
2,000
256
A conspiracy of ancient sages, who decided to redirect rivers for their own convenience, has put the world on the brink. But before implementing their grand plan, they decided to carefully think through their strategy β€” that's what sages do. There are $n$ countries, each with exactly $k$ regions. For the $j$-th region...
The first line contains three integers $n$, $k$, and $q$ ($1 \leq n, k, q \leq 10^5$) β€” the number of countries, regions, and queries, respectively. Next, there are $n$ lines, where the $i$-th line contains $k$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,k}$ ($1 \leq a_{i,j} \leq 10^9$), where $a_{i,j}$ is the value of th...
For each query, output a single integer on a new line β€” the smallest number of the suitable country, or $-1$ if no such country exists.
[ [ "3 4 4\n1 3 5 9\n4 6 5 3\n2 1 2 7\n3\n1 > 4\n2 < 8\n1 < 6\n2\n1 < 8\n2 > 8\n1\n3 > 5\n2\n4 > 8\n1 < 8", "2\n-1\n3\n1" ] ]
In the example, the initial values of the regions are as follows: $1$| $3$| $5$| $9$ ---|---|---|--- $4$| $6$| $5$| $3$ $2$| $1$| $2$| $7$ After creating the channels, the new values will look like this: $1$| $3$| $5$| $9$ ---|---|---|--- $1 | 4$| $3 | 6$| $5 | 5$| $9 | 3$ $1 | 4 | 2$| $3 | 6 | 1$| $...
Title: Reverse the Rivers time_limit_ms: 2000 memory_limit_mb: 256 Description: A conspiracy of ancient sages, who decided to redirect rivers for their own convenience, has put the world on the brink. But before implementing their grand plan, they decided to carefully think through their strategy β€” that's what sages do...
2000B
https://codeforces.com/problemset/problem/2000/B
Seating in a Bus
800
[ "two pointers" ]
Div. 3
2,000
256
In Berland, a bus consists of a row of $n$ seats numbered from $1$ to $n$. Passengers are advised to always board the bus following these rules: * If there are no occupied seats in the bus, a passenger can sit in any free seat; * Otherwise, a passenger should sit in any free seat that has at least one occupied nei...
The first line of input contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The following describes the input test cases. The first line of each test case contains exactly one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of seats in the bus and the number of passengers who boarded ...
For each test case, output on a separate line: * "YES", if all passengers followed the recommendations; * "NO" otherwise. You may output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
[ [ "4\n5\n5 4 2 1 3\n3\n2 3 1\n4\n2 3 1 4\n5\n1 2 3 5 4", "NO\nYES\nYES\nNO" ] ]
The first test case is explained in the problem statement.
Title: Seating in a Bus time_limit_ms: 2000 memory_limit_mb: 256 Description: In Berland, a bus consists of a row of $n$ seats numbered from $1$ to $n$. Passengers are advised to always board the bus following these rules: * If there are no occupied seats in the bus, a passenger can sit in any free seat; * Otherwi...
2002B
https://codeforces.com/problemset/problem/2002/B
Removals Game
1,000
[ "constructive algorithms", "games" ]
Div. 1 + 2
1,000
256
Alice got a permutation $a_1, a_2, \ldots, a_n$ of $[1,2,\ldots,n]$, and Bob got another permutation $b_1, b_2, \ldots, b_n$ of $[1,2,\ldots,n]$. They are going to play a game with these arrays. In each turn, the following events happen in order: * Alice chooses either the first or the last element of her array and...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^4$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1\le n\le 3\cdot 10^5$). The next line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le n$...
For each test case, print a single line with the name of the winner, assuming both players play optimally. If Alice wins, print $\texttt{Alice}$; otherwise, print $\texttt{Bob}$.
[ [ "2\n2\n1 2\n1 2\n3\n1 2 3\n2 3 1", "Bob\nAlice" ] ]
In the first test case, Bob can win the game by deleting the same element as Alice did. In the second test case, Alice can delete $3$ in the first turn, and then in the second turn, delete the element that is different from the one Bob deleted in the first turn to win the game.
Title: Removals Game time_limit_ms: 1000 memory_limit_mb: 256 Description: Alice got a permutation $a_1, a_2, \ldots, a_n$ of $[1,2,\ldots,n]$, and Bob got another permutation $b_1, b_2, \ldots, b_n$ of $[1,2,\ldots,n]$. They are going to play a game with these arrays. In each turn, the following events happen in orde...
2033G
https://codeforces.com/problemset/problem/2033/G
Sakurako and Chefir
2,200
[ "data structures", "dfs and similar", "dp", "greedy", "trees" ]
Div. 3
4,000
256
Given a tree with $n$ vertices rooted at vertex $1$. While walking through it with her cat Chefir, Sakurako got distracted, and Chefir ran away. To help Sakurako, Kosuke recorded his $q$ guesses. In the $i$-th guess, he assumes that Chefir got lost at vertex $v_i$ and had $k_i$ stamina. Also, for each guess, Kosuke a...
The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Each test case is described as follows: * The first line contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of vertices in the tree. * The next $n-1$ lines contain the edges of the tree. It is guarante...
For each test case and for each guess, output the maximum distance to the farthest vertex that Chefir could reach from the starting point $v_i$ having $k_i$ stamina.
[ [ "3\n5\n1 2\n2 3\n3 4\n3 5\n3\n5 1\n3 1\n2 0\n9\n8 1\n1 7\n1 4\n7 3\n4 9\n3 2\n1 5\n3 6\n7\n6 0\n2 3\n6 2\n8 2\n2 4\n9 2\n6 3\n6\n2 1\n2 5\n2 4\n5 6\n4 3\n3\n3 1\n1 3\n6 5", "2 1 2 \n0 5 2 4 5 5 5 \n1 3 4" ] ]
In the first example: * In the first query, you can go from vertex $5$ to vertex $3$ (after which your stamina will decrease by $1$ and become $0$), and then you can go to vertex $4$; * In the second query, from vertex $3$ with $1$ stamina, you can only reach vertices $2$, $3$, $4$, and $5$; * In the third que...
Title: Sakurako and Chefir time_limit_ms: 4000 memory_limit_mb: 256 Description: Given a tree with $n$ vertices rooted at vertex $1$. While walking through it with her cat Chefir, Sakurako got distracted, and Chefir ran away. To help Sakurako, Kosuke recorded his $q$ guesses. In the $i$-th guess, he assumes that Chefi...
2035D
https://codeforces.com/problemset/problem/2035/D
Yet Another Real Number Problem
1,800
[ "binary search", "data structures", "divide and conquer", "greedy", "implementation", "math" ]
Div. 1 + 2
2,000
256
Three r there are's in strawberry. You are given an array $b$ of length $m$. You can perform the following operation any number of times (possibly zero): * Choose two distinct indices $i$ and $j$ where $\bf{1\le i < j\le m}$ and $b_i$ is even, divide $b_i$ by $2$ and multiply $b_j$ by $2$. Your task is to maximiz...
The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the length of $a$. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the starting values of arr...
For each test case, output $n$ integers representing the answer for each prefix of $a$ modulo $10^9+7$.
[ [ "3\n10\n1 2 3 4 5 6 7 8 9 10\n11\n1 6 9 4 7 4 4 10 3 2 3\n4\n527792568 502211460 850237282 374773208", "1 3 8 13 46 59 126 149 1174 1311 \n1 7 22 26 70 74 150 1303 1306 1308 1568 \n527792568 83665723 399119771 773892979" ] ]
For each prefix in the first example, a possible array after operations is: * $[1]$ and the sum is $1$; * $[1, 2]$ and the sum is $3$; * $[1, 1, 6]$ and the sum is $8$; * $[1, 1, 3, 8]$ and the sum is $13$; * $[1, 1, 3, 1, 40]$ and the sum is $46$; * $[1, 1, 3, 1, 5, 48]$ and the sum is $59$; * $[1...
Title: Yet Another Real Number Problem time_limit_ms: 2000 memory_limit_mb: 256 Description: Three r there are's in strawberry. You are given an array $b$ of length $m$. You can perform the following operation any number of times (possibly zero): * Choose two distinct indices $i$ and $j$ where $\bf{1\le i < j\le m}...
2026E
https://codeforces.com/problemset/problem/2026/E
Best Subsequence
2,500
[ "bitmasks", "dfs and similar", "flows", "graph matchings", "graphs" ]
Div. 2
2,000
256
Given an integer array $a$ of size $n$. Let's define the value of the array as its size minus the number of set bits in the bitwise OR of all elements of the array. For example, for the array $[1, 0, 1, 2]$, the bitwise OR is $3$ (which contains $2$ set bits), and the value of the array is $4-2=2$. Your task is to c...
The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$). The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < 2^{60}$).
For each test case, print the maximum possible value of some subsequence of the given array.
[ [ "4\n3\n0 0 0\n4\n1 0 1 2\n1\n5\n8\n7 1 48 14 13 8 7 6", "3\n2\n0\n3" ] ]
Title: Best Subsequence time_limit_ms: 2000 memory_limit_mb: 256 Description: Given an integer array $a$ of size $n$. Let's define the value of the array as its size minus the number of set bits in the bitwise OR of all elements of the array. For example, for the array $[1, 0, 1, 2]$, the bitwise OR is $3$ (which con...
2002E
https://codeforces.com/problemset/problem/2002/E
Cosmic Rays
2,300
[ "brute force", "data structures", "dp" ]
Div. 1 + 2
2,000
512
Given an array of integers $s_1, s_2, \ldots, s_l$, every second, cosmic rays will cause all $s_i$ such that $i=1$ or $s_i\neq s_{i-1}$ to be deleted simultaneously, and the remaining parts will be concatenated together in order to form the new array $s_1, s_2, \ldots, s_{l'}$. Define the strength of an array as the n...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^4$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1\le n\le3\cdot10^5$) β€” the length of sequence $a$. The next $n$ lines contain two integers each $...
For each test case, print one line containing $n$ integers β€” the answer for each prefix of pairs.
[ [ "4\n4\n2 0\n1 1\n3 0\n5 1\n6\n4 6\n1 3\n4 6\n4 0\n7 6\n6 3\n7\n9 0\n7 1\n5 0\n7 1\n9 0\n1 1\n2 0\n10\n10 7\n4 9\n2 2\n7 9\n2 8\n8 5\n11 7\n15 5\n12 7\n4 0", "2 2 4 5 \n4 4 7 7 10 10 \n9 9 9 9 9 9 10 \n10 10 10 10 10 10 12 15 15 15" ] ]
In the first test case, for the prefix of length $4$, the changes will be $[0,0,1,0,0,0,1,1,1,1,1]\rightarrow[0,0,0,1,1,1,1]\rightarrow[0,0,1,1,1]\rightarrow[0,1,1]\rightarrow[1]\rightarrow[]$, so the array becomes empty after $5$ seconds. In the second test case, for the prefix of length $4$, the changes will be $[6,...
Title: Cosmic Rays time_limit_ms: 2000 memory_limit_mb: 512 Description: Given an array of integers $s_1, s_2, \ldots, s_l$, every second, cosmic rays will cause all $s_i$ such that $i=1$ or $s_i\neq s_{i-1}$ to be deleted simultaneously, and the remaining parts will be concatenated together in order to form the new ar...
1986D
https://codeforces.com/problemset/problem/1986/D
Mathematical Problem
1,400
[ "brute force", "dp", "greedy", "implementation", "math", "two pointers" ]
Div. 3
2,000
256
You are given a string $s$ of length $n > 1$, consisting of digits from $0$ to $9$. You must insert exactly $n - 2$ symbols $+$ (addition) or $\times$ (multiplication) into this string to form a valid arithmetic expression. In this problem, the symbols cannot be placed before the first or after the last character of t...
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. Then follows their description. The first line of each test case contains a single integer $n$ ($2 \leq n \leq 20$) β€” the length of the string $s$. The second line of each test ca...
For each test case, output the minimum result of the arithmetic expression that can be obtained by inserting exactly $n - 2$ addition or multiplication symbols into the given string.
[ [ "18\n2\n10\n2\n74\n2\n00\n2\n01\n3\n901\n3\n101\n5\n23311\n6\n987009\n7\n1111111\n20\n99999999999999999999\n20\n00000000000000000000\n4\n0212\n18\n057235283621345395\n4\n1112\n20\n19811678487321784121\n4\n1121\n4\n2221\n3\n011", "10\n74\n0\n1\n9\n1\n19\n0\n11\n261\n0\n0\n0\n12\n93\n12\n24\n0" ] ]
In the first four test cases, we cannot add symbols, so the answer will be the original number. In the fifth test case, the optimal answer looks as follows: $9 \times 01 = 9 \times 1 = 9$. In the sixth test case, the optimal answer looks as follows: $1 \times 01 = 1 \times 1 = 1$. In the seventh test case, the optim...
Title: Mathematical Problem time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given a string $s$ of length $n > 1$, consisting of digits from $0$ to $9$. You must insert exactly $n - 2$ symbols $+$ (addition) or $\times$ (multiplication) into this string to form a valid arithmetic expression. In this probl...
2008A
https://codeforces.com/problemset/problem/2008/A
Sakurako's Exam
800
[ "brute force", "constructive algorithms", "greedy", "math" ]
Div. 3
1,000
256
Today, Sakurako has a math exam. The teacher gave the array, consisting of $a$ ones and $b$ twos. In an array, Sakurako must place either a '+' or a '-' in front of each element so that the sum of all elements in the array equals $0$. Sakurako is not sure if it is possible to solve this problem, so determine whether ...
The first line contains a single integer $t$ ($1\le t\le 100$) β€” the number of test cases. The only line of each test case contains two integers $a$ and $b$ ($0\le a,b<10$) β€” the number of '1's and the number of '2's in the array.
For each test case, output "Yes" if you can make the sum of the entire array equal to $0$, and "No" otherwise. You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.
[ [ "5\n0 1\n0 3\n2 0\n2 3\n3 1", "NO\nNO\nYES\nYES\nNO" ] ]
1. $a=0$, $b=1$: This means the array is $[2]$ β€” it is impossible to add the signs '+' or '-' to get $0$ as a result; 2. $a=0$, $b=3$: This means the array is $[2, 2, 2]$ β€” it is impossible to add the signs '+' or '-' to get $0$ as a result; 3. $a=2$, $b=0$: This means the array is $[1, 1]$ β€” it is possible to add ...
Title: Sakurako's Exam time_limit_ms: 1000 memory_limit_mb: 256 Description: Today, Sakurako has a math exam. The teacher gave the array, consisting of $a$ ones and $b$ twos. In an array, Sakurako must place either a '+' or a '-' in front of each element so that the sum of all elements in the array equals $0$. Sakura...
2002D1
https://codeforces.com/problemset/problem/2002/D1
DFS Checker (Easy Version)
1,900
[ "brute force", "data structures", "dfs and similar", "graphs", "hashing", "trees" ]
Div. 1 + 2
2,000
512
This is the easy version of the problem. In this version, the given tree is a perfect binary tree and the constraints on $n$ and $q$ are lower. You can make hacks only if both versions of the problem are solved. You are given a perfect binary tree$^\dagger$ consisting of $n$ vertices. The vertices are numbered from $1...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^4$). The description of the test cases follows. The first line of each test case contains two integers $n$, $q$ ($3\le n\le 65\,535$, $2\le q\le 5 \cdot 10^4$) β€” the number of vertices in the tree and the number ...
For each test case, print $q$ lines corresponding to the $q$ queries. For each query, output $\texttt{YES}$ if there is a DFS order that exactly equals the current permutation, and output $\texttt{NO}$ otherwise. You can output $\texttt{Yes}$ and $\texttt{No}$ in any case (for example, strings $\texttt{yEs}$, $\texttt...
[ [ "2\n3 3\n1 1\n1 2 3\n2 3\n3 2\n1 3\n7 4\n1 1 2 2 3 3\n1 2 3 4 5 6 7\n3 5\n2 5\n3 7\n4 6", "YES\nYES\nNO\nYES\nNO\nNO\nYES" ] ]
In the first test case, the permutation $p_1, p_2, \ldots, p_n$ after each modification is $[1,3,2],[1,2,3],[3,2,1]$, respectively. The first two permutations are valid DFS orders; the third is not a DFS order. In the second test case, the permutation $p_1, p_2, \ldots, p_n$ after each modification is $[1,2,5,4,3,6,7]...
Title: DFS Checker (Easy Version) time_limit_ms: 2000 memory_limit_mb: 512 Description: This is the easy version of the problem. In this version, the given tree is a perfect binary tree and the constraints on $n$ and $q$ are lower. You can make hacks only if both versions of the problem are solved. You are given a per...
2005D
https://codeforces.com/problemset/problem/2005/D
Alter the GCD
2,400
[ "binary search", "brute force", "data structures", "divide and conquer", "implementation", "number theory" ]
Div. 2
4,000
256
You are given two arrays $a_1, a_2, \ldots, a_n$ and $b_1, b_2, \ldots, b_n$. You must perform the following operation exactly once: * choose any indices $l$ and $r$ such that $1 \le l \le r \le n$; * swap $a_i$ and $b_i$ for all $i$ such that $l \leq i \leq r$. Find the maximum possible value of $\text{gcd}(a...
In the first line of the input, you are given a single integer $t$ ($1 \le t \le 10^5$), the number of test cases. Then the description of each test case follows. In the first line of each test case, you are given a single integer $n$ ($1 \le n \le 2 \cdot 10^5$), representing the number of integers in each array. In...
For each test case, output a line with two integers: the maximum value of $\text{gcd}(a_1, a_2, \ldots, a_n) + \text{gcd}(b_1, b_2, \ldots, b_n)$ after performing the operation exactly once, and the number of ways.
[ [ "5\n8\n11 4 16 17 3 24 25 8\n8 10 4 21 17 18 25 21\n4\n6 4 24 13\n15 3 1 14\n2\n13 14\n5 8\n8\n20 17 15 11 21 10 3 7\n9 9 4 20 14 9 13 1\n2\n18 13\n15 20", "2 36\n3 2\n2 3\n2 36\n6 1" ] ]
In the first, third, and fourth test cases, there's no way to achieve a higher GCD than $1$ in any of the arrays, so the answer is $1 + 1 = 2$. Any pair $(l, r)$ achieves the same result; for example, in the first test case there are $36$ such pairs. In the last test case, you must choose $l = 1$, $r = 2$ to maximize ...
Title: Alter the GCD time_limit_ms: 4000 memory_limit_mb: 256 Description: You are given two arrays $a_1, a_2, \ldots, a_n$ and $b_1, b_2, \ldots, b_n$. You must perform the following operation exactly once: * choose any indices $l$ and $r$ such that $1 \le l \le r \le n$; * swap $a_i$ and $b_i$ for all $i$ such...
1976A
https://codeforces.com/problemset/problem/1976/A
Verify Password
800
[ "implementation", "sortings", "strings" ]
Div. 2
2,000
256
Monocarp is working on his new site, and the current challenge is to make the users pick strong passwords. Monocarp decided that strong passwords should satisfy the following conditions: * password should consist only of lowercase Latin letters and digits; * there should be no digit that comes after a letter (so...
The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of testcases. The first line of each testcase contains a single integer $n$ ($1 \le n \le 20$) β€” the length of the password. The second line contains a string, consisting of exactly $n$ characters. Each character is either a lowercase Lati...
For each testcase, print "YES" if the given password is strong and "NO" otherwise.
[ [ "5\n4\n12ac\n5\n123wa\n9\nallllmost\n5\nac123\n6\n011679", "YES\nNO\nYES\nNO\nYES" ] ]
In the second testcase, the letters are not sorted in the non-decreasing order. In the fourth testcase, there is a digit that comes after a letter β€” digit '1' after a letter 'c'.
Title: Verify Password time_limit_ms: 2000 memory_limit_mb: 256 Description: Monocarp is working on his new site, and the current challenge is to make the users pick strong passwords. Monocarp decided that strong passwords should satisfy the following conditions: * password should consist only of lowercase Latin le...
1986E
https://codeforces.com/problemset/problem/1986/E
Beautiful Array
1,700
[ "greedy", "math", "number theory", "sortings" ]
Div. 3
2,000
256
You are given an array of integers $a_1, a_2, \ldots, a_n$ and an integer $k$. You need to make it beautiful with the least amount of operations. Before applying operations, you can shuffle the array elements as you like. For one operation, you can do the following: * Choose an index $1 \leq i \leq n$, * Make $a_...
Each test consists of several sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of sets of input data. Then follows their description. The first line of each set of input data contains two integers $n$ and $k$ ($1 \leq n \leq 10^5$, $1 \leq k \leq 10^9$) β€” the size of...
For each set of input data, output the minimum number of operations needed to make the array beautiful, or $-1$ if it is impossible.
[ [ "11\n1 1000000000\n1\n2 1\n624323799 708290323\n3 1\n3 2 1\n4 1\n7 1 5 3\n5 1\n11 2 15 7 10\n7 1\n1 8 2 16 8 16 31\n13 1\n2 1 1 3 3 11 12 22 45 777 777 1500 74\n10 2\n1 2 1 2 1 2 1 2 1 2\n11 2\n1 2 1 2 1 2 1 2 1 2 1\n13 3\n2 3 9 14 17 10 22 20 18 30 1 4 28\n5 1\n2 3 5 3 5", "0\n83966524\n1\n4\n6\n1\n48\n-...
In the first set of input data, the array is already beautiful. In the second set of input data, you can shuffle the array before the operations and perform the operation with index $i = 1$ for $83966524$ times. In the third set of input data, you can shuffle the array $a$ and make it equal to $[2, 3, 1]$. Then apply...
Title: Beautiful Array time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given an array of integers $a_1, a_2, \ldots, a_n$ and an integer $k$. You need to make it beautiful with the least amount of operations. Before applying operations, you can shuffle the array elements as you like. For one operation, y...
1987G2
https://codeforces.com/problemset/problem/1987/G2
Spinning Round (Hard Version)
3,500
[ "divide and conquer", "dp", "trees" ]
Div. 1 + 2
7,000
1,024
This is the hard version of the problem. The only difference between the two versions are the allowed characters in $s$. You can make hacks only if both versions of the problem are solved. You are given a permutation $p$ of length $n$. You are also given a string $s$ of length $n$, where each character is either L, R,...
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \le n \le 4 \cdot 10^5$) β€” the length of the permutation $...
For each test case, output the maximum possible diameter over all connected graphs that you form, or $-1$ if it is not possible to form any connected graphs.
[ [ "8\n5\n2 1 4 3 5\nR?RL?\n2\n1 2\nLR\n3\n3 1 2\nL?R\n7\n5 3 1 6 4 2 7\n?R?R?R?\n5\n5 2 1 3 4\n?????\n6\n6 2 3 4 5 1\n?LLRLL\n8\n1 7 5 6 2 8 4 3\n?R??????\n12\n6 10 7 1 8 5 12 2 11 3 4 9\n????????????", "3\n-1\n-1\n4\n4\n3\n5\n8" ] ]
In the first test case, there are two connected graphs (the labels are indices): ![](CDN_BASE_URL/4561c9025a4357785c7aff0bb1bbff9a)| ![](CDN_BASE_URL/a680f9e3bffa38fd90dd7bbc4c3e5f3b) ---|--- The graph on the left has a diameter of $2$, while the graph on the right has a diameter of $3$, so the answer is $3$. ...
Title: Spinning Round (Hard Version) time_limit_ms: 7000 memory_limit_mb: 1024 Description: This is the hard version of the problem. The only difference between the two versions are the allowed characters in $s$. You can make hacks only if both versions of the problem are solved. You are given a permutation $p$ of len...
2033E
https://codeforces.com/problemset/problem/2033/E
Sakurako, Kosuke, and the Permutation
1,400
[ "brute force", "data structures", "dfs and similar", "dsu", "graphs", "greedy", "math" ]
Div. 3
2,000
256
Sakurako's exams are over, and she did excellently. As a reward, she received a permutation $p$. Kosuke was not entirely satisfied because he failed one exam and did not receive a gift. He decided to sneak into her room (thanks to the code for her lock) and spoil the permutation so that it becomes simple. A permutatio...
The first line contains one integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Each test case is described by two lines. * The first line contains one integer $n$ ($1\le n \le 10^6$) β€” the length of the permutation $p$. * The second line contains $n$ integers $p_i$ ($1\le p_i\le n$) β€” the elements of the...
For each test case, output the minimum number of operations that Kosuke needs to perform to make the permutation simple.
[ [ "6\n5\n1 2 3 4 5\n5\n5 4 3 2 1\n5\n2 3 4 5 1\n4\n2 3 4 1\n3\n1 3 2\n7\n2 3 1 5 6 7 4", "0\n0\n2\n1\n0\n2" ] ]
In the first and second examples, the permutations are already simple. In the fourth example, it is sufficient to swap $p_2$ and $p_4$. Thus, the permutation will become $[2, 1, 4, 3]$ in $1$ operation.
Title: Sakurako, Kosuke, and the Permutation time_limit_ms: 2000 memory_limit_mb: 256 Description: Sakurako's exams are over, and she did excellently. As a reward, she received a permutation $p$. Kosuke was not entirely satisfied because he failed one exam and did not receive a gift. He decided to sneak into her room (...