problem_id
stringlengths 5
6
| url
stringlengths 48
49
| title
stringlengths 2
46
| rating
int64 800
3.5k
| tags
sequencelengths 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
sequencelengths 1
5
| note
stringlengths 0
1.68k
| prompt
stringlengths 505
6.96k
|
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2006A
|
https://codeforces.com/problemset/problem/2006/A
|
Iris and Game on the Tree
| 1,700
|
[
"constructive algorithms",
"dfs and similar",
"games",
"graphs",
"greedy",
"trees"
] |
Div. 1
| 2,000
| 256
|
Iris has a tree rooted at vertex $1$. Each vertex has a value of $\mathtt 0$ or $\mathtt 1$.
Let's consider a leaf of the tree (the vertex $1$ is never considered a leaf) and define its weight. Construct a string formed by the values of the vertices on the path starting at the root and ending in this leaf. Then the weight of the leaf is the difference between the number of occurrences of $\mathtt{10}$ and $\mathtt{01}$ substrings in it.
Take the following tree as an example. Green vertices have a value of $\mathtt 1$ while white vertices have a value of $\mathtt 0$.

* Let's calculate the weight of the leaf $5$: the formed string is $\mathtt{10110}$. The number of occurrences of substring $\mathtt{10}$ is $2$, the number of occurrences of substring $\mathtt{01}$ is $1$, so the difference is $2 - 1 = 1$. * Let's calculate the weight of the leaf $6$: the formed string is $\mathtt{101}$. The number of occurrences of substring $\mathtt{10}$ is $1$, the number of occurrences of substring $\mathtt{01}$ is $1$, so the difference is $1 - 1 = 0$.
The score of a tree is defined as the number of leaves with non-zero weight in the tree.
But the values of some vertices haven't been decided and will be given to you as $\texttt{?}$. Filling the blanks would be so boring, so Iris is going to invite Dora to play a game. On each turn, one of the girls chooses any of the remaining vertices with value $\texttt{?}$ and changes its value to $\mathtt{0}$ or $\mathtt{1}$, with Iris going first. The game continues until there are no vertices with value $\mathtt{?}$ left in the tree. Iris aims to maximize the score of the tree, while Dora aims to minimize that.
Assuming that both girls play optimally, please determine the final score of the tree.
|
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 5\cdot 10^4$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$) β the number of vertices in the tree.
The following $n - 1$ lines each contain two integers $u$ and $v$ ($1 \leq u, v \leq n$) β denoting an edge between vertices $u$ and $v$.
It's guaranteed that the given edges form a tree.
The last line contains a string $s$ of length $n$. The $i$-th character of $s$ represents the value of vertex $i$. It's guaranteed that $s$ only contains characters $\mathtt{0}$, $\mathtt{1}$ and $\mathtt{?}$.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2\cdot 10^5$.
|
For each test case, output a single integer β the final score of the tree.
|
[
[
"6\n4\n1 2\n1 3\n4 1\n0101\n4\n1 2\n3 2\n2 4\n???0\n5\n1 2\n1 3\n2 4\n2 5\n?1?01\n6\n1 2\n2 3\n3 4\n5 3\n3 6\n?0????\n5\n1 2\n1 3\n1 4\n1 5\n11?1?\n2\n2 1\n??",
"2\n1\n1\n2\n1\n0"
]
] |
In the first test case, all the values of the vertices have been determined. There are three different paths from the root to a leaf:
* From vertex $1$ to vertex $2$. The string formed by the path is $\mathtt{01}$, so the weight of the leaf is $0-1=-1$. * From vertex $1$ to vertex $3$. The string formed by the path is $\mathtt{00}$, so the weight of the leaf is $0-0=0$. * From vertex $1$ to vertex $4$. The string formed by the path is $\mathtt{01}$, so the weight of the leaf is $0-1=-1$.
Thus, there are two leaves with non-zero weight, so the score of the tree is $2$.
In the second test case, one of the sequences of optimal choices for the two players can be:
* Iris chooses to change the value of the vertex $3$ to $\mathtt 1$. * Dora chooses to change the value of the vertex $1$ to $\mathtt 0$. * Iris chooses to change the value of the vertex $2$ to $\mathtt 0$.
The final tree is as follows:

The only leaf with non-zero weight is $3$, so the score of the tree is $1$. Note that this may not be the only sequence of optimal choices for Iris and Dora.
|
Title: Iris and Game on the Tree
time_limit_ms: 2000
memory_limit_mb: 256
Description: Iris has a tree rooted at vertex $1$. Each vertex has a value of $\mathtt 0$ or $\mathtt 1$.
Let's consider a leaf of the tree (the vertex $1$ is never considered a leaf) and define its weight. Construct a string formed by the values of the vertices on the path starting at the root and ending in this leaf. Then the weight of the leaf is the difference between the number of occurrences of $\mathtt{10}$ and $\mathtt{01}$ substrings in it.
Take the following tree as an example. Green vertices have a value of $\mathtt 1$ while white vertices have a value of $\mathtt 0$.

* Let's calculate the weight of the leaf $5$: the formed string is $\mathtt{10110}$. The number of occurrences of substring $\mathtt{10}$ is $2$, the number of occurrences of substring $\mathtt{01}$ is $1$, so the difference is $2 - 1 = 1$. * Let's calculate the weight of the leaf $6$: the formed string is $\mathtt{101}$. The number of occurrences of substring $\mathtt{10}$ is $1$, the number of occurrences of substring $\mathtt{01}$ is $1$, so the difference is $1 - 1 = 0$.
The score of a tree is defined as the number of leaves with non-zero weight in the tree.
But the values of some vertices haven't been decided and will be given to you as $\texttt{?}$. Filling the blanks would be so boring, so Iris is going to invite Dora to play a game. On each turn, one of the girls chooses any of the remaining vertices with value $\texttt{?}$ and changes its value to $\mathtt{0}$ or $\mathtt{1}$, with Iris going first. The game continues until there are no vertices with value $\mathtt{?}$ left in the tree. Iris aims to maximize the score of the tree, while Dora aims to minimize that.
Assuming that both girls play optimally, please determine the final score of the tree.
Input: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 5\cdot 10^4$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$) β the number of vertices in the tree.
The following $n - 1$ lines each contain two integers $u$ and $v$ ($1 \leq u, v \leq n$) β denoting an edge between vertices $u$ and $v$.
It's guaranteed that the given edges form a tree.
The last line contains a string $s$ of length $n$. The $i$-th character of $s$ represents the value of vertex $i$. It's guaranteed that $s$ only contains characters $\mathtt{0}$, $\mathtt{1}$ and $\mathtt{?}$.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2\cdot 10^5$.
Output: For each test case, output a single integer β the final score of the tree.
Examples:
input:
6
4
1 2
1 3
4 1
0101
4
1 2
3 2
2 4
???0
5
1 2
1 3
2 4
2 5
?1?01
6
1 2
2 3
3 4
5 3
3 6
?0????
5
1 2
1 3
1 4
1 5
11?1?
2
2 1
??
output:
2
1
1
2
1
0
Note: In the first test case, all the values of the vertices have been determined. There are three different paths from the root to a leaf:
* From vertex $1$ to vertex $2$. The string formed by the path is $\mathtt{01}$, so the weight of the leaf is $0-1=-1$. * From vertex $1$ to vertex $3$. The string formed by the path is $\mathtt{00}$, so the weight of the leaf is $0-0=0$. * From vertex $1$ to vertex $4$. The string formed by the path is $\mathtt{01}$, so the weight of the leaf is $0-1=-1$.
Thus, there are two leaves with non-zero weight, so the score of the tree is $2$.
In the second test case, one of the sequences of optimal choices for the two players can be:
* Iris chooses to change the value of the vertex $3$ to $\mathtt 1$. * Dora chooses to change the value of the vertex $1$ to $\mathtt 0$. * Iris chooses to change the value of the vertex $2$ to $\mathtt 0$.
The final tree is as follows:

The only leaf with non-zero weight is $3$, so the score of the tree is $1$. Note that this may not be the only sequence of optimal choices for Iris and Dora.
|
2006B
|
https://codeforces.com/problemset/problem/2006/B
|
Iris and the Tree
| 1,800
|
[
"brute force",
"data structures",
"dfs and similar",
"dsu",
"math",
"trees"
] |
Div. 1
| 3,000
| 256
|
Given a rooted tree with the root at vertex $1$. For any vertex $i$ ($1 < i \leq n$) in the tree, there is an edge connecting vertices $i$ and $p_i$ ($1 \leq p_i < i$), with a weight equal to $t_i$.
Iris does not know the values of $t_i$, but she knows that $\displaystyle\sum_{i=2}^n t_i = w$ and each of the $t_i$ is a non- negative integer.
The vertices of the tree are numbered in a special way: the numbers of the vertices in each subtree are consecutive integers. In other words, the vertices of the tree are numbered in the order of a depth-first search.
 The tree in this picture satisfies the condition. For example, in the subtree of vertex $2$, the vertex numbers are $2, 3, 4, 5$, which are consecutive integers.  The tree in this picture does not satisfy the condition, as in the subtree of vertex $2$, the vertex numbers $2$ and $4$ are not consecutive integers.
We define $\operatorname{dist}(u, v)$ as the length of the simple path between vertices $u$ and $v$ in the tree.
Next, there will be $n - 1$ events:
* Iris is given integers $x$ and $y$, indicating that $t_x = y$.
After each event, Iris wants to know the maximum possible value of $\operatorname{dist}(i, i \bmod n + 1)$ independently for each $i$ ($1\le i\le n$). She only needs to know the sum of these $n$ values. Please help Iris quickly get the answers.
Note that when calculating the maximum possible values of $\operatorname{dist}(i, i \bmod n + 1)$ and $\operatorname{dist}(j, j \bmod n + 1)$ for $i \ne j$, the unknown edge weights may be different.
|
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $n$ and $w$ ($2 \le n \le 2 \cdot 10^5$, $0 \leq w \leq 10^{12}$) β the number of vertices in the tree and the sum of the edge weights.
The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \leq p_i < i$) β the description of the edges of the tree.
Then follow $n-1$ lines indicating the events. Each line contains two integers $x$ and $y$ ($2 \leq x \leq n$, $0 \leq y \leq w$), indicating that $t_x = y$.
It is guaranteed that all $x$ in the events are distinct. It is also guaranteed that the sum of all $y$ equals $w$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output one line containing $n-1$ integers, each representing the answer after each event.
|
[
[
"4\n2 1000000000000\n1\n2 1000000000000\n4 9\n1 1 1\n2 2\n4 4\n3 3\n6 100\n1 2 3 2 1\n6 17\n3 32\n2 4\n4 26\n5 21\n10 511\n1 2 2 4 2 1 1 8 8\n3 2\n6 16\n10 256\n9 128\n2 1\n5 8\n8 64\n4 4\n7 32",
"2000000000000\n25 18 18\n449 302 247 200 200\n4585 4473 2681 1567 1454 1322 1094 1022 1022"
]
] |
In the first test case, $\operatorname{dist}(1, 2) = \operatorname{dist}(2, 1) = t_2 = w = 10^{12}$, so $\operatorname{dist}(1, 2) + \operatorname{dist}(2, 1) = 2 \cdot 10^{12}$.
In the second test case, the tree after Iris found out all $t_x$ is shown below:

$\operatorname{dist}(1, 2) = t_2$, $\operatorname{dist}(2, 3) = t_2 + t_3$, $\operatorname{dist}(3, 4) = t_3 + t_4$, $\operatorname{dist}(4, 1) = t_4$. After the first event, she found out that $t_2 = 2$, so $\operatorname{dist}(1, 2) = 2$. At the same time:
* $\operatorname{dist}(2, 3)$ is maximized if $t_3 = 7$, $t_4 = 0$. Then $\operatorname{dist}(2, 3) = 9$. * $\operatorname{dist}(3, 4)$ and $\operatorname{dist}(4, 1)$ are maximized if $t_3 = 0$, $t_4 = 7$. Then $\operatorname{dist}(3, 4) = \operatorname{dist}(4, 1) = 7$.
Thus, the answer is $2 + 9 + 7 + 7 = 25$.
After the second event, she found out that $t_4 = 4$, then $t_3 = w - t_2 - t_4 = 4$. $\operatorname{dist}(1, 2) = 2$, $\operatorname{dist}(2, 3) = 2 + 3 = 5$, $\operatorname{dist}(3, 4) = 3 + 4 = 7$, $\operatorname{dist}(4, 1) = 4$. Thus, the answer is $2 + 5 + 7 + 4 = 18$.
|
Title: Iris and the Tree
time_limit_ms: 3000
memory_limit_mb: 256
Description: Given a rooted tree with the root at vertex $1$. For any vertex $i$ ($1 < i \leq n$) in the tree, there is an edge connecting vertices $i$ and $p_i$ ($1 \leq p_i < i$), with a weight equal to $t_i$.
Iris does not know the values of $t_i$, but she knows that $\displaystyle\sum_{i=2}^n t_i = w$ and each of the $t_i$ is a non- negative integer.
The vertices of the tree are numbered in a special way: the numbers of the vertices in each subtree are consecutive integers. In other words, the vertices of the tree are numbered in the order of a depth-first search.
 The tree in this picture satisfies the condition. For example, in the subtree of vertex $2$, the vertex numbers are $2, 3, 4, 5$, which are consecutive integers.  The tree in this picture does not satisfy the condition, as in the subtree of vertex $2$, the vertex numbers $2$ and $4$ are not consecutive integers.
We define $\operatorname{dist}(u, v)$ as the length of the simple path between vertices $u$ and $v$ in the tree.
Next, there will be $n - 1$ events:
* Iris is given integers $x$ and $y$, indicating that $t_x = y$.
After each event, Iris wants to know the maximum possible value of $\operatorname{dist}(i, i \bmod n + 1)$ independently for each $i$ ($1\le i\le n$). She only needs to know the sum of these $n$ values. Please help Iris quickly get the answers.
Note that when calculating the maximum possible values of $\operatorname{dist}(i, i \bmod n + 1)$ and $\operatorname{dist}(j, j \bmod n + 1)$ for $i \ne j$, the unknown edge weights may be different.
Input: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $n$ and $w$ ($2 \le n \le 2 \cdot 10^5$, $0 \leq w \leq 10^{12}$) β the number of vertices in the tree and the sum of the edge weights.
The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \leq p_i < i$) β the description of the edges of the tree.
Then follow $n-1$ lines indicating the events. Each line contains two integers $x$ and $y$ ($2 \leq x \leq n$, $0 \leq y \leq w$), indicating that $t_x = y$.
It is guaranteed that all $x$ in the events are distinct. It is also guaranteed that the sum of all $y$ equals $w$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output one line containing $n-1$ integers, each representing the answer after each event.
Examples:
input:
4
2 1000000000000
1
2 1000000000000
4 9
1 1 1
2 2
4 4
3 3
6 100
1 2 3 2 1
6 17
3 32
2 4
4 26
5 21
10 511
1 2 2 4 2 1 1 8 8
3 2
6 16
10 256
9 128
2 1
5 8
8 64
4 4
7 32
output:
2000000000000
25 18 18
449 302 247 200 200
4585 4473 2681 1567 1454 1322 1094 1022 1022
Note: In the first test case, $\operatorname{dist}(1, 2) = \operatorname{dist}(2, 1) = t_2 = w = 10^{12}$, so $\operatorname{dist}(1, 2) + \operatorname{dist}(2, 1) = 2 \cdot 10^{12}$.
In the second test case, the tree after Iris found out all $t_x$ is shown below:

$\operatorname{dist}(1, 2) = t_2$, $\operatorname{dist}(2, 3) = t_2 + t_3$, $\operatorname{dist}(3, 4) = t_3 + t_4$, $\operatorname{dist}(4, 1) = t_4$. After the first event, she found out that $t_2 = 2$, so $\operatorname{dist}(1, 2) = 2$. At the same time:
* $\operatorname{dist}(2, 3)$ is maximized if $t_3 = 7$, $t_4 = 0$. Then $\operatorname{dist}(2, 3) = 9$. * $\operatorname{dist}(3, 4)$ and $\operatorname{dist}(4, 1)$ are maximized if $t_3 = 0$, $t_4 = 7$. Then $\operatorname{dist}(3, 4) = \operatorname{dist}(4, 1) = 7$.
Thus, the answer is $2 + 9 + 7 + 7 = 25$.
After the second event, she found out that $t_4 = 4$, then $t_3 = w - t_2 - t_4 = 4$. $\operatorname{dist}(1, 2) = 2$, $\operatorname{dist}(2, 3) = 2 + 3 = 5$, $\operatorname{dist}(3, 4) = 3 + 4 = 7$, $\operatorname{dist}(4, 1) = 4$. Thus, the answer is $2 + 5 + 7 + 4 = 18$.
|
2006C
|
https://codeforces.com/problemset/problem/2006/C
|
Eri and Expanded Sets
| 2,300
|
[
"data structures",
"divide and conquer",
"math",
"number theory",
"two pointers"
] |
Div. 1
| 3,000
| 512
|
Let there be a set that contains distinct positive integers. To expand the set to contain as many integers as possible, Eri can choose two integers $x\neq y$ from the set such that their average $\frac{x+y}2$ is still a positive integer and isn't contained in the set, and add it to the set. The integers $x$ and $y$ remain in the set.
Let's call the set of integers consecutive if, after the elements are sorted, the difference between any pair of adjacent elements is $1$. For example, sets $\\{2\\}$, $\\{2, 5, 4, 3\\}$, $\\{5, 6, 8, 7\\}$ are consecutive, while $\\{2, 4, 5, 6\\}$, $\\{9, 7\\}$ are not.
Eri likes consecutive sets. Suppose there is an array $b$, then Eri puts all elements in $b$ into the set. If after a finite number of operations described above, the set can become consecutive, the array $b$ will be called brilliant.
Note that if the same integer appears in the array multiple times, we only put it into the set once, as a set always contains distinct positive integers.
Eri has an array $a$ of $n$ positive integers. Please help him to count the number of pairs of integers $(l,r)$ such that $1 \leq l \leq r \leq n$ and the subarray $a_l, a_{l+1}, \ldots, a_r$ is brilliant.
|
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 4 \cdot 10^5$) β length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots a_n$ ($1 \leq a_i \leq 10^9$) β the elements of the array $a$.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $4 \cdot 10^5$.
|
For each test case, output a single integer β the number of brilliant subarrays.
|
[
[
"6\n2\n2 2\n6\n1 3 6 10 15 21\n5\n6 30 18 36 9\n1\n1000000000\n6\n1 1 4 5 1 4\n12\n70 130 90 90 90 108 612 500 451 171 193 193",
"3\n18\n5\n1\n18\n53"
]
] |
In the first test case, the array $a = [2, 2]$ has $3$ subarrays: $[2]$, $[2]$, and $[2, 2]$. For all of them, the set only contains a single integer $2$, therefore it's always consecutive. All these subarrays are brilliant, so the answer is $3$.
In the second test case, let's consider the subarray $[3, 6, 10]$. We can do operations as follows:
$$\\{3,6,10\\} \xrightarrow{x=6,y=10} \\{3,6,8,10\\} \xrightarrow{x=6,y=8} \\{3,6,7,8,10\\} \xrightarrow{x=3,y=7} \\{3,5,6,7,8,10\\}$$ $$\xrightarrow{x=3,y=5} \\{3,4,5,6,7,8,10\\} \xrightarrow{x=8,y=10} \\{3,4,5,6,7,8,9,10\\}$$
$\\{3,4,5,6,7,8,9,10\\}$ is a consecutive set, so the subarray $[3, 6, 10]$ is brilliant.
|
Title: Eri and Expanded Sets
time_limit_ms: 3000
memory_limit_mb: 512
Description: Let there be a set that contains distinct positive integers. To expand the set to contain as many integers as possible, Eri can choose two integers $x\neq y$ from the set such that their average $\frac{x+y}2$ is still a positive integer and isn't contained in the set, and add it to the set. The integers $x$ and $y$ remain in the set.
Let's call the set of integers consecutive if, after the elements are sorted, the difference between any pair of adjacent elements is $1$. For example, sets $\\{2\\}$, $\\{2, 5, 4, 3\\}$, $\\{5, 6, 8, 7\\}$ are consecutive, while $\\{2, 4, 5, 6\\}$, $\\{9, 7\\}$ are not.
Eri likes consecutive sets. Suppose there is an array $b$, then Eri puts all elements in $b$ into the set. If after a finite number of operations described above, the set can become consecutive, the array $b$ will be called brilliant.
Note that if the same integer appears in the array multiple times, we only put it into the set once, as a set always contains distinct positive integers.
Eri has an array $a$ of $n$ positive integers. Please help him to count the number of pairs of integers $(l,r)$ such that $1 \leq l \leq r \leq n$ and the subarray $a_l, a_{l+1}, \ldots, a_r$ is brilliant.
Input: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 4 \cdot 10^5$) β length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots a_n$ ($1 \leq a_i \leq 10^9$) β the elements of the array $a$.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $4 \cdot 10^5$.
Output: For each test case, output a single integer β the number of brilliant subarrays.
Examples:
input:
6
2
2 2
6
1 3 6 10 15 21
5
6 30 18 36 9
1
1000000000
6
1 1 4 5 1 4
12
70 130 90 90 90 108 612 500 451 171 193 193
output:
3
18
5
1
18
53
Note: In the first test case, the array $a = [2, 2]$ has $3$ subarrays: $[2]$, $[2]$, and $[2, 2]$. For all of them, the set only contains a single integer $2$, therefore it's always consecutive. All these subarrays are brilliant, so the answer is $3$.
In the second test case, let's consider the subarray $[3, 6, 10]$. We can do operations as follows:
$$\\{3,6,10\\} \xrightarrow{x=6,y=10} \\{3,6,8,10\\} \xrightarrow{x=6,y=8} \\{3,6,7,8,10\\} \xrightarrow{x=3,y=7} \\{3,5,6,7,8,10\\}$$ $$\xrightarrow{x=3,y=5} \\{3,4,5,6,7,8,10\\} \xrightarrow{x=8,y=10} \\{3,4,5,6,7,8,9,10\\}$$
$\\{3,4,5,6,7,8,9,10\\}$ is a consecutive set, so the subarray $[3, 6, 10]$ is brilliant.
|
2018A
|
https://codeforces.com/problemset/problem/2018/A
|
Cards Partition
| 1,600
|
[
"2-sat",
"brute force",
"greedy",
"implementation",
"math"
] |
Div. 1
| 2,000
| 256
|
[DJ Genki vs Gram - Einherjar Joker](https://soundcloud.com/leon- hwang-368077289/einherjar-joker-dj-genki-vs-gram)
β
You have some cards. An integer between $1$ and $n$ is written on each card: specifically, for each $i$ from $1$ to $n$, you have $a_i$ cards which have the number $i$ written on them.
There is also a shop which contains unlimited cards of each type. You have $k$ coins, so you can buy at most $k$ new cards in total, and the cards you buy can contain any integer between $\mathbf{1}$ and $\mathbf{n}$, inclusive.
After buying the new cards, you must partition all your cards into decks, according to the following rules:
* all the decks must have the same size; * there are no pairs of cards with the same value in the same deck.
Find the maximum possible size of a deck after buying cards and partitioning them optimally.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains two integers $n$, $k$ ($1 \leq n \leq 2 \cdot 10^5$, $0 \leq k \leq 10^{16}$) β the number of distinct types of cards and the number of coins.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq 10^{10}$, $\sum a_i \geq 1$) β the number of cards of type $i$ you have at the beginning, for each $1 \leq i \leq n$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output a single integer: the maximum possible size of a deck if you operate optimally.
|
[
[
"9\n3 1\n3 2 2\n5 4\n2 6 1 2 4\n2 100\n1410065408 10000000000\n10 8\n7 4 6 6 9 3 10 2 8 7\n2 12\n2 2\n2 70\n0 1\n1 0\n1\n3 0\n2 1 2\n3 1\n0 3 3",
"2\n3\n1\n7\n2\n2\n1\n1\n2"
]
] |
In the first test case, you can buy one card with the number $1$, and your cards become $[1, 1, 1, 1, 2, 2, 3, 3]$. You can partition them into the decks $[1, 2], [1, 2], [1, 3], [1, 3]$: they all have size $2$, and they all contain distinct values. You can show that you cannot get a partition with decks of size greater than $2$, so the answer is $2$.
In the second test case, you can buy two cards with the number $1$ and one card with the number $3$, and your cards become $[1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 5, 5]$, which can be partitioned into $[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 5], [2, 3, 5], [2, 4, 5]$. You can show that you cannot get a partition with decks of size greater than $3$, so the answer is $3$.
|
Title: Cards Partition
time_limit_ms: 2000
memory_limit_mb: 256
Description: [DJ Genki vs Gram - Einherjar Joker](https://soundcloud.com/leon- hwang-368077289/einherjar-joker-dj-genki-vs-gram)
β
You have some cards. An integer between $1$ and $n$ is written on each card: specifically, for each $i$ from $1$ to $n$, 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 at most $k$ new cards in total, and the cards you buy can contain any integer between $\mathbf{1}$ and $\mathbf{n}$, inclusive.
After buying the new cards, you must partition all your cards into decks, according to the following rules:
* all the decks must have the same size; * there are no pairs of cards with the same value in the same deck.
Find the maximum possible size of a deck after buying cards and partitioning them optimally.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains two integers $n$, $k$ ($1 \leq n \leq 2 \cdot 10^5$, $0 \leq k \leq 10^{16}$) β the number of distinct types of cards and the number of coins.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq 10^{10}$, $\sum a_i \geq 1$) β the number of cards of type $i$ you have at the beginning, for each $1 \leq i \leq n$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output a single integer: the maximum possible size of a deck if you operate optimally.
Examples:
input:
9
3 1
3 2 2
5 4
2 6 1 2 4
2 100
1410065408 10000000000
10 8
7 4 6 6 9 3 10 2 8 7
2 12
2 2
2 70
0 1
1 0
1
3 0
2 1 2
3 1
0 3 3
output:
2
3
1
7
2
2
1
1
2
Note: In the first test case, you can buy one card with the number $1$, and your cards become $[1, 1, 1, 1, 2, 2, 3, 3]$. You can partition them into the decks $[1, 2], [1, 2], [1, 3], [1, 3]$: they all have size $2$, and they all contain distinct values. You can show that you cannot get a partition with decks of size greater than $2$, so the answer is $2$.
In the second test case, you can buy two cards with the number $1$ and one card with the number $3$, and your cards become $[1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 5, 5]$, which can be partitioned into $[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 5], [2, 3, 5], [2, 4, 5]$. You can show that you cannot get a partition with decks of size greater than $3$, so the answer is $3$.
|
2018B
|
https://codeforces.com/problemset/problem/2018/B
|
Speedbreaker
| 1,900
|
[
"binary search",
"data structures",
"dp",
"greedy",
"implementation",
"two pointers"
] |
Div. 1
| 2,000
| 256
|
[Djjaner - Speedbreaker](https://soundcloud.com/luciano- ferrari-151560131/speedbreaker)
β
There are $n$ cities in a row, numbered $1, 2, \ldots, n$ left to right.
* At time $1$, you conquer exactly one city, called the starting city. * At time $2, 3, \ldots, n$, you can choose a city adjacent to the ones conquered so far and conquer it.
You win if, for each $i$, you conquer city $i$ at a time no later than $a_i$. A winning strategy may or may not exist, also depending on the starting city. How many starting cities allow you to win?
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of cities.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) β the deadlines for conquering the cities.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output a single integer: the number of starting cities that allow you to win.
|
[
[
"3\n6\n6 3 3 3 5 5\n6\n5 6 4 1 4 5\n9\n8 6 4 2 1 3 5 7 9",
"3\n0\n1"
]
] |
In the first test case, cities $2$, $3$, and $4$ are good starting cities.
In the second test case, there are no good starting cities.
In the third test case, the only good starting city is city $5$.
|
Title: Speedbreaker
time_limit_ms: 2000
memory_limit_mb: 256
Description: [Djjaner - Speedbreaker](https://soundcloud.com/luciano- ferrari-151560131/speedbreaker)
β
There are $n$ cities in a row, numbered $1, 2, \ldots, n$ left to right.
* At time $1$, you conquer exactly one city, called the starting city. * At time $2, 3, \ldots, n$, you can choose a city adjacent to the ones conquered so far and conquer it.
You win if, for each $i$, you conquer city $i$ at a time no later than $a_i$. A winning strategy may or may not exist, also depending on the starting city. How many starting cities allow you to win?
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of cities.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) β the deadlines for conquering the cities.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output a single integer: the number of starting cities that allow you to win.
Examples:
input:
3
6
6 3 3 3 5 5
6
5 6 4 1 4 5
9
8 6 4 2 1 3 5 7 9
output:
3
0
1
Note: In the first test case, cities $2$, $3$, and $4$ are good starting cities.
In the second test case, there are no good starting cities.
In the third test case, the only good starting city is city $5$.
|
2018C
|
https://codeforces.com/problemset/problem/2018/C
|
Tree Pruning
| 1,700
|
[
"brute force",
"dfs and similar",
"greedy",
"sortings",
"trees"
] |
Div. 1
| 3,000
| 256
|
[t+pazolite, ginkiha, Hommarju - Paved Garden](https://soundcloud.com/fractalex-gd/ginkiha-paved-garden-little)
β
You are given a tree with $n$ nodes, rooted at node $1$. In this problem, a leaf is a non-root node with degree $1$.
In one operation, you can remove a leaf and the edge adjacent to it (possibly, new leaves appear). What is the minimum number of operations that you have to perform to get a tree, also rooted at node $1$, where all the leaves are at the same distance from the root?
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($3 \leq n \leq 5 \cdot 10^5$) β the number of nodes.
Each of the next $n-1$ lines contains two integers $u$, $v$ ($1 \leq u, v \leq n$, $u \neq v$), describing an edge that connects $u$ and $v$. It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.
|
For each test case, output a single integer: the minimum number of operations needed to achieve your goal.
|
[
[
"3\n7\n1 2\n1 3\n2 4\n2 5\n4 6\n4 7\n7\n1 2\n1 3\n1 4\n2 5\n3 6\n5 7\n15\n12 9\n1 6\n6 14\n9 11\n8 7\n3 5\n13 5\n6 10\n13 15\n13 6\n14 12\n7 2\n8 1\n1 4",
"2\n2\n5"
]
] |
In the first two examples, the tree is as follows:

In the first example, by removing edges $(1, 3)$ and $(2, 5)$, the resulting tree has all leaves (nodes $6$ and $7$) at the same distance from the root (node $1$), which is $3$. The answer is $2$, as it is the minimum number of edges that need to be removed to achieve the goal.
In the second example, removing edges $(1, 4)$ and $(5, 7)$ results in a tree where all leaves (nodes $4$ and $5$) are at the same distance from the root (node $1$), which is $2$.
|
Title: Tree Pruning
time_limit_ms: 3000
memory_limit_mb: 256
Description: [t+pazolite, ginkiha, Hommarju - Paved Garden](https://soundcloud.com/fractalex-gd/ginkiha-paved-garden-little)
β
You are given a tree with $n$ nodes, rooted at node $1$. In this problem, a leaf is a non-root node with degree $1$.
In one operation, you can remove a leaf and the edge adjacent to it (possibly, new leaves appear). What is the minimum number of operations that you have to perform to get a tree, also rooted at node $1$, where all the leaves are at the same distance from the root?
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($3 \leq n \leq 5 \cdot 10^5$) β the number of nodes.
Each of the next $n-1$ lines contains two integers $u$, $v$ ($1 \leq u, v \leq n$, $u \neq v$), describing an edge that connects $u$ and $v$. It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.
Output: For each test case, output a single integer: the minimum number of operations needed to achieve your goal.
Examples:
input:
3
7
1 2
1 3
2 4
2 5
4 6
4 7
7
1 2
1 3
1 4
2 5
3 6
5 7
15
12 9
1 6
6 14
9 11
8 7
3 5
13 5
6 10
13 15
13 6
14 12
7 2
8 1
1 4
output:
2
2
5
Note: In the first two examples, the tree is as follows:

In the first example, by removing edges $(1, 3)$ and $(2, 5)$, the resulting tree has all leaves (nodes $6$ and $7$) at the same distance from the root (node $1$), which is $3$. The answer is $2$, as it is the minimum number of edges that need to be removed to achieve the goal.
In the second example, removing edges $(1, 4)$ and $(5, 7)$ results in a tree where all leaves (nodes $4$ and $5$) are at the same distance from the root (node $1$), which is $2$.
|
2018D
|
https://codeforces.com/problemset/problem/2018/D
|
Max Plus Min Plus Size
| 2,200
|
[
"data structures",
"dp",
"dsu",
"greedy",
"implementation",
"matrices",
"sortings"
] |
Div. 1
| 2,000
| 256
|
[EnV - The Dusty Dragon Tavern](https://soundcloud.com/envyofficial/env-the- dusty-dragon-tavern)
β
You are given an array $a_1, a_2, \ldots, a_n$ of positive integers.
You can color some elements of the array red, but there cannot be two adjacent red elements (i.e., for $1 \leq i \leq n-1$, at least one of $a_i$ and $a_{i+1}$ must not be red).
Your score is the maximum value of a red element, plus the minimum value of a red element, plus the number of red elements. Find the maximum score you can get.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of the array.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β the given array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output a single integer: the maximum possible score you can get after coloring some elements red according to the statement.
|
[
[
"4\n3\n5 4 5\n3\n4 5 4\n10\n3 3 3 3 4 1 2 3 5 4\n10\n17 89 92 42 29 41 92 14 70 45",
"12\n11\n12\n186"
]
] |
In the first test case, you can color the array as follows: $[\color{red}{5}, 4, \color{red}{5}]$. Your score is $\max([5, 5]) + \min([5, 5]) + \text{size}([5, 5]) = 5+5+2 = 12$. This is the maximum score you can get.
In the second test case, you can color the array as follows: $[4, \color{red}{5}, 4]$. Your score is $\max([5]) + \min([5]) + \text{size}([5]) = 5+5+1 = 11$. This is the maximum score you can get.
In the third test case, you can color the array as follows: $[\color{red}{3}, 3, \color{red}{3}, 3, \color{red}{4}, 1, 2, \color{red}{3}, 5, \color{red}{4}]$. Your score is $\max([3, 3, 4, 3, 4]) + \min([3, 3, 4, 3, 4]) + \text{size}([3, 3, 4, 3, 4]) = 4+3+5 = 12$. This is the maximum score you can get.
|
Title: Max Plus Min Plus Size
time_limit_ms: 2000
memory_limit_mb: 256
Description: [EnV - The Dusty Dragon Tavern](https://soundcloud.com/envyofficial/env-the- dusty-dragon-tavern)
β
You are given an array $a_1, a_2, \ldots, a_n$ of positive integers.
You can color some elements of the array red, but there cannot be two adjacent red elements (i.e., for $1 \leq i \leq n-1$, at least one of $a_i$ and $a_{i+1}$ must not be red).
Your score is the maximum value of a red element, plus the minimum value of a red element, plus the number of red elements. Find the maximum score you can get.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of the array.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β the given array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output a single integer: the maximum possible score you can get after coloring some elements red according to the statement.
Examples:
input:
4
3
5 4 5
3
4 5 4
10
3 3 3 3 4 1 2 3 5 4
10
17 89 92 42 29 41 92 14 70 45
output:
12
11
12
186
Note: In the first test case, you can color the array as follows: $[\color{red}{5}, 4, \color{red}{5}]$. Your score is $\max([5, 5]) + \min([5, 5]) + \text{size}([5, 5]) = 5+5+2 = 12$. This is the maximum score you can get.
In the second test case, you can color the array as follows: $[4, \color{red}{5}, 4]$. Your score is $\max([5]) + \min([5]) + \text{size}([5]) = 5+5+1 = 11$. This is the maximum score you can get.
In the third test case, you can color the array as follows: $[\color{red}{3}, 3, \color{red}{3}, 3, \color{red}{4}, 1, 2, \color{red}{3}, 5, \color{red}{4}]$. Your score is $\max([3, 3, 4, 3, 4]) + \min([3, 3, 4, 3, 4]) + \text{size}([3, 3, 4, 3, 4]) = 4+3+5 = 12$. This is the maximum score you can get.
|
2023B
|
https://codeforces.com/problemset/problem/2023/B
|
Skipping
| 1,700
|
[
"binary search",
"dp",
"graphs",
"shortest paths"
] |
Div. 1
| 2,000
| 256
|
It is already the year $3024$, ideas for problems have long run out, and the olympiad now takes place in a modified individual format. The olympiad consists of $n$ problems, numbered from $1$ to $n$. The $i$-th problem has its own score $a_i$ and a certain parameter $b_i$ ($1 \le b_i \le n$).
Initially, the testing system gives the participant the first problem. When the participant is given the $i$-th problem, they have two options:
* They can submit the problem and receive $a_i$ points; * They can skip the problem, in which case they will never be able to submit it.
Then, the testing system selects the next problem for the participant from problems with indices $j$, such that:
* If he submitted the $i$-th problem, it looks at problems with indices $j < i$; * If he skipped the $i$-th problem, it looks at problems with indices $j \leq b_i$.
Among these problems, it selects the problem with the maximum index that it has not previously given to the participant (he has neither submitted nor skipped it before). If there is no such problem, then the competition for the participant ends, and their result is equal to the sum of points for all submitted problems. In particular, if the participant submits the first problem, then the competition for them ends. Note that the participant receives each problem at most once.
Prokhor has prepared thoroughly for the olympiad, and now he can submit any problem. Help him determine the maximum number of points he can achieve.
|
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 4 \cdot 10^5$) β the number of problems in the olympiad.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) β the scores of the problems.
The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq n$) β the parameters of the problems.
It is guaranteed that the sum of $n$ over all test cases does not exceed $4 \cdot 10^5$.
|
For each test case, output a single integer β the maximum number of points that Prokhor can achieve.
|
[
[
"4\n2\n15 16\n2 1\n5\n10 10 100 100 1000\n3 4 1 1 1\n3\n100 49 50\n3 2 2\n4\n100 200 300 1000\n2 3 4 1",
"16\n200\n100\n1000"
]
] |
In the first test case, Prokhor can skip the first problem; then he will receive the problem with index $b_1 = 2$. Prokhor can submit it and receive $a_2 = 16$ points. After that, the competition will end because Prokhor has already received all problems. Note that if Prokhor submits the first problem, he will receive $a_1 = 15$ points, but the competition will end immediately.
In the second test case, Prokhor can skip the first problem; then he will receive the problem with index $b_1 = 3$. Prokhor can submit it and receive $a_3 = 100$ points. After that, Prokhor will receive the second problem, which he can skip to receive the problem with index $b_2 = 4$. Prokhor can submit the fourth problem and receive another $a_4 = 100$ points. After that, the competition ends because Prokhor has already received all problems with indices not exceeding $4$. Thus, Prokhor will receive a total of $200$ points.
In the third test case, Prokhor can submit the first problem and receive $100$ points, after which the competition will end immediately.
|
Title: Skipping
time_limit_ms: 2000
memory_limit_mb: 256
Description: It is already the year $3024$, ideas for problems have long run out, and the olympiad now takes place in a modified individual format. The olympiad consists of $n$ problems, numbered from $1$ to $n$. The $i$-th problem has its own score $a_i$ and a certain parameter $b_i$ ($1 \le b_i \le n$).
Initially, the testing system gives the participant the first problem. When the participant is given the $i$-th problem, they have two options:
* They can submit the problem and receive $a_i$ points; * They can skip the problem, in which case they will never be able to submit it.
Then, the testing system selects the next problem for the participant from problems with indices $j$, such that:
* If he submitted the $i$-th problem, it looks at problems with indices $j < i$; * If he skipped the $i$-th problem, it looks at problems with indices $j \leq b_i$.
Among these problems, it selects the problem with the maximum index that it has not previously given to the participant (he has neither submitted nor skipped it before). If there is no such problem, then the competition for the participant ends, and their result is equal to the sum of points for all submitted problems. In particular, if the participant submits the first problem, then the competition for them ends. Note that the participant receives each problem at most once.
Prokhor has prepared thoroughly for the olympiad, and now he can submit any problem. Help him determine the maximum number of points he can achieve.
Input: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 4 \cdot 10^5$) β the number of problems in the olympiad.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) β the scores of the problems.
The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq n$) β the parameters of the problems.
It is guaranteed that the sum of $n$ over all test cases does not exceed $4 \cdot 10^5$.
Output: For each test case, output a single integer β the maximum number of points that Prokhor can achieve.
Examples:
input:
4
2
15 16
2 1
5
10 10 100 100 1000
3 4 1 1 1
3
100 49 50
3 2 2
4
100 200 300 1000
2 3 4 1
output:
16
200
100
1000
Note: In the first test case, Prokhor can skip the first problem; then he will receive the problem with index $b_1 = 2$. Prokhor can submit it and receive $a_2 = 16$ points. After that, the competition will end because Prokhor has already received all problems. Note that if Prokhor submits the first problem, he will receive $a_1 = 15$ points, but the competition will end immediately.
In the second test case, Prokhor can skip the first problem; then he will receive the problem with index $b_1 = 3$. Prokhor can submit it and receive $a_3 = 100$ points. After that, Prokhor will receive the second problem, which he can skip to receive the problem with index $b_2 = 4$. Prokhor can submit the fourth problem and receive another $a_4 = 100$ points. After that, the competition ends because Prokhor has already received all problems with indices not exceeding $4$. Thus, Prokhor will receive a total of $200$ points.
In the third test case, Prokhor can submit the first problem and receive $100$ points, after which the competition will end immediately.
|
2023C
|
https://codeforces.com/problemset/problem/2023/C
|
C+K+S
| 2,400
|
[
"constructive algorithms",
"dfs and similar",
"graphs",
"greedy",
"hashing",
"implementation",
"strings"
] |
Div. 1
| 3,000
| 256
|
You are given two strongly connected$^{\dagger}$ directed graphs, each with exactly $n$ vertices, but possibly different numbers of edges. Upon closer inspection, you noticed an important feature β the length of any cycle in these graphs is divisible by $k$.
Each of the $2n$ vertices belongs to exactly one of two types: incoming or outgoing. For each vertex, its type is known to you.
You need to determine whether it is possible to draw exactly $n$ directed edges between the source graphs such that the following four conditions are met:
* The ends of any added edge lie in different graphs. * From each outgoing vertex, exactly one added edge originates. * Into each incoming vertex, exactly one added edge enters. * In the resulting graph, the length of any cycle is divisible by $k$.
$^{\dagger}$A strongly connected graph is a graph in which there is a path from every vertex to every other vertex.
|
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($2 \le k \le n \le 2 \cdot 10^5$) β the number of vertices in each graph and the value by which the length of each cycle is divisible.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i \in \\{0, 1\\}$). If $a_i = 0$, then vertex $i$ of the first graph is incoming. If $a_i = 1$, then vertex $i$ of the first graph is outgoing.
The third line of each test case contains a single integer $m_1$ ($1 \le m_1 \le 5 \cdot 10^5$) β the number of edges in the first graph.
The next $m_1$ lines contain descriptions of the edges of the first graph. The $i$-th of them contains two integers $v_i$ and $u_i$ ($1 \le v_i, u_i \le n$) β an edge in the first graph leading from vertex $v_i$ to vertex $u_i$.
Next, in the same format, follows the description of the second graph.
The next line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($b_i \in \\{0, 1\\}$). If $b_i = 0$, then vertex $i$ of the second graph is incoming. If $b_i = 1$, then vertex $i$ of the second graph is outgoing.
The next line contains a single integer $m_2$ ($1 \le m_2 \le 5 \cdot 10^5$) β the number of edges in the second graph.
The next $m_2$ lines contain descriptions of the edges of the second graph. The $i$-th of them contains two integers $v_i$ and $u_i$ ($1 \le v_i, u_i \le n$) β an edge in the second graph leading from vertex $v_i$ to vertex $u_i$.
It is guaranteed that both graphs are strongly connected, and the lengths of all cycles are divisible by $k$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. It is guaranteed that the sum of $m_1$ and the sum of $m_2$ over all test cases does not exceed $5 \cdot 10^5$.
|
For each test case, output "YES" (without quotes) if it is possible to draw $n$ new edges such that all conditions are met, and "NO" (without quotes) otherwise.
You may output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
|
[
[
"3\n4 2\n1 0 0 1\n4\n1 2\n2 3\n3 4\n4 1\n1 0 0 1\n4\n1 3\n3 2\n2 4\n4 1\n3 3\n0 0 0\n3\n1 2\n2 3\n3 1\n1 1 0\n3\n1 2\n2 3\n3 1\n4 2\n1 1 1 1\n4\n1 2\n2 3\n3 4\n4 1\n0 0 0 0\n6\n1 2\n2 1\n1 3\n3 1\n1 4\n4 1",
"YES\nNO\nYES"
]
] |
In the first test case, it is possible to draw edges from the first graph to the second graph as $(1, 3)$ and $(4, 2)$ (the first number in the pair is the vertex number in the first graph, and the second number in the pair is the vertex number in the second graph), and from the second graph to the first graph as $(1, 2)$, $(4, 3)$ (the first number in the pair is the vertex number in the second graph, and the second number in the pair is the vertex number in the first graph).
In the second test case, there are a total of $4$ incoming vertices and $2$ outgoing vertices, so it is not possible to draw $3$ edges.
|
Title: C+K+S
time_limit_ms: 3000
memory_limit_mb: 256
Description: You are given two strongly connected$^{\dagger}$ directed graphs, each with exactly $n$ vertices, but possibly different numbers of edges. Upon closer inspection, you noticed an important feature β the length of any cycle in these graphs is divisible by $k$.
Each of the $2n$ vertices belongs to exactly one of two types: incoming or outgoing. For each vertex, its type is known to you.
You need to determine whether it is possible to draw exactly $n$ directed edges between the source graphs such that the following four conditions are met:
* The ends of any added edge lie in different graphs. * From each outgoing vertex, exactly one added edge originates. * Into each incoming vertex, exactly one added edge enters. * In the resulting graph, the length of any cycle is divisible by $k$.
$^{\dagger}$A strongly connected graph is a graph in which there is a path from every vertex to every other vertex.
Input: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($2 \le k \le n \le 2 \cdot 10^5$) β the number of vertices in each graph and the value by which the length of each cycle is divisible.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i \in \\{0, 1\\}$). If $a_i = 0$, then vertex $i$ of the first graph is incoming. If $a_i = 1$, then vertex $i$ of the first graph is outgoing.
The third line of each test case contains a single integer $m_1$ ($1 \le m_1 \le 5 \cdot 10^5$) β the number of edges in the first graph.
The next $m_1$ lines contain descriptions of the edges of the first graph. The $i$-th of them contains two integers $v_i$ and $u_i$ ($1 \le v_i, u_i \le n$) β an edge in the first graph leading from vertex $v_i$ to vertex $u_i$.
Next, in the same format, follows the description of the second graph.
The next line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($b_i \in \\{0, 1\\}$). If $b_i = 0$, then vertex $i$ of the second graph is incoming. If $b_i = 1$, then vertex $i$ of the second graph is outgoing.
The next line contains a single integer $m_2$ ($1 \le m_2 \le 5 \cdot 10^5$) β the number of edges in the second graph.
The next $m_2$ lines contain descriptions of the edges of the second graph. The $i$-th of them contains two integers $v_i$ and $u_i$ ($1 \le v_i, u_i \le n$) β an edge in the second graph leading from vertex $v_i$ to vertex $u_i$.
It is guaranteed that both graphs are strongly connected, and the lengths of all cycles are divisible by $k$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. It is guaranteed that the sum of $m_1$ and the sum of $m_2$ over all test cases does not exceed $5 \cdot 10^5$.
Output: For each test case, output "YES" (without quotes) if it is possible to draw $n$ new edges such that all conditions are met, and "NO" (without quotes) otherwise.
You may output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
Examples:
input:
3
4 2
1 0 0 1
4
1 2
2 3
3 4
4 1
1 0 0 1
4
1 3
3 2
2 4
4 1
3 3
0 0 0
3
1 2
2 3
3 1
1 1 0
3
1 2
2 3
3 1
4 2
1 1 1 1
4
1 2
2 3
3 4
4 1
0 0 0 0
6
1 2
2 1
1 3
3 1
1 4
4 1
output:
YES
NO
YES
Note: In the first test case, it is possible to draw edges from the first graph to the second graph as $(1, 3)$ and $(4, 2)$ (the first number in the pair is the vertex number in the first graph, and the second number in the pair is the vertex number in the second graph), and from the second graph to the first graph as $(1, 2)$, $(4, 3)$ (the first number in the pair is the vertex number in the second graph, and the second number in the pair is the vertex number in the first graph).
In the second test case, there are a total of $4$ incoming vertices and $2$ outgoing vertices, so it is not possible to draw $3$ edges.
|
1993A
|
https://codeforces.com/problemset/problem/1993/A
|
Question Marks
| 800
|
[
"greedy",
"implementation"
] |
Div. 2
| 1,000
| 256
|
Tim is doing a test consisting of $4n$ questions; each question has $4$ options: 'A', 'B', 'C', and 'D'. For each option, there are exactly $n$ correct answers corresponding to that option β meaning there are $n$ questions with the answer 'A', $n$ questions with the answer 'B', $n$ questions with the answer 'C', and $n$ questions with the answer 'D'.
For each question, Tim wrote his answer on the answer sheet. If he could not figure out the answer, he would leave a question mark '?' for that question.
You are given his answer sheet of $4n$ characters. What is the maximum number of correct answers Tim can get?
|
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains an integer $n$ ($1 \le n \le 100$).
The second line of each test case contains a string $s$ of $4n$ characters ($s_i \in \\{\texttt{A}, \texttt{B}, \texttt{C}, \texttt{D}, \texttt{?}\\}$) β Tim's answers for the questions.
|
For each test case, print a single integer β the maximum score that Tim can achieve.
|
[
[
"6\n1\nABCD\n2\nAAAAAAAA\n2\nAAAABBBB\n2\n????????\n3\nABCABCABCABC\n5\nACADC??ACAC?DCAABC?C",
"4\n2\n4\n0\n9\n13"
]
] |
In the first test case, there is exactly one question with each answer 'A', 'B', 'C', and 'D'; so it's possible that Tim gets all his answers correct.
In the second test case, there are only two correct answers 'A' which makes him get exactly $2$ points in any case.
In the third test case, Tim can get at most $2$ correct answers with option 'A' and $2$ correct answers with option 'B'. For example, he would get $4$ points if the answers were 'AACCBBDD'.
In the fourth test case, he refuses to answer any question at all, which makes him get $0$ points.
|
Title: Question Marks
time_limit_ms: 1000
memory_limit_mb: 256
Description: Tim is doing a test consisting of $4n$ questions; each question has $4$ options: 'A', 'B', 'C', and 'D'. For each option, there are exactly $n$ correct answers corresponding to that option β meaning there are $n$ questions with the answer 'A', $n$ questions with the answer 'B', $n$ questions with the answer 'C', and $n$ questions with the answer 'D'.
For each question, Tim wrote his answer on the answer sheet. If he could not figure out the answer, he would leave a question mark '?' for that question.
You are given his answer sheet of $4n$ characters. What is the maximum number of correct answers Tim can get?
Input: The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains an integer $n$ ($1 \le n \le 100$).
The second line of each test case contains a string $s$ of $4n$ characters ($s_i \in \\{\texttt{A}, \texttt{B}, \texttt{C}, \texttt{D}, \texttt{?}\\}$) β Tim's answers for the questions.
Output: For each test case, print a single integer β the maximum score that Tim can achieve.
Examples:
input:
6
1
ABCD
2
AAAAAAAA
2
AAAABBBB
2
????????
3
ABCABCABCABC
5
ACADC??ACAC?DCAABC?C
output:
4
2
4
0
9
13
Note: In the first test case, there is exactly one question with each answer 'A', 'B', 'C', and 'D'; so it's possible that Tim gets all his answers correct.
In the second test case, there are only two correct answers 'A' which makes him get exactly $2$ points in any case.
In the third test case, Tim can get at most $2$ correct answers with option 'A' and $2$ correct answers with option 'B'. For example, he would get $4$ points if the answers were 'AACCBBDD'.
In the fourth test case, he refuses to answer any question at all, which makes him get $0$ points.
|
1984C1
|
https://codeforces.com/problemset/problem/1984/C1
|
Magnitude (Easy Version)
| 1,300
|
[
"dp",
"greedy",
"math"
] |
Div. 1 + 2
| 2,000
| 256
|
The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved.
You are given an array $a$ of length $n$. Start with $c = 0$. Then, for each $i$ from $1$ to $n$ (in increasing order) do exactly one of the following:
* Option $1$: set $c$ to $c + a_i$. * Option $2$: set $c$ to $|c + a_i|$, where $|x|$ is the absolute value of $x$.
Let the maximum final value of $c$ after the procedure described above be equal to $k$. Find $k$.
|
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$).
The second line of each case contains $n$ integers $a_1$, $a_2$, $a_3$, $\ldots$, $a_n$ ($-10^9 \leq a_i \leq 10^9$).
The sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
|
For each test case, output a single integer β the value of $k$.
|
[
[
"5\n4\n10 -9 -3 4\n8\n1 4 3 4 1 4 3 4\n3\n-1 -2 -3\n4\n-1000000000 1000000000 1000000000 1000000000\n4\n1 9 8 4",
"6\n24\n6\n4000000000\n22"
]
] |
In the first test case, if we set $c$ to its absolute value every time we add to it, we end up with $6$. It can be shown that this is the maximum result.
In the second test case, taking the absolute value will never change anything, so we can just sum the array without doing anything to get $24$.
In the third test case, it is optimal to wait until the end to set $c$ to its absolute value, resulting in an answer of $6$.
|
Title: Magnitude (Easy Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved.
You are given an array $a$ of length $n$. Start with $c = 0$. Then, for each $i$ from $1$ to $n$ (in increasing order) do exactly one of the following:
* Option $1$: set $c$ to $c + a_i$. * Option $2$: set $c$ to $|c + a_i|$, where $|x|$ is the absolute value of $x$.
Let the maximum final value of $c$ after the procedure described above be equal to $k$. Find $k$.
Input: The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$).
The second line of each case contains $n$ integers $a_1$, $a_2$, $a_3$, $\ldots$, $a_n$ ($-10^9 \leq a_i \leq 10^9$).
The sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
Output: For each test case, output a single integer β the value of $k$.
Examples:
input:
5
4
10 -9 -3 4
8
1 4 3 4 1 4 3 4
3
-1 -2 -3
4
-1000000000 1000000000 1000000000 1000000000
4
1 9 8 4
output:
6
24
6
4000000000
22
Note: In the first test case, if we set $c$ to its absolute value every time we add to it, we end up with $6$. It can be shown that this is the maximum result.
In the second test case, taking the absolute value will never change anything, so we can just sum the array without doing anything to get $24$.
In the third test case, it is optimal to wait until the end to set $c$ to its absolute value, resulting in an answer of $6$.
|
1973B
|
https://codeforces.com/problemset/problem/1973/B
|
Cat, Fox and the Lonely Array
| 1,300
|
[
"binary search",
"bitmasks",
"data structures",
"greedy",
"math",
"two pointers"
] |
Div. 2
| 2,000
| 256
|
Today, Cat and Fox found an array $a$ consisting of $n$ non-negative integers.
Define the loneliness of $a$ as the smallest positive integer $k$ ($1 \le k \le n$) such that for any two positive integers $i$ and $j$ ($1 \leq i, j \leq n - k +1$), the following holds: $$a_i | a_{i+1} | \ldots | a_{i+k-1} = a_j | a_{j+1} | \ldots | a_{j+k-1},$$ where $x | y$ denotes the [bitwise OR](https://en.wikipedia.org/wiki/Bitwise_operation#OR) of $x$ and $y$. In other words, for every $k$ consecutive elements, their bitwise OR should be the same. Note that the loneliness of $a$ is well-defined, because for $k = n$ the condition is satisfied.
Cat and Fox want to know how lonely the array $a$ is. Help them calculate the loneliness of the found array.
|
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4 $) β the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 10^5$) β the length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < 2^{20}$) β the elements of the array.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
|
For each test case, print one integer β the loneliness of the given array.
|
[
[
"7\n1\n0\n3\n2 2 2\n3\n1 0 2\n5\n3 0 1 4 2\n5\n2 0 4 0 2\n7\n0 0 0 0 1 2 4\n8\n0 1 3 2 2 1 0 3",
"1\n1\n3\n4\n4\n7\n3"
]
] |
In the first example, the loneliness of an array with a single element is always $1$, so the answer is $1$.
In the second example, the OR of each subarray of length $k = 1$ is $2$, so the loneliness of the whole array is $1$.
In the seventh example, it's true that $(0 | 1 | 3) = (1 | 3 | 2) = (3 | 2 | 2) = (2 | 2 | 1) = (2 | 1 | 0) = (1 | 0 | 3) = 3$, so the condition is satisfied for $k = 3$. We can verify that the condition is not true for any smaller $k$, so the answer is indeed $3$.
|
Title: Cat, Fox and the Lonely Array
time_limit_ms: 2000
memory_limit_mb: 256
Description: Today, Cat and Fox found an array $a$ consisting of $n$ non-negative integers.
Define the loneliness of $a$ as the smallest positive integer $k$ ($1 \le k \le n$) such that for any two positive integers $i$ and $j$ ($1 \leq i, j \leq n - k +1$), the following holds: $$a_i | a_{i+1} | \ldots | a_{i+k-1} = a_j | a_{j+1} | \ldots | a_{j+k-1},$$ where $x | y$ denotes the [bitwise OR](https://en.wikipedia.org/wiki/Bitwise_operation#OR) of $x$ and $y$. In other words, for every $k$ consecutive elements, their bitwise OR should be the same. Note that the loneliness of $a$ is well-defined, because for $k = n$ the condition is satisfied.
Cat and Fox want to know how lonely the array $a$ is. Help them calculate the loneliness of the found array.
Input: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4 $) β the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 10^5$) β the length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < 2^{20}$) β the elements of the array.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
Output: For each test case, print one integer β the loneliness of the given array.
Examples:
input:
7
1
0
3
2 2 2
3
1 0 2
5
3 0 1 4 2
5
2 0 4 0 2
7
0 0 0 0 1 2 4
8
0 1 3 2 2 1 0 3
output:
1
1
3
4
4
7
3
Note: In the first example, the loneliness of an array with a single element is always $1$, so the answer is $1$.
In the second example, the OR of each subarray of length $k = 1$ is $2$, so the loneliness of the whole array is $1$.
In the seventh example, it's true that $(0 | 1 | 3) = (1 | 3 | 2) = (3 | 2 | 2) = (2 | 2 | 1) = (2 | 1 | 0) = (1 | 0 | 3) = 3$, so the condition is satisfied for $k = 3$. We can verify that the condition is not true for any smaller $k$, so the answer is indeed $3$.
|
2027D2
|
https://codeforces.com/problemset/problem/2027/D2
|
The Endspeaker (Hard Version)
| 2,200
|
[
"binary search",
"data structures",
"dp",
"greedy",
"implementation",
"two pointers"
] |
Div. 2
| 2,000
| 256
|
This is the hard version of this problem. The only difference is that you need to also output the number of optimal sequences in this version. You must solve both versions to be able to hack.
You're given an array $a$ of length $n$, and an array $b$ of length $m$ ($b_i > b_{i+1}$ for all $1 \le i < m$). Initially, the value of $k$ is $1$. Your aim is to make the array $a$ empty by performing one of these two operations repeatedly:
* Type $1$ β If the value of $k$ is less than $m$ and the array $a$ is not empty, you can increase the value of $k$ by $1$. This does not incur any cost. * Type $2$ β You remove a non-empty prefix of array $a$, such that its sum does not exceed $b_k$. This incurs a cost of $m - k$.
You need to minimize the total cost of the operations to make array $a$ empty. If it's impossible to do this through any sequence of operations, output $-1$. Otherwise, output the minimum total cost of the operations, and the number of sequences of operations which yield this minimum cost modulo $10^9 + 7$.
Two sequences of operations are considered different if you choose a different type of operation at any step, or the size of the removed prefix is different at any step.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 3 \cdot 10^5$, $\boldsymbol{1 \le n \cdot m \le 3 \cdot 10^5}$).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
The third line of each test case contains $m$ integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le 10^9$).
It is also guaranteed that $b_i > b_{i+1}$ for all $1 \le i < m$.
It is guaranteed that the sum of $\boldsymbol{n \cdot m}$ over all test cases does not exceed $3 \cdot 10^5$.
|
For each test case, if it's possible to make $a$ empty, then output two integers. The first should be the minimum total cost of the operations, and the second should be the number of sequences of operations which achieve this minimum cost, modulo $10^9 + 7$.
If there is no possible sequence of operations which makes $a$ empty, then output a single integer $-1$.
|
[
[
"5\n4 2\n9 3 4 3\n11 7\n1 2\n20\n19 18\n10 2\n2 5 2 1 10 3 2 9 9 6\n17 9\n10 11\n2 2 2 2 2 2 2 2 2 2\n20 18 16 14 12 10 8 6 4 2 1\n1 6\n10\n32 16 8 4 2 1",
"1 3\n-1\n2 11\n10 42\n4 1"
]
] |
In the first test case, there are $3$ optimal sequences of operations which yield a total cost of $1$:
* All $3$ sequences begin with a type $2$ operation, removing the prefix $[9]$ to make $a = [3, 4, 3]$, incurring a cost of $1$. Then, we perform a type $1$ operation to increase the value of $k$ by $1$. All subsequent operations now incur a cost of $0$. * One sequence continues by removing the prefixes $[3, 4]$ then $[3]$. * Another sequence continues by removing the prefixes $[3]$ then $[4, 3]$. * Another sequence continues by removing the prefixes $[3]$ then $[4]$ then $[3]$.
In the second test case, it's impossible to remove any prefix of the array since $a_1 > b_1$, so array $a$ cannot be made empty by any sequence of operations.
|
Title: The Endspeaker (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: This is the hard version of this problem. The only difference is that you need to also output the number of optimal sequences in this version. You must solve both versions to be able to hack.
You're given an array $a$ of length $n$, and an array $b$ of length $m$ ($b_i > b_{i+1}$ for all $1 \le i < m$). Initially, the value of $k$ is $1$. Your aim is to make the array $a$ empty by performing one of these two operations repeatedly:
* Type $1$ β If the value of $k$ is less than $m$ and the array $a$ is not empty, you can increase the value of $k$ by $1$. This does not incur any cost. * Type $2$ β You remove a non-empty prefix of array $a$, such that its sum does not exceed $b_k$. This incurs a cost of $m - k$.
You need to minimize the total cost of the operations to make array $a$ empty. If it's impossible to do this through any sequence of operations, output $-1$. Otherwise, output the minimum total cost of the operations, and the number of sequences of operations which yield this minimum cost modulo $10^9 + 7$.
Two sequences of operations are considered different if you choose a different type of operation at any step, or the size of the removed prefix is different at any step.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 3 \cdot 10^5$, $\boldsymbol{1 \le n \cdot m \le 3 \cdot 10^5}$).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
The third line of each test case contains $m$ integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le 10^9$).
It is also guaranteed that $b_i > b_{i+1}$ for all $1 \le i < m$.
It is guaranteed that the sum of $\boldsymbol{n \cdot m}$ over all test cases does not exceed $3 \cdot 10^5$.
Output: For each test case, if it's possible to make $a$ empty, then output two integers. The first should be the minimum total cost of the operations, and the second should be the number of sequences of operations which achieve this minimum cost, modulo $10^9 + 7$.
If there is no possible sequence of operations which makes $a$ empty, then output a single integer $-1$.
Examples:
input:
5
4 2
9 3 4 3
11 7
1 2
20
19 18
10 2
2 5 2 1 10 3 2 9 9 6
17 9
10 11
2 2 2 2 2 2 2 2 2 2
20 18 16 14 12 10 8 6 4 2 1
1 6
10
32 16 8 4 2 1
output:
1 3
-1
2 11
10 42
4 1
Note: In the first test case, there are $3$ optimal sequences of operations which yield a total cost of $1$:
* All $3$ sequences begin with a type $2$ operation, removing the prefix $[9]$ to make $a = [3, 4, 3]$, incurring a cost of $1$. Then, we perform a type $1$ operation to increase the value of $k$ by $1$. All subsequent operations now incur a cost of $0$. * One sequence continues by removing the prefixes $[3, 4]$ then $[3]$. * Another sequence continues by removing the prefixes $[3]$ then $[4, 3]$. * Another sequence continues by removing the prefixes $[3]$ then $[4]$ then $[3]$.
In the second test case, it's impossible to remove any prefix of the array since $a_1 > b_1$, so array $a$ cannot be made empty by any sequence of operations.
|
1993F1
|
https://codeforces.com/problemset/problem/1993/F1
|
Dyn-scripted Robot (Easy Version)
| 2,400
|
[
"brute force",
"chinese remainder theorem",
"constructive algorithms",
"math",
"number theory"
] |
Div. 2
| 3,000
| 256
|
This is the easy version of the problem. The only difference is that in this version $k \le n$. You can make hacks only if both versions of the problem are solved. Given a $w \times h$ rectangle on the $Oxy$ plane, with points $(0, 0)$ at the bottom-left and $(w, h)$ at the top-right of the rectangle.
You also have a robot initially at point $(0, 0)$ and a script $s$ of $n$ characters. Each character is either L, R, U, or D, which tells the robot to move left, right, up, or down respectively.
The robot can only move inside the rectangle; otherwise, it will change the script $s$ as follows:
* If it tries to move outside a vertical border, it changes all L characters to R's (and vice versa, all R's to L's). * If it tries to move outside a horizontal border, it changes all U characters to D's (and vice versa, all D's to U's).
Then, it will execute the changed script starting from the character which it couldn't execute.
 An example of the robot's movement process, $s = \texttt{"ULULURD"}$
The script $s$ will be executed for $k$ times continuously. All changes to the string $s$ will be retained even when it is repeated. During this process, how many times will the robot move to the point $(0, 0)$ in total? Note that the initial position does NOT count.
|
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains four integers $n$, $k$, $w$, and $h$ ($1 \le n, w, h \le 10^6$; $1 \le k \le n$).
The second line contains a single string $s$ of size $n$ ($s_i \in \\{\texttt{L}, \texttt{R}, \texttt{U}, \texttt{D}\\}$) β the script to be executed.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
|
For each test case, print a single integer β the number of times the robot reaches $(0, 0)$ when executing script $s$ for $k$ times continuously.
|
[
[
"5\n2 2 2 2\nUR\n4 2 1 1\nLLDD\n6 3 3 1\nRLRRRL\n5 5 3 3\nRUURD\n7 5 3 4\nRRDLUUU",
"0\n4\n3\n0\n1"
]
] |
In the first test case, the robot only moves up and right. In the end, it occupies the position $(2, 2)$ but never visits $(0, 0)$. So the answer is $0$.
In the second test case, each time executing the script the robot visits the origin twice. And since $k=2$, it visits the origin $2 \cdot 2 = 4$ times overall.

In the third test case, the visualization is shown as below:

|
Title: Dyn-scripted Robot (Easy Version)
time_limit_ms: 3000
memory_limit_mb: 256
Description: This is the easy version of the problem. The only difference is that in this version $k \le n$. You can make hacks only if both versions of the problem are solved. Given a $w \times h$ rectangle on the $Oxy$ plane, with points $(0, 0)$ at the bottom-left and $(w, h)$ at the top-right of the rectangle.
You also have a robot initially at point $(0, 0)$ and a script $s$ of $n$ characters. Each character is either L, R, U, or D, which tells the robot to move left, right, up, or down respectively.
The robot can only move inside the rectangle; otherwise, it will change the script $s$ as follows:
* If it tries to move outside a vertical border, it changes all L characters to R's (and vice versa, all R's to L's). * If it tries to move outside a horizontal border, it changes all U characters to D's (and vice versa, all D's to U's).
Then, it will execute the changed script starting from the character which it couldn't execute.
 An example of the robot's movement process, $s = \texttt{"ULULURD"}$
The script $s$ will be executed for $k$ times continuously. All changes to the string $s$ will be retained even when it is repeated. During this process, how many times will the robot move to the point $(0, 0)$ in total? Note that the initial position does NOT count.
Input: The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains four integers $n$, $k$, $w$, and $h$ ($1 \le n, w, h \le 10^6$; $1 \le k \le n$).
The second line contains a single string $s$ of size $n$ ($s_i \in \\{\texttt{L}, \texttt{R}, \texttt{U}, \texttt{D}\\}$) β the script to be executed.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
Output: For each test case, print a single integer β the number of times the robot reaches $(0, 0)$ when executing script $s$ for $k$ times continuously.
Examples:
input:
5
2 2 2 2
UR
4 2 1 1
LLDD
6 3 3 1
RLRRRL
5 5 3 3
RUURD
7 5 3 4
RRDLUUU
output:
0
4
3
0
1
Note: In the first test case, the robot only moves up and right. In the end, it occupies the position $(2, 2)$ but never visits $(0, 0)$. So the answer is $0$.
In the second test case, each time executing the script the robot visits the origin twice. And since $k=2$, it visits the origin $2 \cdot 2 = 4$ times overall.

In the third test case, the visualization is shown as below:

|
1990C
|
https://codeforces.com/problemset/problem/1990/C
|
Mad MAD Sum
| 1,500
|
[
"brute force",
"greedy",
"math"
] |
Div. 2
| 2,000
| 256
|
We define the $\operatorname{MAD}$ (Maximum Appearing Duplicate) in an array as the largest number that appears at least twice in the array. Specifically, if there is no number that appears at least twice, the $\operatorname{MAD}$ value is $0$.
For example, $\operatorname{MAD}([1, 2, 1]) = 1$, $\operatorname{MAD}([2, 2, 3, 3]) = 3$, $\operatorname{MAD}([1, 2, 3, 4]) = 0$.
You are given an array $a$ of size $n$. Initially, a variable $sum$ is set to $0$.
The following process will be executed in a sequential loop until all numbers in $a$ become $0$:
1. Set $sum := sum + \sum_{i=1}^{n} a_i$; 2. Let $b$ be an array of size $n$. Set $b_i :=\ \operatorname{MAD}([a_1, a_2, \ldots, a_i])$ for all $1 \le i \le n$, and then set $a_i := b_i$ for all $1 \le i \le n$.
Find the value of $sum$ after the process.
|
The first line contains an integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) β the number of test cases.
For each test case:
* The first line contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the size of the array $a$; * The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) β the elements of the array.
It is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
|
For each test case, output the value of $sum$ in a new line.
|
[
[
"4\n1\n1\n3\n2 2 3\n4\n2 1 1 2\n4\n4 4 4 4",
"1\n13\n9\n40"
]
] |
In the first test case, $a=[1]$ initially.
In the first loop:
1. Set $sum := sum + a_1 = 0+1=1$; 2. Set $b_1 :=\ \operatorname{MAD}([a_1])=\ \operatorname{MAD}([1])=0$, and then set $a_1 := b_1$.
After the first loop, $a=[0]$ and the process ends. The value of $sum$ after the process is $1$.
In the second test case, $a=[2,2,3]$ initially.
After the first loop, $a=[0,2,2]$ and $sum=7$.
After the second loop, $a=[0,0,2]$ and $sum=11$.
After the third loop, $a=[0,0,0]$ and $sum=13$. Then the process ends.
The value of $sum$ after the process is $13$.
|
Title: Mad MAD Sum
time_limit_ms: 2000
memory_limit_mb: 256
Description: We define the $\operatorname{MAD}$ (Maximum Appearing Duplicate) in an array as the largest number that appears at least twice in the array. Specifically, if there is no number that appears at least twice, the $\operatorname{MAD}$ value is $0$.
For example, $\operatorname{MAD}([1, 2, 1]) = 1$, $\operatorname{MAD}([2, 2, 3, 3]) = 3$, $\operatorname{MAD}([1, 2, 3, 4]) = 0$.
You are given an array $a$ of size $n$. Initially, a variable $sum$ is set to $0$.
The following process will be executed in a sequential loop until all numbers in $a$ become $0$:
1. Set $sum := sum + \sum_{i=1}^{n} a_i$; 2. Let $b$ be an array of size $n$. Set $b_i :=\ \operatorname{MAD}([a_1, a_2, \ldots, a_i])$ for all $1 \le i \le n$, and then set $a_i := b_i$ for all $1 \le i \le n$.
Find the value of $sum$ after the process.
Input: The first line contains an integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) β the number of test cases.
For each test case:
* The first line contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the size of the array $a$; * The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) β the elements of the array.
It is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
Output: For each test case, output the value of $sum$ in a new line.
Examples:
input:
4
1
1
3
2 2 3
4
2 1 1 2
4
4 4 4 4
output:
1
13
9
40
Note: In the first test case, $a=[1]$ initially.
In the first loop:
1. Set $sum := sum + a_1 = 0+1=1$; 2. Set $b_1 :=\ \operatorname{MAD}([a_1])=\ \operatorname{MAD}([1])=0$, and then set $a_1 := b_1$.
After the first loop, $a=[0]$ and the process ends. The value of $sum$ after the process is $1$.
In the second test case, $a=[2,2,3]$ initially.
After the first loop, $a=[0,2,2]$ and $sum=7$.
After the second loop, $a=[0,0,2]$ and $sum=11$.
After the third loop, $a=[0,0,0]$ and $sum=13$. Then the process ends.
The value of $sum$ after the process is $13$.
|
1971A
|
https://codeforces.com/problemset/problem/1971/A
|
My First Sorting Problem
| 800
|
[
"implementation",
"sortings"
] |
Div. 4
| 1,000
| 256
|
You are given two integers $x$ and $y$.
Output two integers: the minimum of $x$ and $y$, followed by the maximum of $x$ and $y$.
|
The first line contains a single integer $t$ ($1 \leq t \leq 100$) β the number of test cases.
The only line of each test case contains two space-separated integers $x$ and $y$ ($0 \leq x, y \leq 9$).
|
For each test case, output two integers: the minimum of $x$ and $y$, followed by the maximum of $x$ and $y$.
|
[
[
"10\n1 9\n8 4\n1 4\n3 4\n2 0\n2 4\n6 9\n3 3\n0 0\n9 9",
"1 9\n4 8\n1 4\n3 4\n0 2\n2 4\n6 9\n3 3\n0 0\n9 9"
]
] |
Title: My First Sorting Problem
time_limit_ms: 1000
memory_limit_mb: 256
Description: You are given two integers $x$ and $y$.
Output two integers: the minimum of $x$ and $y$, followed by the maximum of $x$ and $y$.
Input: The first line contains a single integer $t$ ($1 \leq t \leq 100$) β the number of test cases.
The only line of each test case contains two space-separated integers $x$ and $y$ ($0 \leq x, y \leq 9$).
Output: For each test case, output two integers: the minimum of $x$ and $y$, followed by the maximum of $x$ and $y$.
Examples:
input:
10
1 9
8 4
1 4
3 4
2 0
2 4
6 9
3 3
0 0
9 9
output:
1 9
4 8
1 4
3 4
0 2
2 4
6 9
3 3
0 0
9 9
Note:
|
|
1994C
|
https://codeforces.com/problemset/problem/1994/C
|
Hungry Games
| 1,600
|
[
"binary search",
"dp",
"two pointers"
] |
Div. 1 + 2
| 2,000
| 256
|
Yaroslav is playing a computer game, and at one of the levels, he encountered $n$ mushrooms arranged in a row. Each mushroom has its own level of toxicity; the $i$-th mushroom from the beginning has a toxicity level of $a_i$. Yaroslav can choose two integers $1 \le l \le r \le n$, and then his character will take turns from left to right to eat mushrooms from this subsegment one by one, i.e., the mushrooms with numbers $l, l+1, l+2, \ldots, r$.
The character has a toxicity level $g$, initially equal to $0$. The computer game is defined by the number $x$ β the maximum toxicity level at any given time. When eating a mushroom with toxicity level $k$, the following happens:
1. The toxicity level of the character is increased by $k$. 2. If $g \leq x$, the process continues; otherwise, $g$ becomes zero and the process continues.
Yaroslav became interested in how many ways there are to choose the values of $l$ and $r$ such that the final value of $g$ is not zero. Help Yaroslav find this number!
|
Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) β the number of test cases. Then follows the description of the test cases.
The first line of each test case contains two integers $n$, $x$ ($1 \leq n \leq 2 \cdot 10^5, 1 \le x \le 10^9$) β the number of mushrooms and the maximum toxicity level.
The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output a single number β the number of subsegments such that the final value of $g$ will not be zero.
|
[
[
"5\n4 2\n1 1 1 1\n3 2\n1 2 3\n1 6\n10\n6 3\n1 2 1 4 3 8\n5 999999999\n999999999 999999998 1000000000 1000000000 500000000",
"8\n2\n0\n10\n7"
]
] |
In the first test case, the subsegments $(1, 1)$, $(1, 2)$, $(1, 4)$, $(2, 2)$, $(2, 3)$, $(3, 3)$, $(3, 4)$ and $(4, 4)$ are suitable.
In the second test case, non-zero $g$ will remain only on the subsegments $(1, 1)$ and $(2, 2)$.
In the third test case, on the only possible subsegment, $g$ will be zero.
|
Title: Hungry Games
time_limit_ms: 2000
memory_limit_mb: 256
Description: Yaroslav is playing a computer game, and at one of the levels, he encountered $n$ mushrooms arranged in a row. Each mushroom has its own level of toxicity; the $i$-th mushroom from the beginning has a toxicity level of $a_i$. Yaroslav can choose two integers $1 \le l \le r \le n$, and then his character will take turns from left to right to eat mushrooms from this subsegment one by one, i.e., the mushrooms with numbers $l, l+1, l+2, \ldots, r$.
The character has a toxicity level $g$, initially equal to $0$. The computer game is defined by the number $x$ β the maximum toxicity level at any given time. When eating a mushroom with toxicity level $k$, the following happens:
1. The toxicity level of the character is increased by $k$. 2. If $g \leq x$, the process continues; otherwise, $g$ becomes zero and the process continues.
Yaroslav became interested in how many ways there are to choose the values of $l$ and $r$ such that the final value of $g$ is not zero. Help Yaroslav find this number!
Input: Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) β the number of test cases. Then follows the description of the test cases.
The first line of each test case contains two integers $n$, $x$ ($1 \leq n \leq 2 \cdot 10^5, 1 \le x \le 10^9$) β the number of mushrooms and the maximum toxicity level.
The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output a single number β the number of subsegments such that the final value of $g$ will not be zero.
Examples:
input:
5
4 2
1 1 1 1
3 2
1 2 3
1 6
10
6 3
1 2 1 4 3 8
5 999999999
999999999 999999998 1000000000 1000000000 500000000
output:
8
2
0
10
7
Note: In the first test case, the subsegments $(1, 1)$, $(1, 2)$, $(1, 4)$, $(2, 2)$, $(2, 3)$, $(3, 3)$, $(3, 4)$ and $(4, 4)$ are suitable.
In the second test case, non-zero $g$ will remain only on the subsegments $(1, 1)$ and $(2, 2)$.
In the third test case, on the only possible subsegment, $g$ will be zero.
|
2023E
|
https://codeforces.com/problemset/problem/2023/E
|
Tree of Life
| 3,300
|
[
"dp",
"greedy",
"trees"
] |
Div. 1
| 2,000
| 512
|
In the heart of an ancient kingdom grows the legendary Tree of Life β the only one of its kind and the source of magical power for the entire world. The tree consists of $n$ nodes. Each node of this tree is a magical source, connected to other such sources through magical channels (edges). In total, there are $n-1$ channels in the tree, with the $i$-th channel connecting nodes $v_i$ and $u_i$. Moreover, there exists a unique simple path through the channels between any two nodes in the tree.
However, the magical energy flowing through these channels must be balanced; otherwise, the power of the Tree of Life may disrupt the natural order and cause catastrophic consequences. The sages of the kingdom discovered that when two magical channels converge at a single node, a dangerous "magical resonance vibration" occurs between them. To protect the Tree of Life and maintain its balance, it is necessary to select several paths and perform special rituals along them. A path is a sequence of distinct nodes $v_1, v_2, \ldots, v_k$, where each pair of adjacent nodes $v_i$ and $v_{i+1}$ is connected by a channel. When the sages perform a ritual along such a path, the resonance vibration between the channels $(v_i, v_{i+1})$ and $(v_{i+1}, v_{i+2})$ is blocked for each $1 \leq i \leq k - 2$.
The sages' task is to select the minimum number of paths and perform rituals along them to block all resonance vibrations. This means that for every pair of channels emanating from a single node, there must exist at least one selected path that contains both of these channels.
Help the sages find the minimum number of such paths so that the magical balance of the Tree of Life is preserved, and its power continues to nourish the entire world!
|
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 4 \cdot 10^4$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 5 \cdot 10^5$) β the number of nodes in the Tree of Life.
The $i$-th of the following $n - 1$ lines of each test case contains two integers $v_i$ and $u_i$ ($1 \leq v_i < u_i \leq n$) β the channel connecting nodes $v_i$ and $u_i$.
It is guaranteed that there exists a unique simple path through the channels between any two nodes.
It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.
|
For each test case, output a single integer β the minimum number of paths that the sages need to select to prevent a catastrophe.
|
[
[
"5\n4\n1 2\n2 3\n3 4\n2\n1 2\n4\n1 2\n1 3\n1 4\n8\n3 7\n2 4\n1 2\n2 5\n3 6\n1 3\n3 8\n6\n2 3\n1 2\n3 6\n1 5\n1 4",
"1\n0\n3\n7\n3"
]
] |
In the first test case, there are two pairs of channels emanating from a single node: $(1, 2)$ and $(2, 3)$, $(2, 3)$ and $(3, 4)$. It is sufficient to perform the ritual along the path $1-2-3-4$. Thus, the answer is $1$.
In the second test case, there are no pairs of channels emanating from a single node, so the answer is $0$.
In the third test case, rituals can be performed along the paths $2-1-3$, $2-1-4$, and $3-1-4$.
|
Title: Tree of Life
time_limit_ms: 2000
memory_limit_mb: 512
Description: In the heart of an ancient kingdom grows the legendary Tree of Life β the only one of its kind and the source of magical power for the entire world. The tree consists of $n$ nodes. Each node of this tree is a magical source, connected to other such sources through magical channels (edges). In total, there are $n-1$ channels in the tree, with the $i$-th channel connecting nodes $v_i$ and $u_i$. Moreover, there exists a unique simple path through the channels between any two nodes in the tree.
However, the magical energy flowing through these channels must be balanced; otherwise, the power of the Tree of Life may disrupt the natural order and cause catastrophic consequences. The sages of the kingdom discovered that when two magical channels converge at a single node, a dangerous "magical resonance vibration" occurs between them. To protect the Tree of Life and maintain its balance, it is necessary to select several paths and perform special rituals along them. A path is a sequence of distinct nodes $v_1, v_2, \ldots, v_k$, where each pair of adjacent nodes $v_i$ and $v_{i+1}$ is connected by a channel. When the sages perform a ritual along such a path, the resonance vibration between the channels $(v_i, v_{i+1})$ and $(v_{i+1}, v_{i+2})$ is blocked for each $1 \leq i \leq k - 2$.
The sages' task is to select the minimum number of paths and perform rituals along them to block all resonance vibrations. This means that for every pair of channels emanating from a single node, there must exist at least one selected path that contains both of these channels.
Help the sages find the minimum number of such paths so that the magical balance of the Tree of Life is preserved, and its power continues to nourish the entire world!
Input: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 4 \cdot 10^4$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 5 \cdot 10^5$) β the number of nodes in the Tree of Life.
The $i$-th of the following $n - 1$ lines of each test case contains two integers $v_i$ and $u_i$ ($1 \leq v_i < u_i \leq n$) β the channel connecting nodes $v_i$ and $u_i$.
It is guaranteed that there exists a unique simple path through the channels between any two nodes.
It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.
Output: For each test case, output a single integer β the minimum number of paths that the sages need to select to prevent a catastrophe.
Examples:
input:
5
4
1 2
2 3
3 4
2
1 2
4
1 2
1 3
1 4
8
3 7
2 4
1 2
2 5
3 6
1 3
3 8
6
2 3
1 2
3 6
1 5
1 4
output:
1
0
3
7
3
Note: In the first test case, there are two pairs of channels emanating from a single node: $(1, 2)$ and $(2, 3)$, $(2, 3)$ and $(3, 4)$. It is sufficient to perform the ritual along the path $1-2-3-4$. Thus, the answer is $1$.
In the second test case, there are no pairs of channels emanating from a single node, so the answer is $0$.
In the third test case, rituals can be performed along the paths $2-1-3$, $2-1-4$, and $3-1-4$.
|
1971D
|
https://codeforces.com/problemset/problem/1971/D
|
Binary Cut
| 1,100
|
[
"dp",
"greedy",
"implementation",
"sortings",
"strings"
] |
Div. 4
| 2,000
| 256
|
You are given a binary string$^{\dagger}$. Please find the minimum number of pieces you need to cut it into, so that the resulting pieces can be rearranged into a sorted binary string.

Note that:
* each character must lie in exactly one of the pieces; * the pieces must be contiguous substrings of the original string; * you must use all the pieces in the rearrangement.
$^{\dagger}$ A binary string is a string consisting of characters $\texttt{0}$ and $\texttt{1}$. A sorted binary string is a binary string such that all characters $\texttt{0}$ come before all characters $\texttt{1}$.
|
The first line contains a single integer $t$ ($1 \leq t \leq 500$) β the number of test cases.
The only line of each test case contains a single string $s$ ($1 \leq |s| \leq 500$) consisting of characters $\texttt{0}$ and $\texttt{1}$, where $|s|$ denotes the length of the string $s$.
|
For each test case, output a single integer β the minimum number of pieces needed to be able to rearrange the string into a sorted binary string.
|
[
[
"6\n11010\n00000000\n1\n10\n0001111\n0110",
"3\n1\n1\n2\n1\n2"
]
] |
The first test case is pictured in the statement. It can be proven that you can't use fewer than $3$ pieces.
In the second and third test cases, the binary string is already sorted, so only $1$ piece is needed.
In the fourth test case, you need to make a single cut between the two characters and rearrange them to make the string $\texttt{01}$.
|
Title: Binary Cut
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given a binary string$^{\dagger}$. Please find the minimum number of pieces you need to cut it into, so that the resulting pieces can be rearranged into a sorted binary string.

Note that:
* each character must lie in exactly one of the pieces; * the pieces must be contiguous substrings of the original string; * you must use all the pieces in the rearrangement.
$^{\dagger}$ A binary string is a string consisting of characters $\texttt{0}$ and $\texttt{1}$. A sorted binary string is a binary string such that all characters $\texttt{0}$ come before all characters $\texttt{1}$.
Input: The first line contains a single integer $t$ ($1 \leq t \leq 500$) β the number of test cases.
The only line of each test case contains a single string $s$ ($1 \leq |s| \leq 500$) consisting of characters $\texttt{0}$ and $\texttt{1}$, where $|s|$ denotes the length of the string $s$.
Output: For each test case, output a single integer β the minimum number of pieces needed to be able to rearrange the string into a sorted binary string.
Examples:
input:
6
11010
00000000
1
10
0001111
0110
output:
3
1
1
2
1
2
Note: The first test case is pictured in the statement. It can be proven that you can't use fewer than $3$ pieces.
In the second and third test cases, the binary string is already sorted, so only $1$ piece is needed.
In the fourth test case, you need to make a single cut between the two characters and rearrange them to make the string $\texttt{01}$.
|
1972C
|
https://codeforces.com/problemset/problem/1972/C
|
Permutation Counting
| 1,400
|
[
"binary search",
"constructive algorithms",
"greedy",
"implementation",
"math",
"sortings"
] |
Div. 2
| 2,000
| 256
|
You have some cards. An integer between $1$ and $n$ is written on each card: specifically, for each $i$ from $1$ to $n$, you have $a_i$ cards which have the number $i$ written on them.
There is also a shop which contains unlimited cards of each type. You have $k$ coins, so you can buy $k$ new cards in total, and the cards you buy can contain any integer between $1$ and $n$.
After buying the new cards, you rearrange all your cards in a line. The score of a rearrangement is the number of (contiguous) subarrays of length $n$ which are a permutation of $[1, 2, \ldots, n]$. What's the maximum score you can get?
|
Each test contains multiple test cases. The first line contains the number of test cases $t\ (1\le t\le 100)$. The description of the test cases follows.
The first line of each test case contains two integers $n$, $k$ ($1\le n \le 2 \cdot 10^5$, $0\le k \le 10^{12}$) β the number of distinct types of cards and the number of coins.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^{12}$) β the number of cards of type $i$ you have at the beginning.
It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.
|
For each test case, output a single line containing an integer: the maximum score you can get.
|
[
[
"8\n1 10\n1\n2 4\n8 4\n3 4\n6 1 8\n3 9\n7 6 2\n5 3\n6 6 7 4 6\n9 7\n7 6 1 7 6 2 4 3 3\n10 10\n1 3 1 2 1 9 3 5 7 5\n9 8\n5 8 7 5 1 3 2 9 8",
"11\n15\n15\n22\n28\n32\n28\n36"
]
] |
In the first test case, the final (and only) array we can get is $[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]$ (including $11$ single $1$s), which contains $11$ subarrays consisting of a permutation of $[1]$.
In the second test case, we can buy $0$ cards of type $1$ and $4$ cards of type $2$, and then we rearrange the cards as following: $[1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]$. There are $8$ subarrays equal to $[1, 2]$ and $7$ subarrays equal to $[2, 1]$, which make a total of $15$ subarrays which are a permutation of $[1, 2]$. It can also be proved that this is the maximum score we can get.
In the third test case, one of the possible optimal rearrangements is $[3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 3]$.
|
Title: Permutation Counting
time_limit_ms: 2000
memory_limit_mb: 256
Description: You have some cards. An integer between $1$ and $n$ is written on each card: specifically, for each $i$ from $1$ to $n$, you have $a_i$ cards which have the number $i$ written on them.
There is also a shop which contains unlimited cards of each type. You have $k$ coins, so you can buy $k$ new cards in total, and the cards you buy can contain any integer between $1$ and $n$.
After buying the new cards, you rearrange all your cards in a line. The score of a rearrangement is the number of (contiguous) subarrays of length $n$ which are a permutation of $[1, 2, \ldots, n]$. What's the maximum score you can get?
Input: Each test contains multiple test cases. The first line contains the number of test cases $t\ (1\le t\le 100)$. The description of the test cases follows.
The first line of each test case contains two integers $n$, $k$ ($1\le n \le 2 \cdot 10^5$, $0\le k \le 10^{12}$) β the number of distinct types of cards and the number of coins.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^{12}$) β the number of cards of type $i$ you have at the beginning.
It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.
Output: For each test case, output a single line containing an integer: the maximum score you can get.
Examples:
input:
8
1 10
1
2 4
8 4
3 4
6 1 8
3 9
7 6 2
5 3
6 6 7 4 6
9 7
7 6 1 7 6 2 4 3 3
10 10
1 3 1 2 1 9 3 5 7 5
9 8
5 8 7 5 1 3 2 9 8
output:
11
15
15
22
28
32
28
36
Note: In the first test case, the final (and only) array we can get is $[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]$ (including $11$ single $1$s), which contains $11$ subarrays consisting of a permutation of $[1]$.
In the second test case, we can buy $0$ cards of type $1$ and $4$ cards of type $2$, and then we rearrange the cards as following: $[1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]$. There are $8$ subarrays equal to $[1, 2]$ and $7$ subarrays equal to $[2, 1]$, which make a total of $15$ subarrays which are a permutation of $[1, 2]$. It can also be proved that this is the maximum score we can get.
In the third test case, one of the possible optimal rearrangements is $[3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 3]$.
|
1978B
|
https://codeforces.com/problemset/problem/1978/B
|
New Bakery
| 800
|
[
"binary search",
"greedy",
"math",
"ternary search"
] |
Div. 2
| 1,000
| 256
|
Bob decided to open a bakery. On the opening day, he baked $n$ buns that he can sell. The usual price of a bun is $a$ coins, but to attract customers, Bob organized the following promotion:
* Bob chooses some integer $k$ ($0 \le k \le \min(n, b)$). * Bob sells the first $k$ buns at a modified price. In this case, the price of the $i$-th ($1 \le i \le k$) sold bun is $(b - i + 1)$ coins. * The remaining $(n - k)$ buns are sold at $a$ coins each.
Note that $k$ can be equal to $0$. In this case, Bob will sell all the buns at $a$ coins each.
Help Bob determine the maximum profit he can obtain by selling all $n$ buns.
|
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The description of the test cases follows.
The only line of each test case contains three integers $n$, $a$, and $b$ ($1 \le n, a, b \le 10^9$) β the number of buns, the usual price of a bun, and the price of the first bun to be sold at a modified price.
|
For each test case, output a single integer β the maximum profit that Bob can obtain.
|
[
[
"7\n4 4 5\n5 5 9\n10 10 5\n5 5 11\n1000000000 1000000000 1000000000\n1000000000 1000000000 1\n1000 1 1000",
"17\n35\n100\n45\n1000000000000000000\n1000000000000000000\n500500"
]
] |
In the first test case, it is optimal for Bob to choose $k = 1$. Then he will sell one bun for $5$ coins, and three buns at the usual price for $4$ coins each. Then the profit will be $5 + 4 + 4 + 4 = 17$ coins.
In the second test case, it is optimal for Bob to choose $k = 5$. Then he will sell all the buns at the modified price and obtain a profit of $9 + 8 + 7 + 6 + 5 = 35$ coins.
In the third test case, it is optimal for Bob to choose $k = 0$. Then he will sell all the buns at the usual price and obtain a profit of $10 \cdot 10 = 100$ coins.
|
Title: New Bakery
time_limit_ms: 1000
memory_limit_mb: 256
Description: Bob decided to open a bakery. On the opening day, he baked $n$ buns that he can sell. The usual price of a bun is $a$ coins, but to attract customers, Bob organized the following promotion:
* Bob chooses some integer $k$ ($0 \le k \le \min(n, b)$). * Bob sells the first $k$ buns at a modified price. In this case, the price of the $i$-th ($1 \le i \le k$) sold bun is $(b - i + 1)$ coins. * The remaining $(n - k)$ buns are sold at $a$ coins each.
Note that $k$ can be equal to $0$. In this case, Bob will sell all the buns at $a$ coins each.
Help Bob determine the maximum profit he can obtain by selling all $n$ buns.
Input: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The description of the test cases follows.
The only line of each test case contains three integers $n$, $a$, and $b$ ($1 \le n, a, b \le 10^9$) β the number of buns, the usual price of a bun, and the price of the first bun to be sold at a modified price.
Output: For each test case, output a single integer β the maximum profit that Bob can obtain.
Examples:
input:
7
4 4 5
5 5 9
10 10 5
5 5 11
1000000000 1000000000 1000000000
1000000000 1000000000 1
1000 1 1000
output:
17
35
100
45
1000000000000000000
1000000000000000000
500500
Note: In the first test case, it is optimal for Bob to choose $k = 1$. Then he will sell one bun for $5$ coins, and three buns at the usual price for $4$ coins each. Then the profit will be $5 + 4 + 4 + 4 = 17$ coins.
In the second test case, it is optimal for Bob to choose $k = 5$. Then he will sell all the buns at the modified price and obtain a profit of $9 + 8 + 7 + 6 + 5 = 35$ coins.
In the third test case, it is optimal for Bob to choose $k = 0$. Then he will sell all the buns at the usual price and obtain a profit of $10 \cdot 10 = 100$ coins.
|
1985D
|
https://codeforces.com/problemset/problem/1985/D
|
Manhattan Circle
| 900
|
[
"implementation",
"math"
] |
Div. 4
| 2,000
| 256
|
Given a $n$ by $m$ grid consisting of '.' and '#' characters, there exists a whole manhattan circle on the grid. The top left corner of the grid has coordinates $(1,1)$, and the bottom right corner has coordinates $(n, m)$.
Point ($a, b$) belongs to the manhattan circle centered at ($h, k$) if $|h - a| + |k - b| < r$, where $r$ is a positive constant.
On the grid, the set of points that are part of the manhattan circle is marked as '#'. Find the coordinates of the center of the circle.
|
The first line contains $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains $n$ and $m$ ($1 \leq n \cdot m \leq 2 \cdot 10^5$) β the height and width of the grid, respectively.
The next $n$ lines contains $m$ characters '.' or '#'. If the character is '#', then the point is part of the manhattan circle.
It is guaranteed the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$, and there is a whole manhattan circle on the grid.
|
For each test case, output the two integers, the coordinates of the center of the circle.
|
[
[
"6\n5 5\n.....\n.....\n..#..\n.....\n.....\n5 5\n..#..\n.###.\n#####\n.###.\n..#..\n5 6\n......\n......\n.#....\n###...\n.#....\n1 1\n#\n5 6\n...#..\n..###.\n.#####\n..###.\n...#..\n2 10\n..........\n...#......",
"3 3\n3 3\n4 2\n1 1\n3 4\n2 4"
]
] |
Title: Manhattan Circle
time_limit_ms: 2000
memory_limit_mb: 256
Description: Given a $n$ by $m$ grid consisting of '.' and '#' characters, there exists a whole manhattan circle on the grid. The top left corner of the grid has coordinates $(1,1)$, and the bottom right corner has coordinates $(n, m)$.
Point ($a, b$) belongs to the manhattan circle centered at ($h, k$) if $|h - a| + |k - b| < r$, where $r$ is a positive constant.
On the grid, the set of points that are part of the manhattan circle is marked as '#'. Find the coordinates of the center of the circle.
Input: The first line contains $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains $n$ and $m$ ($1 \leq n \cdot m \leq 2 \cdot 10^5$) β the height and width of the grid, respectively.
The next $n$ lines contains $m$ characters '.' or '#'. If the character is '#', then the point is part of the manhattan circle.
It is guaranteed the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$, and there is a whole manhattan circle on the grid.
Output: For each test case, output the two integers, the coordinates of the center of the circle.
Examples:
input:
6
5 5
.....
.....
..#..
.....
.....
5 5
..#..
.###.
#####
.###.
..#..
5 6
......
......
.#....
###...
.#....
1 1
#
5 6
...#..
..###.
.#####
..###.
...#..
2 10
..........
...#......
output:
3 3
3 3
4 2
1 1
3 4
2 4
Note:
|
|
1987D
|
https://codeforces.com/problemset/problem/1987/D
|
World is Mine
| 1,800
|
[
"dp",
"games"
] |
Div. 1 + 2
| 2,000
| 256
|
Alice and Bob are playing a game. Initially, there are $n$ cakes, with the $i$-th cake having a tastiness value of $a_i$.
Alice and Bob take turns eating them, with Alice starting first:
* In her turn, Alice chooses and eats any remaining cake whose tastiness is strictly greater than the maximum tastiness of any of the cakes she's eaten before that. Note that on the first turn, she can choose any cake. * In his turn, Bob chooses any remaining cake and eats it.
The game ends when the current player can't eat a suitable cake. Let $x$ be the number of cakes that Alice ate. Then, Alice wants to maximize $x$, while Bob wants to minimize $x$.
Find out how many cakes Alice will eat if both players play optimally.
|
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$) β the number of cakes.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) β the tastiness values of the cakes.
It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$.
|
For each test case, output a single integer β the number of cakes Alice will eat if both players play optimally.
|
[
[
"9\n4\n1 4 2 3\n3\n1 1 1\n5\n1 4 2 3 4\n4\n3 4 1 4\n1\n1\n8\n4 3 2 5 6 8 3 4\n7\n6 1 1 3 5 3 1\n11\n6 11 6 8 7 5 3 11 2 3 5\n17\n2 6 5 3 9 1 6 2 5 6 3 2 3 9 6 1 6",
"2\n1\n3\n2\n1\n3\n2\n4\n4"
]
] |
In the first test case, one possible sequence of turns is:
1. Alice eats a cake with a tastiness value of $1$. The remaining cakes are $[4, 2, 3]$. 2. Bob eats a cake with a tastiness value of $2$. The remaining cakes are $[4, 3]$. 3. Alice eats a cake with a tastiness of $3$. The remaining cakes are $[4]$. 4. Bob eats a cake with a tastiness value of $4$. The remaining cakes are $[]$. 5. Since there are no more cakes left, the game ends.
In the second test case, one possible sequence of turns is:
1. Alice eats a cake with a tastiness value of $1$. The remaining cakes are $[1, 1]$. 2. Bob eats a cake with a tastiness value of $1$. The remaining cakes are $[1]$. 3. Since Alice has already eaten a cake with a tastiness value of $1$, she cannot make a turn, so the game ends.
|
Title: World is Mine
time_limit_ms: 2000
memory_limit_mb: 256
Description: Alice and Bob are playing a game. Initially, there are $n$ cakes, with the $i$-th cake having a tastiness value of $a_i$.
Alice and Bob take turns eating them, with Alice starting first:
* In her turn, Alice chooses and eats any remaining cake whose tastiness is strictly greater than the maximum tastiness of any of the cakes she's eaten before that. Note that on the first turn, she can choose any cake. * In his turn, Bob chooses any remaining cake and eats it.
The game ends when the current player can't eat a suitable cake. Let $x$ be the number of cakes that Alice ate. Then, Alice wants to maximize $x$, while Bob wants to minimize $x$.
Find out how many cakes Alice will eat if both players play optimally.
Input: Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$) β the number of cakes.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) β the tastiness values of the cakes.
It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$.
Output: For each test case, output a single integer β the number of cakes Alice will eat if both players play optimally.
Examples:
input:
9
4
1 4 2 3
3
1 1 1
5
1 4 2 3 4
4
3 4 1 4
1
1
8
4 3 2 5 6 8 3 4
7
6 1 1 3 5 3 1
11
6 11 6 8 7 5 3 11 2 3 5
17
2 6 5 3 9 1 6 2 5 6 3 2 3 9 6 1 6
output:
2
1
3
2
1
3
2
4
4
Note: In the first test case, one possible sequence of turns is:
1. Alice eats a cake with a tastiness value of $1$. The remaining cakes are $[4, 2, 3]$. 2. Bob eats a cake with a tastiness value of $2$. The remaining cakes are $[4, 3]$. 3. Alice eats a cake with a tastiness of $3$. The remaining cakes are $[4]$. 4. Bob eats a cake with a tastiness value of $4$. The remaining cakes are $[]$. 5. Since there are no more cakes left, the game ends.
In the second test case, one possible sequence of turns is:
1. Alice eats a cake with a tastiness value of $1$. The remaining cakes are $[1, 1]$. 2. Bob eats a cake with a tastiness value of $1$. The remaining cakes are $[1]$. 3. Since Alice has already eaten a cake with a tastiness value of $1$, she cannot make a turn, so the game ends.
|
1980A
|
https://codeforces.com/problemset/problem/1980/A
|
Problem Generator
| 800
|
[
"math"
] |
Div. 3
| 1,000
| 256
|
Vlad is planning to hold $m$ rounds next month. Each round should contain one problem of difficulty levels 'A', 'B', 'C', 'D', 'E', 'F', and 'G'.
Vlad already has a bank of $n$ problems, where the $i$-th problem has a difficulty level of $a_i$. There may not be enough of these problems, so he may have to come up with a few more problems.
Vlad wants to come up with as few problems as possible, so he asks you to find the minimum number of problems he needs to come up with in order to hold $m$ rounds.
For example, if $m=1$, $n = 10$, $a=$ 'BGECDCBDED', then he needs to come up with two problems: one of difficulty level 'A' and one of difficulty level 'F'.
|
The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 50$, $1 \le m \le 5$) β the number of problems in the bank and the number of upcoming rounds, respectively.
The second line of each test case contains a string $a$ of $n$ characters from 'A' to 'G' β the difficulties of the problems in the bank.
|
For each test case, output a single integer β the minimum number of problems that need to come up with to hold $m$ rounds.
|
[
[
"3\n10 1\nBGECDCBDED\n10 2\nBGECDCBDED\n9 1\nBBCDEFFGG",
"2\n5\n1"
]
] |
Title: Problem Generator
time_limit_ms: 1000
memory_limit_mb: 256
Description: Vlad is planning to hold $m$ rounds next month. Each round should contain one problem of difficulty levels 'A', 'B', 'C', 'D', 'E', 'F', and 'G'.
Vlad already has a bank of $n$ problems, where the $i$-th problem has a difficulty level of $a_i$. There may not be enough of these problems, so he may have to come up with a few more problems.
Vlad wants to come up with as few problems as possible, so he asks you to find the minimum number of problems he needs to come up with in order to hold $m$ rounds.
For example, if $m=1$, $n = 10$, $a=$ 'BGECDCBDED', then he needs to come up with two problems: one of difficulty level 'A' and one of difficulty level 'F'.
Input: The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 50$, $1 \le m \le 5$) β the number of problems in the bank and the number of upcoming rounds, respectively.
The second line of each test case contains a string $a$ of $n$ characters from 'A' to 'G' β the difficulties of the problems in the bank.
Output: For each test case, output a single integer β the minimum number of problems that need to come up with to hold $m$ rounds.
Examples:
input:
3
10 1
BGECDCBDED
10 2
BGECDCBDED
9 1
BBCDEFFGG
output:
2
5
1
Note:
|
|
1984C2
|
https://codeforces.com/problemset/problem/1984/C2
|
Magnitude (Hard Version)
| 1,700
|
[
"combinatorics",
"dp",
"greedy",
"math"
] |
Div. 1 + 2
| 2,000
| 256
|
The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved.
You are given an array $a$ of length $n$. Start with $c = 0$. Then, for each $i$ from $1$ to $n$ (in increasing order) do exactly one of the following:
* Option $1$: set $c$ to $c + a_i$. * Option $2$: set $c$ to $|c + a_i|$, where $|x|$ is the absolute value of $x$.
Let the maximum final value of $c$ after the procedure described above be equal to $k$. Find the number of unique procedures that result in $c = k$. Two procedures are different if at any index $i$, one procedure chose option $1$ and another chose option $2$, even if the value of $c$ is equal for both procedures after that turn.
Since the answer may be large, output it modulo $998\,244\,353$.
|
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$).
The sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
|
For each test case, output a single integer β the number of unique procedures that result in $c = k$, modulo $998\,244\,353$.
|
[
[
"5\n4\n2 -5 3 -3\n8\n1 4 3 4 1 4 3 4\n3\n-1 -2 -3\n4\n-1000000000 1000000000 1000000000 1000000000\n4\n1 9 8 4",
"12\n256\n1\n8\n16"
]
] |
In the first test case, it can be shown that our maximal final value of $c$ is $3$. There are $12$ ways to achieve this because in order to get $3$, we have to take absolute value at indices $2$ or $4$, or both, resulting in $3$ ways. For the other two indices, it doesn't change the value whether we take absolute value or not, so we have $2 \cdot 2 = 4$ ways for them. In total, we have $3 \cdot 4 = 12$ ways.
In the second test case, taking the absolute value will never change anything, so we can either take absolute value or not, for every index. This gives us $2^8 = 256$ possible ways.
|
Title: Magnitude (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved.
You are given an array $a$ of length $n$. Start with $c = 0$. Then, for each $i$ from $1$ to $n$ (in increasing order) do exactly one of the following:
* Option $1$: set $c$ to $c + a_i$. * Option $2$: set $c$ to $|c + a_i|$, where $|x|$ is the absolute value of $x$.
Let the maximum final value of $c$ after the procedure described above be equal to $k$. Find the number of unique procedures that result in $c = k$. Two procedures are different if at any index $i$, one procedure chose option $1$ and another chose option $2$, even if the value of $c$ is equal for both procedures after that turn.
Since the answer may be large, output it modulo $998\,244\,353$.
Input: The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$).
The sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
Output: For each test case, output a single integer β the number of unique procedures that result in $c = k$, modulo $998\,244\,353$.
Examples:
input:
5
4
2 -5 3 -3
8
1 4 3 4 1 4 3 4
3
-1 -2 -3
4
-1000000000 1000000000 1000000000 1000000000
4
1 9 8 4
output:
12
256
1
8
16
Note: In the first test case, it can be shown that our maximal final value of $c$ is $3$. There are $12$ ways to achieve this because in order to get $3$, we have to take absolute value at indices $2$ or $4$, or both, resulting in $3$ ways. For the other two indices, it doesn't change the value whether we take absolute value or not, so we have $2 \cdot 2 = 4$ ways for them. In total, we have $3 \cdot 4 = 12$ ways.
In the second test case, taking the absolute value will never change anything, so we can either take absolute value or not, for every index. This gives us $2^8 = 256$ possible ways.
|
1976D
|
https://codeforces.com/problemset/problem/1976/D
|
Invertible Bracket Sequences
| 2,000
|
[
"binary search",
"combinatorics",
"data structures",
"divide and conquer",
"implementation",
"two pointers"
] |
Div. 2
| 2,000
| 256
|
A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example:
* bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"); * bracket sequences ")(", "(" and ")" are not.
Let's define the inverse of the bracket sequence as follows: replace all brackets '(' with ')', and vice versa (all brackets ')' with '('). For example, strings "()((" and ")())" are inverses of each other.
You are given a regular bracket sequence $s$. Calculate the number of pairs of integers $(l,r)$ ($1 \le l \le r \le |s|$) such that if you replace the substring of $s$ from the $l$-th character to the $r$-th character (inclusive) with its inverse, $s$ will still be a regular bracket sequence.
|
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The only line of each test case contains a non-empty regular bracket sequence; it consists only of characters '(' and/or ')'.
Additional constraint on the input: the total length of the regular bracket sequences over all test cases doesn't exceed $2 \cdot 10^5$.
|
For each test case, print a single integer β the number of pairs $(l,r)$ meeting the conditions from the statement.
|
[
[
"4\n(())\n()\n()()()\n(()())(())",
"1\n0\n3\n13"
]
] |
In the first example, there is only one pair:
* $(2, 3)$: (()) $\rightarrow$ ()().
In the second example, there are no pairs.
In the third example, there are three pairs:
* $(2, 3)$: ()()() $\rightarrow$ (())(); * $(4, 5)$: ()()() $\rightarrow$ ()(()); * $(2, 5)$: ()()() $\rightarrow$ (()());
|
Title: Invertible Bracket Sequences
time_limit_ms: 2000
memory_limit_mb: 256
Description: A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example:
* bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"); * bracket sequences ")(", "(" and ")" are not.
Let's define the inverse of the bracket sequence as follows: replace all brackets '(' with ')', and vice versa (all brackets ')' with '('). For example, strings "()((" and ")())" are inverses of each other.
You are given a regular bracket sequence $s$. Calculate the number of pairs of integers $(l,r)$ ($1 \le l \le r \le |s|$) such that if you replace the substring of $s$ from the $l$-th character to the $r$-th character (inclusive) with its inverse, $s$ will still be a regular bracket sequence.
Input: The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The only line of each test case contains a non-empty regular bracket sequence; it consists only of characters '(' and/or ')'.
Additional constraint on the input: the total length of the regular bracket sequences over all test cases doesn't exceed $2 \cdot 10^5$.
Output: For each test case, print a single integer β the number of pairs $(l,r)$ meeting the conditions from the statement.
Examples:
input:
4
(())
()
()()()
(()())(())
output:
1
0
3
13
Note: In the first example, there is only one pair:
* $(2, 3)$: (()) $\rightarrow$ ()().
In the second example, there are no pairs.
In the third example, there are three pairs:
* $(2, 3)$: ()()() $\rightarrow$ (())(); * $(4, 5)$: ()()() $\rightarrow$ ()(()); * $(2, 5)$: ()()() $\rightarrow$ (()());
|
2002F1
|
https://codeforces.com/problemset/problem/2002/F1
|
Court Blue (Easy Version)
| 2,600
|
[
"brute force",
"dfs and similar",
"dp",
"math",
"number theory"
] |
Div. 1 + 2
| 3,000
| 512
|
This is the easy version of the problem. In this version, $n=m$ and the time limit is lower. You can make hacks only if both versions of the problem are solved.
In the court of the Blue King, Lelle and Flamm are having a performance match. The match consists of several rounds. In each round, either Lelle or Flamm wins.
Let $W_L$ and $W_F$ denote the number of wins of Lelle and Flamm, respectively. The Blue King considers a match to be successful if and only if:
* after every round, $\gcd(W_L,W_F)\le 1$; * at the end of the match, $W_L\le n, W_F\le m$.
Note that $\gcd(0,x)=\gcd(x,0)=x$ for every non-negative integer $x$.
Lelle and Flamm can decide to stop the match whenever they want, and the final score of the performance is $l \cdot W_L + f \cdot W_F$.
Please help Lelle and Flamm coordinate their wins and losses such that the performance is successful, and the total score of the performance is maximized.
|
The first line contains an integer $t$ ($1\leq t \leq 10^3$) β the number of test cases.
The only line of each test case contains four integers $n$, $m$, $l$, $f$ ($2\leq n\leq m \leq 2\cdot 10^7$, $1\leq l,f \leq 10^9$, $\bf{n=m}$): $n$, $m$ gives the upper bound on the number of Lelle and Flamm's wins, $l$ and $f$ determine the final score of the performance.
Unusual additional constraint: it is guaranteed that, for each test, there are no pairs of test cases with the same pair of $n$, $m$.
|
For each test case, output a single integer β the maximum total score of a successful performance.
|
[
[
"8\n3 3 2 5\n4 4 1 4\n6 6 2 2\n7 7 2 3\n9 9 9 1\n2 2 1 4\n5 5 1 4\n8 8 6 7",
"19\n17\n18\n33\n86\n9\n24\n86"
],
[
"1\n20000000 20000000 1341 331",
"33439999007"
],
[
"2\n1984 1984 19 84\n9982 9982 44 35",
"204143\n788403"
]
] |
In the first test case, a possible performance is as follows:
* Flamm wins, $\gcd(0,1)=1$. * Lelle wins, $\gcd(1,1)=1$. * Flamm wins, $\gcd(1,2)=1$. * Flamm wins, $\gcd(1,3)=1$. * Lelle wins, $\gcd(2,3)=1$. * Lelle and Flamm agree to stop the match.
The final score is $2\cdot2+3\cdot5=19$.
In the third test case, a possible performance is as follows:
* Flamm wins, $\gcd(0,1)=1$. * Lelle wins, $\gcd(1,1)=1$. * Lelle wins, $\gcd(2,1)=1$. * Lelle wins, $\gcd(3,1)=1$. * Lelle wins, $\gcd(4,1)=1$. * Lelle wins, $\gcd(5,1)=1$. * Flamm wins, $\gcd(5,2)=1$. * Flamm wins, $\gcd(5,3)=1$. * Flamm wins, $\gcd(5,4)=1$. * Lelle and Flamm agree to stop the match.
The final score is $5\cdot2+4\cdot2=18$. Note that Lelle and Flamm can stop the match even if neither of them has $n$ wins.
|
Title: Court Blue (Easy Version)
time_limit_ms: 3000
memory_limit_mb: 512
Description: This is the easy version of the problem. In this version, $n=m$ and the time limit is lower. You can make hacks only if both versions of the problem are solved.
In the court of the Blue King, Lelle and Flamm are having a performance match. The match consists of several rounds. In each round, either Lelle or Flamm wins.
Let $W_L$ and $W_F$ denote the number of wins of Lelle and Flamm, respectively. The Blue King considers a match to be successful if and only if:
* after every round, $\gcd(W_L,W_F)\le 1$; * at the end of the match, $W_L\le n, W_F\le m$.
Note that $\gcd(0,x)=\gcd(x,0)=x$ for every non-negative integer $x$.
Lelle and Flamm can decide to stop the match whenever they want, and the final score of the performance is $l \cdot W_L + f \cdot W_F$.
Please help Lelle and Flamm coordinate their wins and losses such that the performance is successful, and the total score of the performance is maximized.
Input: The first line contains an integer $t$ ($1\leq t \leq 10^3$) β the number of test cases.
The only line of each test case contains four integers $n$, $m$, $l$, $f$ ($2\leq n\leq m \leq 2\cdot 10^7$, $1\leq l,f \leq 10^9$, $\bf{n=m}$): $n$, $m$ gives the upper bound on the number of Lelle and Flamm's wins, $l$ and $f$ determine the final score of the performance.
Unusual additional constraint: it is guaranteed that, for each test, there are no pairs of test cases with the same pair of $n$, $m$.
Output: For each test case, output a single integer β the maximum total score of a successful performance.
Examples:
input:
8
3 3 2 5
4 4 1 4
6 6 2 2
7 7 2 3
9 9 9 1
2 2 1 4
5 5 1 4
8 8 6 7
output:
19
17
18
33
86
9
24
86
input:
1
20000000 20000000 1341 331
output:
33439999007
input:
2
1984 1984 19 84
9982 9982 44 35
output:
204143
788403
Note: In the first test case, a possible performance is as follows:
* Flamm wins, $\gcd(0,1)=1$. * Lelle wins, $\gcd(1,1)=1$. * Flamm wins, $\gcd(1,2)=1$. * Flamm wins, $\gcd(1,3)=1$. * Lelle wins, $\gcd(2,3)=1$. * Lelle and Flamm agree to stop the match.
The final score is $2\cdot2+3\cdot5=19$.
In the third test case, a possible performance is as follows:
* Flamm wins, $\gcd(0,1)=1$. * Lelle wins, $\gcd(1,1)=1$. * Lelle wins, $\gcd(2,1)=1$. * Lelle wins, $\gcd(3,1)=1$. * Lelle wins, $\gcd(4,1)=1$. * Lelle wins, $\gcd(5,1)=1$. * Flamm wins, $\gcd(5,2)=1$. * Flamm wins, $\gcd(5,3)=1$. * Flamm wins, $\gcd(5,4)=1$. * Lelle and Flamm agree to stop the match.
The final score is $5\cdot2+4\cdot2=18$. Note that Lelle and Flamm can stop the match even if neither of them has $n$ wins.
|
2027B
|
https://codeforces.com/problemset/problem/2027/B
|
Stalin Sort
| 1,100
|
[
"brute force",
"greedy"
] |
Div. 2
| 1,000
| 256
|
Stalin Sort is a humorous sorting algorithm designed to eliminate elements which are out of place instead of bothering to sort them properly, lending itself to an $\mathcal{O}(n)$ time complexity.
It goes as follows: starting from the second element in the array, if it is strictly smaller than the previous element (ignoring those which have already been deleted), then delete it. Continue iterating through the array until it is sorted in non-decreasing order. For example, the array $[1, 4, 2, 3, 6, 5, 5, 7, 7]$ becomes $[1, 4, 6, 7, 7]$ after a Stalin Sort.
We define an array as vulnerable if you can sort it in non-increasing order by repeatedly applying a Stalin Sort to any of its subarrays$^{\text{β}}$, as many times as is needed.
Given an array $a$ of $n$ integers, determine the minimum number of integers which must be removed from the array to make it vulnerable.
$^{\text{β}}$An array $a$ is a subarray of an array $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
|
Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases. This is followed by descriptions of the test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2000$) β the size of the array.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$.
|
For each test case, output a single integer β the minimum number of integers which must be removed from the array to make it vulnerable.
|
[
[
"6\n7\n3 6 4 9 2 5 2\n5\n5 4 4 2 2\n8\n2 2 4 4 6 6 10 10\n1\n1000\n9\n6 8 9 10 12 9 7 5 4\n7\n300000000 600000000 400000000 900000000 200000000 400000000 200000000",
"2\n0\n6\n0\n4\n2"
]
] |
In the first test case, the optimal answer is to remove the numbers $3$ and $9$. Then we are left with $a = [6, 4, 2, 5, 2]$. To show this array is vulnerable, we can first apply a Stalin Sort on the subarray $[4, 2, 5]$ to get $a = [6, 4, 5, 2]$ and then apply a Stalin Sort on the subarray $[6, 4, 5]$ to get $a = [6, 2]$, which is non-increasing.
In the second test case, the array is already non-increasing, so we don't have to remove any integers.
|
Title: Stalin Sort
time_limit_ms: 1000
memory_limit_mb: 256
Description: Stalin Sort is a humorous sorting algorithm designed to eliminate elements which are out of place instead of bothering to sort them properly, lending itself to an $\mathcal{O}(n)$ time complexity.
It goes as follows: starting from the second element in the array, if it is strictly smaller than the previous element (ignoring those which have already been deleted), then delete it. Continue iterating through the array until it is sorted in non-decreasing order. For example, the array $[1, 4, 2, 3, 6, 5, 5, 7, 7]$ becomes $[1, 4, 6, 7, 7]$ after a Stalin Sort.
We define an array as vulnerable if you can sort it in non-increasing order by repeatedly applying a Stalin Sort to any of its subarrays$^{\text{β}}$, as many times as is needed.
Given an array $a$ of $n$ integers, determine the minimum number of integers which must be removed from the array to make it vulnerable.
$^{\text{β}}$An array $a$ is a subarray of an array $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input: Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases. This is followed by descriptions of the test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2000$) β the size of the array.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$.
Output: For each test case, output a single integer β the minimum number of integers which must be removed from the array to make it vulnerable.
Examples:
input:
6
7
3 6 4 9 2 5 2
5
5 4 4 2 2
8
2 2 4 4 6 6 10 10
1
1000
9
6 8 9 10 12 9 7 5 4
7
300000000 600000000 400000000 900000000 200000000 400000000 200000000
output:
2
0
6
0
4
2
Note: In the first test case, the optimal answer is to remove the numbers $3$ and $9$. Then we are left with $a = [6, 4, 2, 5, 2]$. To show this array is vulnerable, we can first apply a Stalin Sort on the subarray $[4, 2, 5]$ to get $a = [6, 4, 5, 2]$ and then apply a Stalin Sort on the subarray $[6, 4, 5]$ to get $a = [6, 2]$, which is non-increasing.
In the second test case, the array is already non-increasing, so we don't have to remove any integers.
|
1982F
|
https://codeforces.com/problemset/problem/1982/F
|
Sorting Problem Again
| 2,600
|
[
"binary search",
"data structures",
"sortings"
] |
Div. 2
| 2,500
| 256
|
You have an array $a$ of $n$ elements. There are also $q$ modifications of the array. Before the first modification and after each modification, you would like to know the following:
What is the minimum length subarray that needs to be sorted in non-decreasing order in order for the array $a$ to be completely sorted in non-decreasing order?
More formally, you want to select a subarray of the array $(l, r)$ with the minimum value of $r - l + 1$. After that, you will sort the elements $a_{l}, a_{l + 1}, \ldots, a_{r}$ and want the condition $a_{i} \le a_{i + 1}$ to hold for all $1 \le i < n$. If the array is already sorted in non-decreasing order, then $l$ and $r$ should be considered as equal to $-1$.
Note that finding such $(l, r)$ does not change the array in any way. The modifications themselves take the form: assign $a_{pos} = x$ for given $pos$ and $x$.
|
Each test consists of several test cases. The first line contains an integer $t$ ($1 \le t \le 10$) β the number of test cases. Then follows the description of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 5 \cdot 10^{5}$).
The second line of each test case contains $n$ integers $a_{i}$ ($0 \le |a_{i}| \le 10^{9}$) β the initial elements of the array $a$.
The third line of each test case contains a number $q$ ($0 \le q \le 5 \cdot 10^{5}$) β the number of modifications to the array.
The following $q$ lines of each test case contain two integers $pos_{i}$ ($1 \le pos_{i} \le n$) and $val_{i}$ ($0 \le |val_{i}| \le 10^{9}$) β this means that for the $i$-th modification, $a_{pos_{i}}$ is assigned the value $val_{i}$.
It is guaranteed that the sum of $n$ and the sum of $q$ for all test cases does not exceed $5 \cdot 10^{5}$.
|
For each test case, output $q + 1$ lines. Each line should contain $2$ integers $l, r$ β the boundaries of the minimum subarray, such that sorting it will make the array $a$ completely sorted. If $a$ is already sorted, then output $l = -1$, $r = -1$.
|
[
[
"2\n5\n2 2 3 4 5\n3\n2 1\n4 1\n1 1\n5\n1 2 3 4 5\n9\n1 4\n2 3\n5 2\n3 1\n1 1\n5 1\n4 1\n3 1\n2 1",
"-1 -1\n1 2\n1 4\n3 4\n-1 -1\n1 3\n1 3\n1 5\n1 5\n2 5\n2 5\n2 5\n2 5\n-1 -1"
]
] |
Let's consider the first test case:
* Initially, the array is sorted in non-decreasing order: $[2, 2, 3, 4, 5]$ * After the first query, the array looks like this: $[\color{red}{2}, \color{red}{1}, 3, 4, 5]$. * After the second query, the array looks like this: $[\color{red}{2}, \color{red}{1}, \color{red}{3}, \color{red}{1}, 5]$. * After the third query, the array looks like this: $[1, 1, \color{red}{3}, \color{red}{1}, 5]$.
The red segments indicate the subarrays that need to be sorted in order for the entire array to be sorted in non-decreasing order.
|
Title: Sorting Problem Again
time_limit_ms: 2500
memory_limit_mb: 256
Description: You have an array $a$ of $n$ elements. There are also $q$ modifications of the array. Before the first modification and after each modification, you would like to know the following:
What is the minimum length subarray that needs to be sorted in non-decreasing order in order for the array $a$ to be completely sorted in non-decreasing order?
More formally, you want to select a subarray of the array $(l, r)$ with the minimum value of $r - l + 1$. After that, you will sort the elements $a_{l}, a_{l + 1}, \ldots, a_{r}$ and want the condition $a_{i} \le a_{i + 1}$ to hold for all $1 \le i < n$. If the array is already sorted in non-decreasing order, then $l$ and $r$ should be considered as equal to $-1$.
Note that finding such $(l, r)$ does not change the array in any way. The modifications themselves take the form: assign $a_{pos} = x$ for given $pos$ and $x$.
Input: Each test consists of several test cases. The first line contains an integer $t$ ($1 \le t \le 10$) β the number of test cases. Then follows the description of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 5 \cdot 10^{5}$).
The second line of each test case contains $n$ integers $a_{i}$ ($0 \le |a_{i}| \le 10^{9}$) β the initial elements of the array $a$.
The third line of each test case contains a number $q$ ($0 \le q \le 5 \cdot 10^{5}$) β the number of modifications to the array.
The following $q$ lines of each test case contain two integers $pos_{i}$ ($1 \le pos_{i} \le n$) and $val_{i}$ ($0 \le |val_{i}| \le 10^{9}$) β this means that for the $i$-th modification, $a_{pos_{i}}$ is assigned the value $val_{i}$.
It is guaranteed that the sum of $n$ and the sum of $q$ for all test cases does not exceed $5 \cdot 10^{5}$.
Output: For each test case, output $q + 1$ lines. Each line should contain $2$ integers $l, r$ β the boundaries of the minimum subarray, such that sorting it will make the array $a$ completely sorted. If $a$ is already sorted, then output $l = -1$, $r = -1$.
Examples:
input:
2
5
2 2 3 4 5
3
2 1
4 1
1 1
5
1 2 3 4 5
9
1 4
2 3
5 2
3 1
1 1
5 1
4 1
3 1
2 1
output:
-1 -1
1 2
1 4
3 4
-1 -1
1 3
1 3
1 5
1 5
2 5
2 5
2 5
2 5
-1 -1
Note: Let's consider the first test case:
* Initially, the array is sorted in non-decreasing order: $[2, 2, 3, 4, 5]$ * After the first query, the array looks like this: $[\color{red}{2}, \color{red}{1}, 3, 4, 5]$. * After the second query, the array looks like this: $[\color{red}{2}, \color{red}{1}, \color{red}{3}, \color{red}{1}, 5]$. * After the third query, the array looks like this: $[1, 1, \color{red}{3}, \color{red}{1}, 5]$.
The red segments indicate the subarrays that need to be sorted in order for the entire array to be sorted in non-decreasing order.
|
1992B
|
https://codeforces.com/problemset/problem/1992/B
|
Angry Monk
| 800
|
[
"greedy",
"math",
"sortings"
] |
Div. 3
| 2,000
| 256
|
To celebrate his recovery, k1o0n has baked an enormous $n$ metres long potato casserole.
Turns out, Noobish_Monk just can't stand potatoes, so he decided to ruin k1o0n's meal. He has cut it into $k$ pieces, of lengths $a_1, a_2, \dots, a_k$ meters.
k1o0n wasn't keen on that. Luckily, everything can be fixed. In order to do that, k1o0n can do one of the following operations:
* Pick a piece with length $a_i \ge 2$ and divide it into two pieces with lengths $1$ and $a_i - 1$. As a result, the number of pieces will increase by $1$; * Pick a slice $a_i$ and another slice with length $a_j=1$ ($i \ne j$) and merge them into one piece with length $a_i+1$. As a result, the number of pieces will decrease by $1$.
Help k1o0n to find the minimum number of operations he needs to do in order to merge the casserole into one piece with length $n$.
For example, if $n=5$, $k=2$ and $a = [3, 2]$, it is optimal to do the following:
1. Divide the piece with length $2$ into two pieces with lengths $2-1=1$ and $1$, as a result $a = [3, 1, 1]$. 2. Merge the piece with length $3$ and the piece with length $1$, as a result $a = [4, 1]$. 3. Merge the piece with length $4$ and the piece with length $1$, as a result $a = [5]$.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$).
Description of each test case consists of two lines. The first line contains two integers $n$ and $k$ ($2 \le n \le 10^9$, $2 \le k \le 10^5$) β length of casserole and the number of pieces.
The second line contains $k$ integers $a_1, a_2, \ldots, a_k$ ($1 \le a_i \le n - 1$, $\sum a_i = n$) β lengths of pieces of casserole, which Noobish_Monk has cut.
It is guaranteed that the sum of $k$ over all $t$ test cases doesn't exceed $2 \cdot 10^5$.
|
For each test case, output the minimum number of operations K1o0n needs to restore his pie after the terror of Noobish_Monk.
|
[
[
"4\n5 3\n3 1 1\n5 2\n3 2\n11 4\n2 3 1 5\n16 6\n1 6 1 1 1 6",
"2\n3\n9\n15"
]
] |
Title: Angry Monk
time_limit_ms: 2000
memory_limit_mb: 256
Description: To celebrate his recovery, k1o0n has baked an enormous $n$ metres long potato casserole.
Turns out, Noobish_Monk just can't stand potatoes, so he decided to ruin k1o0n's meal. He has cut it into $k$ pieces, of lengths $a_1, a_2, \dots, a_k$ meters.
k1o0n wasn't keen on that. Luckily, everything can be fixed. In order to do that, k1o0n can do one of the following operations:
* Pick a piece with length $a_i \ge 2$ and divide it into two pieces with lengths $1$ and $a_i - 1$. As a result, the number of pieces will increase by $1$; * Pick a slice $a_i$ and another slice with length $a_j=1$ ($i \ne j$) and merge them into one piece with length $a_i+1$. As a result, the number of pieces will decrease by $1$.
Help k1o0n to find the minimum number of operations he needs to do in order to merge the casserole into one piece with length $n$.
For example, if $n=5$, $k=2$ and $a = [3, 2]$, it is optimal to do the following:
1. Divide the piece with length $2$ into two pieces with lengths $2-1=1$ and $1$, as a result $a = [3, 1, 1]$. 2. Merge the piece with length $3$ and the piece with length $1$, as a result $a = [4, 1]$. 3. Merge the piece with length $4$ and the piece with length $1$, as a result $a = [5]$.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$).
Description of each test case consists of two lines. The first line contains two integers $n$ and $k$ ($2 \le n \le 10^9$, $2 \le k \le 10^5$) β length of casserole and the number of pieces.
The second line contains $k$ integers $a_1, a_2, \ldots, a_k$ ($1 \le a_i \le n - 1$, $\sum a_i = n$) β lengths of pieces of casserole, which Noobish_Monk has cut.
It is guaranteed that the sum of $k$ over all $t$ test cases doesn't exceed $2 \cdot 10^5$.
Output: For each test case, output the minimum number of operations K1o0n needs to restore his pie after the terror of Noobish_Monk.
Examples:
input:
4
5 3
3 1 1
5 2
3 2
11 4
2 3 1 5
16 6
1 6 1 1 1 6
output:
2
3
9
15
Note:
|
|
1974C
|
https://codeforces.com/problemset/problem/1974/C
|
Beautiful Triple Pairs
| 1,400
|
[
"combinatorics",
"data structures"
] |
Div. 3
| 4,000
| 256
|
Polycarp was given an array $a$ of $n$ integers. He really likes triples of numbers, so for each $j$ ($1 \le j \le n - 2$) he wrote down a triple of elements $[a_j, a_{j + 1}, a_{j + 2}]$.
Polycarp considers a pair of triples $b$ and $c$ beautiful if they differ in exactly one position, that is, one of the following conditions is satisfied:
* $b_1 \ne c_1$ and $b_2 = c_2$ and $b_3 = c_3$; * $b_1 = c_1$ and $b_2 \ne c_2$ and $b_3 = c_3$; * $b_1 = c_1$ and $b_2 = c_2$ and $b_3 \ne c_3$.
Find the number of beautiful pairs of triples among the written triples $[a_j, a_{j + 1}, a_{j + 2}]$.
|
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$) β the elements of the array.
It is guaranteed that the sum of the values of $n$ for all test cases in the test does not exceed $2 \cdot 10^5$.
|
For each test case, output a single integer β the number of beautiful pairs of triples among the pairs of the form $[a_j, a_{j + 1}, a_{j + 2}]$.
Note that the answer may not fit into 32-bit data types.
|
[
[
"8\n5\n3 2 2 2 3\n5\n1 2 1 2 1\n8\n1 2 3 2 2 3 4 2\n4\n2 1 1 1\n8\n2 1 1 2 1 1 1 1\n7\n2 1 1 1 1 1 1\n6\n2 1 1 1 1 1\n5\n2 1 1 1 1",
"2\n0\n3\n1\n8\n4\n3\n2"
]
] |
In the first example, $a = [3, 2, 2, 2, 3]$, Polycarp will write the following triples:
1. $[3, 2, 2]$; 2. $[2, 2, 2]$; 3. $[2, 2, 3]$.
The beautiful pairs are triple $1$ with triple $2$ and triple $2$ with triple $3$.
In the third example, $a = [1, 2, 3, 2, 2, 3, 4, 2]$, Polycarp will write the following triples:
1. $[1, 2, 3]$; 2. $[2, 3, 2]$; 3. $[3, 2, 2]$; 4. $[2, 2, 3]$; 5. $[2, 3, 4]$; 6. $[3, 4, 2]$;
The beautiful pairs are triple $1$ with triple $4$, triple $2$ with triple $5$, and triple $3$ with triple $6$.
|
Title: Beautiful Triple Pairs
time_limit_ms: 4000
memory_limit_mb: 256
Description: Polycarp was given an array $a$ of $n$ integers. He really likes triples of numbers, so for each $j$ ($1 \le j \le n - 2$) he wrote down a triple of elements $[a_j, a_{j + 1}, a_{j + 2}]$.
Polycarp considers a pair of triples $b$ and $c$ beautiful if they differ in exactly one position, that is, one of the following conditions is satisfied:
* $b_1 \ne c_1$ and $b_2 = c_2$ and $b_3 = c_3$; * $b_1 = c_1$ and $b_2 \ne c_2$ and $b_3 = c_3$; * $b_1 = c_1$ and $b_2 = c_2$ and $b_3 \ne c_3$.
Find the number of beautiful pairs of triples among the written triples $[a_j, a_{j + 1}, a_{j + 2}]$.
Input: The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$) β the elements of the array.
It is guaranteed that the sum of the values of $n$ for all test cases in the test does not exceed $2 \cdot 10^5$.
Output: For each test case, output a single integer β the number of beautiful pairs of triples among the pairs of the form $[a_j, a_{j + 1}, a_{j + 2}]$.
Note that the answer may not fit into 32-bit data types.
Examples:
input:
8
5
3 2 2 2 3
5
1 2 1 2 1
8
1 2 3 2 2 3 4 2
4
2 1 1 1
8
2 1 1 2 1 1 1 1
7
2 1 1 1 1 1 1
6
2 1 1 1 1 1
5
2 1 1 1 1
output:
2
0
3
1
8
4
3
2
Note: In the first example, $a = [3, 2, 2, 2, 3]$, Polycarp will write the following triples:
1. $[3, 2, 2]$; 2. $[2, 2, 2]$; 3. $[2, 2, 3]$.
The beautiful pairs are triple $1$ with triple $2$ and triple $2$ with triple $3$.
In the third example, $a = [1, 2, 3, 2, 2, 3, 4, 2]$, Polycarp will write the following triples:
1. $[1, 2, 3]$; 2. $[2, 3, 2]$; 3. $[3, 2, 2]$; 4. $[2, 2, 3]$; 5. $[2, 3, 4]$; 6. $[3, 4, 2]$;
The beautiful pairs are triple $1$ with triple $4$, triple $2$ with triple $5$, and triple $3$ with triple $6$.
|
2021B
|
https://codeforces.com/problemset/problem/2021/B
|
Maximize Mex
| 1,200
|
[
"brute force",
"greedy",
"math",
"number theory"
] |
Div. 2
| 1,000
| 256
|
You are given an array $a$ of $n$ positive integers and an integer $x$. You can do the following two-step operation any (possibly zero) number of times:
1. Choose an index $i$ ($1 \leq i \leq n$). 2. Increase $a_i$ by $x$, in other words $a_i := a_i + x$.
Find the maximum value of the $\operatorname{MEX}$ of $a$ if you perform the operations optimally.
The $\operatorname{MEX}$ (minimum excluded value) of an array is the smallest non-negative integer that is not in the array. For example:
* The $\operatorname{MEX}$ of $[2,2,1]$ is $0$ because $0$ is not in the array. * The $\operatorname{MEX}$ of $[3,1,0,1]$ is $2$ because $0$ and $1$ are in the array but $2$ is not. * The $\operatorname{MEX}$ of $[0,3,1,2]$ is $4$ because $0$, $1$, $2$ and $3$ are in the array but $4$ is not.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 2 \cdot 10^5$; $1 \le x \le 10^9$) β the length of the array and the integer to be used in the operation.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) β the given array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output a single integer: the maximum $\operatorname{MEX}$ of $a$ if you perform the operations optimally.
|
[
[
"3\n6 3\n0 3 2 1 5 2\n6 2\n1 3 4 1 0 2\n4 5\n2 5 10 3",
"4\n6\n0"
]
] |
In the first test case, the $\operatorname{MEX}$ of $a$ is $4$ without performing any operations, which is the maximum.
In the second test case, the $\operatorname{MEX}$ of $a$ is $5$ without performing any operations. If we perform two operations both with $i=1$, we will have the array $a=[5,3,4,1,0,2]$. Then, the $\operatorname{MEX}$ of $a$ will become $6$, which is the maximum.
In the third test case, the $\operatorname{MEX}$ of $a$ is $0$ without performing any operations, which is the maximum.
|
Title: Maximize Mex
time_limit_ms: 1000
memory_limit_mb: 256
Description: You are given an array $a$ of $n$ positive integers and an integer $x$. You can do the following two-step operation any (possibly zero) number of times:
1. Choose an index $i$ ($1 \leq i \leq n$). 2. Increase $a_i$ by $x$, in other words $a_i := a_i + x$.
Find the maximum value of the $\operatorname{MEX}$ of $a$ if you perform the operations optimally.
The $\operatorname{MEX}$ (minimum excluded value) of an array is the smallest non-negative integer that is not in the array. For example:
* The $\operatorname{MEX}$ of $[2,2,1]$ is $0$ because $0$ is not in the array. * The $\operatorname{MEX}$ of $[3,1,0,1]$ is $2$ because $0$ and $1$ are in the array but $2$ is not. * The $\operatorname{MEX}$ of $[0,3,1,2]$ is $4$ because $0$, $1$, $2$ and $3$ are in the array but $4$ is not.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 2 \cdot 10^5$; $1 \le x \le 10^9$) β the length of the array and the integer to be used in the operation.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) β the given array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output a single integer: the maximum $\operatorname{MEX}$ of $a$ if you perform the operations optimally.
Examples:
input:
3
6 3
0 3 2 1 5 2
6 2
1 3 4 1 0 2
4 5
2 5 10 3
output:
4
6
0
Note: In the first test case, the $\operatorname{MEX}$ of $a$ is $4$ without performing any operations, which is the maximum.
In the second test case, the $\operatorname{MEX}$ of $a$ is $5$ without performing any operations. If we perform two operations both with $i=1$, we will have the array $a=[5,3,4,1,0,2]$. Then, the $\operatorname{MEX}$ of $a$ will become $6$, which is the maximum.
In the third test case, the $\operatorname{MEX}$ of $a$ is $0$ without performing any operations, which is the maximum.
|
1973F
|
https://codeforces.com/problemset/problem/1973/F
|
Maximum GCD Sum Queries
| 3,100
|
[
"bitmasks",
"brute force",
"dp",
"implementation",
"number theory"
] |
Div. 2
| 5,000
| 512
|
For $k$ positive integers $x_1, x_2, \ldots, x_k$, the value $\gcd(x_1, x_2, \ldots, x_k)$ is the greatest common divisor of the integers $x_1, x_2, \ldots, x_k$ β the largest integer $z$ such that all the integers $x_1, x_2, \ldots, x_k$ are divisible by $z$.
You are given three arrays $a_1, a_2, \ldots, a_n$, $b_1, b_2, \ldots, b_n$ and $c_1, c_2, \ldots, c_n$ of length $n$, containing positive integers.
You also have a machine that allows you to swap $a_i$ and $b_i$ for any $i$ ($1 \le i \le n$). Each swap costs you $c_i$ coins.
Find the maximum possible value of $$\gcd(a_1, a_2, \ldots, a_n) + \gcd(b_1, b_2, \ldots, b_n)$$ that you can get by paying in total at most $d$ coins for swapping some elements. The amount of coins you have changes a lot, so find the answer to this question for each of the $q$ possible values $d_1, d_2, \ldots, d_q$.
|
There are two integers on the first line β the numbers $n$ and $q$ ($1 \leq n \leq 5 \cdot 10^5$, $1 \leq q \leq 5 \cdot 10^5$).
On the second line, there are $n$ integers β the numbers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^8$).
On the third line, there are $n$ integers β the numbers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq 10^8$).
On the fourth line, there are $n$ integers β the numbers $c_1, c_2, \ldots, c_n$ ($1 \leq c_i \leq 10^9$).
On the fifth line, there are $q$ integers β the numbers $d_1, d_2, \ldots, d_q$ ($0 \leq d_i \leq 10^{15}$).
|
Print $q$ integers β the maximum value you can get for each of the $q$ possible values $d$.
|
[
[
"3 4\n1 2 3\n4 5 6\n1 1 1\n0 1 2 3",
"2 3 3 3"
],
[
"5 5\n3 4 6 8 4\n8 3 4 9 3\n10 20 30 40 50\n5 55 13 1000 113",
"2 7 3 7 7"
],
[
"1 1\n3\n4\n5\n0",
"7"
]
] |
In the first query of the first example, we are not allowed to do any swaps at all, so the answer is $\gcd(1, 2, 3) + \gcd(4, 5, 6) = 2$. In the second query, one of the ways to achieve the optimal value is to swap $a_2$ and $b_2$, then the answer is $\gcd(1, 5, 3) + \gcd(4, 2, 6) = 3$.
In the second query of the second example, it's optimal to perform swaps on positions $1$ and $3$, then the answer is $\gcd(3, 3, 6, 9, 3) + \gcd(8, 4, 4, 8, 4) = 7$ and we have to pay $40$ coins in total.
|
Title: Maximum GCD Sum Queries
time_limit_ms: 5000
memory_limit_mb: 512
Description: For $k$ positive integers $x_1, x_2, \ldots, x_k$, the value $\gcd(x_1, x_2, \ldots, x_k)$ is the greatest common divisor of the integers $x_1, x_2, \ldots, x_k$ β the largest integer $z$ such that all the integers $x_1, x_2, \ldots, x_k$ are divisible by $z$.
You are given three arrays $a_1, a_2, \ldots, a_n$, $b_1, b_2, \ldots, b_n$ and $c_1, c_2, \ldots, c_n$ of length $n$, containing positive integers.
You also have a machine that allows you to swap $a_i$ and $b_i$ for any $i$ ($1 \le i \le n$). Each swap costs you $c_i$ coins.
Find the maximum possible value of $$\gcd(a_1, a_2, \ldots, a_n) + \gcd(b_1, b_2, \ldots, b_n)$$ that you can get by paying in total at most $d$ coins for swapping some elements. The amount of coins you have changes a lot, so find the answer to this question for each of the $q$ possible values $d_1, d_2, \ldots, d_q$.
Input: There are two integers on the first line β the numbers $n$ and $q$ ($1 \leq n \leq 5 \cdot 10^5$, $1 \leq q \leq 5 \cdot 10^5$).
On the second line, there are $n$ integers β the numbers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^8$).
On the third line, there are $n$ integers β the numbers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq 10^8$).
On the fourth line, there are $n$ integers β the numbers $c_1, c_2, \ldots, c_n$ ($1 \leq c_i \leq 10^9$).
On the fifth line, there are $q$ integers β the numbers $d_1, d_2, \ldots, d_q$ ($0 \leq d_i \leq 10^{15}$).
Output: Print $q$ integers β the maximum value you can get for each of the $q$ possible values $d$.
Examples:
input:
3 4
1 2 3
4 5 6
1 1 1
0 1 2 3
output:
2 3 3 3
input:
5 5
3 4 6 8 4
8 3 4 9 3
10 20 30 40 50
5 55 13 1000 113
output:
2 7 3 7 7
input:
1 1
3
4
5
0
output:
7
Note: In the first query of the first example, we are not allowed to do any swaps at all, so the answer is $\gcd(1, 2, 3) + \gcd(4, 5, 6) = 2$. In the second query, one of the ways to achieve the optimal value is to swap $a_2$ and $b_2$, then the answer is $\gcd(1, 5, 3) + \gcd(4, 2, 6) = 3$.
In the second query of the second example, it's optimal to perform swaps on positions $1$ and $3$, then the answer is $\gcd(3, 3, 6, 9, 3) + \gcd(8, 4, 4, 8, 4) = 7$ and we have to pay $40$ coins in total.
|
1985F
|
https://codeforces.com/problemset/problem/1985/F
|
Final Boss
| 1,500
|
[
"binary search",
"data structures"
] |
Div. 4
| 2,000
| 256
|
You are facing the final boss in your favorite video game. The boss enemy has $h$ health. Your character has $n$ attacks. The $i$'th attack deals $a_i$ damage to the boss but has a cooldown of $c_i$ turns, meaning the next time you can use this attack is turn $x + c_i$ if your current turn is $x$. Each turn, you can use all attacks that are not currently on cooldown, all at once. If all attacks are on cooldown, you do nothing for the turn and skip to the next turn.
Initially, all attacks are not on cooldown. How many turns will you take to beat the boss? The boss is beaten when its health is $0$ or less.
|
The first line contains $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The first line of each test case contains two integers $h$ and $n$ ($1 \leq h, n \leq 2 \cdot 10^5$) β the health of the boss and the number of attacks you have.
The following line of each test case contains $n$ integers $a_1, a_2, ..., a_n$ ($1 \leq a_i \leq 2 \cdot 10^5$) β the damage of your attacks.
The following line of each test case contains $n$ integers $c_1, c_2, ..., c_n$ ($1 \leq c_i \leq 2 \cdot 10^5$) β the cooldown of your attacks.
It is guaranteed that the sum of $h$ and $n$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output an integer, the minimum number of turns required to beat the boss.
|
[
[
"8\n3 2\n2 1\n2 1\n5 2\n2 1\n2 1\n50 3\n5 6 7\n5 6 7\n50 3\n2 2 2\n3 3 3\n90000 2\n200000 200000\n1 1\n100000 1\n1\n200000\n6 7\n3 2 3 2 3 1 2\n6 5 9 5 10 7 7\n21 6\n1 1 1 1 1 1\n5 5 8 10 7 6",
"1\n3\n15\n25\n1\n19999800001\n1\n21"
]
] |
For the first test case, you can use attacks $1$ and $2$ on the first turn, dealing $3$ damage in total, and slaying the boss.
For the second case, you can beat the boss in $3$ turns by using the following attacks:
Turn $1$: Use attacks $1$ and $2$, dealing $3$ damage to the boss. The boss now has $2$ health left.
Turn $2$: Use attack $2$, dealing $1$ damage to the boss. The boss now has $1$ health left.
Turn $3$: Use attack $1$, dealing $2$ damage to the boss. The boss now has $-1$ health left. Since its health is less than or equal to $0$, you beat the boss.
For the sixth test case: remember to use 64-bit integers as the answer can get large.
|
Title: Final Boss
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are facing the final boss in your favorite video game. The boss enemy has $h$ health. Your character has $n$ attacks. The $i$'th attack deals $a_i$ damage to the boss but has a cooldown of $c_i$ turns, meaning the next time you can use this attack is turn $x + c_i$ if your current turn is $x$. Each turn, you can use all attacks that are not currently on cooldown, all at once. If all attacks are on cooldown, you do nothing for the turn and skip to the next turn.
Initially, all attacks are not on cooldown. How many turns will you take to beat the boss? The boss is beaten when its health is $0$ or less.
Input: The first line contains $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The first line of each test case contains two integers $h$ and $n$ ($1 \leq h, n \leq 2 \cdot 10^5$) β the health of the boss and the number of attacks you have.
The following line of each test case contains $n$ integers $a_1, a_2, ..., a_n$ ($1 \leq a_i \leq 2 \cdot 10^5$) β the damage of your attacks.
The following line of each test case contains $n$ integers $c_1, c_2, ..., c_n$ ($1 \leq c_i \leq 2 \cdot 10^5$) β the cooldown of your attacks.
It is guaranteed that the sum of $h$ and $n$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output an integer, the minimum number of turns required to beat the boss.
Examples:
input:
8
3 2
2 1
2 1
5 2
2 1
2 1
50 3
5 6 7
5 6 7
50 3
2 2 2
3 3 3
90000 2
200000 200000
1 1
100000 1
1
200000
6 7
3 2 3 2 3 1 2
6 5 9 5 10 7 7
21 6
1 1 1 1 1 1
5 5 8 10 7 6
output:
1
3
15
25
1
19999800001
1
21
Note: For the first test case, you can use attacks $1$ and $2$ on the first turn, dealing $3$ damage in total, and slaying the boss.
For the second case, you can beat the boss in $3$ turns by using the following attacks:
Turn $1$: Use attacks $1$ and $2$, dealing $3$ damage to the boss. The boss now has $2$ health left.
Turn $2$: Use attack $2$, dealing $1$ damage to the boss. The boss now has $1$ health left.
Turn $3$: Use attack $1$, dealing $2$ damage to the boss. The boss now has $-1$ health left. Since its health is less than or equal to $0$, you beat the boss.
For the sixth test case: remember to use 64-bit integers as the answer can get large.
|
2028F
|
https://codeforces.com/problemset/problem/2028/F
|
Alice's Adventures in Addition
| 2,700
|
[
"bitmasks",
"brute force",
"dp",
"implementation"
] |
Div. 2
| 3,000
| 32
|
Note that the memory limit is unusual.
The Cheshire Cat has a riddle for Alice: given $n$ integers $a_1, a_2, \ldots, a_n$ and a target $m$, is there a way to insert $+$ and $\times$ into the circles of the expression $$a_1 \circ a_2 \circ \cdots \circ a_n = m$$ to make it true? We follow the usual order of operations: $\times$ is done before $+$.
Although Alice is excellent at chess, she is not good at math. Please help her so she can find a way out of Wonderland!
|
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $n, m$ ($1\le n\le 2\cdot 10^5$; $1\le m\le 10^4$) β the number of integers and the target, respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0\le a_i\le 10^4$) β the elements of the array $a$.
The sum of $n$ over all test cases does not exceed $2\cdot 10^5$.
|
For each test case, output "YES" without quotes if it is possible to get the target by inserting $+$ or $\times$ and "NO" otherwise.
You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
|
[
[
"6\n5 4\n2 1 1 1 2\n5 5\n2 1 1 1 2\n5 6\n2 1 1 1 2\n5 7\n2 1 1 1 2\n5 8\n2 1 1 1 2\n5 6\n2 0 2 2 3",
"YES\nYES\nYES\nYES\nNO\nYES"
]
] |
Possible solutions for the first four test cases are shown below. $$\begin{align*} 2 \times 1 + 1 \times 1 \times 2 &= 4 \\\ 2 \times 1 + 1 + 1 \times 2 &= 5 \\\ 2 \times 1 + 1 + 1 \times 2 &= 6 \\\ 2 + 1 + 1 + 1 + 2 &= 7 \\\ \end{align*}$$ It is impossible to get a result of $8$ in the fifth test case.
|
Title: Alice's Adventures in Addition
time_limit_ms: 3000
memory_limit_mb: 32
Description: Note that the memory limit is unusual.
The Cheshire Cat has a riddle for Alice: given $n$ integers $a_1, a_2, \ldots, a_n$ and a target $m$, is there a way to insert $+$ and $\times$ into the circles of the expression $$a_1 \circ a_2 \circ \cdots \circ a_n = m$$ to make it true? We follow the usual order of operations: $\times$ is done before $+$.
Although Alice is excellent at chess, she is not good at math. Please help her so she can find a way out of Wonderland!
Input: Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $n, m$ ($1\le n\le 2\cdot 10^5$; $1\le m\le 10^4$) β the number of integers and the target, respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0\le a_i\le 10^4$) β the elements of the array $a$.
The sum of $n$ over all test cases does not exceed $2\cdot 10^5$.
Output: For each test case, output "YES" without quotes if it is possible to get the target by inserting $+$ or $\times$ and "NO" otherwise.
You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
Examples:
input:
6
5 4
2 1 1 1 2
5 5
2 1 1 1 2
5 6
2 1 1 1 2
5 7
2 1 1 1 2
5 8
2 1 1 1 2
5 6
2 0 2 2 3
output:
YES
YES
YES
YES
NO
YES
Note: Possible solutions for the first four test cases are shown below. $$\begin{align*} 2 \times 1 + 1 \times 1 \times 2 &= 4 \\\ 2 \times 1 + 1 + 1 \times 2 &= 5 \\\ 2 \times 1 + 1 + 1 \times 2 &= 6 \\\ 2 + 1 + 1 + 1 + 2 &= 7 \\\ \end{align*}$$ It is impossible to get a result of $8$ in the fifth test case.
|
1986C
|
https://codeforces.com/problemset/problem/1986/C
|
Update Queries
| 1,100
|
[
"data structures",
"greedy",
"sortings"
] |
Div. 3
| 2,000
| 256
|
Let's consider the following simple problem. You are given a string $s$ of length $n$, consisting of lowercase Latin letters, as well as an array of indices $ind$ of length $m$ ($1 \leq ind_i \leq n$) and a string $c$ of length $m$, consisting of lowercase Latin letters. Then, in order, you perform the update operations, namely, during the $i$-th operation, you set $s_{ind_i} = c_i$. Note that you perform all $m$ operations from the first to the last.
Of course, if you change the order of indices in the array $ind$ and/or the order of letters in the string $c$, you can get different results. Find the lexicographically smallest string $s$ that can be obtained after $m$ update operations, if you can rearrange the indices in the array $ind$ and the letters in the string $c$ as you like.
A string $a$ is lexicographically less than a string $b$ if and only if one of the following conditions is met:
* $a$ is a prefix of $b$, but $a \neq b$; * in the first position where $a$ and $b$ differ, the symbol in string $a$ is earlier in the alphabet than the corresponding symbol in string $b$.
|
Each test consists of several sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of sets of input data. Then follows their description.
The first line of each set of input data contains two integers $n$ and $m$ ($1 \leq n, m \leq 10^5$) β the length of the string $s$ and the number of updates.
The second line of each set of input data contains a string $s$ of length $n$, consisting of lowercase Latin letters.
The third line of each set of input data contains $m$ integers $ind_1, ind_2, \ldots, ind_m$ ($1 \leq ind_i \leq n$) β the array of indices $ind$.
The fourth line of each set of input data contains a string $c$ of length $m$, consisting of lowercase Latin letters.
It is guaranteed that the sum of $n$ over all sets of input data does not exceed $2 \cdot 10^5$. Similarly, the sum of $m$ over all sets of input data does not exceed $2 \cdot 10^5$.
|
For each set of input data, output the lexicographically smallest string $s$ that can be obtained by rearranging the indices in the array $ind$ and the letters in the string $c$ as you like.
|
[
[
"4\n1 2\na\n1 1\ncb\n4 4\nmeow\n1 2 1 4\nzcwz\n7 4\nabacaba\n1 3 5 7\ndamn\n7 10\ntraktor\n7 6 5 4 3 2 1 6 4 2\ncodeforces",
"b\ncwoz\nabdcmbn\nccdeefo"
]
] |
In the first set of input data, you can leave the array $ind$ and the string $c$ unchanged and simply perform all operations in that order.
In the second set of input data, you can set the array $ind = [1, 1, 4, 2]$ and $c =$ "zczw". Then the string $s$ will change as follows: $meow \rightarrow zeow \rightarrow ceow \rightarrow ceoz \rightarrow cwoz$.
In the third set of input data, you can leave the array $ind$ unchanged and set $c = $ "admn". Then the string $s$ will change as follows: $abacaba \rightarrow abacaba \rightarrow abdcaba \rightarrow abdcmba \rightarrow abdcmbn$.
|
Title: Update Queries
time_limit_ms: 2000
memory_limit_mb: 256
Description: Let's consider the following simple problem. You are given a string $s$ of length $n$, consisting of lowercase Latin letters, as well as an array of indices $ind$ of length $m$ ($1 \leq ind_i \leq n$) and a string $c$ of length $m$, consisting of lowercase Latin letters. Then, in order, you perform the update operations, namely, during the $i$-th operation, you set $s_{ind_i} = c_i$. Note that you perform all $m$ operations from the first to the last.
Of course, if you change the order of indices in the array $ind$ and/or the order of letters in the string $c$, you can get different results. Find the lexicographically smallest string $s$ that can be obtained after $m$ update operations, if you can rearrange the indices in the array $ind$ and the letters in the string $c$ as you like.
A string $a$ is lexicographically less than a string $b$ if and only if one of the following conditions is met:
* $a$ is a prefix of $b$, but $a \neq b$; * in the first position where $a$ and $b$ differ, the symbol in string $a$ is earlier in the alphabet than the corresponding symbol in string $b$.
Input: Each test consists of several sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of sets of input data. Then follows their description.
The first line of each set of input data contains two integers $n$ and $m$ ($1 \leq n, m \leq 10^5$) β the length of the string $s$ and the number of updates.
The second line of each set of input data contains a string $s$ of length $n$, consisting of lowercase Latin letters.
The third line of each set of input data contains $m$ integers $ind_1, ind_2, \ldots, ind_m$ ($1 \leq ind_i \leq n$) β the array of indices $ind$.
The fourth line of each set of input data contains a string $c$ of length $m$, consisting of lowercase Latin letters.
It is guaranteed that the sum of $n$ over all sets of input data does not exceed $2 \cdot 10^5$. Similarly, the sum of $m$ over all sets of input data does not exceed $2 \cdot 10^5$.
Output: For each set of input data, output the lexicographically smallest string $s$ that can be obtained by rearranging the indices in the array $ind$ and the letters in the string $c$ as you like.
Examples:
input:
4
1 2
a
1 1
cb
4 4
meow
1 2 1 4
zcwz
7 4
abacaba
1 3 5 7
damn
7 10
traktor
7 6 5 4 3 2 1 6 4 2
codeforces
output:
b
cwoz
abdcmbn
ccdeefo
Note: In the first set of input data, you can leave the array $ind$ and the string $c$ unchanged and simply perform all operations in that order.
In the second set of input data, you can set the array $ind = [1, 1, 4, 2]$ and $c =$ "zczw". Then the string $s$ will change as follows: $meow \rightarrow zeow \rightarrow ceow \rightarrow ceoz \rightarrow cwoz$.
In the third set of input data, you can leave the array $ind$ unchanged and set $c = $ "admn". Then the string $s$ will change as follows: $abacaba \rightarrow abacaba \rightarrow abdcaba \rightarrow abdcmba \rightarrow abdcmbn$.
|
2025E
|
https://codeforces.com/problemset/problem/2025/E
|
Card Game
| 2,200
|
[
"combinatorics",
"dp",
"fft",
"greedy",
"math"
] |
Div. 2
| 2,000
| 512
|
In the most popular card game in Berland, a deck of $n \times m$ cards is used. Each card has two parameters: suit and rank. Suits in the game are numbered from $1$ to $n$, and ranks are numbered from $1$ to $m$. There is exactly one card in the deck for each combination of suit and rank.
A card with suit $a$ and rank $b$ can beat a card with suit $c$ and rank $d$ in one of two cases:
* $a = 1$, $c \ne 1$ (a card of suit $1$ can beat a card of any other suit); * $a = c$, $b > d$ (a card can beat any other card of the same suit but of a lower rank).
Two players play the game. Before the game starts, they receive exactly half of the deck each. The first player wins if for every card of the second player, he can choose his card that can beat it, and there is no card that is chosen twice (i. e. there exists a matching of the first player's cards with the second player's cards such that in each pair the first player's card beats the second player's card). Otherwise, the second player wins.
Your task is to calculate the number of ways to distribute the cards so that the first player wins. Two ways are considered different if there exists a card such that in one way it belongs to the first player and in the other way it belongs to the second player. The number of ways can be very large, so print it modulo $998244353$.
|
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 500$).
Additional constraint on the input: $m$ is even.
|
Print a single integer β the number of ways to distribute the cards so that the first player wins, taken modulo $998244353$.
|
[
[
"1 4",
"2"
],
[
"2 2",
"2"
],
[
"3 6",
"1690"
],
[
"5 4",
"568"
],
[
"500 500",
"84693741"
]
] |
Title: Card Game
time_limit_ms: 2000
memory_limit_mb: 512
Description: In the most popular card game in Berland, a deck of $n \times m$ cards is used. Each card has two parameters: suit and rank. Suits in the game are numbered from $1$ to $n$, and ranks are numbered from $1$ to $m$. There is exactly one card in the deck for each combination of suit and rank.
A card with suit $a$ and rank $b$ can beat a card with suit $c$ and rank $d$ in one of two cases:
* $a = 1$, $c \ne 1$ (a card of suit $1$ can beat a card of any other suit); * $a = c$, $b > d$ (a card can beat any other card of the same suit but of a lower rank).
Two players play the game. Before the game starts, they receive exactly half of the deck each. The first player wins if for every card of the second player, he can choose his card that can beat it, and there is no card that is chosen twice (i. e. there exists a matching of the first player's cards with the second player's cards such that in each pair the first player's card beats the second player's card). Otherwise, the second player wins.
Your task is to calculate the number of ways to distribute the cards so that the first player wins. Two ways are considered different if there exists a card such that in one way it belongs to the first player and in the other way it belongs to the second player. The number of ways can be very large, so print it modulo $998244353$.
Input: The only line contains two integers $n$ and $m$ ($1 \le n, m \le 500$).
Additional constraint on the input: $m$ is even.
Output: Print a single integer β the number of ways to distribute the cards so that the first player wins, taken modulo $998244353$.
Examples:
input:
1 4
output:
2
input:
2 2
output:
2
input:
3 6
output:
1690
input:
5 4
output:
568
input:
500 500
output:
84693741
Note:
|
|
1989B
|
https://codeforces.com/problemset/problem/1989/B
|
Substring and Subsequence
| 1,200
|
[
"brute force",
"greedy",
"strings"
] |
Div. 2
| 2,000
| 256
|
You are given two strings $a$ and $b$, both consisting of lowercase Latin letters.
A subsequence of a string is a string which can be obtained by removing several (possibly zero) characters from the original string. A substring of a string is a contiguous subsequence of that string.
For example, consider the string abac:
* a, b, c, ab, aa, ac, ba, bc, aba, abc, aac, bac and abac are its subsequences; * a, b, c, ab, ba, ac, aba, bac and abac are its substrings.
Your task is to calculate the minimum possible length of the string that contains $a$ as a substring and $b$ as a subsequence.
|
The first line contains a single integer $t$ ($1 \le t \le 10^3$) β the number of test cases.
The first line of each test case contains a string $a$ ($1 \le |a| \le 100$), consisting of lowercase Latin letters.
The second line of each test case contains a string $b$ ($1 \le |b| \le 100$), consisting of lowercase Latin letters.
|
For each test case, print a single integer β the minimum possible length of the string that contains $a$ as a substring and $b$ as a subsequence.
|
[
[
"5\naba\ncb\ner\ncf\nmmm\nmmm\ncontest\ntest\ncde\nabcefg",
"4\n4\n3\n7\n7"
]
] |
In the examples below, the characters that correspond to the subsequence equal to $b$ are bolded.
In the first example, one of the possible answers is caba.
In the second example, one of the possible answers is ercf.
In the third example, one of the possible answers is mmm.
In the fourth example, one of the possible answers is contest.
In the fifth example, one of the possible answers is abcdefg.
|
Title: Substring and Subsequence
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given two strings $a$ and $b$, both consisting of lowercase Latin letters.
A subsequence of a string is a string which can be obtained by removing several (possibly zero) characters from the original string. A substring of a string is a contiguous subsequence of that string.
For example, consider the string abac:
* a, b, c, ab, aa, ac, ba, bc, aba, abc, aac, bac and abac are its subsequences; * a, b, c, ab, ba, ac, aba, bac and abac are its substrings.
Your task is to calculate the minimum possible length of the string that contains $a$ as a substring and $b$ as a subsequence.
Input: The first line contains a single integer $t$ ($1 \le t \le 10^3$) β the number of test cases.
The first line of each test case contains a string $a$ ($1 \le |a| \le 100$), consisting of lowercase Latin letters.
The second line of each test case contains a string $b$ ($1 \le |b| \le 100$), consisting of lowercase Latin letters.
Output: For each test case, print a single integer β the minimum possible length of the string that contains $a$ as a substring and $b$ as a subsequence.
Examples:
input:
5
aba
cb
er
cf
mmm
mmm
contest
test
cde
abcefg
output:
4
4
3
7
7
Note: In the examples below, the characters that correspond to the subsequence equal to $b$ are bolded.
In the first example, one of the possible answers is caba.
In the second example, one of the possible answers is ercf.
In the third example, one of the possible answers is mmm.
In the fourth example, one of the possible answers is contest.
In the fifth example, one of the possible answers is abcdefg.
|
1986F
|
https://codeforces.com/problemset/problem/1986/F
|
Non-academic Problem
| 1,900
|
[
"dfs and similar",
"graphs",
"trees"
] |
Div. 3
| 2,000
| 256
|
You are given a connected undirected graph, the vertices of which are numbered with integers from $1$ to $n$. Your task is to minimize the number of pairs of vertices $1 \leq u < v \leq n$ between which there exists a path in this graph. To achieve this, you can remove exactly one edge from the graph.
Find the smallest number of pairs of vertices!
|
Each test consists of several sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of sets of input data. Then follows their description.
The first line of each set of input data contains two integers $n$ and $m$ ($2 \leq n \leq 10^5$, $n - 1 \leq m \leq \min(10^5, \frac{n \cdot (n - 1)}{2})$) β the number of vertices in the graph and the number of edges.
Each of the next $m$ lines contains two integers $u$ and $v$ ($1 \leq u, v \leq n, u \neq v$), indicating that there is an undirected edge in the graph between vertices $u$ and $v$.
It is guaranteed that the given graph is connected and without multiple edges.
It is guaranteed that the sum of $n$ and the sum of $m$ over all sets of input data does not exceed $2 \cdot 10^5$.
|
For each set of input data, output the smallest number of pairs of reachable vertices, if exactly one edge can be removed.
|
[
[
"6\n2 1\n1 2\n3 3\n1 2\n2 3\n1 3\n5 5\n1 2\n1 3\n3 4\n4 5\n5 3\n6 7\n1 2\n1 3\n2 3\n3 4\n4 5\n4 6\n5 6\n5 5\n1 2\n1 3\n2 3\n2 4\n3 5\n10 12\n1 2\n1 3\n2 3\n2 4\n4 5\n5 6\n6 7\n7 4\n3 8\n8 9\n9 10\n10 8",
"0\n3\n4\n6\n6\n21"
]
] |
In the first set of input data, we will remove the single edge $(1, 2)$ and the only pair of vertices $(1, 2)$ will become unreachable from each other.
In the second set of input data, no matter which edge we remove, all vertices will be reachable from each other.
In the fourth set of input data, the graph looks like this initially.

We will remove the edge $(3, 4)$ and then the only reachable pairs of vertices will be $(1, 2)$, $(1, 3)$, $(2, 3)$, $(4, 5)$, $(4, 6)$, $(5, 6)$.

In the sixth set of input data, the graph looks like this initially.

After removing the edge $(2, 4)$, the graph will look like this. Thus, there will be $21$ pairs of reachable vertices.

|
Title: Non-academic Problem
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given a connected undirected graph, the vertices of which are numbered with integers from $1$ to $n$. Your task is to minimize the number of pairs of vertices $1 \leq u < v \leq n$ between which there exists a path in this graph. To achieve this, you can remove exactly one edge from the graph.
Find the smallest number of pairs of vertices!
Input: Each test consists of several sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of sets of input data. Then follows their description.
The first line of each set of input data contains two integers $n$ and $m$ ($2 \leq n \leq 10^5$, $n - 1 \leq m \leq \min(10^5, \frac{n \cdot (n - 1)}{2})$) β the number of vertices in the graph and the number of edges.
Each of the next $m$ lines contains two integers $u$ and $v$ ($1 \leq u, v \leq n, u \neq v$), indicating that there is an undirected edge in the graph between vertices $u$ and $v$.
It is guaranteed that the given graph is connected and without multiple edges.
It is guaranteed that the sum of $n$ and the sum of $m$ over all sets of input data does not exceed $2 \cdot 10^5$.
Output: For each set of input data, output the smallest number of pairs of reachable vertices, if exactly one edge can be removed.
Examples:
input:
6
2 1
1 2
3 3
1 2
2 3
1 3
5 5
1 2
1 3
3 4
4 5
5 3
6 7
1 2
1 3
2 3
3 4
4 5
4 6
5 6
5 5
1 2
1 3
2 3
2 4
3 5
10 12
1 2
1 3
2 3
2 4
4 5
5 6
6 7
7 4
3 8
8 9
9 10
10 8
output:
0
3
4
6
6
21
Note: In the first set of input data, we will remove the single edge $(1, 2)$ and the only pair of vertices $(1, 2)$ will become unreachable from each other.
In the second set of input data, no matter which edge we remove, all vertices will be reachable from each other.
In the fourth set of input data, the graph looks like this initially.

We will remove the edge $(3, 4)$ and then the only reachable pairs of vertices will be $(1, 2)$, $(1, 3)$, $(2, 3)$, $(4, 5)$, $(4, 6)$, $(5, 6)$.

In the sixth set of input data, the graph looks like this initially.

After removing the edge $(2, 4)$, the graph will look like this. Thus, there will be $21$ pairs of reachable vertices.

|
End of preview. Expand
in Data Studio
README.md exists but content is empty.
- Downloads last month
- 40