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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1986G2 | https://codeforces.com/problemset/problem/1986/G2 | Permutation Problem (Hard Version) | 2,500 | [
"brute force",
"data structures",
"hashing",
"math",
"number theory"
] | Div. 3 | 3,000 | 128 | This is the hard version of the problem. The only difference is that in this version $n \leq 5 \cdot 10^5$ and the sum of $n$ for all sets of input data does not exceed $5 \cdot 10^5$.
You are given a permutation $p$ of length $n$. Calculate the number of index pairs $1 \leq i < j \leq n$ such that $p_i \cdot p_j$ is ... | 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 a single integer $n$ ($1 \leq n \leq 5 \cdot 10^5$) β the length of the permutation... | For each set of input data, output the number of index pairs $1 \leq i < j \leq n$ such that $p_i \cdot p_j$ is divisible by $i \cdot j$ without remainder. | [
[
"6\n1\n1\n2\n1 2\n3\n2 3 1\n5\n2 4 1 3 5\n12\n8 9 7 12 1 10 6 3 2 4 11 5\n15\n1 2 4 6 8 10 12 14 3 9 15 5 7 11 13",
"0\n1\n1\n3\n9\n3"
]
] | In the first set of input data, there are no index pairs, as the size of the permutation is $1$.
In the second set of input data, there is one index pair $(1, 2)$ and it is valid.
In the third set of input data, the index pair $(1, 2)$ is valid.
In the fourth set of input data, the index pairs $(1, 2)$, $(1, 5)$, an... | Title: Permutation Problem (Hard Version)
time_limit_ms: 3000
memory_limit_mb: 128
Description: This is the hard version of the problem. The only difference is that in this version $n \leq 5 \cdot 10^5$ and the sum of $n$ for all sets of input data does not exceed $5 \cdot 10^5$.
You are given a permutation $p$ of len... |
1983G | https://codeforces.com/problemset/problem/1983/G | Your Loss | 3,000 | [
"bitmasks",
"brute force",
"dp",
"trees"
] | Div. 2 | 3,000 | 256 | You are given a tree with $n$ nodes numbered from $1$ to $n$, along with an array of size $n$. The value of $i$-th node is $a_{i}$. There are $q$ queries. In each query, you are given 2 nodes numbered as $x$ and $y$.
Consider the path from the node numbered as $x$ to the node numbered as $y$. Let the path be represent... | The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. Each test case contains several sets of input data.
The first line of each set of input data contains a single integer $n$ ($1 \le n \le 5 \cdot 10^5$) β the number of nodes.
The next $n-1$ lines of each set of input data co... | For each query, output a single number β the sum from the problem statement. | [
[
"1\n4\n1 2\n2 3\n3 4\n2 3 6 5\n3\n1 4\n3 4\n1 1",
"14\n10\n2"
]
] | Title: Your Loss
time_limit_ms: 3000
memory_limit_mb: 256
Description: You are given a tree with $n$ nodes numbered from $1$ to $n$, along with an array of size $n$. The value of $i$-th node is $a_{i}$. There are $q$ queries. In each query, you are given 2 nodes numbered as $x$ and $y$.
Consider the path from the node... | |
1977C | https://codeforces.com/problemset/problem/1977/C | Nikita and LCM | 1,900 | [
"brute force",
"data structures",
"dp",
"greedy",
"math",
"number theory",
"sortings"
] | Div. 2 | 2,000 | 256 | Nikita is a student passionate about number theory and algorithms. He faces an interesting problem related to an array of numbers.
Suppose Nikita has an array of integers $a$ of length $n$. He will call a subsequence$^\dagger$ of the array special if its [least common multiple (LCM)](https://en.wikipedia.org/wiki/Leas... | Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 2000$) β 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 2000$) β the length of the array $a$.
The second line o... | For each test case, output a single integer β the length of the longest special subsequence of $a$. | [
[
"6\n5\n1 2 4 8 16\n6\n3 2 10 20 60 1\n7\n2 3 4 6 12 100003 1200036\n9\n2 42 7 3 6 7 7 1 6\n8\n4 99 57 179 10203 2 11 40812\n1\n1",
"0\n4\n4\n5\n8\n0"
]
] | In the first test case, the LCM of any non-empty subsequence is contained in $a$, so the answer is $0$.
In the second test case, we can take the subsequence $[3, 2, 10, 1]$, its LCM is equal to $30$, which is not contained in $a$.
In the third test case, we can take the subsequence $[2, 3, 6, 100\,003]$, its LCM is e... | Title: Nikita and LCM
time_limit_ms: 2000
memory_limit_mb: 256
Description: Nikita is a student passionate about number theory and algorithms. He faces an interesting problem related to an array of numbers.
Suppose Nikita has an array of integers $a$ of length $n$. He will call a subsequence$^\dagger$ of the array spe... |
1987G1 | https://codeforces.com/problemset/problem/1987/G1 | Spinning Round (Easy Version) | 2,900 | [
"divide and conquer",
"dp",
"trees"
] | Div. 1 + 2 | 7,000 | 1,024 | This is the easy version of the problem. The only difference between the two versions are the allowed characters in $s$. In the easy version, $s$ only contains the character ?. 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... | 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\n?????\n2\n1 2\n??\n3\n3 1 2\n???\n7\n5 3 1 6 4 2 7\n???????\n5\n5 2 1 3 4\n?????\n6\n6 2 3 4 5 1\n??????\n8\n1 7 5 6 2 8 4 3\n????????\n12\n6 10 7 1 8 5 12 2 11 3 4 9\n????????????",
"4\n1\n2\n6\n4\n5\n5\n8"
]
] | In the first test case, here are some possible connected graphs that you can form (the labels are indices):
| |  ---|---|--- In the second test case, the only con... | Title: Spinning Round (Easy Version)
time_limit_ms: 7000
memory_limit_mb: 1024
Description: This is the easy version of the problem. The only difference between the two versions are the allowed characters in $s$. In the easy version, $s$ only contains the character ?. You can make hacks only if both versions of the pro... |
1983F | https://codeforces.com/problemset/problem/1983/F | array-value | 2,500 | [
"binary search",
"bitmasks",
"data structures",
"greedy",
"two pointers"
] | Div. 2 | 4,000 | 256 | You have an array of non-negative integers $a_1, a_2, \ldots, a_n$.
The value of a sub-array of length $\ge 2$, $a[l, r] = [a_l, a_{l+1}, \ldots, a_r]$ is the minimum value of $a_i \oplus a_j$ such that $l \le i < j \le r$, where $\oplus$ is the xor (exclusive-or) operator.
You have to find the $k$-th smallest value ... | The first line of the input contains multiple test cases $t$ ($1 \le t \le 2 \cdot 10^4$).
The first line of each test case contains integer numbers $n$ and $k$ ($2 \le n \le 10^5$, $1 \le k \le \frac{n\cdot(n-1)}{2}$).
The second line of the input contains $n$ non-negative integer numbers $a_1, a_2, \ldots, a_n$ ($0... | Print the $k$-th smallest value obtained over all subarrays of length at least $2$. | [
[
"4\n5 2\n1 2 3 4 5\n2 1\n4 3\n4 6\n1 2 4 8\n5 9\n1 2 3 4 5",
"1\n7\n12\n3"
]
] | In the first testcase, we have subarrays with their smallest exclusive-or pair as:
$[1,2]: 3$
$[2,3]: 1$
$[3,4]: 7$
$[4,5]: 1$
$[1,2,3]: 1$
$[2,3,4]: 1$
$[3,4,5]: 1$
$[1,2,3,4]: 1$
$[2,3,4,5]: 1$
$[1,2,3,4,5]: 1$
The sorted order would be: $1, 1, 1, 1, 1, 1, 1, 1, 3, 7$. Therefore, the second smallest elemen... | Title: array-value
time_limit_ms: 4000
memory_limit_mb: 256
Description: You have an array of non-negative integers $a_1, a_2, \ldots, a_n$.
The value of a sub-array of length $\ge 2$, $a[l, r] = [a_l, a_{l+1}, \ldots, a_r]$ is the minimum value of $a_i \oplus a_j$ such that $l \le i < j \le r$, where $\oplus$ is the ... |
2026F | https://codeforces.com/problemset/problem/2026/F | Bermart Ice Cream | 2,700 | [
"data structures",
"dfs and similar",
"divide and conquer",
"dp",
"implementation",
"trees"
] | Div. 2 | 2,000 | 1,024 | In the Bermart chain of stores, a variety of ice cream is sold. Each type of ice cream has two parameters: price and tastiness.
Initially, there is one store numbered $1$, which sells nothing. You have to process $q$ queries of the following types:
* $1~x$ β a new store opens, that sells the same types of ice cream... | The first line contains a single integer $q$ ($1 \le q \le 3 \cdot 10^4$) β the number of queries.
Each of the following $q$ lines contains a query in the format described in the statement:
* $1~x$; * $2~x~p~t$ ($1 \le p, t \le 2000$); * $3~x$; * $4~x~p$ ($1 \le p \le 2000$).
Additional constraints on th... | For each query of type $4$, output a single integer β for store $x$, find the maximum total tastiness of a subset of types of ice cream that are sold there, such that the total price does not exceed $p$ (each type can be used in the subset no more than once). | [
[
"12\n2 1 5 7\n2 1 3 4\n4 1 4\n4 1 8\n4 1 2\n1 1\n2 2 4 10\n4 1 9\n4 2 9\n3 1\n4 1 9\n4 2 9",
"4\n11\n0\n11\n17\n4\n17"
]
] | Title: Bermart Ice Cream
time_limit_ms: 2000
memory_limit_mb: 1024
Description: In the Bermart chain of stores, a variety of ice cream is sold. Each type of ice cream has two parameters: price and tastiness.
Initially, there is one store numbered $1$, which sells nothing. You have to process $q$ queries of the followi... | |
2008E | https://codeforces.com/problemset/problem/2008/E | Alternating String | 1,500 | [
"brute force",
"data structures",
"dp",
"greedy",
"implementation",
"strings"
] | Div. 3 | 2,000 | 256 | Sakurako really loves alternating strings. She calls a string $s$ of lowercase Latin letters an alternating string if characters in the even positions are the same, if characters in the odd positions are the same, and the length of the string is even.
For example, the strings 'abab' and 'gg' are alternating, while the... | 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 number $n$ ($1 \le n\le 2\cdot 10^5$) β the length of the string.
The second line of each test case contains a string $s$, consisting of lowercase Latin letters.
It is gu... | For each test case, output a single integer β the minimum number of operations required to turn the string $s$ into an alternating one. | [
[
"10\n1\na\n2\nca\n3\naab\n5\nababa\n6\nacdada\n9\nejibmyyju\n6\nbbccbc\n6\nabacba\n5\nbcbca\n5\ndcbdb",
"1\n0\n1\n1\n2\n6\n2\n3\n1\n1"
]
] | For the string ababa, you can delete the first character to get baba, which is an alternating string.
For the string acdada, you can change the first two characters to get dadada, which is an alternating string. | Title: Alternating String
time_limit_ms: 2000
memory_limit_mb: 256
Description: Sakurako really loves alternating strings. She calls a string $s$ of lowercase Latin letters an alternating string if characters in the even positions are the same, if characters in the odd positions are the same, and the length of the stri... |
1971C | https://codeforces.com/problemset/problem/1971/C | Clock and Strings | 900 | [
"implementation"
] | Div. 4 | 1,000 | 256 | There is a clock labeled with the numbers $1$ through $12$ in clockwise order, as shown below.

In this example, $(a,b,c,d)=(2,9,10,6)$, and the strings intersect.
Alice and Bob have four distinct integers $a$, $b$, $c$, $d$ not more than $12$. Alice ties a red strin... | The first line contains a single integer $t$ ($1 \leq t \leq 5940$) β the number of test cases.
The only line of each test case contains four distinct integers $a$, $b$, $c$, $d$ ($1 \leq a, b, c, d \leq 12$). | For each test case, output "YES" (without quotes) if the strings intersect, and "NO" (without quotes) otherwise.
You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", and "Yes" will be recognized as a positive response). | [
[
"15\n2 9 10 6\n3 8 9 1\n1 2 3 4\n5 3 4 12\n1 8 2 10\n3 12 11 8\n9 10 12 1\n12 1 10 2\n3 12 6 9\n1 9 8 4\n6 7 9 12\n7 12 9 6\n10 12 11 1\n3 9 6 12\n1 4 3 5",
"YES\nNO\nNO\nYES\nYES\nNO\nNO\nNO\nNO\nNO\nNO\nYES\nYES\nYES\nYES"
]
] | The first test case is pictured in the statement.
In the second test case, the strings do not intersect, as shown below.
 | Title: Clock and Strings
time_limit_ms: 1000
memory_limit_mb: 256
Description: There is a clock labeled with the numbers $1$ through $12$ in clockwise order, as shown below.

In this example, $(a,b,c,d)=(2,9,10,6)$, and the strings intersect.
Alice and Bob have four ... |
1985E | https://codeforces.com/problemset/problem/1985/E | Secret Box | 1,200 | [
"brute force",
"combinatorics",
"math"
] | Div. 4 | 1,000 | 256 | Ntarsis has a box $B$ with side lengths $x$, $y$, and $z$. It lies in the 3D coordinate plane, extending from $(0,0,0)$ to $(x,y,z)$.
Ntarsis has a secret box $S$. He wants to choose its dimensions such that all side lengths are positive integers, and the volume of $S$ is $k$. He can place $S$ somewhere within $B$ suc... | The first line consists of an integer $t$, the number of test cases ($1 \leq t \leq 2000$). The description of the test cases follows.
The first and only line of each test case contains four integers $x, y, z$ and $k$ ($1 \leq x, y, z \leq 2000$, $1 \leq k \leq x \cdot y \cdot z$).
It is guaranteed the sum of all $x$... | For each test case, output the answer as an integer on a new line. If there is no way to select the dimensions of $S$ so it fits in $B$, output $0$. | [
[
"7\n3 3 3 8\n3 3 3 18\n5 1 1 1\n2 2 2 7\n3 4 2 12\n4 3 1 6\n1800 1800 1800 4913000000",
"8\n2\n5\n0\n4\n4\n1030301"
]
] | For the first test case, it is optimal to choose $S$ with side lengths $2$, $2$, and $2$, which has a volume of $2 \cdot 2 \cdot 2 = 8$. It can be shown there are $8$ ways to put $S$ inside $B$.
The coordinate with the least $x$, $y$, and $z$ values for each possible arrangement of $S$ are:
1. $(0, 0, 0)$ 2. $(1... | Title: Secret Box
time_limit_ms: 1000
memory_limit_mb: 256
Description: Ntarsis has a box $B$ with side lengths $x$, $y$, and $z$. It lies in the 3D coordinate plane, extending from $(0,0,0)$ to $(x,y,z)$.
Ntarsis has a secret box $S$. He wants to choose its dimensions such that all side lengths are positive integers,... |
1978A | https://codeforces.com/problemset/problem/1978/A | Alice and Books | 800 | [
"constructive algorithms",
"greedy",
"sortings"
] | Div. 2 | 1,000 | 256 | Alice has $n$ books. The $1$-st book contains $a_1$ pages, the $2$-nd book contains $a_2$ pages, $\ldots$, the $n$-th book contains $a_n$ pages. Alice does the following:
* She divides all the books into two non-empty piles. Thus, each book ends up in exactly one of the two piles. * Alice reads one book with the h... | Each test consists of multiple test cases. The first line 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$ ($2 \le n \le 100$) β the number of books Alice has.
The second line of each... | For each test case, output a single integer β the maximum number of pages Alice can read. | [
[
"5\n2\n1 1\n4\n2 3 3 1\n5\n2 2 3 2 2\n2\n10 3\n3\n1 2 3",
"2\n4\n5\n13\n5"
]
] | In the first test case, Alice can put book number $1$ in the first pile, and book number $2$ in the second pile. Then she will read $a_1 + a_2 = 1 + 1 = 2$ pages.
In the second test case, Alice can put books with numbers $2$ and $3$ in the first pile, and books with numbers $1$ and $4$ in the second pile. Then she wil... | Title: Alice and Books
time_limit_ms: 1000
memory_limit_mb: 256
Description: Alice has $n$ books. The $1$-st book contains $a_1$ pages, the $2$-nd book contains $a_2$ pages, $\ldots$, the $n$-th book contains $a_n$ pages. Alice does the following:
* She divides all the books into two non-empty piles. Thus, each book... |
2000F | https://codeforces.com/problemset/problem/2000/F | Color Rows and Columns | 1,900 | [
"dp",
"greedy",
"implementation",
"math"
] | Div. 3 | 3,000 | 256 | You have $n$ rectangles, the $i$-th of which has a width of $a_i$ and a height of $b_i$.
You can perform the following operation an unlimited number of times: choose a rectangle and a cell in it, and then color it.
Each time you completely color any row or column, you earn $1$ point. Your task is to score at least $k... | The first line contains an integer $t$ ($1 \le t \le 100$) β the number of test cases. The following are the descriptions of the test cases.
The first line of each test case description contains two integers $n$ and $k$ ($1 \le n \le 1000, 1 \le k \le 100$) β the number of rectangles in the case and the required numbe... | For each test case, output a single integer β the minimum number of operations required to score at least $k$ points. If it is impossible to score at least $k$ points, output -1. | [
[
"7\n1 4\n6 3\n1 5\n4 4\n5 10\n1 1\n1 1\n1 1\n1 1\n1 1\n2 100\n1 2\n5 6\n3 11\n2 2\n3 3\n4 4\n3 25\n9 2\n4 3\n8 10\n4 18\n5 4\n8 5\n8 3\n6 2",
"12\n14\n5\n-1\n17\n80\n35"
]
] | Title: Color Rows and Columns
time_limit_ms: 3000
memory_limit_mb: 256
Description: You have $n$ rectangles, the $i$-th of which has a width of $a_i$ and a height of $b_i$.
You can perform the following operation an unlimited number of times: choose a rectangle and a cell in it, and then color it.
Each time you compl... | |
2014E | https://codeforces.com/problemset/problem/2014/E | Rendez-vous de Marian et Robin | 1,800 | [
"dfs and similar",
"graphs",
"shortest paths"
] | Div. 3 | 5,000 | 256 | In the humble act of meeting, joy doth unfold like a flower in bloom.
Absence makes the heart grow fonder. Marian sold her last ware at the Market at the same time Robin finished training at the Major Oak. They couldn't wait to meet, so they both start without delay.
The travel network is represented as $n$ vertices ... | 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$, $h$ ($2 \le n \le 2 \cdot 10^5, \;1 \le m \le 2 \cdot 10^5, \; 1 \le h \le n$) β the number of vertices, the number of edges and the number o... | For each test case, output a single integer, the earliest time Robin and Marian can meet. If it is impossible for them to meet, output $-1$. | [
[
"6\n2 1 1\n1\n1 2 10\n3 1 2\n2 3\n1 2 10\n3 3 1\n2\n1 2 4\n1 3 10\n2 3 6\n4 3 2\n2 3\n1 2 10\n2 3 18\n3 4 16\n3 2 1\n2\n1 2 4\n1 3 16\n7 7 1\n3\n1 5 2\n2 6 12\n1 2 12\n6 4 8\n7 3 4\n6 3 4\n7 6 4",
"5\n-1\n6\n19\n14\n12"
]
] | In the first test case, Marian rides from vertex $1$ to vertex $2$, Robin waits.
In the second test case, vertices $1$ and $3$ are not connected.
In the third test case, both Marian and Robin travel to vertex $2$ to meet.
In the fourth test case, Marian travels to vertex $2$ without a horse, mounts the horse at vert... | Title: Rendez-vous de Marian et Robin
time_limit_ms: 5000
memory_limit_mb: 256
Description: In the humble act of meeting, joy doth unfold like a flower in bloom.
Absence makes the heart grow fonder. Marian sold her last ware at the Market at the same time Robin finished training at the Major Oak. They couldn't wait to... |
2030D | https://codeforces.com/problemset/problem/2030/D | QED's Favorite Permutation | 1,700 | [
"data structures",
"implementation",
"sortings"
] | Div. 2 | 2,000 | 256 | QED is given a permutation$^{\text{β}}$ $p$ of length $n$. He also has a string $s$ of length $n$ containing only characters $\texttt{L}$ and $\texttt{R}$. QED only likes permutations that are sorted in non-decreasing order. To sort $p$, he can select any of the following operations and perform them any number of times... | 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 $q$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq q \leq 2 \cdot 10^5$) β the length of the permutation and the number of queries.
The following line contains $n$ integers $p_1, p_2, ... | For each query, output "YES" (without quotes) if it is possible, and "NO" (without quotes) otherwise.
You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response). | [
[
"3\n5 3\n1 4 2 5 3\nRLRLL\n2\n4\n3\n8 5\n1 5 2 4 8 3 6 7\nRRLLRRRL\n4\n3\n5\n3\n4\n6 2\n1 2 3 4 5 6\nRLRLRL\n4\n5",
"YES\nYES\nNO\nNO\nYES\nNO\nNO\nNO\nYES\nYES"
]
] | In the first testcase, $s = \texttt{RRRLL}$ after the first query. QED may sort $p$ using the following operations:
* Initially, $p = [1,4,2,5,3]$. * Select $i = 2$ and swap $p_2$ with $p_{3}$. Now, $p = [1,2,4,5,3]$. * Select $i = 5$ and swap $p_5$ with $p_{4}$. Now, $p = [1,2,4,3,5]$. * Select $i = 4$ and... | Title: QED's Favorite Permutation
time_limit_ms: 2000
memory_limit_mb: 256
Description: QED is given a permutation$^{\text{β}}$ $p$ of length $n$. He also has a string $s$ of length $n$ containing only characters $\texttt{L}$ and $\texttt{R}$. QED only likes permutations that are sorted in non-decreasing order. To sort... |
1987C | https://codeforces.com/problemset/problem/1987/C | Basil's Garden | 1,200 | [
"dp",
"greedy"
] | Div. 1 + 2 | 2,000 | 256 | There are $n$ flowers in a row, the $i$-th of them initially has a positive height of $h_i$ meters.
Every second, the wind will blow from the left, causing the height of some flowers to decrease.
Specifically, every second, for each $i$ from $1$ to $n$, in this order, the following happens:
* If $i = n$ or $h_i > ... | 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 a single integer $n$ ($1 \le n \le 10^5$) β the number of flowers.
The second line of each... | For each test case, output a single integer β the number of seconds that will pass before $h_i=0$ for all $1 \le i \le n$. | [
[
"4\n3\n1 1 2\n2\n3 1\n1\n9\n5\n7 4 4 3 2",
"4\n3\n9\n7"
]
] | In the first test case, the flower heights change as follows: $[1, 1, 2] \rightarrow [1, 1, 1] \rightarrow [1, 1, 0] \rightarrow [1, 0, 0] \rightarrow [0, 0, 0]$.
In the second test case, the flower heights change as follows: $[3, 1] \rightarrow [2, 0] \rightarrow [1, 0] \rightarrow [0, 0]$. | Title: Basil's Garden
time_limit_ms: 2000
memory_limit_mb: 256
Description: There are $n$ flowers in a row, the $i$-th of them initially has a positive height of $h_i$ meters.
Every second, the wind will blow from the left, causing the height of some flowers to decrease.
Specifically, every second, for each $i$ from ... |
2000H | https://codeforces.com/problemset/problem/2000/H | Ksyusha and the Loaded Set | 2,200 | [
"binary search",
"brute force",
"data structures",
"implementation"
] | Div. 3 | 3,000 | 512 | Ksyusha decided to start a game development company. To stand out among competitors and achieve success, she decided to write her own game engine. The engine must support a set initially consisting of $n$ distinct integers $a_1, a_2, \ldots, a_n$.
The set will undergo $m$ operations sequentially. The operations can be... | The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The following lines describe the test cases.
The first line contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the initial size of the set.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_1 < a_2 < \ld... | For each test case, output the answers to the operations of type "?". | [
[
"3\n5\n1 2 5 905 2000000\n15\n- 2\n? 2\n? 1\n- 1\n? 1\n+ 4\n+ 2\n? 2\n+ 6\n- 4\n+ 7\n? 2\n? 3\n? 4\n? 2000000\n5\n3 4 5 6 8\n9\n? 5\n- 5\n? 5\n+ 1\n? 2\n- 6\n- 8\n+ 6\n? 5\n5\n6 7 8 9 10\n10\n? 5\n- 6\n? 4\n- 10\n+ 5\n- 8\n+ 3\n+ 2\n- 3\n+ 10",
"2 2 1 6 3 8 8 2000001 \n9 9 9 7 \n1 1"
]
] | Title: Ksyusha and the Loaded Set
time_limit_ms: 3000
memory_limit_mb: 512
Description: Ksyusha decided to start a game development company. To stand out among competitors and achieve success, she decided to write her own game engine. The engine must support a set initially consisting of $n$ distinct integers $a_1, a_2... | |
2003E1 | https://codeforces.com/problemset/problem/2003/E1 | Turtle and Inversions (Easy Version) | 2,600 | [
"brute force",
"divide and conquer",
"dp",
"greedy",
"math"
] | Div. 2 | 2,000 | 512 | This is an easy version of this problem. The differences between the versions are the constraint on $m$ and $r_i < l_{i + 1}$ holds for each $i$ from $1$ to $m - 1$ in the easy version. You can make hacks only if both versions of the problem are solved.
Turtle gives you $m$ intervals $[l_1, r_1], [l_2, r_2], \ldots, [... | Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). The description of the test cases follows.
The first line of each test case contains two integers $n, m$ ($2 \le n \le 5 \cdot 10^3, 0 \le m \le \frac{n}{2}$) β the length of the permutation and the numbe... | For each test case, if there is no interesting permutation, output a single integer $-1$.
Otherwise, output a single integer β the maximum number of inversions. | [
[
"6\n2 0\n2 1\n1 2\n5 1\n2 4\n8 2\n1 4\n6 8\n7 2\n1 3\n4 7\n7 3\n1 2\n3 4\n5 6",
"1\n0\n8\n21\n15\n15"
]
] | In the third test case, the interesting permutation with the maximum number of inversions is $[5, 2, 4, 3, 1]$.
In the fourth test case, the interesting permutation with the maximum number of inversions is $[4, 8, 7, 6, 3, 2, 1, 5]$. In this case, we can let $[k_1, k_2] = [1, 7]$.
In the fifth test case, the interest... | Title: Turtle and Inversions (Easy Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: This is an easy version of this problem. The differences between the versions are the constraint on $m$ and $r_i < l_{i + 1}$ holds for each $i$ from $1$ to $m - 1$ in the easy version. You can make hacks only if both vers... |
1980C | https://codeforces.com/problemset/problem/1980/C | Sofia and the Lost Operations | 1,300 | [
"constructive algorithms",
"greedy"
] | Div. 3 | 2,000 | 256 | Sofia had an array of $n$ integers $a_1, a_2, \ldots, a_n$. One day she got bored with it, so she decided to sequentially apply $m$ modification operations to it.
Each modification operation is described by a pair of numbers $\langle c_j, d_j \rangle$ and means that the element of the array with index $c_j$ should be ... | The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
Then follow the descriptions of the test cases.
The first line of each test case contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the size of the array.
The second line of each test case contains $n$ integers $a_1, a_2, \ld... | Output $t$ lines, each of which is the answer to the corresponding test case. As an answer, output "YES" if there exists a suitable sequence $c_1, c_2, \ldots, c_m$, and "NO" otherwise.
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer... | [
[
"7\n3\n1 2 1\n1 3 2\n4\n1 3 1 2\n4\n1 2 3 5\n2 1 3 5\n2\n2 3\n5\n7 6 1 10 10\n3 6 1 11 11\n3\n4 3 11\n4\n3 1 7 8\n2 2 7 10\n5\n10 3 2 2 1\n5\n5 7 1 7 9\n4 10 1 2 9\n8\n1 1 9 8 7 2 10 4\n4\n1000000000 203 203 203\n203 1000000000 203 1000000000\n2\n203 1000000000\n1\n1\n1\n5\n1 3 4 5 1",
"YES\nNO\nNO\nNO\nY... | Title: Sofia and the Lost Operations
time_limit_ms: 2000
memory_limit_mb: 256
Description: Sofia had an array of $n$ integers $a_1, a_2, \ldots, a_n$. One day she got bored with it, so she decided to sequentially apply $m$ modification operations to it.
Each modification operation is described by a pair of numbers $\l... | |
1975H | https://codeforces.com/problemset/problem/1975/H | 378QAQ and Core | 3,500 | [
"greedy",
"strings"
] | Div. 1 + 2 | 2,000 | 256 | 378QAQ has a string $s$ of length $n$. Define the core of a string as the substring$^\dagger$ with maximum lexicographic$^\ddagger$ order.
For example, the core of "$\mathtt{bazoka}$" is "$\mathtt{zoka}$", and the core of "$\mathtt{aaa}$" is "$\mathtt{aaa}$".
378QAQ wants to rearrange the string $s$ so that the core ... | Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\leq t\leq 10^5$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1\leq n\leq 10^6$) β the length of string $s$.
The next line of each test case contains the str... | For each test case, output the lexicographically minimum possible core over all rearrangements of $s$. | [
[
"6\n3\nqaq\n4\ncccc\n6\nbazoka\n6\nzazzzz\n7\nababbbb\n7\nccbabcc",
"qaq\ncccc\nz\nzzz\nbbababb\ncbcacbc"
]
] | In the first test case, all possible rearrangements and their corresponding cores are as follows:
* "$\mathtt{qaq}$", its core is "$\mathtt{qaq}$". * "$\mathtt{aqq}$", its core is "$\mathtt{qq}$". * "$\mathtt{qqa}$", its core is "$\mathtt{qqa}$".
So the core with the minimum lexicographic order in all rearra... | Title: 378QAQ and Core
time_limit_ms: 2000
memory_limit_mb: 256
Description: 378QAQ has a string $s$ of length $n$. Define the core of a string as the substring$^\dagger$ with maximum lexicographic$^\ddagger$ order.
For example, the core of "$\mathtt{bazoka}$" is "$\mathtt{zoka}$", and the core of "$\mathtt{aaa}$" is ... |
1980B | https://codeforces.com/problemset/problem/1980/B | Choosing Cubes | 800 | [
"sortings"
] | Div. 3 | 1,000 | 256 | Dmitry has $$$n$$$ cubes, numbered from left to right from $$$1$$$ to $$$n$$$. The cube with index $$$f$$$ is his favorite.
Dmitry threw all the cubes on the table, and the $$$i$$$-th cube showed the value $$$a_i$$$ ($$$1 \le a_i \le 100$$$). After that, he arranged the cubes in non-increasing order of their values, f... | The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) β the number of test cases. Then follow the descriptions of the test cases.
The first line of each test case description contains three integers $$$n$$$, $$$f$$$, and $$$k$$$ ($$$1 \le f, k \le n \le 100$$$) β the number of cubes, the index of Dmitry'... | For each test case, output one line β "YES" if the cube will be removed in all cases, "NO" if it will not be removed in any case, "MAYBE" if it may be either removed or left.
You can output the answer in any case. For example, the strings "YES", "nO", "mAyBe" will be accepted as answers. | [
[
"12\n5 2 2\n4 3 3 2 3\n5 5 3\n4 2 1 3 5\n5 5 2\n5 2 4 1 3\n5 5 5\n1 2 5 4 3\n5 5 4\n3 1 2 4 5\n5 5 5\n4 3 2 1 5\n6 5 3\n1 2 3 1 2 3\n10 1 1\n1 1 1 1 1 1 1 1 1 1\n1 1 1\n42\n5 2 3\n2 2 1 1 2\n2 1 1\n2 1\n5 3 1\n3 3 2 3 2",
"MAYBE\nYES\nNO\nYES\nYES\nYES\nMAYBE\nMAYBE\nYES\nYES\nYES\nNO"
]
] | Title: Choosing Cubes
time_limit_ms: 1000
memory_limit_mb: 256
Description: Dmitry has $$$n$$$ cubes, numbered from left to right from $$$1$$$ to $$$n$$$. The cube with index $$$f$$$ is his favorite.
Dmitry threw all the cubes on the table, and the $$$i$$$-th cube showed the value $$$a_i$$$ ($$$1 \le a_i \le 100$$$). ... | |
1969A | https://codeforces.com/problemset/problem/1969/A | Two Friends | 800 | [
"constructive algorithms",
"implementation",
"math"
] | Div. 2 | 2,000 | 256 | Monocarp wants to throw a party. He has $$$n$$$ friends, and he wants to have at least $$$2$$$ of them at his party.
The $$$i$$$-th friend's best friend is $$$p_i$$$. All $$$p_i$$$ are distinct, and for every $$$i \in [1, n]$$$, $$$p_i \ne i$$$.
Monocarp can send invitations to friends. The $$$i$$$-th friend comes to... | The first line contains one integer $$$t$$$ ($$$1 \le t \le 5000$$$)Β β the number of test cases.
Each test case consists of two lines:
- the first line contains one integer $$$n$$$ ($$$2 \le n \le 50$$$)Β β the number of friends;
- the second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le ... | Print one integerΒ β the minimum number of invitations Monocarp has to send. | [
[
"3\n5\n3 1 2 5 4\n4\n2 3 4 1\n2\n2 1",
"2\n3\n2"
]
] | In the first testcase, Monocarp can send invitations to friends $$$4$$$ and $$$5$$$. Both of them will come to the party since they are each other's best friends, and both of them have invitations.
In the second testcase, Monocarp can send invitations to friends $$$1, 2$$$ and $$$3$$$, for example. Then friends $$$1$$... | Title: Two Friends
time_limit_ms: 2000
memory_limit_mb: 256
Description: Monocarp wants to throw a party. He has $$$n$$$ friends, and he wants to have at least $$$2$$$ of them at his party.
The $$$i$$$-th friend's best friend is $$$p_i$$$. All $$$p_i$$$ are distinct, and for every $$$i \in [1, n]$$$, $$$p_i \ne i$$$.
... |
1969B | https://codeforces.com/problemset/problem/1969/B | Shifts and Sorting | 1,000 | [
"constructive algorithms",
"greedy"
] | Div. 2 | 2,000 | 256 | Let's define a cyclic shift of some string $$$s$$$ as a transformation from $$$s_1 s_2 \dots s_{n-1} s_{n}$$$ into $$$s_{n} s_1 s_2 \dots s_{n-1}$$$. In other words, you take one last character $$$s_n$$$ and place it before the first character while moving all other characters to the right.
You are given a binary stri... | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$)Β β the number of test cases.
The first and only line of each test case contains a binary string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$; $$$s_i \in$$$ {0, 1})Β β the string you need to sort.
Additional constraint on the input: the sum of length... | For each test case, print the single integerΒ β the minimum total cost to make string sorted using operation above any number of times. | [
[
"5\n10\n0000\n11000\n101011\n01101001",
"2\n0\n9\n5\n11"
]
] | In the first test case, you can choose the whole string and perform a cyclic shift: 10 $$$\rightarrow$$$ 01. The length of the substring is $$$2$$$, so the cost is $$$2$$$.
In the second test case, the string is already sorted, so you don't need to perform any operations.
In the third test case, one of the optimal st... | Title: Shifts and Sorting
time_limit_ms: 2000
memory_limit_mb: 256
Description: Let's define a cyclic shift of some string $$$s$$$ as a transformation from $$$s_1 s_2 \dots s_{n-1} s_{n}$$$ into $$$s_{n} s_1 s_2 \dots s_{n-1}$$$. In other words, you take one last character $$$s_n$$$ and place it before the first charac... |
1969C | https://codeforces.com/problemset/problem/1969/C | Minimizing the Sum | 1,700 | [
"dp",
"implementation"
] | Div. 2 | 2,000 | 256 | You are given an integer array $$$a$$$ of length $$$n$$$.
You can perform the following operation: choose an element of the array and replace it with any of its neighbor's value.
For example, if $$$a=[3, 1, 2]$$$, you can get one of the arrays $$$[3, 3, 2]$$$, $$$[3, 2, 2]$$$ and $$$[1, 1, 2]$$$ using one operation, ... | 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 \le 3 \cdot 10^5$$$; $$$0 \le k \le 10$$$).
The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \l... | For each test case, print a single integerΒ β the minimum possible total sum of the array if you can perform the aforementioned operation at most $$$k$$$ times. | [
[
"4\n3 1\n3 1 2\n1 3\n5\n4 2\n2 2 1 3\n6 3\n4 1 2 2 4 3",
"4\n5\n5\n10"
]
] | In the first example, one of the possible sequences of operations is the following: $$$[3, 1, 2] \rightarrow [1, 1, 2$$$].
In the second example, you do not need to apply the operation.
In the third example, one of the possible sequences of operations is the following: $$$[2, 2, 1, 3] \rightarrow [2, 1, 1, 3] \righta... | Title: Minimizing the Sum
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given an integer array $$$a$$$ of length $$$n$$$.
You can perform the following operation: choose an element of the array and replace it with any of its neighbor's value.
For example, if $$$a=[3, 1, 2]$$$, you can get one of the a... |
1969D | https://codeforces.com/problemset/problem/1969/D | Shop Game | 1,900 | [
"data structures",
"greedy",
"sortings",
"math"
] | Div. 2 | 2,000 | 256 | Alice and Bob are playing a game in the shop. There are $$$n$$$ items in the shop; each item has two parameters: $$$a_i$$$ (item price for Alice) and $$$b_i$$$ (item price for Bob).
Alice wants to choose a subset (possibly empty) of items and buy them. After that, Bob does the following:
- if Alice bought less than $... | 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 \le 2 \cdot 10^5$$$; $$$0 \le k \le n$$$).
The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le... | For each test case, print a single integerΒ β Alice's profit if both Alice and Bob act optimally. | [
[
"4\n2 0\n2 1\n1 2\n4 1\n1 2 1 4\n3 3 2 3\n4 2\n2 1 1 1\n4 2 3 2\n6 2\n1 3 4 9 1 3\n7 6 8 10 6 8",
"1\n1\n0\n7"
]
] | In the first test case, Alice should buy the $$$2$$$-nd item and sell it to Bob, so her profit is $$$2 - 1 = 1$$$.
In the second test case, Alice should buy the $$$1$$$-st, the $$$2$$$-nd and the $$$3$$$-rd item; then Bob takes the $$$1$$$-st item for free and pays for the $$$2$$$-nd and the $$$3$$$-rd item. Alice's p... | Title: Shop Game
time_limit_ms: 2000
memory_limit_mb: 256
Description: Alice and Bob are playing a game in the shop. There are $$$n$$$ items in the shop; each item has two parameters: $$$a_i$$$ (item price for Alice) and $$$b_i$$$ (item price for Bob).
Alice wants to choose a subset (possibly empty) of items and buy t... |
1969E | https://codeforces.com/problemset/problem/1969/E | Unique Array | 2,400 | [
"dp",
"binary search",
"data structures",
"divide and conquer",
"greedy"
] | Div. 2 | 2,000 | 256 | You are given an integer array $$$a$$$ of length $$$n$$$. A subarray of $$$a$$$ is one of its contiguous subsequences (i. e. an array $$$[a_l, a_{l+1}, \dots, a_r]$$$ for some integers $$$l$$$ and $$$r$$$ such that $$$1 \le l < r \le n$$$). Let's call a subarray unique if there is an integer that occurs exactly once in... | 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 3 \cdot 10^5$$$).
The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$).
Additional constrain... | For each test case, print a single integerΒ β the minimum number of aforementioned operation in order for all the subarrays of the array $$$a$$$ to be unique. | [
[
"4\n3\n2 1 2\n4\n4 4 4 4\n5\n3 1 2 1 2\n5\n1 3 2 1 2",
"0\n2\n1\n0"
]
] | In the second test case, you can replace the $$$1$$$-st and the $$$3$$$-rd element, for example, like this: $$$[3, 4, 1, 4]$$$.
In the third test case, you can replace the $$$4$$$-th element, for example, like this: $$$[3, 1, 2, 3, 2]$$$. | Title: Unique Array
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given an integer array $$$a$$$ of length $$$n$$$. A subarray of $$$a$$$ is one of its contiguous subsequences (i. e. an array $$$[a_l, a_{l+1}, \dots, a_r]$$$ for some integers $$$l$$$ and $$$r$$$ such that $$$1 \le l < r \le n$$$). Let's... |
1948B | https://codeforces.com/problemset/problem/1948/B | Array Fix | 1,100 | [
"greedy",
"brute force",
"dp",
"implementation"
] | Div. 2 | 2,000 | 256 | You are given an integer array $$$a$$$ of length $$$n$$$.
You can perform the following operation any number of times (possibly zero): take any element of the array $$$a$$$, which is at least $$$10$$$, delete it, and instead insert the digits that element consisted of in the same position, in order they appear in that... | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$)Β β the number of test cases.
Each test case consists of two lines:
- the first line contains a single integer $$$n$$$ ($$$2 \le n \le 50$$$).
- the second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 99$$$). | For each test case, print YES if it is possible to make $$$a$$$ sorted in non-decreasing order using the aforementioned operation; otherwise, print NO.
You can print each letter in any case. For example, yes, Yes, YeS will all be recognized as a positive answer. | [
[
"3\n4\n12 3 45 67\n3\n12 28 5\n2\n0 0",
"YES\nNO\nYES"
]
] | In the first example, you can split the first element, then the array becomes $$$[1, 2, 3, 45, 67]$$$.
In the second example, there is no way to get a sorted array.
In the third example, the array is already sorted. | Title: Array Fix
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given an integer array $$$a$$$ of length $$$n$$$.
You can perform the following operation any number of times (possibly zero): take any element of the array $$$a$$$, which is at least $$$10$$$, delete it, and instead insert the digits that ... |
1950A | https://codeforces.com/problemset/problem/1950/A | Stair, Peak, or Neither? | 800 | [
"implementation"
] | Div. 4 | 1,000 | 256 | You are given three digits $$$a$$$, $$$b$$$, and $$$c$$$. Determine whether they form a stair, a peak, or neither.
- A stair satisfies the condition $$$a<b<c$$$.
- A peak satisfies the condition $$$a<b>c$$$. | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)Β β the number of test cases.
The only line of each test case contains three digits $$$a$$$, $$$b$$$, $$$c$$$ ($$$0 \leq a$$$, $$$b$$$, $$$c \leq 9$$$). | For each test case, output "STAIR" if the digits form a stair, "PEAK" if the digits form a peak, and "NONE" otherwise (output the strings without quotes). | [
[
"7\n1 2 3\n3 2 1\n1 5 3\n3 4 1\n0 0 0\n4 1 7\n4 5 7",
"STAIR\nNONE\nPEAK\nPEAK\nNONE\nNONE\nSTAIR"
]
] | Title: Stair, Peak, or Neither?
time_limit_ms: 1000
memory_limit_mb: 256
Description: You are given three digits $$$a$$$, $$$b$$$, and $$$c$$$. Determine whether they form a stair, a peak, or neither.
- A stair satisfies the condition $$$a<b<c$$$.
- A peak satisfies the condition $$$a<b>c$$$.
Input: The first line co... | |
1950B | https://codeforces.com/problemset/problem/1950/B | Upscaling | 800 | [
"implementation"
] | Div. 4 | 1,000 | 256 | You are given an integer $$$n$$$. Output a $$$2n \times 2n$$$ checkerboard made of $$$2 \times 2$$$ squares alternating '$$$\texttt{#}$$$' and '$$$\texttt{.}$$$', with the top-left cell being '$$$\texttt{#}$$$'.
The picture above shows the answers for $$$n=1,2,3,4$$$. | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 20$$$)Β β the number of test cases.
The only line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 20$$$)Β β it means you need to output a checkerboard of side length $$$2n$$$. | For each test case, output $$$2n$$$ lines, each containing $$$2n$$$ characters without spacesΒ β the checkerboard, as described in the statement. Do not output empty lines between test cases. | [
[
"4\n1\n2\n3\n4",
"##\n##\n##..\n##..\n..##\n..##\n##..##\n##..##\n..##..\n..##..\n##..##\n##..##\n##..##..\n##..##..\n..##..##\n..##..##\n##..##..\n##..##..\n..##..##\n..##..##"
]
] | Title: Upscaling
time_limit_ms: 1000
memory_limit_mb: 256
Description: You are given an integer $$$n$$$. Output a $$$2n \times 2n$$$ checkerboard made of $$$2 \times 2$$$ squares alternating '$$$\texttt{#}$$$' and '$$$\texttt{.}$$$', with the top-left cell being '$$$\texttt{#}$$$'.
The picture above shows the answers ... | |
1950C | https://codeforces.com/problemset/problem/1950/C | Clock Conversion | 800 | [
"implementation",
"math"
] | Div. 4 | 1,000 | 256 | Given the time in 24-hour format, output the equivalent time in 12-hour format.
- 24-hour format divides the day into 24 hours from $$$00$$$ to $$$23$$$, each of which has 60 minutes from $$$00$$$ to $$$59$$$.
- 12-hour format divides the day into two halves: the first half is $$$\mathrm{AM}$$$, and the second half is... | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1440$$$)Β β the number of test cases.
The only line of each test case contains a string $$$s$$$ of length $$$5$$$ with format hh:mm representing a valid time in the 24-hour format. hh represents the hour from $$$00$$$ to $$$23$$$, and mm represents the ... | For each test case, output two strings separated by a space ("hh:mm AM" or "hh:mm PM"), which are the 12-hour equivalent to the time provided in the test case (without quotes).
You should output the time exactly as indicated; in particular, you should not remove leading zeroes. | [
[
"11\n09:41\n18:06\n12:14\n00:59\n00:00\n14:34\n01:01\n19:07\n11:59\n12:00\n21:37",
"09:41 AM\n06:06 PM\n12:14 PM\n12:59 AM\n12:00 AM\n02:34 PM\n01:01 AM\n07:07 PM\n11:59 AM\n12:00 PM\n09:37 PM"
]
] | Title: Clock Conversion
time_limit_ms: 1000
memory_limit_mb: 256
Description: Given the time in 24-hour format, output the equivalent time in 12-hour format.
- 24-hour format divides the day into 24 hours from $$$00$$$ to $$$23$$$, each of which has 60 minutes from $$$00$$$ to $$$59$$$.
- 12-hour format divides the da... | |
1950D | https://codeforces.com/problemset/problem/1950/D | Product of Binary Decimals | 1,100 | [
"brute force",
"dp",
"implementation",
"number theory"
] | Div. 4 | 3,000 | 256 | Let's call a number a binary decimal if it is a positive integer and all digits in its decimal notation are either $$$0$$$ or $$$1$$$. For example, $$$1\,010\,111$$$ is a binary decimal, while $$$10\,201$$$ and $$$787\,788$$$ are not.
Given a number $$$n$$$, you are asked whether or not it is possible to represent $$$... | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$)Β β the number of test cases.
The only line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). | For each test case, output "YES" (without quotes) if $$$n$$$ can be represented as a product of binary decimals, and "NO" (without quotes) otherwise.
You can output "YES" and "NO" in any case (for example, strings "yES", "yes", and "Yes" will be recognized as a positive response). | [
[
"11\n121\n1\n14641\n12221\n10110\n100000\n99\n112\n2024\n12421\n1001",
"YES\nYES\nYES\nYES\nYES\nYES\nNO\nNO\nNO\nNO\nYES"
]
] | The first five test cases can be represented as a product of binary decimals as follows:
- $$$121 = 11 \times 11$$$.
- $$$1 = 1$$$ is already a binary decimal.
- $$$14\,641 = 11 \times 11 \times 11 \times 11$$$.
- $$$12\,221 = 11 \times 11 \times 101$$$.
- $$$10\,110 = 10\,110$$$ is already a binary decimal. | Title: Product of Binary Decimals
time_limit_ms: 3000
memory_limit_mb: 256
Description: Let's call a number a binary decimal if it is a positive integer and all digits in its decimal notation are either $$$0$$$ or $$$1$$$. For example, $$$1\,010\,111$$$ is a binary decimal, while $$$10\,201$$$ and $$$787\,788$$$ are no... |
1950E | https://codeforces.com/problemset/problem/1950/E | Nearly Shortest Repeating Substring | 1,500 | [
"strings",
"brute force",
"implementation",
"number theory"
] | Div. 4 | 2,000 | 256 | You are given a string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin characters. Find the length of the shortest string $$$k$$$ such that several (possibly one) copies of $$$k$$$ can be concatenated together to form a string with the same length as $$$s$$$ and, at most, one different character.
More formally... | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^3$$$)Β β the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 2\cdot10^5$$$)Β β the length of string $$$s$$$.
The second line of each test case contains the string $$$s$$$, consisting of lower... | For each test case, print the length of the shortest string $$$k$$$ satisfying the constraints in the statement. | [
[
"5\n4\nabaa\n4\nabba\n13\nslavicgslavic\n8\nhshahaha\n20\nstormflamestornflame",
"1\n4\n13\n2\n10"
]
] | In the first test case, you can select $$$k = \texttt{a}$$$ and $$$k+k+k+k = \texttt{aaaa}$$$, which only differs from $$$s$$$ in the second position.
In the second test case, you cannot select $$$k$$$ of length one or two. We can have $$$k = \texttt{abba}$$$, which is equal to $$$s$$$. | Title: Nearly Shortest Repeating Substring
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given a string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin characters. Find the length of the shortest string $$$k$$$ such that several (possibly one) copies of $$$k$$$ can be concatenated together to fo... |
1950F | https://codeforces.com/problemset/problem/1950/F | 0, 1, 2, Tree! | 1,700 | [
"brute force",
"greedy",
"implementation",
"bitmasks",
"trees"
] | Div. 4 | 2,000 | 256 | Find the minimum height of a rooted tree$$$^{\dagger}$$$ with $$$a+b+c$$$ vertices that satisfies the following conditions:
- $$$a$$$ vertices have exactly $$$2$$$ children,
- $$$b$$$ vertices have exactly $$$1$$$ child, and
- $$$c$$$ vertices have exactly $$$0$$$ children.
The tree above is rooted at the top vertex,... | The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$)Β β the number of test cases.
The only line of each test case contains three integers $$$a$$$, $$$b$$$, and $$$c$$$ ($$$0 \leq a, b, c \leq 10^5$$$; $$$1 \leq a + b + c$$$).
The sum of $$$a + b + c$$$ over all test cases does not exceed $$$3 \cdot 10... | For each test case, if no such tree exists, output $$$-1$$$. Otherwise, output one integerΒ β the minimum height of a tree satisfying the conditions in the statement. | [
[
"10\n2 1 3\n0 0 1\n0 1 1\n1 0 2\n1 1 3\n3 1 4\n8 17 9\n24 36 48\n1 0 0\n0 3 1",
"2\n0\n1\n1\n-1\n3\n6\n-1\n-1\n3"
]
] | The first test case is pictured in the statement. It can be proven that you can't get a height smaller than $$$2$$$.
In the second test case, you can form a tree with a single vertex and no edges. It has height $$$0$$$, which is clearly optimal.
In the third test case, you can form a tree with two vertices joined by ... | Title: 0, 1, 2, Tree!
time_limit_ms: 2000
memory_limit_mb: 256
Description: Find the minimum height of a rooted tree$$$^{\dagger}$$$ with $$$a+b+c$$$ vertices that satisfies the following conditions:
- $$$a$$$ vertices have exactly $$$2$$$ children,
- $$$b$$$ vertices have exactly $$$1$$$ child, and
- $$$c$$$ vertices... |
1950G | https://codeforces.com/problemset/problem/1950/G | Shuffling Songs | 1,900 | [
"dp",
"graphs",
"implementation",
"hashing",
"dfs and similar",
"bitmasks",
"strings"
] | Div. 4 | 3,000 | 256 | Vladislav has a playlist consisting of $$$n$$$ songs, numbered from $$$1$$$ to $$$n$$$. Song $$$i$$$ has genre $$$g_i$$$ and writer $$$w_i$$$. He wants to make a playlist in such a way that every pair of adjacent songs either have the same writer or are from the same genre (or both). He calls such a playlist exciting. ... | 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 a single integer $$$n$$$ ($$$1 \le n \le 16$$$)Β β the number of songs in the original playlist.
Then $$$n$$$ lines follow,... | For each test case, output a single integerΒ β the minimum number of removals necessary so that the resulting playlist can be made exciting. | [
[
"4\n1\npop taylorswift\n4\nelectronic themotans\nelectronic carlasdreams\npop themotans\npop irinarimes\n7\nrap eminem\nrap drdre\nrap kanyewest\npop taylorswift\nindierock arcticmonkeys\nindierock arcticmonkeys\npunkrock theoffspring\n4\na b\nc d\ne f\ng h",
"0\n0\n4\n3"
]
] | In the first test case, the playlist is already exciting.
In the second test case, if you have the songs in the order $$$4, 3, 1, 2$$$, it is exciting, so you don't need to remove any songs.
In the third test case, you can remove songs $$$4, 5, 6, 7$$$. Then the playlist with songs in the order $$$1, 2, 3$$$ is excit... | Title: Shuffling Songs
time_limit_ms: 3000
memory_limit_mb: 256
Description: Vladislav has a playlist consisting of $$$n$$$ songs, numbered from $$$1$$$ to $$$n$$$. Song $$$i$$$ has genre $$$g_i$$$ and writer $$$w_i$$$. He wants to make a playlist in such a way that every pair of adjacent songs either have the same wri... |
1994A | https://codeforces.com/problemset/problem/1994/A | Diverse Game | 800 | [
"constructive algorithms",
"greedy",
"implementation"
] | Div. 1 + Div. 2 | 1,000 | 256 | Petr, watching Sergey's stream, came up with a matrix $$$a$$$, consisting of $$$n$$$ rows and $$$m$$$ columns (the number in the $$$i$$$-th row and $$$j$$$-th column is denoted as $$$a_{i, j}$$$), which contains all integers from $$$1$$$ to $$$n \cdot m$$$. But he didn't like the arrangement of the numbers, and now he ... | Each test consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^3$$$)Β β the number of test cases. Then follows the description of the test cases.
The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 10$$$)Β β the number of rows and co... | For each test case, output $$$n \cdot m$$$ integersΒ β any suitable matrix $$$b$$$, or $$$-1$$$ if such a matrix does not exist. | [
[
"5\n1 1\n1\n2 1\n2\n1\n1 5\n2 4 5 3 1\n2 4\n1 2 3 4\n5 6 7 8\n3 3\n4 2 1\n9 8 3\n6 7 5",
"-1\n1 \n2 \n4 5 3 1 2 \n6 7 8 5 \n2 3 4 1 \n8 3 9 \n7 5 6 \n2 1 4"
]
] | In the first test case, there is only one element in the matrix, so matrix $$$b$$$ is the only matrix and it does not fit.
In the second test case $$$a_{1, 1} = 2 \neq 1 = b_{1, 1}$$$, $$$a_{2, 1} = 1 \neq 2 = b_{2, 1}$$$. | Title: Diverse Game
time_limit_ms: 1000
memory_limit_mb: 256
Description: Petr, watching Sergey's stream, came up with a matrix $$$a$$$, consisting of $$$n$$$ rows and $$$m$$$ columns (the number in the $$$i$$$-th row and $$$j$$$-th column is denoted as $$$a_{i, j}$$$), which contains all integers from $$$1$$$ to $$$n ... |
1994D | https://codeforces.com/problemset/problem/1994/D | Funny Game | 1,900 | [
"graphs",
"math",
"dsu",
"number theory",
"constructive algorithms",
"greedy",
"trees"
] | Div. 1 + Div. 2 | 2,000 | 256 | Vanya has a graph with $$$n$$$ vertices (numbered from $$$1$$$ to $$$n$$$) and an array $$$a$$$ of $$$n$$$ integers; initially, there are no edges in the graph. Vanya got bored, and to have fun, he decided to perform $$$n - 1$$$ operations.
Operation number $$$x$$$ (operations are numbered in order starting from $$$1$... | Each test consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^{3}$$$)Β β the number of test cases. Then follows the description of the test cases.
The first line of each test case contains the number $$$n$$$ ($$$1 \leq n \leq 2000$$$)Β β the number of vertices in the graph.
Th... | For each test case, if there is no solution, then output "No" (without quotes).
Otherwise, output "Yes" (without quotes), and then output $$$n - 1$$$ lines, where in the $$$i$$$-th line, output the numbers $$$u$$$ and $$$v$$$ that need to be chosen for operation $$$i$$$.
You can output each letter in any case (for ex... | [
[
"8\n2\n1 4\n4\n99 7 1 13\n5\n10 2 31 44 73\n5\n87 6 81 44 32\n5\n62 35 33 79 16\n5\n6 51 31 69 42\n5\n52 63 25 21 5\n12\n33 40 3 11 31 43 37 8 50 5 12 22",
"YES\n2 1\nYES\n4 1\n2 1\n3 2\nYES\n5 1\n4 1\n3 1\n2 1\nYES\n4 1\n3 1\n2 1\n5 4\nYES\n3 1\n5 1\n2 1\n4 2\nYES\n4 1\n5 1\n2 1\n3 2\nYES\n2 1\n5 2\n3 1\... | Let's consider the second test case.
- First operation $$$(x = 1)$$$: we can connect vertices $$$4$$$ and $$$1$$$, since $$$|a_4 - a_1| = |13 - 99| = |-86| = 86$$$, and $$$86$$$ is divisible by $$$1$$$.
- Second operation $$$(x = 2)$$$: we can connect vertices $$$2$$$ and $$$1$$$, since $$$|a_2 - a_1| = |7 - 99| = |-... | Title: Funny Game
time_limit_ms: 2000
memory_limit_mb: 256
Description: Vanya has a graph with $$$n$$$ vertices (numbered from $$$1$$$ to $$$n$$$) and an array $$$a$$$ of $$$n$$$ integers; initially, there are no edges in the graph. Vanya got bored, and to have fun, he decided to perform $$$n - 1$$$ operations.
Operat... |
1954A | https://codeforces.com/problemset/problem/1954/A | Painting the Ribbon | 900 | [
"constructive algorithms",
"greedy",
"math"
] | Div. 2 | 2,000 | 512 | Alice and Bob have bought a ribbon consisting of $$$n$$$ parts. Now they want to paint it.
First, Alice will paint every part of the ribbon into one of $$$m$$$ colors. For each part, she can choose its color arbitrarily.
Then, Bob will choose at most $$$k$$$ parts of the ribbon and repaint them into the same color (h... | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) β the number of test cases.
Each test case consists of one line containing three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 \le m, k \le n \le 50$$$) β the number of parts, the number of colors and the number of parts Bob can repaint, respectively. | For each test case, print YES if Alice can paint the ribbon so that Bob cannot make all parts have the same color. Otherwise, print NO.
You can print every letter in any register. For example, Yes, yes, yEs will all be recognized as positive answer. | [
[
"5\n1 1 1\n5 1 1\n5 2 1\n5 2 2\n5 5 3",
"NO\nNO\nYES\nNO\nYES"
]
] | In the first test case, a ribbon consists of $$$1$$$ part. So all its parts will always have the same color.
In the second test case, there is only $$$1$$$ color.
In the third test case, Alice can paint the ribbon as follows: $$$[1, 2, 1, 2, 1]$$$. It's impossible to change the color of at most $$$1$$$ part so that a... | Title: Painting the Ribbon
time_limit_ms: 2000
memory_limit_mb: 512
Description: Alice and Bob have bought a ribbon consisting of $$$n$$$ parts. Now they want to paint it.
First, Alice will paint every part of the ribbon into one of $$$m$$$ colors. For each part, she can choose its color arbitrarily.
Then, Bob will c... |
1954B | https://codeforces.com/problemset/problem/1954/B | Make It Ugly | 1,200 | [
"implementation",
"math"
] | Div. 2 | 2,000 | 256 | Let's call an array $$$a$$$ beautiful if you can make all its elements the same by using the following operation an arbitrary number of times (possibly, zero):
- choose an index $$$i$$$ ($$$2 \le i \le |a| - 1$$$) such that $$$a_{i - 1} = a_{i + 1}$$$, and replace $$$a_i$$$ with $$$a_{i - 1}$$$.
You are given a beaut... | 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 3 \cdot 10^5$$$).
The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$).
Additional constrain... | For each test case, output a single integerΒ β the minimum number of elements you have to remove from the array $$$a$$$ in order for it to stop being beautiful. If it is impossible, then output -1. | [
[
"4\n3\n2 2 2\n5\n1 2 1 2 1\n1\n1\n7\n3 3 3 5 3 3 3",
"-1\n1\n-1\n3"
]
] | In the first testcase, it is impossible to modify the array in such a way that it stops being beautiful. An array consisting of identical numbers will remain beautiful no matter how many numbers we remove from it.
In the second testcase, you can remove the number at the index $$$5$$$, for example.
The resulting array... | Title: Make It Ugly
time_limit_ms: 2000
memory_limit_mb: 256
Description: Let's call an array $$$a$$$ beautiful if you can make all its elements the same by using the following operation an arbitrary number of times (possibly, zero):
- choose an index $$$i$$$ ($$$2 \le i \le |a| - 1$$$) such that $$$a_{i - 1} = a_{i +... |
1954D | https://codeforces.com/problemset/problem/1954/D | Colored Balls | 1,800 | [
"combinatorics",
"dp",
"math",
"sortings"
] | Div. 2 | 2,000 | 1,024 | There are balls of $$$n$$$ different colors; the number of balls of the $$$i$$$-th color is $$$a_i$$$.
The balls can be combined into groups. Each group should contain at most $$$2$$$ balls, and no more than $$$1$$$ ball of each color.
Consider all $$$2^n$$$ sets of colors. For a set of colors, let's denote its value... | The first line contains a single integer $$$n$$$ ($$$1 \le n \le 5000$$$)Β β the number of colors.
The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 5000$$$)Β β the number of balls of the $$$i$$$-th color.
Additional constraint on input: the total number of balls doesn't exceed $$$5... | Print a single integerΒ β the sum of values of all $$$2^n$$$ sets of colors, taken modulo $$$998\,244\,353$$$. | [
[
"3\n1 1 2",
"11"
],
[
"1\n5",
"5"
],
[
"4\n1 3 3 7",
"76"
]
] | Consider the first example. There are $$$8$$$ sets of colors:
- for the empty set, its value is $$$0$$$;
- for the set $$$\{1\}$$$, its value is $$$1$$$;
- for the set $$$\{2\}$$$, its value is $$$1$$$;
- for the set $$$\{3\}$$$, its value is $$$2$$$;
- for the set $$$\{1,2\}$$$, its value is $$$1$$$;
- for the set $$... | Title: Colored Balls
time_limit_ms: 2000
memory_limit_mb: 1024
Description: There are balls of $$$n$$$ different colors; the number of balls of the $$$i$$$-th color is $$$a_i$$$.
The balls can be combined into groups. Each group should contain at most $$$2$$$ balls, and no more than $$$1$$$ ball of each color.
Consid... |
1954E | https://codeforces.com/problemset/problem/1954/E | Chain Reaction | 2,200 | [
"implementation",
"binary search",
"math",
"data structures",
"dsu",
"number theory",
"greedy"
] | Div. 2 | 3,000 | 512 | There are $$$n$$$ monsters standing in a row. The $$$i$$$-th monster has $$$a_i$$$ health points.
Every second, you can choose one alive monster and launch a chain lightning at it. The lightning deals $$$k$$$ damage to it, and also spreads to the left (towards decreasing $$$i$$$) and to the right (towards increasing $... | The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$)Β β the number of monsters.
The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^5$$$)Β β the health points of the $$$i$$$-th monster. | For each $$$k$$$ from $$$1$$$ to $$$\max(a_1, a_2, \dots, a_n)$$$, output the minimum number of seconds it takes to kill all the monsters. | [
[
"3\n5 2 7",
"10 6 4 3 2 2 1"
],
[
"4\n7 7 7 7",
"7 4 3 2 2 2 1"
],
[
"10\n1 9 7 6 2 4 7 8 1 3",
"17 9 5 4 3 3 3 2 1"
]
] | Title: Chain Reaction
time_limit_ms: 3000
memory_limit_mb: 512
Description: There are $$$n$$$ monsters standing in a row. The $$$i$$$-th monster has $$$a_i$$$ health points.
Every second, you can choose one alive monster and launch a chain lightning at it. The lightning deals $$$k$$$ damage to it, and also spreads to ... | |
1965A | https://codeforces.com/problemset/problem/1965/A | Everything Nim | 1,400 | [
"games",
"greedy",
"sortings",
"math"
] | Div. 1 | 2,000 | 256 | Alice and Bob are playing a game on $$$n$$$ piles of stones. On each player's turn, they select a positive integer $$$k$$$ that is at most the size of the smallest nonempty pile and remove $$$k$$$ stones from each nonempty pile at once. The first player who is unable to make a move (because all piles are empty) loses.
... | The first line of the 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 a single integer $$$n$$$ ($$$1 \le n \le 2\cdot 10^5$$$)Β β the number of piles in the game.
The next line of each test... | For each test case, print a single line with the name of the winner, assuming both players play optimally. If Alice wins, print "Alice", otherwise print "Bob" (without quotes). | [
[
"7\n5\n3 3 3 3 3\n2\n1 7\n7\n1 3 9 7 4 2 100\n3\n1 2 3\n6\n2 1 3 4 2 4\n8\n5 7 2 9 6 3 3 2\n1\n1000000000",
"Alice\nBob\nAlice\nAlice\nBob\nAlice\nAlice"
]
] | In the first test case, Alice can win by choosing $$$k=3$$$ on her first turn, which will empty all of the piles at once.
In the second test case, Alice must choose $$$k=1$$$ on her first turn since there is a pile of size $$$1$$$, so Bob can win on the next turn by choosing $$$k=6$$$. | Title: Everything Nim
time_limit_ms: 2000
memory_limit_mb: 256
Description: Alice and Bob are playing a game on $$$n$$$ piles of stones. On each player's turn, they select a positive integer $$$k$$$ that is at most the size of the smallest nonempty pile and remove $$$k$$$ stones from each nonempty pile at once. The fir... |
1965C | https://codeforces.com/problemset/problem/1965/C | Folding Strip | 2,300 | [
"constructive algorithms",
"greedy",
"strings"
] | Div. 1 | 2,000 | 256 | You have a strip of paper with a binary string $$$s$$$ of length $$$n$$$. You can fold the paper in between any pair of adjacent digits.
A set of folds is considered valid if after the folds, all characters that are on top of or below each other match. Note that all folds are made at the same time, so the characters d... | The first line of the 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 a single integer $$$n$$$ ($$$1 \le n \le 2\cdot 10^5$$$)Β β the size of the strip.
The second line of each test case co... | For each test case, output a single integerΒ β the minimum possible length of the strip after a valid folding. | [
[
"6\n6\n101101\n1\n0\n12\n110110110011\n5\n01110\n4\n1111\n2\n01",
"3\n1\n3\n3\n1\n2"
]
] | For the first example case, one optimal folding is to fold the strip in the middle, which produces a strip of length 3.
The third and fourth example cases correspond to the images above. Note that the folding shown above for $$$s = \mathtt{110110110011}$$$ is not of minimal length. | Title: Folding Strip
time_limit_ms: 2000
memory_limit_mb: 256
Description: You have a strip of paper with a binary string $$$s$$$ of length $$$n$$$. You can fold the paper in between any pair of adjacent digits.
A set of folds is considered valid if after the folds, all characters that are on top of or below each othe... |
1991E | https://codeforces.com/problemset/problem/1991/E | Coloring Game | 1,900 | [
"graphs",
"games",
"dfs and similar",
"constructive algorithms",
"interactive",
"greedy"
] | Div. 1 + Div. 2 | 2,000 | 256 | This is an interactive problem.
Consider an undirected connected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. Each vertex can be colored with one of three colors: $$$1$$$, $$$2$$$, or $$$3$$$. Initially, all vertices are uncolored.
Alice and Bob are playing a game consisting of $$$n$$$ rounds. In each roun... | Each test contains multiple test cases. The first line 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 two integers $$$n$$$, $$$m$$$ ($$$1 \le n \le 10^4$$$, $$$n - 1 \le m \le \min(\frac{n \cdot (n ... | null | [
[
"2\n3 3\n1 2\n2 3\n3 1\n\n\n3 1\n\n2 2\n\n1 1\n4 4\n1 2\n2 3\n3 4\n4 1\n\n2 3\n\n1 2\n\n2 1\n\n3 1",
"Alice\n3 1\n\n1 2\n\n2 1\n\n\n\n\n\n\nBob\n\n1 2\n\n2 1\n\n4 1\n\n3 3"
]
] | Note that the sample test cases are example games and do not necessarily represent the optimal strategy for both players.
In the first test case, you choose to play as Alice.
1. Alice chooses two colors: $$$3$$$ and $$$1$$$. Bob chooses vertex $$$3$$$ and colors it with color $$$1$$$.
2. Alice chooses two colors: $$$... | Title: Coloring Game
time_limit_ms: 2000
memory_limit_mb: 256
Description: This is an interactive problem.
Consider an undirected connected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. Each vertex can be colored with one of three colors: $$$1$$$, $$$2$$$, or $$$3$$$. Initially, all vertices are uncolored.
... |
1949B | https://codeforces.com/problemset/problem/1949/B | Charming Meals | 1,500 | [
"greedy",
"brute force",
"binary search",
"sortings"
] | Unrated, ICPC Rules, Teams Preferred | 2,000 | 256 | The Czech cuisine features $$$n$$$ appetizers and $$$n$$$ main dishes. The $$$i$$$-th appetizer has spiciness $$$a_i$$$, and the $$$i$$$-th main dish has spiciness $$$b_i$$$.
A typical Czech meal consists of exactly one appetizer and one main dish. You want to pair up the $$$n$$$ appetizers and $$$n$$$ main dishes int... | Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1\le t\le 1\,000$$$)Β β the number of test cases. The descriptions of the $$$t$$$ test cases follow.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 5\,000$$$)Β βthe number of appetizers and main d... | For each test case, print the largest possible value of the minimum charm you can achieve. | [
[
"4\n3\n0 0 0\n1000000000 1000000000 1000000000\n5\n1 2 3 4 5\n1 2 3 4 5\n6\n0 0 0 100 100 100\n100 100 100 0 0 0\n7\n14 25 62 74 86 95 12\n51 62 71 72 92 20 84",
"1000000000\n2\n100\n30"
]
] | In the first test case, no matter how you pair up the appetizers with the main dishes, each meal will have an appetizer with spiciness $$$0$$$ and a main dish with spiciness $$$1000000000$$$, so the charm of each meal will be $$$1000000000$$$.
In the second test case, one optimal way to pair up appetizers and main dis... | Title: Charming Meals
time_limit_ms: 2000
memory_limit_mb: 256
Description: The Czech cuisine features $$$n$$$ appetizers and $$$n$$$ main dishes. The $$$i$$$-th appetizer has spiciness $$$a_i$$$, and the $$$i$$$-th main dish has spiciness $$$b_i$$$.
A typical Czech meal consists of exactly one appetizer and one main ... |
1949C | https://codeforces.com/problemset/problem/1949/C | Annual Ants' Gathering | 1,900 | [
"dfs and similar",
"greedy",
"dp",
"trees"
] | Unrated, ICPC Rules, Teams Preferred | 2,000 | 256 | Deep within a forest lies an ancient tree, home to $$$n$$$ ants living in $$$n$$$ tiny houses, indexed from $$$1$$$ to $$$n$$$, connected by the branches of the tree.
Once a year, all the ants need to gather to watch the EUC. For this, all ants move along the $$$n-1$$$ branches of the tree they live on to meet at the ... | The first line contains one integer $$$n$$$ ($$$1\leq n\leq 200\,000$$$) β the number of ant homes.
Each of the following $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1\leq u, v\leq n$$$) β there is a branch directly connecting the house $$$u$$$ and house $$$v$$$. It is guaranteed that every ant can r... | Print $$$\texttt{YES}$$$ if it is possible to gather all the ants in a single house. Otherwise, print $$$\texttt{NO}$$$. | [
[
"7\n5 1\n3 2\n4 6\n3 6\n7 1\n1 3",
"YES"
],
[
"5\n1 4\n4 2\n3 2\n5 3",
"NO"
],
[
"6\n4 5\n5 6\n6 1\n2 6\n3 2",
"YES"
]
] | In the first sample, you can gather all the ants at house $$$3$$$ as follows:
- You tell to the ant at house $$$4$$$ to move to house $$$6$$$.
- You tell to the ant at house $$$2$$$ to move to house $$$3$$$.
- You tell to the two ants at house $$$6$$$ to move to house $$$3$$$ (which already contains two ants).
- You t... | Title: Annual Ants' Gathering
time_limit_ms: 2000
memory_limit_mb: 256
Description: Deep within a forest lies an ancient tree, home to $$$n$$$ ants living in $$$n$$$ tiny houses, indexed from $$$1$$$ to $$$n$$$, connected by the branches of the tree.
Once a year, all the ants need to gather to watch the EUC. For this,... |
1949F | https://codeforces.com/problemset/problem/1949/F | Dating | 2,200 | [
"trees",
"greedy",
"sortings"
] | Unrated, ICPC Rules, Teams Preferred | 3,000 | 512 | You are the developer of a dating app which ignores gender completely. The app has $$$n$$$ users, indexed from $$$1$$$ to $$$n$$$. Each user's profile features a list of the activities they enjoy doing. There are $$$m$$$ possible activities, indexed from $$$1$$$ to $$$m$$$.
A match between two users is good if they sh... | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 200\,000$$$, $$$1 \leq m \leq 10^6$$$) β the number of users and the number of activities.
Each of the following $$$n$$$ lines contains a number $$$k_i$$$ ($$$0 \leq k_i \leq m$$$) β the number of activities that user $$$i$$$ likes β followed b... | Print $$$\texttt{YES}$$$ if a good match exists. Otherwise, print $$$\texttt{NO}$$$.
If a good match exists, on the next line print two integersΒ β the indexes of two users that make a match. | [
[
"3 5\n3 1 2 4\n5 1 2 3 4 5\n2 1 5",
"YES\n3 1"
],
[
"3 3\n1 1\n1 2\n3 2 3 1",
"NO"
]
] | In the first sample, users $$$1$$$ and $$$3$$$ form a match, because they share activity $$$1$$$, and, furthermore, user $$$3$$$ likes activity $$$5$$$ (which user $$$1$$$ does not like) and user $$$1$$$ likes activity $$$4$$$ (which user $$$3$$$ does not like). Note that users $$$1$$$ and $$$2$$$, as well as users $$$... | Title: Dating
time_limit_ms: 3000
memory_limit_mb: 512
Description: You are the developer of a dating app which ignores gender completely. The app has $$$n$$$ users, indexed from $$$1$$$ to $$$n$$$. Each user's profile features a list of the activities they enjoy doing. There are $$$m$$$ possible activities, indexed fr... |
1949G | https://codeforces.com/problemset/problem/1949/G | Scooter | 2,300 | [
"greedy",
"graphs"
] | Unrated, ICPC Rules, Teams Preferred | 2,000 | 512 | The Czech Technical University campus consists of $$$n$$$ buildings, indexed from $$$1$$$ to $$$n$$$. In each building, there can be a math class scheduled, or a computer science class, or neither (but not both). Additionally, in each building, there is at most one professor, and each professor is either an expert in m... | The first line contains an integer $$$n$$$ ($$$1\le n \le 2000$$$) β the number of buildings in the campus.
The second line contains a string of $$$c$$$ of length $$$n$$$ consisting of the characters $$$\texttt{-}$$$, $$$\texttt{C}$$$, $$$\texttt{M}$$$ β the $$$i$$$-th character denotes the subject of the class schedu... | In the first line print an integer $$$l$$$ β the number of operations in your chosen itinerary.
The $$$i$$$-th ($$$1 \leq i \leq l$$$) of the next $$$l$$$ lines must contain one of three commands:
1. $$$\texttt{DRIVE } x$$$ β go to the building with the number $$$x$$$ ($$$1 \leq x \leq n$$$);
2. $$$\texttt{PICKUP}$$$... | [
[
"3\nCM-\n-CM",
"7\nDRIVE 3\nPICKUP\nDRIVE 2\nDROPOFF\nPICKUP\nDRIVE 1\nDROPOFF"
],
[
"1\nC\nC",
"0"
],
[
"2\n-M\nMC",
"4\nDRIVE 1\nPICKUP\nDRIVE 2\nDROPOFF"
]
] | In the first sample, You start by driving to building number $$$3$$$. You then pick up the mathematics professor. After dropping him off at building number $$$2$$$, where a mathematics class is being held, you pick up the computer science professor from there, and drop her off at building number $$$1$$$, finishing your... | Title: Scooter
time_limit_ms: 2000
memory_limit_mb: 512
Description: The Czech Technical University campus consists of $$$n$$$ buildings, indexed from $$$1$$$ to $$$n$$$. In each building, there can be a math class scheduled, or a computer science class, or neither (but not both). Additionally, in each building, there ... |
1949I | https://codeforces.com/problemset/problem/1949/I | Disks | 1,800 | [
"dfs and similar",
"graph matchings",
"geometry",
"graphs"
] | Unrated, ICPC Rules, Teams Preferred | 2,000 | 256 | You are given $$$n$$$ disks in the plane. The center of each disk has integer coordinates, and the radius of each disk is a positive integer. No two disks overlap in a region of positive area, but it is possible for disks to be tangent to each other.
Your task is to determine whether it is possible to change the radii... | The first line contains an integer $$$n$$$ ($$$1\le n \le 1000$$$) β the number of disks.
The next $$$n$$$ lines contain three integers each. The $$$i$$$-th of such lines contains $$$x_i$$$, $$$y_i$$$ ($$$-10^9 \leq x_i, y_i \leq 10^9$$$), and $$$r_i$$$ ($$$1 \leq r_i \leq 10^9$$$) β the coordinates of the center, and... | Print $$$\texttt{YES}$$$ if it is possible to change the radii in the desired manner. Otherwise, print $$$\texttt{NO}$$$. | [
[
"5\n0 2 1\n0 0 1\n4 -3 4\n11 0 3\n11 5 2",
"YES"
],
[
"4\n2 2 2\n7 2 3\n7 7 2\n2 7 3",
"NO"
]
] | In the first sample, one can decrease the radii of the first and third disk by $$$0.5$$$, and increase the radius of the second disk by $$$0.5$$$. This way, the sum of all radii decreases by $$$0.5$$$. The situation before and after changing the radii is depicted below.
First sample (left) and a valid way to change th... | Title: Disks
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given $$$n$$$ disks in the plane. The center of each disk has integer coordinates, and the radius of each disk is a positive integer. No two disks overlap in a region of positive area, but it is possible for disks to be tangent to each other.
Y... |
1951A | https://codeforces.com/problemset/problem/1951/A | Dual Trigger | 900 | [
"constructive algorithms",
"greedy",
"math"
] | Codeforces Global Round 25 | 1,000 | 256 | There are $$$n$$$ lamps numbered $$$1$$$ to $$$n$$$ lined up in a row, initially turned off. You can perform the following operation any number of times (possibly zero):
- Choose two non-adjacent$$${}^\dagger$$$ lamps that are currently turned off, then turn them on.
Determine whether you can reach configuration $$$s... | Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 50$$$)Β β the number of lamps.
The second line of each test cas... | For each test case, print on one line "YES" if we can reach the configuration $$$s$$$ via applying the given operation any number of times. Otherwise, print "NO". | [
[
"5\n10\n1101010110\n10\n1001001110\n6\n000000\n1\n1\n12\n111111111111",
"YES\nNO\nYES\nNO\nYES"
]
] | In the first test case, the sequence of operation could have been as follows (note that initially $$$s$$$ is all zero): $$$\mathtt{0000000000} \to \mathtt{\color{red}{1}0000000\color{red}{1}0} \to \mathtt{1\color{red}{1}00000\color{red}{1}10} \to \mathtt{110\color{red}{1}0\color{red}{1}0110}$$$.
In the third test case... | Title: Dual Trigger
time_limit_ms: 1000
memory_limit_mb: 256
Description: There are $$$n$$$ lamps numbered $$$1$$$ to $$$n$$$ lined up in a row, initially turned off. You can perform the following operation any number of times (possibly zero):
- Choose two non-adjacent$$${}^\dagger$$$ lamps that are currently turned o... |
1951B | https://codeforces.com/problemset/problem/1951/B | Battle Cows | 1,200 | [
"data structures",
"binary search",
"greedy"
] | Codeforces Global Round 25 | 1,000 | 256 | There are $$$n$$$ cows participating in a coding tournament. Cow $$$i$$$ has a Cowdeforces rating of $$$a_i$$$ (all distinct), and is initially in position $$$i$$$. The tournament consists of $$$n-1$$$ matches as follows:
- The first match is between the cow in position $$$1$$$ and the cow in position $$$2$$$.
- Subse... | Each test contains multiple test cases. The first line contains an 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 n \le 10^5, 1 \le k \le n$$$)Β β the number of cows and ... | For each test case, print one integer: the maximum number of wins cow $$$k$$$ can achieve if you choose to swap (or do nothing) optimally. | [
[
"3\n6 1\n12 10 14 11 8 3\n6 5\n7 2 727 10 12 13\n2 2\n1000000000 1",
"1\n2\n0"
]
] | In the first test case, it is optimal to do nothing. Let $$$a'$$$ be the Cowdeforces rating of the cows in the original order (with your cow's rating bolded), then
- Initially, $$$a' = [\mathbf{12}, 10, 14, 11, 8, 3]$$$.
- Your cow plays against the cow with Cowdeforces rating $$$10$$$ and wins. $$$a' = [\mathbf{12}, ... | Title: Battle Cows
time_limit_ms: 1000
memory_limit_mb: 256
Description: There are $$$n$$$ cows participating in a coding tournament. Cow $$$i$$$ has a Cowdeforces rating of $$$a_i$$$ (all distinct), and is initially in position $$$i$$$. The tournament consists of $$$n-1$$$ matches as follows:
- The first match is bet... |
1951C | https://codeforces.com/problemset/problem/1951/C | Ticket Hoarding | 1,400 | [
"greedy",
"math",
"sortings"
] | Codeforces Global Round 25 | 2,000 | 256 | As the CEO of a startup company, you want to reward each of your $$$k$$$ employees with a ticket to the upcoming concert. The tickets will be on sale for $$$n$$$ days, and by some time travelling, you have predicted that the price per ticket at day $$$i$$$ will be $$$a_i$$$. However, to prevent ticket hoarding, the con... | Each test contains multiple test cases. The first line contains an 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 three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \le n \le 3 \cdot 10^5, 1 \le m \le 10^9, 1 \le... | For each test case, print one integer: the minimum amount of money needed to purchase exactly $$$k$$$ tickets. | [
[
"4\n4 2 3\n8 6 4 2\n4 2 8\n8 6 4 2\n5 100 1\n10000 1 100 10 1000\n6 3 9\n5 5 5 5 5 5",
"10\n64\n1\n72"
]
] | In the first test case, one optimal way to buy $$$3$$$ tickets is as follows:
- Buy $$$0$$$ tickets on the first day. The prices per ticket for the remaining days are $$$[6, 4, 2]$$$.
- Buy $$$0$$$ tickets on the second day. The prices per ticket for the remaining days are $$$[4, 2]$$$.
- Buy $$$1$$$ ticket on the thi... | Title: Ticket Hoarding
time_limit_ms: 2000
memory_limit_mb: 256
Description: As the CEO of a startup company, you want to reward each of your $$$k$$$ employees with a ticket to the upcoming concert. The tickets will be on sale for $$$n$$$ days, and by some time travelling, you have predicted that the price per ticket a... |
1955A | https://codeforces.com/problemset/problem/1955/A | Yogurt Sale | 800 | [
"math"
] | Div. 3 | 1,000 | 256 | The price of one yogurt at the "Vosmiorochka" store is $$$a$$$ burles, but there is a promotion where you can buy two yogurts for $$$b$$$ burles.
Maxim needs to buy exactly $$$n$$$ yogurts. When buying two yogurts, he can choose to buy them at the regular price or at the promotion price.
What is the minimum amount of... | The first line contains a single integer $$$t$$$ ($$$1 \le t \le {10}^{4}$$$)Β β the number of test cases.
The first and only line of each test case contains three integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \le n \le 100$$$, $$$1 \le a, b \le 30$$$)Β β the number of yogurts Maxim wants to buy, the price for one yogurt... | For each test case, print in a separate line the minimum cost of buying $$$n$$$ yogurts at "Vosmiorochka". | [
[
"4\n2 5 9\n3 5 9\n3 5 11\n4 5 11",
"9\n14\n15\n20"
]
] | In the third test case of the example, it is more advantageous to buy three yogurts for $$$15$$$ burles than two for $$$11$$$ and one for $$$5$$$.
In the fourth test case of the example, you need to buy four yogurts, each for $$$5$$$ burles. | Title: Yogurt Sale
time_limit_ms: 1000
memory_limit_mb: 256
Description: The price of one yogurt at the "Vosmiorochka" store is $$$a$$$ burles, but there is a promotion where you can buy two yogurts for $$$b$$$ burles.
Maxim needs to buy exactly $$$n$$$ yogurts. When buying two yogurts, he can choose to buy them at th... |
1955B | https://codeforces.com/problemset/problem/1955/B | Progressive Square | 1,000 | [
"constructive algorithms",
"data structures",
"implementation",
"sortings"
] | Div. 3 | 2,000 | 256 | A progressive square of size $$$n$$$ is an $$$n \times n$$$ matrix. Maxim chooses three integers $$$a_{1,1}$$$, $$$c$$$, and $$$d$$$ and constructs a progressive square according to the following rules:
$$$$$$a_{i+1,j} = a_{i,j} + c$$$$$$
$$$$$$a_{i,j+1} = a_{i,j} + d$$$$$$
For example, if $$$n = 3$$$, $$$a_{1,1} = ... | The first line contains an integer $$$t$$$ ($$$1 \le t \le {10} ^ 4$$$)Β β the number of test cases.
The first line of each test case contains three integers $$$n$$$, $$$c$$$, and $$$d$$$ ($$$2 \le n \le 500$$$, $$$1 \le c, d \le 10^6$$$)Β β the size of the square and the values of $$$c$$$ and $$$d$$$ as described in th... | For each test case, output "YES" in a separate line if a progressive square for the given $$$n$$$, $$$c$$$, and $$$d$$$ can be constructed from the array elements $$$a$$$, otherwise output "NO".
You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" wil... | [
[
"5\n3 2 3\n3 9 6 5 7 1 10 4 8\n3 2 3\n3 9 6 5 7 1 11 4 8\n2 100 100\n400 300 400 500\n3 2 3\n3 9 6 6 5 1 11 4 8\n4 4 4\n15 27 7 19 23 23 11 15 7 3 19 23 11 15 11 15",
"NO\nYES\nYES\nNO\nNO"
]
] | Title: Progressive Square
time_limit_ms: 2000
memory_limit_mb: 256
Description: A progressive square of size $$$n$$$ is an $$$n \times n$$$ matrix. Maxim chooses three integers $$$a_{1,1}$$$, $$$c$$$, and $$$d$$$ and constructs a progressive square according to the following rules:
$$$$$$a_{i+1,j} = a_{i,j} + c$$$$$$
... | |
1955C | https://codeforces.com/problemset/problem/1955/C | Inhabitant of the Deep Sea | 1,300 | [
"greedy",
"implementation",
"math"
] | Div. 3 | 2,000 | 256 | $$$n$$$ ships set out to explore the depths of the ocean. The ships are numbered from $$$1$$$ to $$$n$$$ and follow each other in ascending order; the $$$i$$$-th ship has a durability of $$$a_i$$$.
The Kraken attacked the ships $$$k$$$ times in a specific order. First, it attacks the first of the ships, then the last,... | The first line contains an 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 \le 2 \cdot 10^5$$$, $$$1 \le k \le 10^{15}$$$)Β β the number of ships and how many times the Kraken will attack the ships.
The second l... | For each test case, output the number of ships sunk by the Kraken on a separate line. | [
[
"6\n4 5\n1 2 4 3\n4 6\n1 2 4 3\n5 20\n2 7 1 8 2\n2 2\n3 2\n2 15\n1 5\n2 7\n5 2",
"2\n3\n5\n0\n2\n2"
]
] | Title: Inhabitant of the Deep Sea
time_limit_ms: 2000
memory_limit_mb: 256
Description: $$$n$$$ ships set out to explore the depths of the ocean. The ships are numbered from $$$1$$$ to $$$n$$$ and follow each other in ascending order; the $$$i$$$-th ship has a durability of $$$a_i$$$.
The Kraken attacked the ships $$$... | |
1955D | https://codeforces.com/problemset/problem/1955/D | Inaccurate Subsequence Search | 1,400 | [
"data structures",
"two pointers"
] | Div. 3 | 2,000 | 256 | Maxim has an array $$$a$$$ of $$$n$$$ integers and an array $$$b$$$ of $$$m$$$ integers ($$$m \le n$$$).
Maxim considers an array $$$c$$$ of length $$$m$$$ to be good if the elements of array $$$c$$$ can be rearranged in such a way that at least $$$k$$$ of them match the elements of array $$$b$$$.
For example, if $$$... | The first line contains an 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$$$ ($$$1 \le k \le m \le n \le 2 \cdot 10^5$$$)Β β the number of elements in arrays $$$a$$$ and $$$b$$$, the required number of matching ele... | For each test case, output the number of good subsegments of array $$$a$$$ on a separate line. | [
[
"5\n7 4 2\n4 1 2 3 4 5 6\n1 2 3 4\n7 4 3\n4 1 2 3 4 5 6\n1 2 3 4\n7 4 4\n4 1 2 3 4 5 6\n1 2 3 4\n11 5 3\n9 9 2 2 10 9 7 6 3 6 3\n6 9 7 8 10\n4 1 1\n4 1 5 6\n6",
"4\n3\n2\n4\n1"
]
] | In the first example, all subsegments are good.
In the second example, good subsegments start at positions $$$1$$$, $$$2$$$, and $$$3$$$.
In the third example, good subsegments start at positions $$$1$$$ and $$$2$$$. | Title: Inaccurate Subsequence Search
time_limit_ms: 2000
memory_limit_mb: 256
Description: Maxim has an array $$$a$$$ of $$$n$$$ integers and an array $$$b$$$ of $$$m$$$ integers ($$$m \le n$$$).
Maxim considers an array $$$c$$$ of length $$$m$$$ to be good if the elements of array $$$c$$$ can be rearranged in such a ... |
1955E | https://codeforces.com/problemset/problem/1955/E | Long Inversions | 1,700 | [
"greedy",
"brute force",
"implementation",
"sortings"
] | Div. 3 | 3,000 | 256 | A binary string $$$s$$$ of length $$$n$$$ is given. A binary string is a string consisting only of the characters '1' and '0'.
You can choose an integer $$$k$$$ ($$$1 \le k \le n$$$) and then apply the following operation any number of times: choose $$$k$$$ consecutive characters of the string and invert them, i.e., r... | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$)Β β the number of test cases.
The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 5000$$$)Β β the length of the string $$$s$$$.
The second line of each test case contains a string $$$s$$$ of length $$$n$$$, consisting of the cha... | For each test case, output the maximum integer $$$k$$$ ($$$1 \le k \le n$$$) for which it is possible to obtain a string $$$s$$$ consisting only of the characters '1' using the described operations. | [
[
"5\n5\n00100\n5\n01000\n7\n1011101\n3\n000\n2\n10",
"3\n2\n4\n3\n1"
]
] | Title: Long Inversions
time_limit_ms: 3000
memory_limit_mb: 256
Description: A binary string $$$s$$$ of length $$$n$$$ is given. A binary string is a string consisting only of the characters '1' and '0'.
You can choose an integer $$$k$$$ ($$$1 \le k \le n$$$) and then apply the following operation any number of times:... | |
1955F | https://codeforces.com/problemset/problem/1955/F | Unfair Game | 1,800 | [
"dp",
"math",
"schedules",
"games",
"greedy"
] | Div. 3 | 2,000 | 256 | Alice and Bob gathered in the evening to play an exciting game on a sequence of $$$n$$$ integers, each integer of the sequence doesn't exceed $$$4$$$. The rules of the game are too complex to describe, so let's just describe the winning condition β Alice wins if the bitwise XOR of all the numbers in the sequence is non... | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$)Β β the number of test cases.
The first and only line of each test case contains four integers $$$p_i$$$ ($$$0 \le p_i \le 200$$$)Β β the number of ones, twos, threes, and fours in the sequence at the beginning of the game. | For each test case, print the maximum number of times Bob will win in a separate line, if Eve removes the numbers optimally. | [
[
"5\n1 1 1 0\n1 0 1 2\n2 2 2 0\n3 3 2 0\n0 9 9 9",
"1\n1\n3\n3\n12"
]
] | In the first example, Bob wins when Eve has not removed any numbers yet.
In the second example, Bob wins if Eve removes one one and one three. | Title: Unfair Game
time_limit_ms: 2000
memory_limit_mb: 256
Description: Alice and Bob gathered in the evening to play an exciting game on a sequence of $$$n$$$ integers, each integer of the sequence doesn't exceed $$$4$$$. The rules of the game are too complex to describe, so let's just describe the winning condition ... |
1955G | https://codeforces.com/problemset/problem/1955/G | GCD on a grid | 1,900 | [
"brute force",
"dp",
"implementation",
"math",
"dfs and similar",
"number theory"
] | Div. 3 | 3,000 | 256 | Not long ago, Egor learned about the Euclidean algorithm for finding the greatest common divisor of two numbers. The greatest common divisor of two numbers $$$a$$$ and $$$b$$$ is the largest number that divides both $$$a$$$ and $$$b$$$ without leaving a remainder. With this knowledge, Egor can solve a problem that he o... | The first line contains an 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 $$$m$$$ ($$$1 \le n, m \le 100$$$)Β β the number of rows and columns of the grid.
Then, there are $$$n$$$ lines, where the $$$i$$$-th line contains $$$m... | For each test case, output the maximum possible GCD along the path from the top left cell to the bottom right cell in a separate line. | [
[
"3\n2 3\n30 20 30\n15 25 40\n3 3\n12 4 9\n3 12 2\n8 3 12\n2 4\n2 4 6 8\n1 3 6 9",
"10\n3\n1"
]
] | Title: GCD on a grid
time_limit_ms: 3000
memory_limit_mb: 256
Description: Not long ago, Egor learned about the Euclidean algorithm for finding the greatest common divisor of two numbers. The greatest common divisor of two numbers $$$a$$$ and $$$b$$$ is the largest number that divides both $$$a$$$ and $$$b$$$ without l... | |
1955H | https://codeforces.com/problemset/problem/1955/H | The Most Reckless Defense | 2,300 | [
"brute force",
"dp",
"graph matchings",
"shortest paths",
"constructive algorithms",
"bitmasks",
"flows"
] | Div. 3 | 3,000 | 256 | You are playing a very popular Tower Defense game called "Runnerfield 2". In this game, the player sets up defensive towers that attack enemies moving from a certain starting point to the player's base.
You are given a grid of size $$$n \times m$$$, on which $$$k$$$ towers are already placed and a path is laid out thr... | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases.
The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$2 \le n, m \le 50, 1 \le k < n \cdot m$$$) β the dimensions of the field and the number of towers on it.
The next $$$n$$$ lines each... | For each test case, output the maximum amount of base health $$$h$$$ on a separate line, for which it is possible to set the ranges so that the player does not lose when an enemy with health $$$h$$$ passes through (without considering the additions for tower ranges).
If it is impossible to choose ranges even for an en... | [
[
"6\n2 2 1\n#.\n##\n1 2 1\n2 2 1\n#.\n##\n1 2 2\n2 2 1\n#.\n##\n1 2 500\n3 3 2\n#..\n##.\n.##\n1 2 4\n3 1 3\n3 5 2\n#.###\n#.#.#\n###.#\n2 2 2\n2 4 2\n5 5 4\n#....\n#....\n#....\n#....\n#####\n3 2 142\n4 5 9\n2 5 79\n1 3 50",
"0\n1\n1491\n11\n8\n1797"
]
] | In the first example, there is no point in increasing the tower range, as it will not be able to deal enough damage to the monster even with $$$1$$$ unit of health.
In the second example, the tower has a range of $$$1$$$, and it deals damage to the monster in cells $$$(1, 1)$$$ and $$$(2, 2)$$$.
In the third example,... | Title: The Most Reckless Defense
time_limit_ms: 3000
memory_limit_mb: 256
Description: You are playing a very popular Tower Defense game called "Runnerfield 2". In this game, the player sets up defensive towers that attack enemies moving from a certain starting point to the player's base.
You are given a grid of size ... |
1968B | https://codeforces.com/problemset/problem/1968/B | Prefiquence | 800 | [
"greedy",
"two pointers"
] | Div. 3 | 2,000 | 256 | You are given two binary strings $$$a$$$ and $$$b$$$. A binary string is a string consisting of the characters '0' and '1'.
Your task is to determine the maximum possible number $$$k$$$ such that a prefix of string $$$a$$$ of length $$$k$$$ is a subsequence of string $$$b$$$.
A sequence $$$a$$$ is a subsequence of a ... | The first line consists of 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 $$$m$$$ ($$$1\le n,m \le 2 \cdot 10^5$$$)Β β the length of string $$$a$$$ and the length of string $$$b$$$, respectively.
The second line of each t... | For each test case, output a single number β the maximum $$$k$$$, such that the first $$$k$$$ characters of $$$a$$$ form a subsequence of $$$b$$$. | [
[
"6\n5 4\n10011\n1110\n3 3\n100\n110\n1 3\n1\n111\n4 4\n1011\n1111\n3 5\n100\n11010\n3 1\n100\n0",
"2\n2\n1\n1\n3\n0"
]
] | In the first example, the string '$$$10$$$' is a subsequence of '$$$1\color{red}11\color{red}0$$$' but the string '$$$100$$$' is not. So the answer is $$$2$$$.
In the fifth example, $$$a$$$='$$$100$$$', $$$b$$$='$$$1\color{red}{10}1\color{red}0$$$', whole string $$$a$$$ is a subsequence of string $$$b$$$. So the answe... | Title: Prefiquence
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given two binary strings $$$a$$$ and $$$b$$$. A binary string is a string consisting of the characters '0' and '1'.
Your task is to determine the maximum possible number $$$k$$$ such that a prefix of string $$$a$$$ of length $$$k$$$ is a ... |
1968D | https://codeforces.com/problemset/problem/1968/D | Permutation Game | 1,300 | [
"brute force",
"graphs",
"math",
"games",
"dfs and similar",
"greedy"
] | Div. 3 | 2,000 | 256 | Bodya and Sasha found a permutation $$$p_1,\dots,p_n$$$ and an array $$$a_1,\dots,a_n$$$. They decided to play a well-known "Permutation game".
A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation... | The first line contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$)Β β the number of testcases.
The first line of each testcase contains integers $$$n$$$, $$$k$$$, $$$P_B$$$, $$$P_S$$$ ($$$1\le P_B,P_S\le n\le 2\cdot 10^5$$$, $$$1\le k\le 10^9$$$)Β β length of the permutation, duration of the game, starting position... | For each testcase output:
- "Bodya" if Bodya wins the game.
- "Sasha" if Sasha wins the game.
- "Draw" if the players have the same score. | [
[
"10\n4 2 3 2\n4 1 2 3\n7 2 5 6\n10 8 2 10\n3 1 4 5 2 7 8 10 6 9\n5 10 5 1 3 7 10 15 4 3\n2 1000000000 1 2\n1 2\n4 4\n8 10 4 1\n5 1 4 3 2 8 6 7\n1 1 2 1 2 100 101 102\n5 1 2 5\n1 2 4 5 3\n4 6 9 4 2\n4 2 3 1\n4 1 3 2\n6 8 5 3\n6 9 5 4\n6 1 3 5 2 4\n6 9 8 9 5 10\n4 8 4 2\n2 3 4 1\n5 2 8 7\n4 2 3 1\n4 1 3 2\n6 8 ... | Below you can find the explanation for the first testcase, where the game consists of $$$k=2$$$ turns.
TurnBodya's positionBodya's scoreBodya's moveSasha's positionSasha's scoreSasha's movefirst$$$3$$$$$$0 + a_3 = 0 + 5 = 5$$$stays on the same position$$$2$$$$$$0 + a_2 = 0 + 2 = 2$$$moves to $$$p_2=1$$$second$$$3$$$$$... | Title: Permutation Game
time_limit_ms: 2000
memory_limit_mb: 256
Description: Bodya and Sasha found a permutation $$$p_1,\dots,p_n$$$ and an array $$$a_1,\dots,a_n$$$. They decided to play a well-known "Permutation game".
A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ ... |
1968F | https://codeforces.com/problemset/problem/1968/F | Equal XOR Segments | 1,800 | [
"data structures",
"binary search"
] | Div. 3 | 5,000 | 256 | Let us call an array $$$x_1,\dots,x_m$$$ interesting if it is possible to divide the array into $$$k>1$$$ parts so that bitwise XOR of values from each part are equal.
More formally, you must split array $$$x$$$ into $$$k$$$ consecutive segments, each element of $$$x$$$ must belong to exactly $$$1$$$ segment. Let $$$y... | 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 $$$q$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le q \le 2 \cdot 10^5$$$)Β β the number of elements in the array and the number of queries respectively.
... | For each query, output "YES" if the subarray is interesting and "NO" otherwise.
You can output "Yes" and "No" in any case (for example, the strings "yES", "yes", and "Yes" will be recognized as correct answers). | [
[
"4\n5 5\n1 1 2 3 0\n1 5\n2 4\n3 5\n1 3\n3 4\n5 5\n1 2 3 4 5\n1 5\n2 4\n3 5\n1 3\n2 3\n7 4\n12 9 10 9 10 11 9\n1 5\n1 7\n2 6\n2 7\n11 4\n0 0 1 0 0 1 0 1 1 0 1\n1 2\n2 5\n6 9\n7 11",
"YES\nYES\nNO\nNO\nNO\n\nYES\nNO\nNO\nYES\nNO\n\nNO\nNO\nNO\nNO\n\nYES\nNO\nYES\nYES"
]
] | Explanation for the first test case:
The first query is described in the statement.
In the second query, we should divide $$$[1,2,3]$$$. A possible division is $$$[1,2],[3]$$$, since $$$1\oplus 2=3$$$.
It can be shown that for queries $$$3,4,5$$$, the subarrays are not interesting. | Title: Equal XOR Segments
time_limit_ms: 5000
memory_limit_mb: 256
Description: Let us call an array $$$x_1,\dots,x_m$$$ interesting if it is possible to divide the array into $$$k>1$$$ parts so that bitwise XOR of values from each part are equal.
More formally, you must split array $$$x$$$ into $$$k$$$ consecutive se... |
1968G1 | https://codeforces.com/problemset/problem/1968/G1 | Division + LCP (easy version) | 1,900 | [
"dp",
"binary search",
"data structures",
"string suffix structures",
"hashing",
"strings"
] | Div. 3 | 2,000 | 256 | This is the easy version of the problem. In this version $$$l=r$$$.
You are given a string $$$s$$$. For a fixed $$$k$$$, consider a division of $$$s$$$ into exactly $$$k$$$ continuous substrings $$$w_1,\dots,w_k$$$. Let $$$f_k$$$ be the maximal possible $$$LCP(w_1,\dots,w_k)$$$ among all divisions.
$$$LCP(w_1,\dots,w... | 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$$$, $$$l$$$, $$$r$$$ ($$$1 \le l = r \le n \le 2 \cdot 10^5$$$)Β β the length of the string and the given range.
The second line of each test case contains s... | For each test case, output $$$r-l+1$$$ values: $$$f_l,\dots,f_r$$$. | [
[
"7\n3 3 3\naba\n3 3 3\naaa\n7 2 2\nabacaba\n9 4 4\nabababcab\n10 1 1\ncodeforces\n9 3 3\nabafababa\n5 3 3\nzpozp",
"0\n1\n3\n2\n10\n2\n0"
]
] | In the first sample $$$n=k$$$, so the only division of $$$aba$$$ is $$$\color{red}a\color{blue}b\color{orange}a$$$. The answer is zero, because those strings do not have a common prefix.
In the second sample, the only division is $$$\color{red}a\color{blue}a\color{orange}a$$$. Their longest common prefix is one. | Title: Division + LCP (easy version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: This is the easy version of the problem. In this version $$$l=r$$$.
You are given a string $$$s$$$. For a fixed $$$k$$$, consider a division of $$$s$$$ into exactly $$$k$$$ continuous substrings $$$w_1,\dots,w_k$$$. Let $$$f_k$$... |
1968G2 | https://codeforces.com/problemset/problem/1968/G2 | Division + LCP (hard version) | 2,200 | [
"brute force",
"dp",
"binary search",
"math",
"data structures",
"string suffix structures",
"hashing",
"strings"
] | Div. 3 | 3,000 | 256 | This is the hard version of the problem. In this version $$$l\le r$$$.
You are given a string $$$s$$$. For a fixed $$$k$$$, consider a division of $$$s$$$ into exactly $$$k$$$ continuous substrings $$$w_1,\dots,w_k$$$. Let $$$f_k$$$ be the maximal possible $$$LCP(w_1,\dots,w_k)$$$ among all divisions.
$$$LCP(w_1,\dot... | 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$$$, $$$l$$$, $$$r$$$ ($$$1 \le l \le r \le n \le 2 \cdot 10^5$$$)Β β the length of the string and the given range.
The second line of each test case contains... | For each test case, output $$$r-l+1$$$ values: $$$f_l,\dots,f_r$$$. | [
[
"7\n3 1 3\naba\n3 2 3\naaa\n7 1 5\nabacaba\n9 1 6\nabababcab\n10 1 10\naaaaaaawac\n9 1 9\nabafababa\n7 2 7\nvvzvvvv",
"3 1 0 \n1 1 \n7 3 1 1 0 \n9 2 2 2 0 0 \n10 3 2 1 1 1 1 1 0 0 \n9 3 2 1 1 0 0 0 0 \n2 2 1 1 1 0"
]
] | Title: Division + LCP (hard version)
time_limit_ms: 3000
memory_limit_mb: 256
Description: This is the hard version of the problem. In this version $$$l\le r$$$.
You are given a string $$$s$$$. For a fixed $$$k$$$, consider a division of $$$s$$$ into exactly $$$k$$$ continuous substrings $$$w_1,\dots,w_k$$$. Let $$$f_... | |
1970A1 | https://codeforces.com/problemset/problem/1970/A1 | Balanced Shuffle (Easy) | 1,000 | [
"implementation",
"sortings"
] | teams allowed, unrated | 2,000 | 256 | A parentheses sequence is a string consisting of characters "(" and ")", for example "(()((".
A balanced parentheses sequence is a parentheses sequence which can become a valid mathematical expression after inserting numbers and operations into it, for example "(()(()))".
The balance of a parentheses sequence is defi... | The only line of input contains a string $$$s$$$ consisting only of characters "(" and ")". This string is guaranteed to be a non-empty balanced parentheses sequence with its length not exceeding $$$500\,000$$$. | Print the parentheses sequence $$$t$$$Β β the balanced shuffle of $$$s$$$. | [
[
"(()(()))",
"()(()())"
]
] | Title: Balanced Shuffle (Easy)
time_limit_ms: 2000
memory_limit_mb: 256
Description: A parentheses sequence is a string consisting of characters "(" and ")", for example "(()((".
A balanced parentheses sequence is a parentheses sequence which can become a valid mathematical expression after inserting numbers and opera... | |
1970A2 | https://codeforces.com/problemset/problem/1970/A2 | Balanced Unshuffle (Medium) | 2,400 | [
"constructive algorithms",
"brute force",
"trees"
] | teams allowed, unrated | 2,000 | 256 | The differences with the easy version of this problem are highlighted in bold.
A parentheses sequence is a string consisting of characters "(" and ")", for example "(()((".
A balanced parentheses sequence is a parentheses sequence which can become a valid mathematical expression after inserting numbers and operations... | The only line of input contains a string $$$s$$$ consisting only of characters "(" and ")". This string is guaranteed to be a non-empty balanced parentheses sequence with its length not exceeding $$$1\,000$$$. | Print the balanced parentheses sequence $$$t$$$ such that the balanced shuffle of $$$t$$$ is equal to $$$s$$$. It is guaranteed that the answer always exists and is unique. | [
[
"()(()())",
"(()(()))"
]
] | Title: Balanced Unshuffle (Medium)
time_limit_ms: 2000
memory_limit_mb: 256
Description: The differences with the easy version of this problem are highlighted in bold.
A parentheses sequence is a string consisting of characters "(" and ")", for example "(()((".
A balanced parentheses sequence is a parentheses sequenc... | |
1970A3 | https://codeforces.com/problemset/problem/1970/A3 | Balanced Unshuffle (Hard) | 2,400 | [
"constructive algorithms",
"trees"
] | teams allowed, unrated | 2,000 | 256 | The only difference with the medium version is the maximum length of the input.
A parentheses sequence is a string consisting of characters "(" and ")", for example "(()((".
A balanced parentheses sequence is a parentheses sequence which can become a valid mathematical expression after inserting numbers and operation... | The only line of input contains a string $$$s$$$ consisting only of characters "(" and ")". This string is guaranteed to be a non-empty balanced parentheses sequence with its length not exceeding $$$500\,000$$$. | Print the balanced parentheses sequence $$$t$$$ such that the balanced shuffle of $$$t$$$ is equal to $$$s$$$. It is guaranteed that the answer always exists and is unique. | [
[
"()(()())",
"(()(()))"
]
] | Title: Balanced Unshuffle (Hard)
time_limit_ms: 2000
memory_limit_mb: 256
Description: The only difference with the medium version is the maximum length of the input.
A parentheses sequence is a string consisting of characters "(" and ")", for example "(()((".
A balanced parentheses sequence is a parentheses sequence... | |
1970C1 | https://codeforces.com/problemset/problem/1970/C1 | Game on Tree (Easy) | 1,400 | [
"games"
] | teams allowed, unrated | 1,000 | 256 | This is the easy version of the problem. The difference in this version is that $$$t=1$$$ and we work on an array-like tree.
Ron and Hermione are playing a game on a tree of $$$n$$$ nodes that are initially inactive. This tree is special because it has exactly two leaves. It can thus be seen as an array. The game cons... | The first line contains integers $$$n$$$ ($$$2 \leq n \leq 2\times 10^5$$$), $$$t$$$ ($$$t=1$$$), the number of nodes in the tree and the number of rounds, respectively.
The next $$$n-1$$$ lines contain two integers $$$1 \leq u, v \leq n$$$ each, corresponding to an edge of the tree. It is guaranteed that the tree has... | The output consists of $$$t=1$$$ line which is either "Ron" or "Hermione". | [
[
"3 1\n2 3\n3 1\n3",
"Ron"
],
[
"5 1\n1 2\n2 3\n3 4\n4 5\n5",
"Hermione"
]
] | Title: Game on Tree (Easy)
time_limit_ms: 1000
memory_limit_mb: 256
Description: This is the easy version of the problem. The difference in this version is that $$$t=1$$$ and we work on an array-like tree.
Ron and Hermione are playing a game on a tree of $$$n$$$ nodes that are initially inactive. This tree is special ... | |
1970C2 | https://codeforces.com/problemset/problem/1970/C2 | Game on Tree (Medium) | 1,700 | [
"dfs and similar",
"games",
"dp",
"trees"
] | teams allowed, unrated | 1,000 | 256 | This is the medium version of the problem. The difference in this version is that $$$t=1$$$ and we work on trees.
Ron and Hermione are playing a game on a tree of $$$n$$$ nodes that are initially inactive. The game consists of $$$t$$$ rounds, each of which starts with a stone on exactly one node, which is considered a... | The first line contains integers $$$n$$$ ($$$2 \leq n \leq 2\times 10^5$$$), $$$t$$$ ($$$t=1$$$), the number of nodes in the tree and the number of rounds, respectively.
The next $$$n-1$$$ lines contain two integers $$$1 \leq u, v \leq n$$$ each, corresponding to an edge of the tree.
The next line contains $$$t$$$ in... | The output consists of $$$t=1$$$ line which is either "Ron" or "Hermione". | [
[
"5 1\n1 2\n1 3\n3 4\n3 5\n1",
"Ron"
]
] | Title: Game on Tree (Medium)
time_limit_ms: 1000
memory_limit_mb: 256
Description: This is the medium version of the problem. The difference in this version is that $$$t=1$$$ and we work on trees.
Ron and Hermione are playing a game on a tree of $$$n$$$ nodes that are initially inactive. The game consists of $$$t$$$ r... | |
1970C3 | https://codeforces.com/problemset/problem/1970/C3 | Game on Tree (Hard) | 1,900 | [
"dfs and similar",
"games",
"dp",
"trees"
] | teams allowed, unrated | 1,000 | 256 | This is the hard version of the problem. The only difference in this version is the constraint on $$$t$$$.
Ron and Hermione are playing a game on a tree of $$$n$$$ nodes that are initially inactive. The game consists of $$$t$$$ rounds, each of which starts with a stone on exactly one node, which is considered as activ... | The first line contains integers $$$n$$$ ($$$2 \leq n \leq 2\times 10^5$$$), $$$t$$$ ($$$1 \leq t \leq n$$$), the number of nodes in the tree and the number of rounds, respectively.
The next $$$n-1$$$ lines contain two integers $$$1 \leq u, v \leq n$$$ each, corresponding to an edge of the tree.
The next line contain... | The output consists of $$$t$$$ lines, each line being either "Ron" or "Hermione". | [
[
"5 2\n1 2\n1 3\n3 4\n3 5\n1 2",
"Ron\nRon"
],
[
"6 3\n1 2\n2 3\n1 4\n4 5\n4 6\n1 4 6",
"Hermione\nRon\nHermione"
]
] | Title: Game on Tree (Hard)
time_limit_ms: 1000
memory_limit_mb: 256
Description: This is the hard version of the problem. The only difference in this version is the constraint on $$$t$$$.
Ron and Hermione are playing a game on a tree of $$$n$$$ nodes that are initially inactive. The game consists of $$$t$$$ rounds, ea... | |
1970D1 | https://codeforces.com/problemset/problem/1970/D1 | Arithmancy (Easy) | 2,100 | [
"constructive algorithms",
"interactive",
"brute force",
"strings"
] | teams allowed, unrated | 2,000 | 256 | Professor Vector is preparing to teach her Arithmancy class. She needs to prepare $$$n$$$ distinct magic words for the class. Each magic word is a string consisting of characters X and O. A spell is a string created by concatenating two magic words together. The power of a spell is equal to the number of its different ... | null | null | [
[
"2\n\n\n2\n15\n\n11",
"XOXO\nX\n\n\n1 1\n\n2 1"
]
] | Title: Arithmancy (Easy)
time_limit_ms: 2000
memory_limit_mb: 256
Description: Professor Vector is preparing to teach her Arithmancy class. She needs to prepare $$$n$$$ distinct magic words for the class. Each magic word is a string consisting of characters X and O. A spell is a string created by concatenating two magi... | |
1970E1 | https://codeforces.com/problemset/problem/1970/E1 | Trails (Easy) | 1,800 | [
"dp"
] | teams allowed, unrated | 1,000 | 256 | Harry Potter is hiking in the Alps surrounding Lake Geneva. In this area there are $$$m$$$ cabins, numbered 1 to $$$m$$$. Each cabin is connected, with one or more trails, to a central meeting point next to the lake. Each trail is either short or long. Cabin $$$i$$$ is connected with $$$s_i$$$ short trails and $$$l_i$$... | The first line contains the integers $$$m$$$ and $$$n$$$.
The second line contains $$$m$$$ integers, $$$s_1, \dots, s_m$$$, where $$$s_i$$$ is the number of short trails between cabin $$$i$$$ and Lake Geneva.
The third and last line contains $$$m$$$ integers, $$$l_1, \dots, l_m$$$, where $$$l_i$$$ is the number of lo... | The number of possible combinations of trails, modulo $$$10^9 + 7$$$. | [
[
"3 2\n1 0 1\n0 1 1",
"18"
]
] | Title: Trails (Easy)
time_limit_ms: 1000
memory_limit_mb: 256
Description: Harry Potter is hiking in the Alps surrounding Lake Geneva. In this area there are $$$m$$$ cabins, numbered 1 to $$$m$$$. Each cabin is connected, with one or more trails, to a central meeting point next to the lake. Each trail is either short o... | |
1970E2 | https://codeforces.com/problemset/problem/1970/E2 | Trails (Medium) | 2,000 | [
"dp",
"matrices"
] | teams allowed, unrated | 1,000 | 256 | Harry Potter is hiking in the Alps surrounding Lake Geneva. In this area there are $$$m$$$ cabins, numbered 1 to $$$m$$$. Each cabin is connected, with one or more trails, to a central meeting point next to the lake. Each trail is either short or long. Cabin $$$i$$$ is connected with $$$s_i$$$ short trails and $$$l_i$$... | The first line contains the integers $$$m$$$ and $$$n$$$.
The second line contains $$$m$$$ integers, $$$s_1, \dots, s_m$$$, where $$$s_i$$$ is the number of short trails between cabin $$$i$$$ and Lake Geneva.
The third and last line contains $$$m$$$ integers, $$$l_1, \dots, l_m$$$, where $$$l_i$$$ is the number of lo... | The number of possible combinations of trails, modulo $$$10^9 + 7$$$. | [
[
"3 2\n1 0 1\n0 1 1",
"18"
]
] | Title: Trails (Medium)
time_limit_ms: 1000
memory_limit_mb: 256
Description: Harry Potter is hiking in the Alps surrounding Lake Geneva. In this area there are $$$m$$$ cabins, numbered 1 to $$$m$$$. Each cabin is connected, with one or more trails, to a central meeting point next to the lake. Each trail is either short... | |
1970E3 | https://codeforces.com/problemset/problem/1970/E3 | Trails (Hard) | 2,200 | [
"dp",
"matrices"
] | teams allowed, unrated | 2,000 | 256 | Harry Potter is hiking in the Alps surrounding Lake Geneva. In this area there are $$$m$$$ cabins, numbered 1 to $$$m$$$. Each cabin is connected, with one or more trails, to a central meeting point next to the lake. Each trail is either short or long. Cabin $$$i$$$ is connected with $$$s_i$$$ short trails and $$$l_i$$... | The first line contains the integers $$$m$$$ and $$$n$$$.
The second line contains $$$m$$$ integers, $$$s_1, \dots, s_m$$$, where $$$s_i$$$ is the number of short trails between cabin $$$i$$$ and Lake Geneva.
The third and last line contains $$$m$$$ integers, $$$l_1, \dots, l_m$$$, where $$$l_i$$$ is the number of lo... | The number of possible combinations of trails, modulo $$$10^9 + 7$$$. | [
[
"3 2\n1 0 1\n0 1 1",
"18"
]
] | Title: Trails (Hard)
time_limit_ms: 2000
memory_limit_mb: 256
Description: Harry Potter is hiking in the Alps surrounding Lake Geneva. In this area there are $$$m$$$ cabins, numbered 1 to $$$m$$$. Each cabin is connected, with one or more trails, to a central meeting point next to the lake. Each trail is either short o... | |
1970F1 | https://codeforces.com/problemset/problem/1970/F1 | Playing Quidditch (Easy) | 2,300 | [
"implementation"
] | teams allowed, unrated | 1,000 | 256 | This afternoon, you decided to enjoy the first days of Spring by taking a walk outside. As you come near the Quidditch field, you hear screams. Once again, there is a conflict about the score: the two teams are convinced that they won the game! To prevent this problem from happening one more time, you decide to get inv... | On the first line, the integers $$$N$$$ and $$$M$$$.
The description of the field follows: $$$N$$$ lines of $$$M$$$ pairs of characters separated by spaces. Each pair of characters represents a position on the field. It can be either:
- .. to represent an empty cell
- R0, ..., R9, B0, ..., B9 to represent a player. T... | You must output the description of the main events of the game, one event per line. More precisely:
- Each time a team scores, you must print t RED GOAL or t BLUE GOAL, depending on the team who scored, where t is the current time (the position of the action in the list of actions, starting from 0). In the case where ... | [
[
"5 5\n.. R1 .. B1 ..\nRG .. .. .. BG\nRG R0 .Q B0 BG\nRG .. .. .. BG\n.. R2 .. B2 ..\n13\nB2 U\nB2 U\nB2 L\nB2 C .Q\nB2 L\nB2 L\nB2 T\nR0 R\nR0 C .Q\nR0 D\nR0 R\nR0 R\nR0 T",
"6 BLUE GOAL\n12 RED GOAL\nFINAL SCORE: 1 1"
],
[
"3 5\n.. .. R0 .. ..\nRG .. .Q .. BG\n.. .. B0 .. ..\n12\nR0 D\nR0 C .Q\n... | In the first example, a blue player goes to the middle of the grid, at the position of the Quaffle. He takes it, goes to the goal of the red team, and lets the ball there. He scores! The ball gets back to the middle of the field, a red player takes it and scores too.
In the second example, the red player takes the Qua... | Title: Playing Quidditch (Easy)
time_limit_ms: 1000
memory_limit_mb: 256
Description: This afternoon, you decided to enjoy the first days of Spring by taking a walk outside. As you come near the Quidditch field, you hear screams. Once again, there is a conflict about the score: the two teams are convinced that they won... |
1970F2 | https://codeforces.com/problemset/problem/1970/F2 | Playing Quidditch (Medium) | 2,300 | [
"implementation"
] | teams allowed, unrated | 1,000 | 256 | This afternoon, you decided to enjoy the first days of Spring by taking a walk outside. As you come near the Quidditch field, you hear screams. Once again, there is a conflict about the score: the two teams are convinced that they won the game! To prevent this problem from happening one more time, you decide to get inv... | On the first line, the integers $$$N$$$ and $$$M$$$.
The description of the field follows: $$$N$$$ lines of $$$M$$$ pairs of characters separated by spaces. Each pair of characters represents a position on the field. It can be either:
- .. to represent an empty cell
- R0, ..., R9, B0, ..., B9 to represent a player. T... | You must output the description of the main events of the game, one event per line. More precisely:
- Each time a team scores, you must print t RED GOAL or t BLUE GOAL, depending on the team who scored, where t is the current time (the position of the action in the list of actions, starting from 0). In the case where ... | [
[
"3 5\n.. .. R0 .. ..\nRG .. .Q .. BG\n.. .. B0 .. ..\n12\nR0 D\nR0 C .Q\nR0 R\nR0 T\nR0 D\nB0 R\nB0 U\nB0 C .Q\nB0 L\nB0 L\nB0 L\nB0 T",
"11 BLUE GOAL\nFINAL SCORE: 0 1"
],
[
"3 5\n.. .. R0 .. ..\nRG .. .Q .. BG\n.. .. B0 .. ..\n5\nR0 D\nR0 C .Q\nR0 L\nR0 L\nR0 T",
"4 BLUE GOAL\nFINAL SCORE: 0... | In the first example, the red player takes the Quaffle, move it and throw it. The blue player catches the ball, goes to the red goal and scores.
In the second example, the red player takes the ball and scores in the goal of their own team: the blue team wins a point.
In the third example, the Bludger goes at the posi... | Title: Playing Quidditch (Medium)
time_limit_ms: 1000
memory_limit_mb: 256
Description: This afternoon, you decided to enjoy the first days of Spring by taking a walk outside. As you come near the Quidditch field, you hear screams. Once again, there is a conflict about the score: the two teams are convinced that they w... |
1970F3 | https://codeforces.com/problemset/problem/1970/F3 | Playing Quidditch (Hard) | 2,300 | [
"implementation"
] | teams allowed, unrated | 1,000 | 256 | This afternoon, you decided to enjoy the first days of Spring by taking a walk outside. As you come near the Quidditch field, you hear screams. Once again, there is a conflict about the score: the two teams are convinced that they won the game! To prevent this problem from happening one more time, you decide to get inv... | On the first line, the integers $$$N$$$ and $$$M$$$.
The description of the field follows: $$$N$$$ lines of $$$M$$$ pairs of characters separated by spaces. Each pair of characters represents a position on the field. It can be either:
- .. to represent an empty cell
- R0, ..., R9, B0, ..., B9 to represent a player. T... | You must output the description of the main events of the game, one event per line. More precisely:
- Each time a team scores, you must print t RED GOAL or t BLUE GOAL, depending on the team who scored, where t is the current time (the position of the action in the list of actions, starting from 0). In the case where ... | [
[
"3 5\n.. .. R0 .. ..\nRG .. .Q .. BG\n.. .. B0 .. ..\n12\nR0 D\nR0 C .Q\nR0 R\nR0 T\nR0 D\nB0 R\nB0 U\nB0 C .Q\nB0 L\nB0 L\nB0 L\nB0 T",
"11 BLUE GOAL\nFINAL SCORE: 0 1"
],
[
"3 5\n.. .. R0 .. ..\nRG .. .Q .. BG\n.. .. B0 .. ..\n5\nR0 D\nR0 C .Q\nR0 L\nR0 L\nR0 T",
"4 BLUE GOAL\nFINAL SCORE: 0... | In the first example, the red player takes the Quaffle, move it and throw it. The blue player catches the ball, goes to the red goal and scores.
In the second example, the red player takes the ball and scores in the goal of their own team: the blue team wins a point.
In the third example, the Bludger goes at the posi... | Title: Playing Quidditch (Hard)
time_limit_ms: 1000
memory_limit_mb: 256
Description: This afternoon, you decided to enjoy the first days of Spring by taking a walk outside. As you come near the Quidditch field, you hear screams. Once again, there is a conflict about the score: the two teams are convinced that they won... |
1970G1 | https://codeforces.com/problemset/problem/1970/G1 | Min-Fund Prison (Easy) | 1,900 | [
"dfs and similar",
"trees"
] | teams allowed, unrated | 1,500 | 256 | In the easy version, $$$m = n-1$$$ and there exists a path between $$$u$$$ and $$$v$$$ for all $$$u, v$$$ ($$$1 \leq u, v \leq n$$$).
After a worker's strike organized by the Dementors asking for equal rights, the prison of Azkaban has suffered some damage. After settling the spirits, the Ministry of Magic is looking ... | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) β the number of test cases. Then $$$t$$$ test cases follow.
The first line of each test case consists of three integers $$$n, m$$$ and $$$c$$$ ($$$2 \leq n \leq 10^5$$$, $$$m = n - 1$$$, $$$1 \leq c \leq 10^9$$$)
$$$m$$$ lines follow, each consist... | Print the $$$\textbf{minimum funding}$$$ required to divide the prison as per the Ministry's requirements or $$$-1$$$ if no division is possible. | [
[
"2\n2 1 3\n1 2\n8 7 76\n3 1\n3 2\n2 4\n2 5\n4 6\n4 7\n7 8",
"2\n32"
]
] | Title: Min-Fund Prison (Easy)
time_limit_ms: 1500
memory_limit_mb: 256
Description: In the easy version, $$$m = n-1$$$ and there exists a path between $$$u$$$ and $$$v$$$ for all $$$u, v$$$ ($$$1 \leq u, v \leq n$$$).
After a worker's strike organized by the Dementors asking for equal rights, the prison of Azkaban has... | |
1970G2 | https://codeforces.com/problemset/problem/1970/G2 | Min-Fund Prison (Medium) | 2,200 | [
"brute force",
"dp",
"graphs",
"dfs and similar",
"trees"
] | teams allowed, unrated | 1,500 | 256 | In the medium version, $$$2 \leq \sum n \leq 300$$$ and $$$1 \leq \sum m \leq 300$$$
After a worker's strike organized by the Dementors asking for equal rights, the prison of Azkaban has suffered some damage. After settling the spirits, the Ministry of Magic is looking to renovate the prison to ensure that the Demento... | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 300$$$) β the number of test cases. Then $$$t$$$ test cases follow.
The first line of each test case consists of three integers $$$n, m$$$ and $$$c$$$ ($$$2 \leq n \leq 300$$$, $$$1 \leq m \leq 300$$$, $$$1 \leq c \leq 10^9$$$)
$$$m$$$ lines follow, each c... | Print the $$$\textbf{minimum funding}$$$ required to divide the prison as per the Ministry's requirements or $$$-1$$$ if no division is possible. | [
[
"4\n4 6 5\n4 3\n2 3\n2 4\n1 2\n4 1\n3 1\n6 6 2\n1 4\n2 5\n3 6\n1 5\n3 5\n6 5\n6 5 7\n1 4\n2 5\n3 6\n3 5\n6 5\n7 5 4\n1 4\n3 6\n3 5\n6 5\n2 7",
"-1\n20\n25\n33"
]
] | In the first test case of the sample input, there is no way to divide the prison according to the Ministry's requirements.
In the second test case, consider the corridor between cells $$$1$$$ and $$$5$$$ as the connection between the $$$2$$$ complexes consisting of $$$\{2, 3, 5, 6\}$$$ and $$$\{1, 4\}$$$ cells respect... | Title: Min-Fund Prison (Medium)
time_limit_ms: 1500
memory_limit_mb: 256
Description: In the medium version, $$$2 \leq \sum n \leq 300$$$ and $$$1 \leq \sum m \leq 300$$$
After a worker's strike organized by the Dementors asking for equal rights, the prison of Azkaban has suffered some damage. After settling the spiri... |
1970G3 | https://codeforces.com/problemset/problem/1970/G3 | Min-Fund Prison (Hard) | 2,400 | [
"dp",
"graphs",
"dfs and similar",
"bitmasks",
"trees"
] | teams allowed, unrated | 1,500 | 256 | In the hard version, $$$2 \leq \sum n \leq 10^5$$$ and $$$1 \leq \sum m \leq 5 \times 10^{5}$$$
After a worker's strike organized by the Dementors asking for equal rights, the prison of Azkaban has suffered some damage. After settling the spirits, the Ministry of Magic is looking to renovate the prison to ensure that ... | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) β the number of test cases. Then $$$t$$$ test cases follow.
The first line of each test case consists of three integers $$$n, m$$$ and $$$c$$$ ($$$2 \leq n \leq 10^5$$$, $$$1 \leq m \leq 5 \times 10^{5}$$$, $$$1 \leq c \leq 10^9$$$)
$$$m$$$ lines ... | Print the $$$\textbf{minimum funding}$$$ required to divide the prison as per the Ministry's requirements or $$$-1$$$ if no division is possible. | [
[
"4\n4 6 5\n4 3\n2 3\n2 4\n1 2\n4 1\n3 1\n6 6 2\n1 4\n2 5\n3 6\n1 5\n3 5\n6 5\n6 5 7\n1 4\n2 5\n3 6\n3 5\n6 5\n7 5 4\n1 4\n3 6\n3 5\n6 5\n2 7",
"-1\n20\n25\n33"
]
] | In the first test case of the sample input, there is no way to divide the prison according to the Ministry's requirements.
In the second test case, consider the corridor between cells $$$1$$$ and $$$5$$$ as the connection between the $$$2$$$ complexes consisting of $$$\{2, 3, 5, 6\}$$$ and $$$\{1, 4\}$$$ cells respect... | Title: Min-Fund Prison (Hard)
time_limit_ms: 1500
memory_limit_mb: 256
Description: In the hard version, $$$2 \leq \sum n \leq 10^5$$$ and $$$1 \leq \sum m \leq 5 \times 10^{5}$$$
After a worker's strike organized by the Dementors asking for equal rights, the prison of Azkaban has suffered some damage. After settling ... |
1957A | https://codeforces.com/problemset/problem/1957/A | Stickogon | 800 | [
"constructive algorithms",
"greedy"
] | Div. 2 | 1,000 | 256 | You are given $$$n$$$ sticks of lengths $$$a_1, a_2, \ldots, a_n$$$. Find the maximum number of regular (equal-sided) polygons you can construct simultaneously, such that:
- Each side of a polygon is formed by exactly one stick.
- No stick is used in more than $$$1$$$ polygon.
Note: Sticks cannot be broken. | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)Β β the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$)Β β the number of sticks available.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ... | For each test case, output a single integer on a new lineΒ β the maximum number of regular (equal-sided) polygons you can make simultaneously from the sticks available. | [
[
"4\n1\n1\n2\n1 1\n6\n2 2 3 3 3 3\n9\n4 2 2 2 2 4 2 4 4",
"0\n0\n1\n2"
]
] | In the first test case, we only have one stick, hence we can't form any polygon.
In the second test case, the two sticks aren't enough to form a polygon either.
In the third test case, we can use the $$$4$$$ sticks of length $$$3$$$ to create a square.
In the fourth test case, we can make a pentagon with side length... | Title: Stickogon
time_limit_ms: 1000
memory_limit_mb: 256
Description: You are given $$$n$$$ sticks of lengths $$$a_1, a_2, \ldots, a_n$$$. Find the maximum number of regular (equal-sided) polygons you can construct simultaneously, such that:
- Each side of a polygon is formed by exactly one stick.
- No stick is used ... |
1957C | https://codeforces.com/problemset/problem/1957/C | How Does the Rook Move? | 1,600 | [
"combinatorics",
"dp",
"math"
] | Div. 2 | 2,000 | 256 | You are given an $$$n \times n$$$ chessboard where you and the computer take turns alternatingly to place white rooks & black rooks on the board respectively. While placing rooks, you have to ensure that no two rooks attack each other. Two rooks attack each other if they share the same row or column regardless of color... | 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 two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 3 \cdot 10^5$$$, $$$0 \leq k \leq n$$$)Β β the size of the chessboard and the number of moves you have already played respe... | For each test case, output a single integer on a new lineΒ β the total number of possible final configurations modulo $$$10^9+7$$$. | [
[
"3\n4 1\n1 2\n8 1\n7 6\n1000 4\n4 4\n952 343\n222 333\n90 91",
"3\n331\n671968183"
]
] | In the first test case, we have a $$$4 \times 4$$$ grid and you've already played $$$1$$$ move. After you and the computer play a turn, we have a white rook at ($$$1$$$, $$$2$$$), and a black rook at ($$$2$$$, $$$1$$$). There are three possible configurations from this stateΒ β
1. You place a white rook at ($$$3$$$, $$... | Title: How Does the Rook Move?
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given an $$$n \times n$$$ chessboard where you and the computer take turns alternatingly to place white rooks & black rooks on the board respectively. While placing rooks, you have to ensure that no two rooks attack each other.... |
1957D | https://codeforces.com/problemset/problem/1957/D | A BIT of an Inequality | 1,900 | [
"bitmasks",
"brute force",
"dp",
"math"
] | Div. 2 | 2,000 | 256 | You are given an array $$$a_1, a_2, \ldots, a_n$$$. Find the number of tuples ($$$x, y, z$$$) such that:
- $$$1 \leq x \leq y \leq z \leq n$$$, and
- $$$f(x, y) \oplus f(y, z) > f(x, z)$$$.
We define $$$f(l, r) = a_l \oplus a_{l + 1} \oplus \ldots \oplus a_{r}$$$, where $$$\oplus$$$ denotes the bitwise XOR operation. | 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$$$ ($$$1 \leq n \leq 10^5$$$).
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$).
... | For each test case, output a single integer on a new lineΒ β the number of described tuples. | [
[
"3\n3\n6 2 4\n1\n3\n5\n7 3 7 2 1",
"4\n0\n16"
]
] | In the first case, there are 4 such tuples in the array $$$[6, 2, 4]$$$:
- ($$$1$$$, $$$2$$$, $$$2$$$): $$$(a_1 \oplus a_2) \oplus (a_2) = 4 \oplus 2 > (a_1 \oplus a_2) = 4$$$
- ($$$1$$$, $$$1$$$, $$$3$$$): $$$(a_1) \oplus (a_1 \oplus a_2 \oplus a_3) = 6 \oplus 0 > (a_1 \oplus a_2 \oplus a_3) = 0$$$
- ($$$1$$$, $$$2$$... | Title: A BIT of an Inequality
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given an array $$$a_1, a_2, \ldots, a_n$$$. Find the number of tuples ($$$x, y, z$$$) such that:
- $$$1 \leq x \leq y \leq z \leq n$$$, and
- $$$f(x, y) \oplus f(y, z) > f(x, z)$$$.
We define $$$f(l, r) = a_l \oplus a_{l + 1} ... |
1957E | https://codeforces.com/problemset/problem/1957/E | Carousel of Combinations | 2,400 | [
"brute force",
"dp",
"math",
"number theory",
"combinatorics"
] | Div. 2 | 2,500 | 256 | You are given an integer $$$n$$$. The function $$$C(i,k)$$$ represents the number of distinct ways you can select $$$k$$$ distinct numbers from the set {$$$1, 2, \ldots, i$$$} and arrange them in a circle$$$^\dagger$$$.
Find the value of $$$$$$ \sum\limits_{i=1}^n \sum\limits_{j=1}^i \left( C(i,j) \bmod j \right). $$$... | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)Β β the number of test cases.
The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^6$$$). | For each test case, output a single integer on a new lineΒ β the value of the expression to be calculated modulo $$$10^9+7$$$. | [
[
"4\n1\n3\n6\n314159",
"0\n4\n24\n78926217"
]
] | In the first test case, $$$C(1,1) \bmod 1 = 0$$$.
In the second test case:
- $$$C(1,1)=1$$$ (the arrangements are: $$$[1]$$$);
- $$$C(2,1)=2$$$ (the arrangements are: $$$[1]$$$, $$$[2]$$$);
- $$$C(2,2)=1$$$ (the arrangements are: $$$[1, 2]$$$);
- $$$C(3,1)=3$$$ (the arrangements are: $$$[1]$$$, $$$[2]$$$, $$$[3]$$$);... | Title: Carousel of Combinations
time_limit_ms: 2500
memory_limit_mb: 256
Description: You are given an integer $$$n$$$. The function $$$C(i,k)$$$ represents the number of distinct ways you can select $$$k$$$ distinct numbers from the set {$$$1, 2, \ldots, i$$$} and arrange them in a circle$$$^\dagger$$$.
Find the valu... |
1966A | https://codeforces.com/problemset/problem/1966/A | Card Exchange | 800 | [
"constructive algorithms",
"games",
"greedy"
] | Div. 2 | 1,000 | 256 | You have a hand of $$$n$$$ cards, where each card has a number written on it, and a fixed integer $$$k$$$. You can perform the following operation any number of times:
- Choose any $$$k$$$ cards from your hand that all have the same number.
- Exchange these cards for $$$k-1$$$ cards, each of which can have any number ... | The first line of the 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 two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$2 \le k \le 100$$$)Β β the number of cards you have, and the ... | For each test case, output a single integerΒ β the minimum number of cards you can have left in your hand after any number of operations. | [
[
"7\n5 3\n4 1 1 4 4\n1 10\n7\n7 2\n4 2 1 100 5 2 3\n10 4\n1 1 1 1 1 1 1 1 1 1\n5 2\n3 8 1 48 7\n6 2\n10 20 30 10 20 40\n6 3\n10 20 30 10 20 40",
"2\n1\n1\n3\n5\n1\n6"
]
] | The first example case corresponds to the picture above. The sequence of operations displayed there is optimal, so the answer is $$$2$$$.
In the second example case, no operations can be performed, so the answer is $$$1$$$.
In the fourth example case, you can repeatedly select $$$4$$$ cards numbered with $$$1$$$ and ... | Title: Card Exchange
time_limit_ms: 1000
memory_limit_mb: 256
Description: You have a hand of $$$n$$$ cards, where each card has a number written on it, and a fixed integer $$$k$$$. You can perform the following operation any number of times:
- Choose any $$$k$$$ cards from your hand that all have the same number.
- E... |
1966B | https://codeforces.com/problemset/problem/1966/B | Rectangle Filling | 1,100 | [
"constructive algorithms",
"implementation"
] | Div. 2 | 1,000 | 256 | There is an $$$n \times m$$$ grid of white and black squares. In one operation, you can select any two squares of the same color, and color all squares in the subrectangle between them that color.
Formally, if you select positions $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$, both of which are currently the same color $$$c$$... | The first line of the 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$$$ and $$$m$$$ ($$$1 \le n, m \le 500$$$)Β β the number of rows and columns in the grid, respectively.... | For each test case, print "YES" if it is possible to make all squares in the grid the same color, and "NO" otherwise.
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. | [
[
"8\n2 1\nW\nB\n6 6\nWWWWBW\nWBWWWW\nBBBWWW\nBWWWBB\nWWBWBB\nBBBWBW\n1 1\nW\n2 2\nBB\nBB\n3 4\nBWBW\nWBWB\nBWBW\n4 2\nBB\nBB\nWW\nWW\n4 4\nWWBW\nBBWB\nWWBB\nBBBB\n1 5\nWBBWB",
"NO\nYES\nYES\nYES\nYES\nNO\nYES\nNO"
]
] | In the first example, it is impossible to ever change the color of any square with an operation, so we output NO.
The second example is the case pictured above. As shown in that diagram, it is possible for all squares to be white after two operations, so we output YES.
In the third and fourth examples, all squares ar... | Title: Rectangle Filling
time_limit_ms: 1000
memory_limit_mb: 256
Description: There is an $$$n \times m$$$ grid of white and black squares. In one operation, you can select any two squares of the same color, and color all squares in the subrectangle between them that color.
Formally, if you select positions $$$(x_1, ... |
1983A | https://codeforces.com/problemset/problem/1983/A | Array Divisibility | 800 | [
"constructive algorithms",
"math"
] | Div. 2 | 1,000 | 256 | An array of integers $$$a_1,a_2,\cdots,a_n$$$ is beautiful subject to an integer $$$k$$$ if it satisfies the following:
- The sum of $$$a_{j}$$$ over all $$$j$$$ such that $$$j$$$ is a multiple of $$$k$$$ and $$$1 \le j \le n $$$, itself, is a multiple of $$$k$$$.
- More formally, if $$$\sum_{k | j} a_{j}$$$ is divisi... | 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 and only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$)Β β the size of the array. | For each test case, print the required array as described in the problem statement. | [
[
"3\n3\n6\n7",
"4 22 18\n10 6 15 32 125 54\n23 18 27 36 5 66 7"
]
] | In the second test case, when $$$n = 6$$$, for all integers $$$k$$$ such that $$$1 \le k \le 6$$$, let $$$S$$$ be the set of all indices of the array that are divisible by $$$k$$$.
- When $$$k = 1$$$, $$$S = \{1, 2, 3,4,5,6\}$$$ meaning $$$a_1+a_2+a_3+a_4+a_5+a_6=242$$$ must be divisible by $$$1$$$.
- When $$$k = 2$$$... | Title: Array Divisibility
time_limit_ms: 1000
memory_limit_mb: 256
Description: An array of integers $$$a_1,a_2,\cdots,a_n$$$ is beautiful subject to an integer $$$k$$$ if it satisfies the following:
- The sum of $$$a_{j}$$$ over all $$$j$$$ such that $$$j$$$ is a multiple of $$$k$$$ and $$$1 \le j \le n $$$, itself, ... |
1973D | https://codeforces.com/problemset/problem/1973/D | Cat, Fox and Maximum Array Split | 2,400 | [
"interactive",
"brute force",
"math"
] | Div. 2 | 3,000 | 256 | This is an interactive problem.
Fox gave Cat two positive integers $$$n$$$ and $$$k$$$. She has a hidden array $$$a_1, \ldots , a_n$$$ of length $$$n$$$, such that $$$1 \leq a_i \leq n$$$ for every $$$i$$$. Now they are going to play the following game:
For any two integers $$$l, r$$$ such that $$$1 \leq l \leq r \le... | null | null | [
[
"3\n1 1\n\n1\n2 2\n\n1\n\n3\n\n1\n6 3\n\n7\n\n2\n\n3\n\n6\n\n1",
"! 1\n\n\n? 1 1\n\n? 2 1\n\n! -1\n\n\n? 1 9\n\n? 1 6\n\n? 3 6\n\n? 4 6\n\n! 6"
]
] | The hidden arrays in the three testcases are $$$[1]$$$, $$$[1, 2]$$$ and $$$[1, 3, 6, 1, 2, 1]$$$. In the second testcase, no split satisfies the constraints, so the answer is $$$-1$$$.
The answer for the first query of the third testcase is $$$7$$$ since no valid $$$r$$$ exists. For the second query of the third test... | Title: Cat, Fox and Maximum Array Split
time_limit_ms: 3000
memory_limit_mb: 256
Description: This is an interactive problem.
Fox gave Cat two positive integers $$$n$$$ and $$$k$$$. She has a hidden array $$$a_1, \ldots , a_n$$$ of length $$$n$$$, such that $$$1 \leq a_i \leq n$$$ for every $$$i$$$. Now they are going... |
1992E | https://codeforces.com/problemset/problem/1992/E | Novice's Mistake | 1,700 | [
"brute force",
"implementation",
"math",
"constructive algorithms",
"strings"
] | Div. 3 | 3,000 | 256 | One of the first programming problems by K1o0n looked like this: "Noobish_Monk has $$$n$$$ $$$(1 \le n \le 100)$$$ friends. Each of them gave him $$$a$$$ $$$(1 \le a \le 10000)$$$ apples for his birthday. Delighted with such a gift, Noobish_Monk returned $$$b$$$ $$$(1 \le b \le \min(10000, a \cdot n))$$$ apples to his ... | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) Β β the number of test cases.
For each test case, a single line of input contains an integer $$$n$$$ ($$$1 \le n \le 100$$$).
It is guaranteed that in all test cases, $$$n$$$ is distinct. | For each test case, output the answer in the following format:
In the first line, output the integer $$$x$$$Β β the number of bad tests for the given $$$n$$$.
In the next $$$x$$$ lines, output two integers $$$a_i$$$ and $$$b_i$$$Β β such integers that K1o0n's solution on the test "$$$n$$$ $$$a_i$$$ $$$b_i$$$" gives the... | [
[
"3\n2\n3\n10",
"3\n20 18 \n219 216 \n2218 2214 \n1\n165 162 \n1\n1262 2519"
]
] | In the first example, $$$a = 20$$$, $$$b = 18$$$ are suitable, as "$$$\text{2}$$$" $$$\cdot 20 - 18 =$$$ "$$$\text{22222222222222222222}$$$"$$$- 18 = 22 = 2 \cdot 20 - 18$$$ | Title: Novice's Mistake
time_limit_ms: 3000
memory_limit_mb: 256
Description: One of the first programming problems by K1o0n looked like this: "Noobish_Monk has $$$n$$$ $$$(1 \le n \le 100)$$$ friends. Each of them gave him $$$a$$$ $$$(1 \le a \le 10000)$$$ apples for his birthday. Delighted with such a gift, Noobish_M... |
1956A | https://codeforces.com/problemset/problem/1956/A | Nene's Game | 800 | [
"brute force",
"binary search",
"games",
"data structures",
"greedy"
] | Div. 2 | 1,000 | 256 | Nene invented a new game based on an increasing sequence of integers $$$a_1, a_2, \ldots, a_k$$$.
In this game, initially $$$n$$$ players are lined up in a row. In each of the rounds of this game, the following happens:
- Nene finds the $$$a_1$$$-th, $$$a_2$$$-th, $$$\ldots$$$, $$$a_k$$$-th players in a row. They are... | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 250$$$). The description of test cases follows.
The first line case contains two integers $$$k$$$ and $$$q$$$ ($$$1 \le k, q \le 100$$$)Β β the length of the sequence $$$a$$$ and the number of values $$$n_i$... | For each test case, output $$$q$$$ integers: the $$$i$$$-th ($$$1\le i \le q$$$) of them should be the number of players declared as winners if initially $$$n_i$$$ players join the game. | [
[
"6\n2 1\n3 5\n5\n5 3\n2 4 6 7 9\n1 3 5\n5 4\n3 4 5 6 7\n1 2 3 4\n2 3\n69 96\n1 10 100\n1 1\n100\n50\n3 3\n10 20 30\n1 10 100",
"2 \n1 1 1 \n1 2 2 2 \n1 10 68 \n50 \n1 9 9"
]
] | The first test case was explained in the statement.
In the second test case, when $$$n=1$$$, the only player stays in the game in the first round. After that, the game ends and the only player is declared as a winner. | Title: Nene's Game
time_limit_ms: 1000
memory_limit_mb: 256
Description: Nene invented a new game based on an increasing sequence of integers $$$a_1, a_2, \ldots, a_k$$$.
In this game, initially $$$n$$$ players are lined up in a row. In each of the rounds of this game, the following happens:
- Nene finds the $$$a_1$$... |
1956B | https://codeforces.com/problemset/problem/1956/B | Nene and the Card Game | 800 | [
"games",
"greedy"
] | Div. 2 | 1,000 | 256 | You and Nene are playing a card game. The deck with $$$2n$$$ cards is used to play this game. Each card has an integer from $$$1$$$ to $$$n$$$ on it, and each of integers $$$1$$$ through $$$n$$$ appears exactly on $$$2$$$ cards. Additionally, there is a table where cards are placed during the game (initially, the table... | 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 test cases follows.
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$)Β β the number of cards you and Nene receive in the beginning of the game.
T... | For each test case, output one integer: the maximum number of points you can get. | [
[
"5\n4\n1 1 2 3\n8\n7 4 1 2 8 8 5 5\n8\n7 1 4 5 3 4 2 6\n3\n1 2 3\n1\n1",
"1\n2\n1\n0\n0"
]
] | In the first test case, the integers written on your cards are $$$1$$$, $$$1$$$, $$$2$$$ and $$$3$$$. The integers written on Nene's cards are $$$2$$$, $$$3$$$, $$$4$$$ and $$$4$$$. The game may proceed as follows:
1. You select one of the cards with an integer $$$1$$$ written on it and place it on the table.
2. Nene ... | Title: Nene and the Card Game
time_limit_ms: 1000
memory_limit_mb: 256
Description: You and Nene are playing a card game. The deck with $$$2n$$$ cards is used to play this game. Each card has an integer from $$$1$$$ to $$$n$$$ on it, and each of integers $$$1$$$ through $$$n$$$ appears exactly on $$$2$$$ cards. Additio... |
1956C | https://codeforces.com/problemset/problem/1956/C | Nene's Magical Matrix | 1,600 | [
"constructive algorithms",
"greedy",
"math"
] | Div. 2 | 2,000 | 256 | The magical girl Nene has an $$$n\times n$$$ matrix $$$a$$$ filled with zeroes. The $$$j$$$-th element of the $$$i$$$-th row of matrix $$$a$$$ is denoted as $$$a_{i, j}$$$.
She can perform operations of the following two types with this matrix:
- Type $$$1$$$ operation: choose an integer $$$i$$$ between $$$1$$$ and $... | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). The description of test cases follows.
The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 500$$$)Β β the size of the matrix $$$a$$$.
It is guaranteed that the sum of ... | For each test case, in the first line output two integers $$$s$$$ and $$$m$$$ ($$$0\leq m\leq 2n$$$)Β β the maximum sum of the numbers in the matrix and the number of operations in your solution.
In the $$$k$$$-th of the next $$$m$$$ lines output the description of the $$$k$$$-th operation:
- an integer $$$c$$$ ($$$c ... | [
[
"2\n1\n2",
"1 1\n1 1 1\n7 3\n1 1 1 2\n1 2 1 2\n2 1 1 2"
]
] | In the first test case, the maximum sum $$$s=1$$$ can be obtained in $$$1$$$ operation by setting $$$a_{1, 1}:=1$$$.
In the second test case, the maximum sum $$$s=7$$$ can be obtained in $$$3$$$ operations as follows:
It can be shown that it is impossible to make the sum of the numbers in the matrix larger than $$$7$... | Title: Nene's Magical Matrix
time_limit_ms: 2000
memory_limit_mb: 256
Description: The magical girl Nene has an $$$n\times n$$$ matrix $$$a$$$ filled with zeroes. The $$$j$$$-th element of the $$$i$$$-th row of matrix $$$a$$$ is denoted as $$$a_{i, j}$$$.
She can perform operations of the following two types with this... |
1956D | https://codeforces.com/problemset/problem/1956/D | Nene and the Mex Operator | 2,000 | [
"brute force",
"dp",
"greedy",
"implementation",
"math",
"divide and conquer",
"constructive algorithms",
"bitmasks"
] | Div. 2 | 2,000 | 256 | Nene gave you an array of integers $$$a_1, a_2, \ldots, a_n$$$ of length $$$n$$$.
You can perform the following operation no more than $$$5\cdot 10^5$$$ times (possibly zero):
- Choose two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$, compute $$$x$$$ as $$$\operatorname{MEX}(\{a_l, a_{l+1}, \ldots... | The first line contains an integer $$$n$$$ ($$$1 \le n \le 18$$$)Β β the length of the array $$$a$$$.
The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0\leq a_i \leq 10^7$$$)Β β the array $$$a$$$. | In the first line, output two integers $$$s$$$ and $$$m$$$ ($$$0\le m\le 5\cdot 10^5$$$)Β β the maximum sum of elements of the array $$$a$$$ and the number of operations in your solution.
In the $$$i$$$-th of the following $$$m$$$ lines, output two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$), representing ... | [
[
"2\n0 1",
"4 1\n1 2"
],
[
"3\n1 3 9",
"13 0"
],
[
"4\n1 100 2 1",
"105 2\n3 3\n3 4"
],
[
"1\n0",
"1 1\n1 1"
]
] | In the first example, after the operation with $$$l=1$$$ and $$$r=2$$$ the array $$$a$$$ becomes equal to $$$[2,2]$$$. It can be shown that it is impossible to achieve a larger sum of the elements of $$$a$$$, so the answer is $$$4$$$.
In the second example, the initial sum of elements is $$$13$$$ which can be shown to... | Title: Nene and the Mex Operator
time_limit_ms: 2000
memory_limit_mb: 256
Description: Nene gave you an array of integers $$$a_1, a_2, \ldots, a_n$$$ of length $$$n$$$.
You can perform the following operation no more than $$$5\cdot 10^5$$$ times (possibly zero):
- Choose two integers $$$l$$$ and $$$r$$$ such that $$$... |
1967A | https://codeforces.com/problemset/problem/1967/A | Permutation Counting | 1,400 | [
"math",
"binary search",
"implementation",
"sortings",
"greedy"
] | Div. 1 | 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 $... | 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 ... | 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$$$,... | 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 whi... |
1967B1 | https://codeforces.com/problemset/problem/1967/B1 | Reverse Card (Easy Version) | 1,400 | [
"brute force",
"math",
"number theory"
] | Div. 1 | 2,000 | 256 | The two versions are different problems. You may want to read both versions. You can make hacks only if both versions are solved.
You are given two positive integers $$$n$$$, $$$m$$$.
Calculate the number of ordered pairs $$$(a, b)$$$ satisfying the following conditions:
- $$$1\le a\le n$$$, $$$1\le b\le m$$$;
- $$$... | 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$$$ ($$$1\le n,m\le 2 \cdot 10^6$$$).
It is guaranteed that neither the sum of $$$n$$$... | For each test case, print a single integer: the number of valid pairs. | [
[
"6\n1 1\n2 3\n3 5\n10 8\n100 1233\n1000000 1145141",
"1\n3\n4\n14\n153\n1643498"
]
] | In the first test case, only $$$(1,1)$$$ satisfies the conditions.
In the fourth test case, $$$(1,1),(2,1),(2,2),(3,1),(4,1),(5,1),(6,1),(6,2),(6,3),(7,1),(8,1),(9,1),(10,1),(10,2)$$$ satisfy the conditions. | Title: Reverse Card (Easy Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: The two versions are different problems. You may want to read both versions. You can make hacks only if both versions are solved.
You are given two positive integers $$$n$$$, $$$m$$$.
Calculate the number of ordered pairs $$$(a, ... |
1967B2 | https://codeforces.com/problemset/problem/1967/B2 | Reverse Card (Hard Version) | 2,200 | [
"brute force",
"math",
"number theory"
] | Div. 1 | 2,000 | 256 | The two versions are different problems. You may want to read both versions. You can make hacks only if both versions are solved.
You are given two positive integers $$$n$$$, $$$m$$$.
Calculate the number of ordered pairs $$$(a, b)$$$ satisfying the following conditions:
- $$$1\le a\le n$$$, $$$1\le b\le m$$$;
- $$$... | 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$$$ ($$$1\le n,m\le 2 \cdot 10^6$$$).
It is guaranteed that neither the sum of $$$n$$$... | For each test case, print a single integer: the number of valid pairs. | [
[
"6\n1 1\n2 3\n3 5\n10 8\n100 1233\n1000000 1145141",
"0\n1\n1\n6\n423\n5933961"
]
] | In the first test case, no pair satisfies the conditions.
In the fourth test case, $$$(2,2),(3,6),(4,4),(6,3),(6,6),(8,8)$$$ satisfy the conditions. | Title: Reverse Card (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: The two versions are different problems. You may want to read both versions. You can make hacks only if both versions are solved.
You are given two positive integers $$$n$$$, $$$m$$$.
Calculate the number of ordered pairs $$$(a, ... |
1998B | https://codeforces.com/problemset/problem/1998/B | Minimize Equal Sum Subarrays | 1,000 | [
"constructive algorithms",
"math",
"number theory"
] | Div. 2 | 1,500 | 256 | You are given a permutation$$$^{\text{β}}$$$ $$$p$$$ of length $$$n$$$.
Find a permutation $$$q$$$ of length $$$n$$$ that minimizes the number of pairs ($$$i, j$$$) ($$$1 \leq i \leq j \leq n$$$) such that $$$p_i + p_{i+1} + \ldots + p_j = q_i + q_{i+1} + \ldots + q_j$$$. | 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$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$).
The following line contains $$$n$$$ space-separated integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \leq p_i \leq n$$$)Β β denoting the permutation $$$... | For each test case, output one line containing any permutation of length $$$n$$$ (the permutation $$$q$$$) such that $$$q$$$ minimizes the number of pairs. | [
[
"3\n2\n1 2\n5\n1 2 3 4 5\n7\n4 7 5 1 2 6 3",
"2 1\n3 5 4 2 1\n6 2 1 4 7 3 5"
]
] | For the first test, there exists only one pair ($$$i, j$$$) ($$$1 \leq i \leq j \leq n$$$) such that $$$p_i + p_{i+1} + \ldots + p_j = q_i + q_{i+1} + \ldots + q_j$$$, which is ($$$1, 2$$$). It can be proven that no such $$$q$$$ exists for which there are no pairs. | Title: Minimize Equal Sum Subarrays
time_limit_ms: 1500
memory_limit_mb: 256
Description: You are given a permutation$$$^{\text{β}}$$$ $$$p$$$ of length $$$n$$$.
Find a permutation $$$q$$$ of length $$$n$$$ that minimizes the number of pairs ($$$i, j$$$) ($$$1 \leq i \leq j \leq n$$$) such that $$$p_i + p_{i+1} + \ldo... |
2001C | https://codeforces.com/problemset/problem/2001/C | Guess The Tree | 1,500 | [
"brute force",
"binary search",
"divide and conquer",
"dfs and similar",
"dsu",
"interactive",
"greedy",
"trees"
] | Div. 2 | 2,000 | 256 | This is an interactive problem.
Misuki has chosen a secret tree with $$$n$$$ nodes, indexed from $$$1$$$ to $$$n$$$, and asked you to guess it by using queries of the following type:
- "? a b"Β β Misuki will tell you which node $$$x$$$ minimizes $$$|d(a,x) - d(b,x)|$$$, where $$$d(x,y)$$$ is the distance between nodes... | Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 200$$$)Β β the number of test cases.
Each test case consists of a single line with an integer $$$n$$$ ($$$2 \le n \le 1000$$$), the number of nodes in the tree.
It is guaranteed that the sum of $$$n$$$ across al... | null | [
[
"1\n4\n1\n1\n3",
"? 1 2\n\n? 1 3\n\n? 1 4\n\n! 1 2 1 3 3 4"
]
] | A tree is an undirected acyclic connected graph. A tree with $$$n$$$ nodes will always have $$$n-1$$$ edges.
In the example case, the answer to "? 1 2" is $$$1$$$. This means that there is an edge between nodes $$$1$$$ and $$$2$$$.
The answer to "? 1 3" is $$$1$$$. This means that there is an edge between nodes $$$1$... | Title: Guess The Tree
time_limit_ms: 2000
memory_limit_mb: 256
Description: This is an interactive problem.
Misuki has chosen a secret tree with $$$n$$$ nodes, indexed from $$$1$$$ to $$$n$$$, and asked you to guess it by using queries of the following type:
- "? a b"Β β Misuki will tell you which node $$$x$$$ minimiz... |
2034A | https://codeforces.com/problemset/problem/2034/A | King Keykhosrow's Mystery | 800 | [
"brute force",
"chinese remainder theorem",
"math",
"number theory"
] | Codeforces Round 989, Div. 1 + Div. 2 | 1,000 | 256 | There is a tale about the wise King Keykhosrow who owned a grand treasury filled with treasures from across the Persian Empire. However, to prevent theft and ensure the safety of his wealth, King Keykhosrow's vault was sealed with a magical lock that could only be opened by solving a riddle.
The riddle involves two sa... | The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$), the number of test cases.
Each test case consists of a single line containing two integers $$$a$$$ and $$$b$$$ ($$$1 \leq a, b \leq 1000$$$). | For each test case, print the smallest integer $$$m$$$ that satisfies the conditions above. | [
[
"2\n4 6\n472 896",
"12\n52864"
]
] | In the first test case, you can see that:
- $$$4 \bmod 4 = 0$$$ but $$$4 \bmod 6 = 4$$$
- $$$5 \bmod 4 = 1$$$ but $$$5 \bmod 6 = 5$$$
- $$$6 \bmod 4 = 2$$$ but $$$6 \bmod 6 = 0$$$
- $$$7 \bmod 4 = 3$$$ but $$$7 \bmod 6 = 1$$$
- $$$8 \bmod 4 = 0$$$ but $$$8 \bmod 6 = 2$$$
- $$$9 \bmod 4 = 1$$$ but $$$9 \bmod 6 = 3$$$
-... | Title: King Keykhosrow's Mystery
time_limit_ms: 1000
memory_limit_mb: 256
Description: There is a tale about the wise King Keykhosrow who owned a grand treasury filled with treasures from across the Persian Empire. However, to prevent theft and ensure the safety of his wealth, King Keykhosrow's vault was sealed with a ... |
2034B | https://codeforces.com/problemset/problem/2034/B | Rakhsh's Revival | 1,000 | [
"two pointers",
"data structures",
"greedy",
"implementation"
] | Codeforces Round 989, Div. 1 + Div. 2 | 1,000 | 256 | Rostam's loyal horse, Rakhsh, has seen better days. Once powerful and fast, Rakhsh has grown weaker over time, struggling to even move. Rostam worries that if too many parts of Rakhsh's body lose strength at once, Rakhsh might stop entirely. To keep his companion going, Rostam decides to strengthen Rakhsh, bit by bit, ... | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$), the number of test cases.
The first line of each test case contains three numbers $$$n$$$, $$$m$$$, $$$k$$$ ($$$1 \le m, k \le n \le 2 \cdot 10^5$$$). The second line of each test case contains a binary string $$$s$$$ of $$$n$$$ characters $$$s_1s_2 ... | For each test case, output the minimum number of times Rostam needs to use Timar to keep Rakhsh moving, ensuring there are no consecutive entirely weak spots of length $$$m$$$. | [
[
"3\n5 1 1\n10101\n5 2 1\n10101\n6 3 2\n000000",
"2\n0\n1"
]
] | In the first test case, we should apply an operation on each 0.
In the second test case, $$$s$$$ is already ok.
In the third test case, we can perform an operation on interval $$$[3,4]$$$ to get 001100. | Title: Rakhsh's Revival
time_limit_ms: 1000
memory_limit_mb: 256
Description: Rostam's loyal horse, Rakhsh, has seen better days. Once powerful and fast, Rakhsh has grown weaker over time, struggling to even move. Rostam worries that if too many parts of Rakhsh's body lose strength at once, Rakhsh might stop entirely. ... |
2034C | https://codeforces.com/problemset/problem/2034/C | Trapped in the Witch's Labyrinth | 1,400 | [
"constructive algorithms",
"dfs and similar",
"graphs",
"implementation"
] | Codeforces Round 989, Div. 1 + Div. 2 | 3,000 | 256 | In the fourth labor of Rostam, the legendary hero from the Shahnameh, an old witch has created a magical maze to trap him. The maze is a rectangular grid consisting of $$$n$$$ rows and $$$m$$$ columns. Each cell in the maze points in a specific direction: up, down, left, or right. The witch has enchanted Rostam so that... | The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$), the number of test cases.
For each test case:
- The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 1000$$$), representing the number of rows and columns in the maze.
- Each of the next $$$n$$$ lines contains... | For each test case, print a single integer, the maximum number of starting cells from which Rostam will be trapped forever after assigning directions to the unspecified cells optimally. | [
[
"3\n3 3\nUUU\nL?R\nDDD\n2 3\n???\n???\n3 3\n?U?\nR?L\nRDL",
"0\n6\n5"
]
] | In the first test case, all of the cells will be good no matter what you do.
In the second test case, if you assign the ?s like the picture below, all of the cells will be bad:
In the third test case, if you assign the ?s like the picture below, you will have $$$5$$$ bad cells (red-shaded cells): | Title: Trapped in the Witch's Labyrinth
time_limit_ms: 3000
memory_limit_mb: 256
Description: In the fourth labor of Rostam, the legendary hero from the Shahnameh, an old witch has created a magical maze to trap him. The maze is a rectangular grid consisting of $$$n$$$ rows and $$$m$$$ columns. Each cell in the maze po... |
2034D | https://codeforces.com/problemset/problem/2034/D | Darius' Wisdom | 1,600 | [
"constructive algorithms",
"greedy",
"implementation",
"sortings"
] | Codeforces Round 989, Div. 1 + Div. 2 | 2,000 | 256 | Darius the Great is constructing $$$n$$$ stone columns, each consisting of a base and between $$$0$$$, $$$1$$$, or $$$2$$$ inscription pieces stacked on top.
In each move, Darius can choose two columns $$$u$$$ and $$$v$$$ such that the difference in the number of inscriptions between these columns is exactly $$$1$$$, ... | The first line contains an integer $$$t$$$ β the number of test cases. ($$$1 \leq t \leq 3000$$$)
The first line of each test case contains an integer $$$n$$$ β the number of stone columns. ($$$1 \leq n \leq 2 \cdot 10^5$$$)
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, where $$$a_i \in \{0,1... | For each test case, output an integer $$$k$$$ β the number of moves used to sort the columns. ($$$0 \leq k \leq n$$$)
Then, output $$$k$$$ lines, each containing two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \leq u_i, v_i \leq n$$$), representing the indices of the columns involved in the $$$i$$$-th move. During each mov... | [
[
"3\n4\n0 2 0 1\n3\n1 2 0\n6\n0 1 1 2 2 2",
"2\n2 4\n2 3\n2\n3 1\n2 3\n0"
]
] | Columns state in the first test case:
- Initial: $$$0, 2, 0, 1$$$
- After the first move: $$$0, 1, 0, 2$$$
- After the second move: $$$0, 0, 1, 2$$$
Columns state in the second test case:
- Initial: $$$1, 2, 0$$$
- After the first move: $$$0, 2, 1$$$
- After the second move: $$$0, 1, 2$$$
In the third test case, th... | Title: Darius' Wisdom
time_limit_ms: 2000
memory_limit_mb: 256
Description: Darius the Great is constructing $$$n$$$ stone columns, each consisting of a base and between $$$0$$$, $$$1$$$, or $$$2$$$ inscription pieces stacked on top.
In each move, Darius can choose two columns $$$u$$$ and $$$v$$$ such that the differe... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.