problem_id
stringlengths
5
6
url
stringlengths
48
49
title
stringlengths
2
46
rating
int64
800
3.5k
tags
listlengths
1
11
div
stringclasses
16 values
time_limit_ms
int64
1k
13k
memory_limit_mb
int64
32
1.02k
description
stringlengths
67
2.52k
input
stringlengths
99
1.93k
βŒ€
output
stringlengths
47
1.35k
βŒ€
examples
listlengths
1
5
note
stringlengths
0
1.68k
prompt
stringlengths
505
6.96k
2006A
https://codeforces.com/problemset/problem/2006/A
Iris and Game on the Tree
1,700
[ "constructive algorithms", "dfs and similar", "games", "graphs", "greedy", "trees" ]
Div. 1
2,000
256
Iris has a tree rooted at vertex $1$. Each vertex has a value of $\mathtt 0$ or $\mathtt 1$. Let's consider a leaf of the tree (the vertex $1$ is never considered a leaf) and define its weight. Construct a string formed by the values of the vertices on the path starting at the root and ending in this leaf. Then the 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$. ![](CDN_BASE_URL/712e8f1acc71d0401cf5eb23441e53a5) * 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: ![](CDN_BASE_URL/c8e20d0cf352b7905e60a7b371f053d6) 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$. ![](CDN_BASE_URL/712e8f1acc71d0401cf5eb23441e53a5) * 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: ![](CDN_BASE_URL/c8e20d0cf352b7905e60a7b371f053d6) 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. ![](CDN_BASE_URL/274244c032854fe172d47861e2eb9c02) 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. ![](CDN_BASE_URL/83174231191d329be697a6e3f67b5eb3) 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: ![](CDN_BASE_URL/2f461ea7db7bb0cda4536a2a78190c2f) $\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. ![](CDN_BASE_URL/274244c032854fe172d47861e2eb9c02) 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. ![](CDN_BASE_URL/83174231191d329be697a6e3f67b5eb3) 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: ![](CDN_BASE_URL/2f461ea7db7bb0cda4536a2a78190c2f) $\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: ![](CDN_BASE_URL/754511fbdb88995aaa49733a1fe75dfc) In the first example, by removing edges $(1, 3)$ and $(2, 5)$, the resulting tree has all leaves (nodes $6$ and $7$) at the same distance from the root (node $1$), which is $3$. The answer is $2$, as it is the 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: ![](CDN_BASE_URL/754511fbdb88995aaa49733a1fe75dfc) In the first example, by removing edges $(1, 3)$ and $(2, 5)$, the resulting tree has all leaves (nodes $6$ and $7$) at the same distance from the root (node $1$), which is $3$. The answer is $2$, as it is the 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. ![](CDN_BASE_URL/ff5ae9758c965c2d8398c936e9581dab) 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. ![](CDN_BASE_URL/ea799abf84f8a212d5a0d782be4d5e9c) In the third test case, the visualization is shown as below: ![](CDN_BASE_URL/c6a5be14982b3d57a5103783598d4683)
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. ![](CDN_BASE_URL/ff5ae9758c965c2d8398c936e9581dab) 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. ![](CDN_BASE_URL/ea799abf84f8a212d5a0d782be4d5e9c) In the third test case, the visualization is shown as below: ![](CDN_BASE_URL/c6a5be14982b3d57a5103783598d4683)
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. ![](CDN_BASE_URL/f49d41e3510eec9591123a65ee9389ab) 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. ![](CDN_BASE_URL/f49d41e3510eec9591123a65ee9389ab) 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. ![](CDN_BASE_URL/c00467697f2843fb1ecf4e292e93aa87) 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)$. ![](CDN_BASE_URL/9d2fd2df4094f1654c448ff596e16eb3) In the sixth set of input data, the graph looks like this initially. ![](CDN_BASE_URL/a18a02e5685863de8060f83f96c068e2) After removing the edge $(2, 4)$, the graph will look like this. Thus, there will be $21$ pairs of reachable vertices. ![](CDN_BASE_URL/f53901ac967e9f60a7423e8ea66c67e4)
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. ![](CDN_BASE_URL/c00467697f2843fb1ecf4e292e93aa87) 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)$. ![](CDN_BASE_URL/9d2fd2df4094f1654c448ff596e16eb3) In the sixth set of input data, the graph looks like this initially. ![](CDN_BASE_URL/a18a02e5685863de8060f83f96c068e2) After removing the edge $(2, 4)$, the graph will look like this. Thus, there will be $21$ pairs of reachable vertices. ![](CDN_BASE_URL/f53901ac967e9f60a7423e8ea66c67e4)
1974E
https://codeforces.com/problemset/problem/1974/E
Money Buys Happiness
1,800
[ "dp" ]
Div. 3
3,000
256
Being a physicist, Charlie likes to plan his life in simple and precise terms. For the next $m$ months, starting with no money, Charlie will work hard and earn $x$ pounds per month. For the $i$-th month $(1 \le i \le m)$, there'll be a single opportunity of paying cost $c_i$ pounds to obtain happiness $h_i$. Borrowing is not allowed. Money earned in the $i$-th month can only be spent in a later $j$-th month ($j>i$). Since physicists don't code, help Charlie find the maximum obtainable sum of happiness.
The first line of input contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers, $m$ and $x$ ($1 \le m \le 50$, $1 \le x \le 10^8$) β€” the total number of months and the monthly salary. The $i$-th of the following $m$ lines contains two integers, $c_i$ and $h_i$ ($0 \le c_i \le 10^8$, $1 \le h_i \le 10^3$) β€” the cost and happiness on offer for the $i$-th month. Note that some happiness may be free ($c_i=0$ for some $i$'s). It is guaranteed that the sum of $\sum_i h_i$ over all test cases does not exceed $10^5$.
For each test case, print a single integer, the maximum sum of happiness Charlie could obtain.
[ [ "7\n1 10\n1 5\n2 80\n0 10\n200 100\n3 100\n70 100\n100 200\n150 150\n5 8\n3 1\n5 3\n3 4\n1 5\n5 3\n2 5\n1 5\n2 1\n5 3\n2 5\n2 4\n4 1\n5 1\n3 4\n5 2\n2 1\n1 2\n3 5\n3 2\n3 2", "0\n10\n200\n15\n1\n9\n9" ] ]
In the first test case, Charlie only gets paid at the end of the month, so is unable to afford anything. In the second test case, Charlie obtains the free happiness in the first month. In the third test case, it's optimal for Charlie to buy happiness in the second month. Even with money left at the end, Charlie could not go back in time to obtain the happiness on offer in the first month.
Title: Money Buys Happiness time_limit_ms: 3000 memory_limit_mb: 256 Description: Being a physicist, Charlie likes to plan his life in simple and precise terms. For the next $m$ months, starting with no money, Charlie will work hard and earn $x$ pounds per month. For the $i$-th month $(1 \le i \le m)$, there'll be a single opportunity of paying cost $c_i$ pounds to obtain happiness $h_i$. Borrowing is not allowed. Money earned in the $i$-th month can only be spent in a later $j$-th month ($j>i$). Since physicists don't code, help Charlie find the maximum obtainable sum of happiness. Input: The first line of input contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers, $m$ and $x$ ($1 \le m \le 50$, $1 \le x \le 10^8$) β€” the total number of months and the monthly salary. The $i$-th of the following $m$ lines contains two integers, $c_i$ and $h_i$ ($0 \le c_i \le 10^8$, $1 \le h_i \le 10^3$) β€” the cost and happiness on offer for the $i$-th month. Note that some happiness may be free ($c_i=0$ for some $i$'s). It is guaranteed that the sum of $\sum_i h_i$ over all test cases does not exceed $10^5$. Output: For each test case, print a single integer, the maximum sum of happiness Charlie could obtain. Examples: input: 7 1 10 1 5 2 80 0 10 200 100 3 100 70 100 100 200 150 150 5 8 3 1 5 3 3 4 1 5 5 3 2 5 1 5 2 1 5 3 2 5 2 4 4 1 5 1 3 4 5 2 2 1 1 2 3 5 3 2 3 2 output: 0 10 200 15 1 9 9 Note: In the first test case, Charlie only gets paid at the end of the month, so is unable to afford anything. In the second test case, Charlie obtains the free happiness in the first month. In the third test case, it's optimal for Charlie to buy happiness in the second month. Even with money left at the end, Charlie could not go back in time to obtain the happiness on offer in the first month.
1989E
https://codeforces.com/problemset/problem/1989/E
Distance to Different
2,300
[ "combinatorics", "dp", "math" ]
Div. 2
2,000
512
Consider an array $a$ of $n$ integers, where every element is from $1$ to $k$, and every integer from $1$ to $k$ appears at least once. Let the array $b$ be constructed as follows: for the $i$-th element of $a$, $b_i$ is the distance to the closest element in $a$ which is not equal to $a_i$. In other words, $b_i = \min \limits_{j \in [1, n], a_j \ne a_i} |i - j|$. For example, if $a = [1, 1, 2, 3, 3, 3, 3, 1]$, then $b = [2, 1, 1, 1, 2, 2, 1, 1]$. Calculate the number of different arrays $b$ you can obtain if you consider all possible arrays $a$, and print it modulo $998244353$.
The only line of the input contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le \min(n, 10)$).
Print one integer β€” the number of different arrays $b$ you can obtain, taken modulo $998244353$.
[ [ "2 2", "1" ], [ "4 3", "3" ], [ "6 2", "20" ], [ "6 5", "3" ], [ "133 7", "336975971" ] ]
Title: Distance to Different time_limit_ms: 2000 memory_limit_mb: 512 Description: Consider an array $a$ of $n$ integers, where every element is from $1$ to $k$, and every integer from $1$ to $k$ appears at least once. Let the array $b$ be constructed as follows: for the $i$-th element of $a$, $b_i$ is the distance to the closest element in $a$ which is not equal to $a_i$. In other words, $b_i = \min \limits_{j \in [1, n], a_j \ne a_i} |i - j|$. For example, if $a = [1, 1, 2, 3, 3, 3, 3, 1]$, then $b = [2, 1, 1, 1, 2, 2, 1, 1]$. Calculate the number of different arrays $b$ you can obtain if you consider all possible arrays $a$, and print it modulo $998244353$. Input: The only line of the input contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le \min(n, 10)$). Output: Print one integer β€” the number of different arrays $b$ you can obtain, taken modulo $998244353$. Examples: input: 2 2 output: 1 input: 4 3 output: 3 input: 6 2 output: 20 input: 6 5 output: 3 input: 133 7 output: 336975971 Note:
2033F
https://codeforces.com/problemset/problem/2033/F
Kosuke's Sloth
1,800
[ "brute force", "math", "number theory" ]
Div. 3
1,000
256
Kosuke is too lazy. He will not give you any legend, just the task: Fibonacci numbers are defined as follows: * $f(1)=f(2)=1$. * $f(n)=f(n-1)+f(n-2)$ $(3\le n)$ We denote $G(n,k)$ as an index of the $n$-th Fibonacci number that is divisible by $k$. For given $n$ and $k$, compute $G(n,k)$. As this number can be too big, output it by modulo $10^9+7$. For example: $G(3,2)=9$ because the $3$-rd Fibonacci number that is divisible by $2$ is $34$. $[1,1,\textbf{2},3,5,\textbf{8},13,21,\textbf{34}]$.
The first line of the input data contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first and only line contains two integers $n$ and $k$ ($1 \le n \le 10^{18}$, $1 \le k \le 10^5$). It is guaranteed that the sum of $k$ across all test cases does not exceed $10^6$.
For each test case, output the only number: the value $G(n,k)$ taken by modulo $10^9+7$.
[ [ "3\n3 2\n100 1\n1000000000000 1377", "9\n100\n999244007" ] ]
Title: Kosuke's Sloth time_limit_ms: 1000 memory_limit_mb: 256 Description: Kosuke is too lazy. He will not give you any legend, just the task: Fibonacci numbers are defined as follows: * $f(1)=f(2)=1$. * $f(n)=f(n-1)+f(n-2)$ $(3\le n)$ We denote $G(n,k)$ as an index of the $n$-th Fibonacci number that is divisible by $k$. For given $n$ and $k$, compute $G(n,k)$. As this number can be too big, output it by modulo $10^9+7$. For example: $G(3,2)=9$ because the $3$-rd Fibonacci number that is divisible by $2$ is $34$. $[1,1,\textbf{2},3,5,\textbf{8},13,21,\textbf{34}]$. Input: The first line of the input data contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first and only line contains two integers $n$ and $k$ ($1 \le n \le 10^{18}$, $1 \le k \le 10^5$). It is guaranteed that the sum of $k$ across all test cases does not exceed $10^6$. Output: For each test case, output the only number: the value $G(n,k)$ taken by modulo $10^9+7$. Examples: input: 3 3 2 100 1 1000000000000 1377 output: 9 100 999244007 Note:
2005E1
https://codeforces.com/problemset/problem/2005/E1
Subtangle Game (Easy Version)
2,100
[ "dp", "games", "greedy", "implementation" ]
Div. 2
2,000
256
This is the easy version of the problem. The differences between the two versions are the constraints on all the variables. You can make hacks only if both versions of the problem are solved. Tsovak and Narek are playing a game. They have an array $a$ and a matrix $b$ of integers with $n$ rows and $m$ columns, numbered from $1$. The cell in the $i$-th row and the $j$-th column is $(i, j)$. They are looking for the elements of $a$ in turns; Tsovak starts first. Each time a player looks for a cell in the matrix containing the current element of $a$ (Tsovak looks for the first, then Narek looks for the second, etc.). Let's say a player has chosen the cell $(r, c)$. The next player has to choose his cell in the submatrix starting at $(r + 1, c + 1)$ and ending in $(n, m)$ (the submatrix can be empty if $r=n$ or $c=m$). If a player cannot find such a cell (or the remaining submatrix is empty) or the array ends (the previous player has chosen the last element), then he loses. Your task is to determine the winner if the players play optimally.
The first line of the input contains $t$ ($1 \le t \le 300$) – the number of test cases. The first line of each test case contains three integers $l$, $n$, and $m$ ($1 \le l, n, m \le 300$) – the size of the array and the sizes of the matrix. The second line contains $l$ integers $a_1, a_2, a_3, \ldots a_l$ ($1 \le a_i \le \min(7, n \cdot m)$) – the elements of the array $a$. The $i$-th of the last $n$ lines contains $m$ integers $b_{i,1}, b_{i,2}, b_{i,3}, \ldots b_{i,m}$ ($1 \le b_{i,j} \le \min(7, n \cdot m)$) – representing the $i$-th row of the matrix. It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. It is guaranteed that the sum of $l$ over all test cases does not exceed $300$.
You should output $t$ lines, the $i$-th of them containing a character representing the answer of the $i$-th test case: "T" if Tsovak wins or "N", otherwise (without quotes).
[ [ "3\n2 2 3\n1 2\n1 3 5\n4 5 2\n2 2 4\n1 2\n1 1 3 2\n4 2 5 1\n2 4 2\n1 2\n3 4\n5 5\n5 5\n5 5", "N\nT\nN" ] ]
In the first example, Tsovak starts by looking for $1$. There is only one occurrence of $1$ at $(1,1)$, so he chooses it. Then Narek needs to look for $2$ in the submatrix of $(2, 2)$, which consists of just the last two elements: $5$ and $2$. He chooses $2$, and then Tsovak loses since the array has ended. In the second example, Tsovak needs to choose $1$. There is a $1$ at the cell $(n,m)$, so he chooses that cell. Then, since the submatrix of $(n + 1, m + 1)$ is empty, Narek cannot find $2$, so he loses.
Title: Subtangle Game (Easy Version) time_limit_ms: 2000 memory_limit_mb: 256 Description: This is the easy version of the problem. The differences between the two versions are the constraints on all the variables. You can make hacks only if both versions of the problem are solved. Tsovak and Narek are playing a game. They have an array $a$ and a matrix $b$ of integers with $n$ rows and $m$ columns, numbered from $1$. The cell in the $i$-th row and the $j$-th column is $(i, j)$. They are looking for the elements of $a$ in turns; Tsovak starts first. Each time a player looks for a cell in the matrix containing the current element of $a$ (Tsovak looks for the first, then Narek looks for the second, etc.). Let's say a player has chosen the cell $(r, c)$. The next player has to choose his cell in the submatrix starting at $(r + 1, c + 1)$ and ending in $(n, m)$ (the submatrix can be empty if $r=n$ or $c=m$). If a player cannot find such a cell (or the remaining submatrix is empty) or the array ends (the previous player has chosen the last element), then he loses. Your task is to determine the winner if the players play optimally. Input: The first line of the input contains $t$ ($1 \le t \le 300$) – the number of test cases. The first line of each test case contains three integers $l$, $n$, and $m$ ($1 \le l, n, m \le 300$) – the size of the array and the sizes of the matrix. The second line contains $l$ integers $a_1, a_2, a_3, \ldots a_l$ ($1 \le a_i \le \min(7, n \cdot m)$) – the elements of the array $a$. The $i$-th of the last $n$ lines contains $m$ integers $b_{i,1}, b_{i,2}, b_{i,3}, \ldots b_{i,m}$ ($1 \le b_{i,j} \le \min(7, n \cdot m)$) – representing the $i$-th row of the matrix. It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. It is guaranteed that the sum of $l$ over all test cases does not exceed $300$. Output: You should output $t$ lines, the $i$-th of them containing a character representing the answer of the $i$-th test case: "T" if Tsovak wins or "N", otherwise (without quotes). Examples: input: 3 2 2 3 1 2 1 3 5 4 5 2 2 2 4 1 2 1 1 3 2 4 2 5 1 2 4 2 1 2 3 4 5 5 5 5 5 5 output: N T N Note: In the first example, Tsovak starts by looking for $1$. There is only one occurrence of $1$ at $(1,1)$, so he chooses it. Then Narek needs to look for $2$ in the submatrix of $(2, 2)$, which consists of just the last two elements: $5$ and $2$. He chooses $2$, and then Tsovak loses since the array has ended. In the second example, Tsovak needs to choose $1$. There is a $1$ at the cell $(n,m)$, so he chooses that cell. Then, since the submatrix of $(n + 1, m + 1)$ is empty, Narek cannot find $2$, so he loses.
1981F
https://codeforces.com/problemset/problem/1981/F
Turtle and Paths on a Tree
3,000
[ "data structures", "dp", "trees" ]
Div. 2
4,000
1,024
Note the unusual definition of $\text{MEX}$ in this problem. Piggy gave Turtle a binary tree$^{\dagger}$ with $n$ vertices and a sequence $a_1, a_2, \ldots, a_n$ on his birthday. The binary tree is rooted at vertex $1$. If a set of paths $P = \\{(x_i, y_i)\\}$ in the tree covers each edge exactly once, then Turtle will think that the set of paths is good. Note that a good set of paths can cover a vertex twice or more. Turtle defines the value of a set of paths as $\sum\limits_{(x, y) \in P} f(x, y)$, where $f(x, y)$ denotes the $\text{MEX}^{\ddagger}$ of all $a_u$ such that vertex $u$ is on the simple path from $x$ to $y$ in the tree (including the starting vertex $x$ and the ending vertex $y$). Turtle wonders the minimum value over all good sets of paths. Please help him calculate the answer! $^{\dagger}$A binary tree is a tree where every non-leaf vertex has at most $2$ sons. $^{\ddagger}\text{MEX}$ of a collection of integers $c_1, c_2, \ldots, c_k$ is defined as the smallest positive integer $x$ which does not occur in the collection $c$. For example, $\text{MEX}$ of $[3, 3, 1, 4]$ is $2$, $\text{MEX}$ of $[2, 3]$ is $1$.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \le n \le 2.5 \cdot 10^4$) β€” the number of vertices in the tree. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the elements of the sequence $a$. The third line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i < i$) β€” the parent of each vertex in the tree. Additional constraint on the input: the given tree is a binary tree, that is, every non-leaf vertex has at most $2$ sons. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
For each test case, output a single integer β€” the minimum value over all good sets of paths.
[ [ "5\n5\n3 2 2 1 1\n1 1 2 2\n5\n3 2 1 1 1\n1 1 2 2\n6\n1 2 1 2 1 3\n1 2 3 3 4\n7\n2 1 2 3 1 2 1\n1 1 2 2 3 3\n10\n1 2 2 1 4 2 3 1 2 1\n1 1 2 2 3 3 4 5 5", "4\n6\n6\n6\n7" ] ]
In the first test case, the tree is as follows. The number in brackets denotes the weight of the vertex: ![](CDN_BASE_URL/45a1635df0fcab1b711d2165163f0dfb) The good set of paths with the minimum value is $\\{(2, 3), (4, 5)\\}$. Note that in this test case $\\{(4, 5)\\}$ and $\\{(3, 4), (4, 5)\\}$ are not good sets of paths because each edge should be covered exactly once. In the second test case, the tree is as follows: ![](CDN_BASE_URL/a41b8a69adf0471a8353a08785fa6375) The set of good paths with the minimum value is $\\{(1, 2), (1, 3), (4, 5)\\}$. In the third test case, the tree is as follows: ![](CDN_BASE_URL/7e62b6f397447336b5c5732c0d915ebd) The set of good paths with the minimum value is $\\{(1, 6), (3, 5)\\}$.
Title: Turtle and Paths on a Tree time_limit_ms: 4000 memory_limit_mb: 1024 Description: Note the unusual definition of $\text{MEX}$ in this problem. Piggy gave Turtle a binary tree$^{\dagger}$ with $n$ vertices and a sequence $a_1, a_2, \ldots, a_n$ on his birthday. The binary tree is rooted at vertex $1$. If a set of paths $P = \\{(x_i, y_i)\\}$ in the tree covers each edge exactly once, then Turtle will think that the set of paths is good. Note that a good set of paths can cover a vertex twice or more. Turtle defines the value of a set of paths as $\sum\limits_{(x, y) \in P} f(x, y)$, where $f(x, y)$ denotes the $\text{MEX}^{\ddagger}$ of all $a_u$ such that vertex $u$ is on the simple path from $x$ to $y$ in the tree (including the starting vertex $x$ and the ending vertex $y$). Turtle wonders the minimum value over all good sets of paths. Please help him calculate the answer! $^{\dagger}$A binary tree is a tree where every non-leaf vertex has at most $2$ sons. $^{\ddagger}\text{MEX}$ of a collection of integers $c_1, c_2, \ldots, c_k$ is defined as the smallest positive integer $x$ which does not occur in the collection $c$. For example, $\text{MEX}$ of $[3, 3, 1, 4]$ is $2$, $\text{MEX}$ of $[2, 3]$ is $1$. 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$ ($2 \le n \le 2.5 \cdot 10^4$) β€” the number of vertices in the tree. 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 elements of the sequence $a$. The third line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i < i$) β€” the parent of each vertex in the tree. Additional constraint on the input: the given tree is a binary tree, that is, every non-leaf vertex has at most $2$ sons. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. Output: For each test case, output a single integer β€” the minimum value over all good sets of paths. Examples: input: 5 5 3 2 2 1 1 1 1 2 2 5 3 2 1 1 1 1 1 2 2 6 1 2 1 2 1 3 1 2 3 3 4 7 2 1 2 3 1 2 1 1 1 2 2 3 3 10 1 2 2 1 4 2 3 1 2 1 1 1 2 2 3 3 4 5 5 output: 4 6 6 6 7 Note: In the first test case, the tree is as follows. The number in brackets denotes the weight of the vertex: ![](CDN_BASE_URL/45a1635df0fcab1b711d2165163f0dfb) The good set of paths with the minimum value is $\\{(2, 3), (4, 5)\\}$. Note that in this test case $\\{(4, 5)\\}$ and $\\{(3, 4), (4, 5)\\}$ are not good sets of paths because each edge should be covered exactly once. In the second test case, the tree is as follows: ![](CDN_BASE_URL/a41b8a69adf0471a8353a08785fa6375) The set of good paths with the minimum value is $\\{(1, 2), (1, 3), (4, 5)\\}$. In the third test case, the tree is as follows: ![](CDN_BASE_URL/7e62b6f397447336b5c5732c0d915ebd) The set of good paths with the minimum value is $\\{(1, 6), (3, 5)\\}$.
2035B
https://codeforces.com/problemset/problem/2035/B
Everyone Loves Tres
900
[ "constructive algorithms", "greedy", "math", "number theory" ]
Div. 1 + 2
1,000
256
There are 3 heroes and 3 villains, so 6 people in total. Given a positive integer $n$. Find the smallest integer whose decimal representation has length $n$ and consists only of $3$s and $6$s such that it is divisible by both $33$ and $66$. If no such integer exists, print $-1$.
The first line contains a single integer $t$ ($1\le t\le 500$) β€” the number of test cases. The only line of each test case contains a single integer $n$ ($1\le n\le 500$) β€” the length of the decimal representation.
For each test case, output the smallest required integer if such an integer exists and $-1$ otherwise.
[ [ "6\n1\n2\n3\n4\n5\n7", "-1\n66\n-1\n3366\n36366\n3336366" ] ]
For $n=1$, no such integer exists as neither $3$ nor $6$ is divisible by $33$. For $n=2$, $66$ consists only of $6$s and it is divisible by both $33$ and $66$. For $n=3$, no such integer exists. Only $363$ is divisible by $33$, but it is not divisible by $66$. For $n=4$, $3366$ and $6666$ are divisible by both $33$ and $66$, and $3366$ is the smallest.
Title: Everyone Loves Tres time_limit_ms: 1000 memory_limit_mb: 256 Description: There are 3 heroes and 3 villains, so 6 people in total. Given a positive integer $n$. Find the smallest integer whose decimal representation has length $n$ and consists only of $3$s and $6$s such that it is divisible by both $33$ and $66$. If no such integer exists, print $-1$. Input: The first line contains a single integer $t$ ($1\le t\le 500$) β€” the number of test cases. The only line of each test case contains a single integer $n$ ($1\le n\le 500$) β€” the length of the decimal representation. Output: For each test case, output the smallest required integer if such an integer exists and $-1$ otherwise. Examples: input: 6 1 2 3 4 5 7 output: -1 66 -1 3366 36366 3336366 Note: For $n=1$, no such integer exists as neither $3$ nor $6$ is divisible by $33$. For $n=2$, $66$ consists only of $6$s and it is divisible by both $33$ and $66$. For $n=3$, no such integer exists. Only $363$ is divisible by $33$, but it is not divisible by $66$. For $n=4$, $3366$ and $6666$ are divisible by both $33$ and $66$, and $3366$ is the smallest.
1973E
https://codeforces.com/problemset/problem/1973/E
Cat, Fox and Swaps
2,500
[ "graphs", "math", "sortings" ]
Div. 2
2,000
256
Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her β€” he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times. They don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \leq l \leq r \leq 2 \cdot n$. You are given the number $n$ and the array $p_1, p_2, \ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \leq x + y \leq r$ (arbitrary number of times, possibly $0$). $^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers: the array $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). It is guaranteed that this array is a permutation of length $n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints.
[ [ "7\n2\n2 1\n3\n3 1 2\n4\n3 2 1 4\n5\n5 3 1 2 4\n5\n1 2 3 4 5\n6\n3 2 1 5 4 6\n6\n1 3 2 4 5 6", "6\n11\n23\n29\n55\n46\n58" ] ]
In the first example, we need to be able to swap $1$ and $2$, so we must be able to swap numbers with sum $3$. There are exactly $6$ pairs satisfying the condition: $(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$ and $(3, 4)$, so the answer is $6$. In the second example, the $11$ pairs satisfying the condition are $(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)$ and $(4, 6)$. For example, if we pick the pair $(3, 4)$ we can first swap the numbers $1$ and $2$ and then the numbers $1$ and $3$, after this, the permutation is sorted.
Title: Cat, Fox and Swaps time_limit_ms: 2000 memory_limit_mb: 256 Description: Fox has found an array $p_1, p_2, \ldots, p_n$, that is a permutation of length $n^\dagger$ of the numbers $1, 2, \ldots, n$. She wants to sort the elements in increasing order. Cat wants to help her β€” he is able to swap any two numbers $x$ and $y$ in the array, but only if $l \leq x + y \leq r$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times. They don't know the numbers $l$, $r$ yet, they only know that it's true that $1 \leq l \leq r \leq 2 \cdot n$. You are given the number $n$ and the array $p_1, p_2, \ldots, p_n$. Determine how many pairs $(l, r)$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $(x, y)$ such that $l \leq x + y \leq r$ (arbitrary number of times, possibly $0$). $^\dagger$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array). 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. Description of each test case consists of two lines. The first line contains one integer $n$ ($1 \leq n \leq 10^5$). The second line contains $n$ integers: the array $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$). It is guaranteed that this array is a permutation of length $n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. Output: For each test case, print the number of pairs of integers $(l, r)$ such that $1 \leq l \leq r \leq 2 \cdot n$, and you can sort the array under the constraints. Examples: input: 7 2 2 1 3 3 1 2 4 3 2 1 4 5 5 3 1 2 4 5 1 2 3 4 5 6 3 2 1 5 4 6 6 1 3 2 4 5 6 output: 6 11 23 29 55 46 58 Note: In the first example, we need to be able to swap $1$ and $2$, so we must be able to swap numbers with sum $3$. There are exactly $6$ pairs satisfying the condition: $(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$ and $(3, 4)$, so the answer is $6$. In the second example, the $11$ pairs satisfying the condition are $(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)$ and $(4, 6)$. For example, if we pick the pair $(3, 4)$ we can first swap the numbers $1$ and $2$ and then the numbers $1$ and $3$, after this, the permutation is sorted.
2029G
https://codeforces.com/problemset/problem/2029/G
Balanced Problem
3,000
[ "data structures", "dp" ]
Div. 1 + 2
3,000
1,024
There is an array $a$ consisting of $n$ integers. Initially, all elements of $a$ are equal to $0$. Kevin can perform several operations on the array. Each operation is one of the following two types: * Prefix addition β€” Kevin first selects an index $x$ ($1\le x\le n$), and then for each $1\le j\le x$, increases $a_j$ by $1$; * Suffix addition β€” Kevin first selects an index $x$ ($1\le x\le n$), and then for each $x\le j\le n$, increases $a_j$ by $1$. In the country of KDOI, people think that the integer $v$ is balanced. Thus, Iris gives Kevin an array $c$ consisting of $n$ integers and defines the beauty of the array $a$ as follows: * Initially, set $b=0$; * For each $1\le i\le n$, if $a_i=v$, add $c_i$ to $b$; * The beauty of $a$ is the final value of $b$. Kevin wants to maximize the beauty of $a$ after all the operations. However, he had already performed $m$ operations when he was sleepy. Now, he can perform an arbitrary number (possibly zero) of new operations. You have to help Kevin find the maximum possible beauty if he optimally performs the new operations. However, to make sure that you are not just rolling the dice, Kevin gives you an integer $V$, and you need to solve the problem for each $1\le v\le V$.
Each test contains multiple test cases. The first line of the input contains a single integer $t$ ($1\le t\le 1000$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains three integers $n$, $m$, and $V$ ($1\le n, m\le 2\cdot 10^5$, $1\le V\le 2000$) β€” the length of the array $a$, the number of initial operations, and the number that Kevin gives you. The second line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1\le c_i\le 10^9$) β€” the elements in the array $c$. Then $m$ lines follow, the $i$-th line containing a character $op$ and an integer $x$ ($op=\mathtt{L}$ or $\mathtt{R}$, $1\le x\le n$) β€” the type of the $i$-th operation and the selected index. * If $op=\mathtt{L}$, this operation is a prefix addition on index $x$; * If $op=\mathtt{R}$, this operation is a suffix addition on index $x$. It is guaranteed that: * the sum of $n$ over all test cases does not exceed $2\cdot 10^5$; * the sum of $m$ over all test cases does not exceed $2\cdot 10^5$; * the sum of $V^2$ over all test cases does not exceed $4\cdot 10^6$.
For each test case, output $V$ integers in a single line, the $i$-th integer denoting the maximum possible beauty after Kevin performs some new operations when $v=i$.
[ [ "5\n3 3 2\n1 2 4\nL 3\nR 3\nL 1\n3 3 2\n5 1 4\nL 3\nR 3\nL 1\n5 4 5\n1 1 1 1 1\nL 3\nR 2\nL 5\nL 4\n10 12 9\n10 9 8 7 6 5 4 3 2 1\nL 2\nL 4\nR 4\nR 4\nL 6\nR 8\nL 3\nL 2\nR 1\nR 10\nL 8\nL 1\n1 1 4\n1000000000\nL 1", "2 6\n1 9\n0 1 3 5 5\n0 0 0 6 25 32 35 44 51\n1000000000 1000000000 1000000000 1000000000" ] ]
In the first test case, the array $a$ changes as follows for the initial operations: $[0, 0, 0] \xrightarrow{\mathtt{L}\ 3} [1, 1, 1] \xrightarrow{\mathtt{R}\ 3} [1, 1, 2] \xrightarrow{\mathtt{L}\ 1} [2, 1, 2]$. * For $v=1$, it is optimal to not perform any new operations, and the beauty is $b=c_2=2$; * For $v=2$, it is optimal to perform a prefix addition operation on index $2$. After that, $a$ becomes $[3,2,2]$, and the beauty is $b=c_2+c_3=6$. In the second test case, for both $v=1$ and $v=2$, it is optimal to not perform any new operations.
Title: Balanced Problem time_limit_ms: 3000 memory_limit_mb: 1024 Description: There is an array $a$ consisting of $n$ integers. Initially, all elements of $a$ are equal to $0$. Kevin can perform several operations on the array. Each operation is one of the following two types: * Prefix addition β€” Kevin first selects an index $x$ ($1\le x\le n$), and then for each $1\le j\le x$, increases $a_j$ by $1$; * Suffix addition β€” Kevin first selects an index $x$ ($1\le x\le n$), and then for each $x\le j\le n$, increases $a_j$ by $1$. In the country of KDOI, people think that the integer $v$ is balanced. Thus, Iris gives Kevin an array $c$ consisting of $n$ integers and defines the beauty of the array $a$ as follows: * Initially, set $b=0$; * For each $1\le i\le n$, if $a_i=v$, add $c_i$ to $b$; * The beauty of $a$ is the final value of $b$. Kevin wants to maximize the beauty of $a$ after all the operations. However, he had already performed $m$ operations when he was sleepy. Now, he can perform an arbitrary number (possibly zero) of new operations. You have to help Kevin find the maximum possible beauty if he optimally performs the new operations. However, to make sure that you are not just rolling the dice, Kevin gives you an integer $V$, and you need to solve the problem for each $1\le v\le V$. Input: Each test contains multiple test cases. The first line of the input contains a single integer $t$ ($1\le t\le 1000$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains three integers $n$, $m$, and $V$ ($1\le n, m\le 2\cdot 10^5$, $1\le V\le 2000$) β€” the length of the array $a$, the number of initial operations, and the number that Kevin gives you. The second line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1\le c_i\le 10^9$) β€” the elements in the array $c$. Then $m$ lines follow, the $i$-th line containing a character $op$ and an integer $x$ ($op=\mathtt{L}$ or $\mathtt{R}$, $1\le x\le n$) β€” the type of the $i$-th operation and the selected index. * If $op=\mathtt{L}$, this operation is a prefix addition on index $x$; * If $op=\mathtt{R}$, this operation is a suffix addition on index $x$. It is guaranteed that: * the sum of $n$ over all test cases does not exceed $2\cdot 10^5$; * the sum of $m$ over all test cases does not exceed $2\cdot 10^5$; * the sum of $V^2$ over all test cases does not exceed $4\cdot 10^6$. Output: For each test case, output $V$ integers in a single line, the $i$-th integer denoting the maximum possible beauty after Kevin performs some new operations when $v=i$. Examples: input: 5 3 3 2 1 2 4 L 3 R 3 L 1 3 3 2 5 1 4 L 3 R 3 L 1 5 4 5 1 1 1 1 1 L 3 R 2 L 5 L 4 10 12 9 10 9 8 7 6 5 4 3 2 1 L 2 L 4 R 4 R 4 L 6 R 8 L 3 L 2 R 1 R 10 L 8 L 1 1 1 4 1000000000 L 1 output: 2 6 1 9 0 1 3 5 5 0 0 0 6 25 32 35 44 51 1000000000 1000000000 1000000000 1000000000 Note: In the first test case, the array $a$ changes as follows for the initial operations: $[0, 0, 0] \xrightarrow{\mathtt{L}\ 3} [1, 1, 1] \xrightarrow{\mathtt{R}\ 3} [1, 1, 2] \xrightarrow{\mathtt{L}\ 1} [2, 1, 2]$. * For $v=1$, it is optimal to not perform any new operations, and the beauty is $b=c_2=2$; * For $v=2$, it is optimal to perform a prefix addition operation on index $2$. After that, $a$ becomes $[3,2,2]$, and the beauty is $b=c_2+c_3=6$. In the second test case, for both $v=1$ and $v=2$, it is optimal to not perform any new operations.
1982B
https://codeforces.com/problemset/problem/1982/B
Collatz Conjecture
1,200
[ "brute force", "implementation", "math", "number theory" ]
Div. 2
1,000
256
Recently, the first-year student Maxim learned about the Collatz conjecture, but he didn't pay much attention during the lecture, so he believes that the following process is mentioned in the conjecture: There is a variable $x$ and a constant $y$. The following operation is performed $k$ times: * increase $x$ by $1$, then * while the number $x$ is divisible by $y$, divide it by $y$. Note that both of these actions are performed sequentially within one operation. For example, if the number $x = 16$, $y = 3$, and $k = 2$, then after one operation $x$ becomes $17$, and after another operation $x$ becomes $2$, because after adding one, $x = 18$ is divisible by $3$ twice. Given the initial values of $x$, $y$, and $k$, Maxim wants to know what is the final value of $x$.
Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) β€” the number of test cases. Then follows the description of the test cases. The only line of each test case contains three integers $x$, $y$, and $k$ ($1 \le x, k \le 10^{9}$, $2 \le y \le 10^{9}$) β€” the initial variable, constant and the number of operations.
For each test case, output a single integer β€” the number obtained after applying $k$ operations.
[ [ "13\n1 3 1\n2 3 1\n24 5 5\n16 3 2\n2 2 1\n1337 18 1\n1 2 144133\n12345678 3 10\n998244353 2 998244353\n998244353 123456789 998244352\n998244354 998241111 998244352\n998244355 2 9982443\n1000000000 1000000000 1000000000", "2\n1\n1\n2\n3\n1338\n1\n16936\n1\n21180097\n6486\n1\n2" ] ]
In the first test case, there is only one operation applied to $x = 1$, resulting in $x$ becoming $2$. In the second test case, for $x = 2$, within one operation, one is added to $x$ and it's divided by $y = 3$, resulting in $x$ becoming $1$. In the third test case, $x$ changes as follows: * After the first operation, $x = 1$, because $24 + 1 = 25$ and $25$ is divisible by $y = 5$ twice within one operation. * After the second operation, $x = 2$. * After the third operation, $x = 3$. * After the fourth operation, $x = 4$. * After the fifth operation, $x = 1$.
Title: Collatz Conjecture time_limit_ms: 1000 memory_limit_mb: 256 Description: Recently, the first-year student Maxim learned about the Collatz conjecture, but he didn't pay much attention during the lecture, so he believes that the following process is mentioned in the conjecture: There is a variable $x$ and a constant $y$. The following operation is performed $k$ times: * increase $x$ by $1$, then * while the number $x$ is divisible by $y$, divide it by $y$. Note that both of these actions are performed sequentially within one operation. For example, if the number $x = 16$, $y = 3$, and $k = 2$, then after one operation $x$ becomes $17$, and after another operation $x$ becomes $2$, because after adding one, $x = 18$ is divisible by $3$ twice. Given the initial values of $x$, $y$, and $k$, Maxim wants to know what is the final value of $x$. 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 only line of each test case contains three integers $x$, $y$, and $k$ ($1 \le x, k \le 10^{9}$, $2 \le y \le 10^{9}$) β€” the initial variable, constant and the number of operations. Output: For each test case, output a single integer β€” the number obtained after applying $k$ operations. Examples: input: 13 1 3 1 2 3 1 24 5 5 16 3 2 2 2 1 1337 18 1 1 2 144133 12345678 3 10 998244353 2 998244353 998244353 123456789 998244352 998244354 998241111 998244352 998244355 2 9982443 1000000000 1000000000 1000000000 output: 2 1 1 2 3 1338 1 16936 1 21180097 6486 1 2 Note: In the first test case, there is only one operation applied to $x = 1$, resulting in $x$ becoming $2$. In the second test case, for $x = 2$, within one operation, one is added to $x$ and it's divided by $y = 3$, resulting in $x$ becoming $1$. In the third test case, $x$ changes as follows: * After the first operation, $x = 1$, because $24 + 1 = 25$ and $25$ is divisible by $y = 5$ twice within one operation. * After the second operation, $x = 2$. * After the third operation, $x = 3$. * After the fourth operation, $x = 4$. * After the fifth operation, $x = 1$.
2032C
https://codeforces.com/problemset/problem/2032/C
Trinity
1,400
[ "binary search", "math", "sortings", "two pointers" ]
Div. 2
2,000
256
You are given an array $a$ of $n$ elements $a_1, a_2, \ldots, a_n$. You can perform the following operation any number (possibly $0$) of times: * Choose two integers $i$ and $j$, where $1 \le i, j \le n$, and assign $a_i := a_j$. Find the minimum number of operations required to make the array $a$ satisfy the condition: * For every pairwise distinct triplet of indices $(x, y, z)$ ($1 \le x, y, z \le n$, $x \ne y$, $y \ne z$, $x \ne z$), there exists a non-degenerate triangle with side lengths $a_x$, $a_y$ and $a_z$, i.e. $a_x + a_y > a_z$, $a_y + a_z > a_x$ and $a_z + a_x > a_y$.
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($3 \le n \le 2 \cdot 10^5$) β€” the number of elements in the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the elements of the array $a$. 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 minimum number of operations required.
[ [ "4\n7\n1 2 3 4 5 6 7\n3\n1 3 2\n3\n4 5 3\n15\n9 3 8 1 6 5 3 8 2 1 4 2 9 4 7", "3\n1\n0\n8" ] ]
In the first test case, one of the possible series of operations would be: * Assign $a_1 := a_4 = 4$. The array will become $[4, 2, 3, 4, 5, 6, 7]$. * Assign $a_2 := a_5 = 5$. The array will become $[4, 5, 3, 4, 5, 6, 7]$. * Assign $a_7 := a_1 = 4$. The array will become $[4, 5, 3, 4, 5, 6, 4]$. It can be proven that any triplet of elements with pairwise distinct indices in the final array forms a non-degenerate triangle, and there is no possible answer using less than $3$ operations. In the second test case, we can assign $a_1 := a_2 = 3$ to make the array $a = [3, 3, 2]$. In the third test case, since $3$, $4$ and $5$ are valid side lengths of a triangle, we don't need to perform any operation to the array.
Title: Trinity time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given an array $a$ of $n$ elements $a_1, a_2, \ldots, a_n$. You can perform the following operation any number (possibly $0$) of times: * Choose two integers $i$ and $j$, where $1 \le i, j \le n$, and assign $a_i := a_j$. Find the minimum number of operations required to make the array $a$ satisfy the condition: * For every pairwise distinct triplet of indices $(x, y, z)$ ($1 \le x, y, z \le n$, $x \ne y$, $y \ne z$, $x \ne z$), there exists a non-degenerate triangle with side lengths $a_x$, $a_y$ and $a_z$, i.e. $a_x + a_y > a_z$, $a_y + a_z > a_x$ and $a_z + a_x > a_y$. 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 a single integer $n$ ($3 \le n \le 2 \cdot 10^5$) β€” the number of elements in the array $a$. 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 elements of the array $a$. 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 minimum number of operations required. Examples: input: 4 7 1 2 3 4 5 6 7 3 1 3 2 3 4 5 3 15 9 3 8 1 6 5 3 8 2 1 4 2 9 4 7 output: 3 1 0 8 Note: In the first test case, one of the possible series of operations would be: * Assign $a_1 := a_4 = 4$. The array will become $[4, 2, 3, 4, 5, 6, 7]$. * Assign $a_2 := a_5 = 5$. The array will become $[4, 5, 3, 4, 5, 6, 7]$. * Assign $a_7 := a_1 = 4$. The array will become $[4, 5, 3, 4, 5, 6, 4]$. It can be proven that any triplet of elements with pairwise distinct indices in the final array forms a non-degenerate triangle, and there is no possible answer using less than $3$ operations. In the second test case, we can assign $a_1 := a_2 = 3$ to make the array $a = [3, 3, 2]$. In the third test case, since $3$, $4$ and $5$ are valid side lengths of a triangle, we don't need to perform any operation to the array.
1992F
https://codeforces.com/problemset/problem/1992/F
Valuable Cards
1,900
[ "brute force", "dp", "greedy", "number theory", "two pointers" ]
Div. 3
4,000
512
In his favorite cafe Kmes once again wanted to try the herring under a fur coat. Previously, it would not have been difficult for him to do this, but the cafe recently introduced a new purchasing policy. Now, in order to make a purchase, Kmes needs to solve the following problem: $n$ cards with prices for different positions are laid out in front of him, on the $i$-th card there is an integer $a_i$, among these prices there is no whole positive integer $x$. Kmes is asked to divide these cards into the minimum number of bad segments (so that each card belongs to exactly one segment). A segment is considered bad if it is impossible to select a subset of cards with a product equal to $x$. All segments, in which Kmes will divide the cards, must be bad. Formally, the segment $(l, r)$ is bad if there are no indices $i_1 < i_2 < \ldots < i_k$ such that $l \le i_1, i_k \le r$, and $a_{i_1} \cdot a_{i_2} \ldots \cdot a_{i_k} = x$. Help Kmes determine the minimum number of bad segments in order to enjoy his favorite dish.
The first line contains a single integer $t$ ($1 \le t \le 10^3$) β€” the number of test cases. The first line of each set of input data gives you $2$ integers $n$ and $x$ ($1 \le n \le 10^5, 2 \le x \le 10^5$) β€” the number of cards and the integer, respectively. The second line of each set of input data contains $n$ integers $a_i$ ($1 \le a_i \le 2 \cdot 10^5, a_i \neq x$) β€” the prices on the cards. It is guaranteed that the sum of $n$ over all sets of test data does not exceed $10^5$.
For each set of input data, output the minimum number of bad segments.
[ [ "8\n6 4\n2 3 6 2 1 2\n9 100000\n50000 25000 12500 6250 3125 2 4 8 16\n5 2\n1 1 1 1 1\n8 6\n4 3 4 3 4 3 4 3\n7 12\n6 11 1 3 11 10 2\n10 5\n2 4 4 2 4 4 4 3 1 1\n7 8\n4 6 5 1 2 4 1\n8 27\n3 9 17 26 2 20 9 3", "3\n2\n1\n1\n2\n1\n3\n3" ] ]
Title: Valuable Cards time_limit_ms: 4000 memory_limit_mb: 512 Description: In his favorite cafe Kmes once again wanted to try the herring under a fur coat. Previously, it would not have been difficult for him to do this, but the cafe recently introduced a new purchasing policy. Now, in order to make a purchase, Kmes needs to solve the following problem: $n$ cards with prices for different positions are laid out in front of him, on the $i$-th card there is an integer $a_i$, among these prices there is no whole positive integer $x$. Kmes is asked to divide these cards into the minimum number of bad segments (so that each card belongs to exactly one segment). A segment is considered bad if it is impossible to select a subset of cards with a product equal to $x$. All segments, in which Kmes will divide the cards, must be bad. Formally, the segment $(l, r)$ is bad if there are no indices $i_1 < i_2 < \ldots < i_k$ such that $l \le i_1, i_k \le r$, and $a_{i_1} \cdot a_{i_2} \ldots \cdot a_{i_k} = x$. Help Kmes determine the minimum number of bad segments in order to enjoy his favorite dish. 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 set of input data gives you $2$ integers $n$ and $x$ ($1 \le n \le 10^5, 2 \le x \le 10^5$) β€” the number of cards and the integer, respectively. The second line of each set of input data contains $n$ integers $a_i$ ($1 \le a_i \le 2 \cdot 10^5, a_i \neq x$) β€” the prices on the cards. It is guaranteed that the sum of $n$ over all sets of test data does not exceed $10^5$. Output: For each set of input data, output the minimum number of bad segments. Examples: input: 8 6 4 2 3 6 2 1 2 9 100000 50000 25000 12500 6250 3125 2 4 8 16 5 2 1 1 1 1 1 8 6 4 3 4 3 4 3 4 3 7 12 6 11 1 3 11 10 2 10 5 2 4 4 2 4 4 4 3 1 1 7 8 4 6 5 1 2 4 1 8 27 3 9 17 26 2 20 9 3 output: 3 2 1 1 2 1 3 3 Note:
2004G
https://codeforces.com/problemset/problem/2004/G
Substring Compression
3,200
[ "data structures", "dp", "matrices" ]
Div. 2
2,000
1,024
Let's define the operation of compressing a string $t$, consisting of at least $2$ digits from $1$ to $9$, as follows: * split it into an even number of non-empty substrings β€” let these substrings be $t_1, t_2, \dots, t_m$ (so, $t = t_1 + t_2 + \dots + t_m$, where $+$ is the concatenation operation); * write the string $t_2$ $t_1$ times, then the string $t_4$ $t_3$ times, and so on. For example, for a string "12345", one could do the following: split it into ("1", "23", "4", "5"), and write "235555". Let the function $f(t)$ for a string $t$ return the minimum length of the string that can be obtained as a result of that process. You are given a string $s$, consisting of $n$ digits from $1$ to $9$, and an integer $k$. Calculate the value of the function $f$ for all contiguous substrings of $s$ of length exactly $k$.
The first line contains two integers $n$ and $k$ ($2 \le k \le n \le 2 \cdot 10^5$). The second line contains the string $s$ ($|s| = n$), consisting only of digits from $1$ to $9$.
Output $n - k + 1$ integers β€” $f(s_{1,k}), f(s_{2,k+1}), \dots, f(s_{n - k + 1, n})$.
[ [ "4 4\n5999", "14" ], [ "10 3\n1111111111", "2 2 2 2 2 2 2 2" ], [ "11 4\n49998641312", "12 18 17 15 12 7 7 2" ] ]
Title: Substring Compression time_limit_ms: 2000 memory_limit_mb: 1024 Description: Let's define the operation of compressing a string $t$, consisting of at least $2$ digits from $1$ to $9$, as follows: * split it into an even number of non-empty substrings β€” let these substrings be $t_1, t_2, \dots, t_m$ (so, $t = t_1 + t_2 + \dots + t_m$, where $+$ is the concatenation operation); * write the string $t_2$ $t_1$ times, then the string $t_4$ $t_3$ times, and so on. For example, for a string "12345", one could do the following: split it into ("1", "23", "4", "5"), and write "235555". Let the function $f(t)$ for a string $t$ return the minimum length of the string that can be obtained as a result of that process. You are given a string $s$, consisting of $n$ digits from $1$ to $9$, and an integer $k$. Calculate the value of the function $f$ for all contiguous substrings of $s$ of length exactly $k$. Input: The first line contains two integers $n$ and $k$ ($2 \le k \le n \le 2 \cdot 10^5$). The second line contains the string $s$ ($|s| = n$), consisting only of digits from $1$ to $9$. Output: Output $n - k + 1$ integers β€” $f(s_{1,k}), f(s_{2,k+1}), \dots, f(s_{n - k + 1, n})$. Examples: input: 4 4 5999 output: 14 input: 10 3 1111111111 output: 2 2 2 2 2 2 2 2 input: 11 4 49998641312 output: 12 18 17 15 12 7 7 2 Note:
2005C
https://codeforces.com/problemset/problem/2005/C
Lazy Narek
1,800
[ "dp", "implementation", "strings" ]
Div. 2
2,000
256
Narek is too lazy to create the third problem of this contest. His friend Artur suggests that he should use ChatGPT. ChatGPT creates $n$ problems, each consisting of $m$ letters, so Narek has $n$ strings. To make the problem harder, he combines the problems by selecting some of the $n$ strings possibly none and concatenating them without altering their order. His chance of solving the problem is defined as $score_n - score_c$, where $score_n$ is Narek's score and $score_c$ is ChatGPT's score. Narek calculates $score_n$ by examining the selected string (he moves from left to right). He initially searches for the letter $\texttt{"n"}$, followed by $\texttt{"a"}$, $\texttt{"r"}$, $\texttt{"e"}$, and $\texttt{"k"}$. Upon finding all occurrences of these letters, he increments $score_n$ by $5$ and resumes searching for $\texttt{"n"}$ again (he doesn't go back, and he just continues from where he left off). After Narek finishes, ChatGPT scans through the array and increments $score_c$ by $1$ for each letter $\texttt{"n"}$, $\texttt{"a"}$, $\texttt{"r"}$, $\texttt{"e"}$, or $\texttt{"k"}$ that Narek fails to utilize (note that if Narek fails to complete the last occurrence by finding all of the $5$ letters, then all of the letters he used are counted in ChatGPT's score $score_c$, and Narek doesn't get any points if he doesn't finish finding all the 5 letters). Narek aims to maximize the value of $score_n - score_c$ by selecting the most optimal subset of the initial strings.
In the first line of the input, you're given a single integer $t$ ($1 \le t \le 10^5$), the number of test cases. Then the description of each test case follows. In the first line of each test case, you're given two integers $n, m$ ($1 \le n, m \le 10^3$), the number of strings and the length of each string. In the next $n$ lines, you're given $n$ strings, each having a length of $m$. The strings only contain lowercase letters of the English alphabet. The sum of values of $n \cdot m$ over all test cases does not exceed $10^6$.
For each test case, output a single integer: the maximal possible value of $score_n - score_c$.
[ [ "4\n5 2\nnn\naa\nrr\nee\nkk\n1 5\nnarek\n1 4\nnare\n5 7\nnrrarek\nnrnekan\nuuuuuuu\nppppppp\nnkarekz", "0\n5\n0\n7" ] ]
In the first test case, one of the optimal answers is when Narek doesn't choose any of the strings, so the answer is $0$. He can alternatively choose all the strings. In this case, the full string becomes "nnaarreekk". Narek can choose the first appearances of all letters and add $5$ to the score. His opponent will add $1$ for all second appearances, which will be $5$ in total. So the answer will be $5 - 5 = 0$. In the third test case, the only optimal answer is when Narek doesn't choose the string. Note that if he were to choose the string, he wouldn't be able to find the last letter "k", so his score would stay at $0$ instead of becoming $5$. Then ChatGPT would add $4$ for all of the $4$ letters, and the answer would become $0 - 4 = -4$. In the last test case, Narek needs to choose the first and the last strings. After putting these two next to each other, he gets "${\color{red}{n}}rr{\color{red}{a}}{\color{red}{r}}{\color{red}{e}}{\color{red}{k}}{\color{red}{n}}k{\color{red}{a}}{\color{red}{r}}{\color{red}{e}}{\color{red}{k}}{\color{blue}{z}}$". Narek can choose the letters marked with red and add $10$ to his score. Since the black colored letters Narek left behind are eligible for the opponent to claim (they are used in the word "narek"), the opponent adds all other letters to the score and gets a score of $3$. Therefore, the answer is $10 - 3 = 7$.
Title: Lazy Narek time_limit_ms: 2000 memory_limit_mb: 256 Description: Narek is too lazy to create the third problem of this contest. His friend Artur suggests that he should use ChatGPT. ChatGPT creates $n$ problems, each consisting of $m$ letters, so Narek has $n$ strings. To make the problem harder, he combines the problems by selecting some of the $n$ strings possibly none and concatenating them without altering their order. His chance of solving the problem is defined as $score_n - score_c$, where $score_n$ is Narek's score and $score_c$ is ChatGPT's score. Narek calculates $score_n$ by examining the selected string (he moves from left to right). He initially searches for the letter $\texttt{"n"}$, followed by $\texttt{"a"}$, $\texttt{"r"}$, $\texttt{"e"}$, and $\texttt{"k"}$. Upon finding all occurrences of these letters, he increments $score_n$ by $5$ and resumes searching for $\texttt{"n"}$ again (he doesn't go back, and he just continues from where he left off). After Narek finishes, ChatGPT scans through the array and increments $score_c$ by $1$ for each letter $\texttt{"n"}$, $\texttt{"a"}$, $\texttt{"r"}$, $\texttt{"e"}$, or $\texttt{"k"}$ that Narek fails to utilize (note that if Narek fails to complete the last occurrence by finding all of the $5$ letters, then all of the letters he used are counted in ChatGPT's score $score_c$, and Narek doesn't get any points if he doesn't finish finding all the 5 letters). Narek aims to maximize the value of $score_n - score_c$ by selecting the most optimal subset of the initial strings. Input: In the first line of the input, you're given a single integer $t$ ($1 \le t \le 10^5$), the number of test cases. Then the description of each test case follows. In the first line of each test case, you're given two integers $n, m$ ($1 \le n, m \le 10^3$), the number of strings and the length of each string. In the next $n$ lines, you're given $n$ strings, each having a length of $m$. The strings only contain lowercase letters of the English alphabet. The sum of values of $n \cdot m$ over all test cases does not exceed $10^6$. Output: For each test case, output a single integer: the maximal possible value of $score_n - score_c$. Examples: input: 4 5 2 nn aa rr ee kk 1 5 narek 1 4 nare 5 7 nrrarek nrnekan uuuuuuu ppppppp nkarekz output: 0 5 0 7 Note: In the first test case, one of the optimal answers is when Narek doesn't choose any of the strings, so the answer is $0$. He can alternatively choose all the strings. In this case, the full string becomes "nnaarreekk". Narek can choose the first appearances of all letters and add $5$ to the score. His opponent will add $1$ for all second appearances, which will be $5$ in total. So the answer will be $5 - 5 = 0$. In the third test case, the only optimal answer is when Narek doesn't choose the string. Note that if he were to choose the string, he wouldn't be able to find the last letter "k", so his score would stay at $0$ instead of becoming $5$. Then ChatGPT would add $4$ for all of the $4$ letters, and the answer would become $0 - 4 = -4$. In the last test case, Narek needs to choose the first and the last strings. After putting these two next to each other, he gets "${\color{red}{n}}rr{\color{red}{a}}{\color{red}{r}}{\color{red}{e}}{\color{red}{k}}{\color{red}{n}}k{\color{red}{a}}{\color{red}{r}}{\color{red}{e}}{\color{red}{k}}{\color{blue}{z}}$". Narek can choose the letters marked with red and add $10$ to his score. Since the black colored letters Narek left behind are eligible for the opponent to claim (they are used in the word "narek"), the opponent adds all other letters to the score and gets a score of $3$. Therefore, the answer is $10 - 3 = 7$.
1978F
https://codeforces.com/problemset/problem/1978/F
Large Graph
2,400
[ "data structures", "dfs and similar", "dsu", "graphs", "number theory", "two pointers" ]
Div. 2
4,000
512
Given an array $a$ of length $n$. Let's construct a square matrix $b$ of size $n \times n$, in which the $i$-th row contains the array $a$ cyclically shifted to the right by $(i - 1)$. For example, for the array $a = [3, 4, 5]$, the obtained matrix is $$b = \begin{bmatrix} 3 & 4 & 5 \\\ 5 & 3 & 4 \\\ 4 & 5 & 3 \end{bmatrix}$$ Let's construct the following graph: * The graph contains $n^2$ vertices, each of which corresponds to one of the elements of the matrix. Let's denote the vertex corresponding to the element $b_{i, j}$ as $(i, j)$. * We will draw an edge between vertices $(i_1, j_1)$ and $(i_2, j_2)$ if $|i_1 - i_2| + |j_1 - j_2| \le k$ and $\gcd(b_{i_1, j_1}, b_{i_2, j_2}) > 1$, where $\gcd(x, y)$ denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers $x$ and $y$. Your task is to calculate the number of connected components$^{\dagger}$ in the obtained graph. $^{\dagger}$A connected component of a graph is a set of vertices in which any vertex is reachable from any other via edges, and adding any other vertex to the set violates this rule.
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^6$, $2 \le k \le 2 \cdot 10^6$) β€” the length of the array and the parameter $k$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^6$) β€” the elements of the array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
For each test case, output a single integer β€” the number of connected components in the obtained graph.
[ [ "6\n3 3\n3 4 5\n3 3\n3 4 9\n3 2\n3 4 9\n2 2\n2 8\n5 3\n8 27 5 4 3\n4 10\n2 2 2 2", "3\n2\n3\n1\n4\n1" ] ]
In the first test case, the matrix $b$ is given in the statement. The first connected component contains the vertices $(1, 1)$, $(2, 2)$, and $(3, 3)$. The second connected component contains the vertices $(1, 2)$, $(2, 3)$, and $(3, 1)$. The third connected component contains the vertices $(1, 3)$, $(2, 1)$, and $(3, 2)$. Thus, the graph has $3$ connected components. In the second test case, the following matrix is obtained: $$b = \begin{bmatrix} 3 & 4 & 9 \\\ 9 & 3 & 4 \\\ 4 & 9 & 3 \end{bmatrix}$$ The first connected component contains all vertices corresponding to elements with values $3$ and $9$. The second connected component contains all vertices corresponding to elements with the value $4$. In the fourth test case, all vertices are in one connected component.
Title: Large Graph time_limit_ms: 4000 memory_limit_mb: 512 Description: Given an array $a$ of length $n$. Let's construct a square matrix $b$ of size $n \times n$, in which the $i$-th row contains the array $a$ cyclically shifted to the right by $(i - 1)$. For example, for the array $a = [3, 4, 5]$, the obtained matrix is $$b = \begin{bmatrix} 3 & 4 & 5 \\\ 5 & 3 & 4 \\\ 4 & 5 & 3 \end{bmatrix}$$ Let's construct the following graph: * The graph contains $n^2$ vertices, each of which corresponds to one of the elements of the matrix. Let's denote the vertex corresponding to the element $b_{i, j}$ as $(i, j)$. * We will draw an edge between vertices $(i_1, j_1)$ and $(i_2, j_2)$ if $|i_1 - i_2| + |j_1 - j_2| \le k$ and $\gcd(b_{i_1, j_1}, b_{i_2, j_2}) > 1$, where $\gcd(x, y)$ denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers $x$ and $y$. Your task is to calculate the number of connected components$^{\dagger}$ in the obtained graph. $^{\dagger}$A connected component of a graph is a set of vertices in which any vertex is reachable from any other via edges, and adding any other vertex to the set violates this rule. 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 two integers $n$ and $k$ ($2 \le n \le 10^6$, $2 \le k \le 2 \cdot 10^6$) β€” the length of the array and the parameter $k$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^6$) β€” the elements of the array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$. Output: For each test case, output a single integer β€” the number of connected components in the obtained graph. Examples: input: 6 3 3 3 4 5 3 3 3 4 9 3 2 3 4 9 2 2 2 8 5 3 8 27 5 4 3 4 10 2 2 2 2 output: 3 2 3 1 4 1 Note: In the first test case, the matrix $b$ is given in the statement. The first connected component contains the vertices $(1, 1)$, $(2, 2)$, and $(3, 3)$. The second connected component contains the vertices $(1, 2)$, $(2, 3)$, and $(3, 1)$. The third connected component contains the vertices $(1, 3)$, $(2, 1)$, and $(3, 2)$. Thus, the graph has $3$ connected components. In the second test case, the following matrix is obtained: $$b = \begin{bmatrix} 3 & 4 & 9 \\\ 9 & 3 & 4 \\\ 4 & 9 & 3 \end{bmatrix}$$ The first connected component contains all vertices corresponding to elements with values $3$ and $9$. The second connected component contains all vertices corresponding to elements with the value $4$. In the fourth test case, all vertices are in one connected component.
2002F2
https://codeforces.com/problemset/problem/2002/F2
Court Blue (Hard Version)
2,800
[ "brute force", "dp", "math", "number theory" ]
Div. 1 + 2
4,000
512
This is the hard version of the problem. In this version, it is not guaranteed that $n=m$, and the time limit is higher. You can make hacks only if both versions of the problem are solved. In the court of the Blue King, Lelle and Flamm are having a performance match. The match consists of several rounds. In each round, 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$): $n$, $m$ give 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 4 2 5\n4 4 1 4\n6 6 2 2\n7 9 2 3\n8 9 9 1\n2 7 1 4\n5 9 1 4\n5 6 6 7", "22\n17\n18\n37\n77\n30\n41\n59" ], [ "2\n3082823 20000000 1341 331\n20000000 20000000 3 5", "10754065643\n159999991" ], [ "1\n139 1293 193 412", "559543" ] ]
In the first test case, a possible performance is as follows: * Flamm wins, $\gcd(0,1)=1$. * Lelle wins, $\gcd(1,1)=1$. * Flamm wins, $\gcd(1,2)=1$. * Flamm wins, $\gcd(1,3)=1$. * Flamm wins, $\gcd(1,4)=1$. * Lelle and Flamm agree to stop the match. The final score is $1\cdot2+4\cdot5=22$.
Title: Court Blue (Hard Version) time_limit_ms: 4000 memory_limit_mb: 512 Description: This is the hard version of the problem. In this version, it is not guaranteed that $n=m$, and the time limit is higher. You can make hacks only if both versions of the problem are solved. In the court of the Blue King, Lelle and 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$): $n$, $m$ give 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 4 2 5 4 4 1 4 6 6 2 2 7 9 2 3 8 9 9 1 2 7 1 4 5 9 1 4 5 6 6 7 output: 22 17 18 37 77 30 41 59 input: 2 3082823 20000000 1341 331 20000000 20000000 3 5 output: 10754065643 159999991 input: 1 139 1293 193 412 output: 559543 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$. * Flamm wins, $\gcd(1,4)=1$. * Lelle and Flamm agree to stop the match. The final score is $1\cdot2+4\cdot5=22$.
2036C
https://codeforces.com/problemset/problem/2036/C
Anya and 1100
1,100
[ "brute force", "implementation" ]
Div. 3
3,000
256
While rummaging through things in a distant drawer, Anya found a beautiful string $s$ consisting only of zeros and ones. Now she wants to make it even more beautiful by performing $q$ operations on it. Each operation is described by two integers $i$ ($1 \le i \le |s|$) and $v$ ($v \in \\{0, 1\\}$) and means that the $i$-th character of the string is assigned the value $v$ (that is, the assignment $s_i = v$ is performed). But Anya loves the number $1100$, so after each query, she asks you to tell her whether the substring "1100" is present in her string (i.e. there exist such $1 \le i \le |s| - 3$ that $s_{i}s_{i + 1}s_{i + 2}s_{i + 3} = \texttt{1100}$).
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first line of the test case contains the string $s$ ($1 \leq |s| \leq 2 \cdot 10^5$), consisting only of the characters "0" and "1". Here $|s|$ denotes the length of the string $s$. The next line contains an integer $q$ ($1 \leq q \leq 2 \cdot 10^5$) β€” the number of queries. The following $q$ lines contain two integers $i$ ($1 \leq i \leq |s|$) and $v$ ($v \in \\{0, 1\\}$), describing the query. It is guaranteed that the sum of $|s|$ across all test cases does not exceed $2 \cdot 10^5$. It is also guaranteed that the sum of $q$ across all test cases does not exceed $2 \cdot 10^5$.
For each query, output "YES", if "1100" is present in Anya's string; otherwise, output "NO". You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
[ [ "4\n100\n4\n1 1\n2 0\n2 0\n3 1\n1100000\n3\n6 1\n7 1\n4 1\n111010\n4\n1 1\n5 0\n4 1\n5 0\n0100\n4\n3 1\n1 1\n2 0\n2 1", "NO\nNO\nNO\nNO\nYES\nYES\nNO\nNO\nYES\nYES\nYES\nNO\nNO\nNO\nNO" ] ]
Title: Anya and 1100 time_limit_ms: 3000 memory_limit_mb: 256 Description: While rummaging through things in a distant drawer, Anya found a beautiful string $s$ consisting only of zeros and ones. Now she wants to make it even more beautiful by performing $q$ operations on it. Each operation is described by two integers $i$ ($1 \le i \le |s|$) and $v$ ($v \in \\{0, 1\\}$) and means that the $i$-th character of the string is assigned the value $v$ (that is, the assignment $s_i = v$ is performed). But Anya loves the number $1100$, so after each query, she asks you to tell her whether the substring "1100" is present in her string (i.e. there exist such $1 \le i \le |s| - 3$ that $s_{i}s_{i + 1}s_{i + 2}s_{i + 3} = \texttt{1100}$). Input: The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first line of the test case contains the string $s$ ($1 \leq |s| \leq 2 \cdot 10^5$), consisting only of the characters "0" and "1". Here $|s|$ denotes the length of the string $s$. The next line contains an integer $q$ ($1 \leq q \leq 2 \cdot 10^5$) β€” the number of queries. The following $q$ lines contain two integers $i$ ($1 \leq i \leq |s|$) and $v$ ($v \in \\{0, 1\\}$), describing the query. It is guaranteed that the sum of $|s|$ across all test cases does not exceed $2 \cdot 10^5$. It is also guaranteed that the sum of $q$ across all test cases does not exceed $2 \cdot 10^5$. Output: For each query, output "YES", if "1100" is present in Anya's string; otherwise, output "NO". You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Examples: input: 4 100 4 1 1 2 0 2 0 3 1 1100000 3 6 1 7 1 4 1 111010 4 1 1 5 0 4 1 5 0 0100 4 3 1 1 1 2 0 2 1 output: NO NO NO NO YES YES NO NO YES YES YES NO NO NO NO Note:
2028C
https://codeforces.com/problemset/problem/2028/C
Alice's Adventures in Cutting Cake
1,600
[ "binary search", "dp", "greedy", "two pointers" ]
Div. 2
2,000
256
Alice is at the Mad Hatter's tea party! There is a long sheet cake made up of $n$ sections with tastiness values $a_1, a_2, \ldots, a_n$. There are $m$ creatures at the tea party, excluding Alice. Alice will cut the cake into $m + 1$ pieces. Formally, she will partition the cake into $m + 1$ subarrays, where each subarray consists of some number of adjacent sections. The tastiness of a piece is the sum of tastiness of its sections. Afterwards, she will divvy these $m + 1$ pieces up among the $m$ creatures and herself (her piece can be empty). However, each of the $m$ creatures will only be happy when the tastiness of its piece is $v$ or more. Alice wants to make sure every creature is happy. Limited by this condition, she also wants to maximize the tastiness of her own piece. Can you help Alice find the maximum tastiness her piece can have? If there is no way to make sure every creature is happy, output $-1$.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first line of each test case contains three integers $n, m, v$ ($1\le m\le n\le 2\cdot 10^5$; $1\le v\le 10^9$) β€” the number of sections, the number of creatures, and the creatures' minimum requirement for tastiness, respectively. The next line contains $n$ space separated integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the tastinesses of the sections. The sum of $n$ over all test cases does not exceed $2\cdot 10^5$.
For each test case, output the maximum tastiness Alice can achieve for her piece, or $-1$ if there is no way to make sure every creature is happy.
[ [ "7\n6 2 1\n1 1 10 1 1 10\n6 2 2\n1 1 10 1 1 10\n6 2 3\n1 1 10 1 1 10\n6 2 10\n1 1 10 1 1 10\n6 2 11\n1 1 10 1 1 10\n6 2 12\n1 1 10 1 1 10\n6 2 12\n1 1 1 1 10 10", "22\n12\n2\n2\n2\n0\n-1" ] ]
For the first test case, Alice can give the first and second section as their own pieces, and then take the remaining $10 + 1 + 1 + 10 = 22$ tastiness for herself. We can show that she cannot do any better. For the second test case, Alice could give the first and second section as one piece, and the sixth section as one piece. She can then take the remaining $10 + 1 + 1 = 12$ tastiness for herself. We can show that she cannot do any better. For the seventh test case, Alice cannot give each creature a piece of at least $12$ tastiness.
Title: Alice's Adventures in Cutting Cake time_limit_ms: 2000 memory_limit_mb: 256 Description: Alice is at the Mad Hatter's tea party! There is a long sheet cake made up of $n$ sections with tastiness values $a_1, a_2, \ldots, a_n$. There are $m$ creatures at the tea party, excluding Alice. Alice will cut the cake into $m + 1$ pieces. Formally, she will partition the cake into $m + 1$ subarrays, where each subarray consists of some number of adjacent sections. The tastiness of a piece is the sum of tastiness of its sections. Afterwards, she will divvy these $m + 1$ pieces up among the $m$ creatures and herself (her piece can be empty). However, each of the $m$ creatures will only be happy when the tastiness of its piece is $v$ or more. Alice wants to make sure every creature is happy. Limited by this condition, she also wants to maximize the tastiness of her own piece. Can you help Alice find the maximum tastiness her piece can have? If there is no way to make sure every creature is happy, output $-1$. 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 three integers $n, m, v$ ($1\le m\le n\le 2\cdot 10^5$; $1\le v\le 10^9$) β€” the number of sections, the number of creatures, and the creatures' minimum requirement for tastiness, respectively. The next line contains $n$ space separated integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the tastinesses of the sections. The sum of $n$ over all test cases does not exceed $2\cdot 10^5$. Output: For each test case, output the maximum tastiness Alice can achieve for her piece, or $-1$ if there is no way to make sure every creature is happy. Examples: input: 7 6 2 1 1 1 10 1 1 10 6 2 2 1 1 10 1 1 10 6 2 3 1 1 10 1 1 10 6 2 10 1 1 10 1 1 10 6 2 11 1 1 10 1 1 10 6 2 12 1 1 10 1 1 10 6 2 12 1 1 1 1 10 10 output: 22 12 2 2 2 0 -1 Note: For the first test case, Alice can give the first and second section as their own pieces, and then take the remaining $10 + 1 + 1 + 10 = 22$ tastiness for herself. We can show that she cannot do any better. For the second test case, Alice could give the first and second section as one piece, and the sixth section as one piece. She can then take the remaining $10 + 1 + 1 = 12$ tastiness for herself. We can show that she cannot do any better. For the seventh test case, Alice cannot give each creature a piece of at least $12$ tastiness.
2014A
https://codeforces.com/problemset/problem/2014/A
Robin Helps
800
[ "greedy", "implementation" ]
Div. 3
1,000
256
There is a little bit of the outlaw in everyone, and a little bit of the hero too. The heroic outlaw Robin Hood is famous for taking from the rich and giving to the poor. Robin encounters $n$ people starting from the $1$-st and ending with the $n$-th. The $i$-th person has $a_i$ gold. If $a_i \ge k$, Robin will take all $a_i$ gold, and if $a_i=0$, Robin will give $1$ gold if he has any. Robin starts with $0$ gold. Find out how many people Robin gives gold to.
The first line of the input contains a single integer $t$ ($1\leq t \leq 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 50, 1 \le k \le 100$) β€” the number of people and the threshold at which Robin Hood takes the gold. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 100$) β€” the gold of each person.
For each test case, output a single integer, the number of people that will get gold from Robin Hood.
[ [ "4\n2 2\n2 0\n3 2\n3 0 0\n6 2\n0 3 0 0 0 0\n2 5\n5 4", "1\n2\n3\n0" ] ]
In the first test case, Robin takes $2$ gold from the first person and gives a gold to the second person. In the second test case, Robin takes $3$ gold and gives $1$ gold to each of the next $2$ people. In the third test case, Robin takes $3$ gold and so only gives gold to $3$ other people.
Title: Robin Helps time_limit_ms: 1000 memory_limit_mb: 256 Description: There is a little bit of the outlaw in everyone, and a little bit of the hero too. The heroic outlaw Robin Hood is famous for taking from the rich and giving to the poor. Robin encounters $n$ people starting from the $1$-st and ending with the $n$-th. The $i$-th person has $a_i$ gold. If $a_i \ge k$, Robin will take all $a_i$ gold, and if $a_i=0$, Robin will give $1$ gold if he has any. Robin starts with $0$ gold. Find out how many people Robin gives gold to. Input: The first line of the input contains a single integer $t$ ($1\leq t \leq 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 50, 1 \le k \le 100$) β€” the number of people and the threshold at which Robin Hood takes the gold. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 100$) β€” the gold of each person. Output: For each test case, output a single integer, the number of people that will get gold from Robin Hood. Examples: input: 4 2 2 2 0 3 2 3 0 0 6 2 0 3 0 0 0 0 2 5 5 4 output: 1 2 3 0 Note: In the first test case, Robin takes $2$ gold from the first person and gives a gold to the second person. In the second test case, Robin takes $3$ gold and gives $1$ gold to each of the next $2$ people. In the third test case, Robin takes $3$ gold and so only gives gold to $3$ other people.
1998D
https://codeforces.com/problemset/problem/1998/D
Determine Winning Islands in Race
2,100
[ "data structures", "dp", "graphs", "greedy", "shortest paths" ]
Div. 2
2,000
256
MOOOOOOOOOOOOOOOOO β€” Bessie the Cow, The Art of Racing on Islands Two of Farmer John's cows, Bessie and Elsie, are planning to race on $n$ islands. There are $n - 1$ main bridges, connecting island $i$ to island $i + 1$ for all $1 \leq i \leq n - 1$. Additionally, there are $m$ alternative bridges. Elsie can use both main and alternative bridges, while Bessie can only use main bridges. All bridges are one way and can only be used to travel from an island with a lower index to an island with a higher index. Initially, Elsie starts on island $1$, and Bessie starts on island $s$. The cows alternate turns, with Bessie making the first turn. Suppose the cow is on island $i$. During a cow's turn, if there are any bridges connecting island $i$ to island $j$, then the cow can move to island $j$. Then, island $i$ collapses, and all bridges connecting to island $i$ also collapse. Also, note the following: * If there are no bridges connecting island $i$ to another island, then island $i$ collapses, and this cow is eliminated from the race. * If the other cow is also on island $i$, then after this cow moves to another island, the island collapses, and the other cow is eliminated from the race. * After an island or bridge collapses, no cows may use them. * If a cow is eliminated, their turn is skipped for the rest of the race. The race ends once either cow reaches island $n$. It can be shown that regardless of the cows' strategies, at least one cow reaches island $n$. Bessie wins if and only if she reaches island $n$ first. For each $1 \leq s \leq n - 1$, determine whether Bessie wins if she starts the race on island $s$. Assume both cows follow optimal strategies to ensure their own respective victories.
The first line contains $t$ ($1 \leq t \leq 10^4$) – the number of test cases. The first line of each test case contains $n$ and $m$ ($2 \leq n \leq 2 \cdot 10^5$, $0 \leq m \leq 2 \cdot 10^5$) – the number of islands and the number of alternative bridges. The next $m$ lines of each test case contain $u$ and $v$ ($1 \leq u < v \leq n$) – the islands that the alternative bridge connects. It is guaranteed all alternative bridges are distinct, and they do not coincide with the main bridges. It is guaranteed that neither the sum of $n$ nor the sum of $m$ over all test cases exceeds $2 \cdot 10^5$.
For each test case, output a binary string of length $n - 1$ on a new line. The $i$'th character is $1$ if it is possible for Bessie to win if she starts on island $i$. Otherwise, it is $0$.
[ [ "5\n6 0\n6 1\n2 6\n6 1\n1 5\n10 4\n1 3\n1 6\n2 7\n3 8\n15 3\n2 8\n4 9\n8 15", "11111\n11011\n10011\n100001111\n11000111000111" ] ]
In the first test case, there are no alternative bridges for Elsie to overtake Bessie and reach island $n$ first, so Bessie will win on all islands because she always moves first. In the second case, Bessie will lose if she starts on island $3$ because: * Bessie's Turn: Take a main bridge from island $3$ to island $4$. * Elsie's Turn: Take a main bridge from island $1$ to island $2$. * Bessie's Turn: Take a main bridge from island $4$ to island $5$. * Elsie's Turn: Take an alternative bridge from island $2$ to island $6$. Elsie reaches island $n$ first.
Title: Determine Winning Islands in Race time_limit_ms: 2000 memory_limit_mb: 256 Description: MOOOOOOOOOOOOOOOOO β€” Bessie the Cow, The Art of Racing on Islands Two of Farmer John's cows, Bessie and Elsie, are planning to race on $n$ islands. There are $n - 1$ main bridges, connecting island $i$ to island $i + 1$ for all $1 \leq i \leq n - 1$. Additionally, there are $m$ alternative bridges. Elsie can use both main and alternative bridges, while Bessie can only use main bridges. All bridges are one way and can only be used to travel from an island with a lower index to an island with a higher index. Initially, Elsie starts on island $1$, and Bessie starts on island $s$. The cows alternate turns, with Bessie making the first turn. Suppose the cow is on island $i$. During a cow's turn, if there are any bridges connecting island $i$ to island $j$, then the cow can move to island $j$. Then, island $i$ collapses, and all bridges connecting to island $i$ also collapse. Also, note the following: * If there are no bridges connecting island $i$ to another island, then island $i$ collapses, and this cow is eliminated from the race. * If the other cow is also on island $i$, then after this cow moves to another island, the island collapses, and the other cow is eliminated from the race. * After an island or bridge collapses, no cows may use them. * If a cow is eliminated, their turn is skipped for the rest of the race. The race ends once either cow reaches island $n$. It can be shown that regardless of the cows' strategies, at least one cow reaches island $n$. Bessie wins if and only if she reaches island $n$ first. For each $1 \leq s \leq n - 1$, determine whether Bessie wins if she starts the race on island $s$. Assume both cows follow optimal strategies to ensure their own respective victories. 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 $n$ and $m$ ($2 \leq n \leq 2 \cdot 10^5$, $0 \leq m \leq 2 \cdot 10^5$) – the number of islands and the number of alternative bridges. The next $m$ lines of each test case contain $u$ and $v$ ($1 \leq u < v \leq n$) – the islands that the alternative bridge connects. It is guaranteed all alternative bridges are distinct, and they do not coincide with the main bridges. It is guaranteed that neither the sum of $n$ nor the sum of $m$ over all test cases exceeds $2 \cdot 10^5$. Output: For each test case, output a binary string of length $n - 1$ on a new line. The $i$'th character is $1$ if it is possible for Bessie to win if she starts on island $i$. Otherwise, it is $0$. Examples: input: 5 6 0 6 1 2 6 6 1 1 5 10 4 1 3 1 6 2 7 3 8 15 3 2 8 4 9 8 15 output: 11111 11011 10011 100001111 11000111000111 Note: In the first test case, there are no alternative bridges for Elsie to overtake Bessie and reach island $n$ first, so Bessie will win on all islands because she always moves first. In the second case, Bessie will lose if she starts on island $3$ because: * Bessie's Turn: Take a main bridge from island $3$ to island $4$. * Elsie's Turn: Take a main bridge from island $1$ to island $2$. * Bessie's Turn: Take a main bridge from island $4$ to island $5$. * Elsie's Turn: Take an alternative bridge from island $2$ to island $6$. Elsie reaches island $n$ first.
1986B
https://codeforces.com/problemset/problem/1986/B
Matrix Stabilization
1,000
[ "brute force", "data structures", "greedy", "sortings" ]
Div. 3
2,000
256
You are given a matrix of size $n \times m$, where the rows are numbered from $1$ to $n$ from top to bottom, and the columns are numbered from $1$ to $m$ from left to right. The element at the intersection of the $i$-th row and the $j$-th column is denoted by $a_{ij}$. Consider the algorithm for stabilizing matrix $a$: 1. Find the cell $(i, j)$ such that its value is strictly greater than the values of all its neighboring cells. If there is no such cell, terminate the algorithm. If there are multiple such cells, choose the cell with the smallest value of $i$, and if there are still multiple cells, choose the one with the smallest value of $j$. 2. Set $a_{ij} = a_{ij} - 1$. 3. Go to step $1$. In this problem, cells $(a, b)$ and $(c, d)$ are considered neighbors if they share a common side, i.e., $|a - c| + |b - d| = 1$. Your task is to output the matrix $a$ after the stabilization algorithm has been executed. It can be shown that this algorithm cannot run for an infinite number of iterations.
Each test consists of multiple sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of sets of input data. This is followed by their description. The first line of each set of input data contains two integers $n$ and $m$ ($1 \leq n, m \leq 100, n \cdot m > 1$) β€” the number of rows and columns of matrix $a$. The next $n$ lines describe the corresponding rows of the matrix. The $i$-th line contains $m$ integers $a_{i1}, a_{i2}, \ldots, a_{im}$ ($1 \leq a_{ij} \leq 10^9$). It is guaranteed that the sum of $n \cdot m$ over all sets of input data does not exceed $2 \cdot 10^5$.
For each set of input data, output $n$ lines with $m$ numbers in each line β€” the values of the cells of matrix $a$ after the stabilization algorithm.
[ [ "6\n1 2\n3 1\n2 1\n1\n1\n2 2\n1 2\n3 4\n2 3\n7 4 5\n1 8 10\n5 4\n92 74 31 74\n74 92 17 7\n31 17 92 3\n74 7 3 92\n7 31 1 1\n3 3\n1000000000 1 1000000000\n1 1000000000 1\n1000000000 1 1000000000", "1 1 \n1 \n1 \n1 2 \n3 3 \n4 4 5 \n1 8 8 \n74 74 31 31 \n74 74 17 7 \n31 17 17 3 \n31 7 3 3 \n7 7 1 1 \n1 1 1 \n1 1 1 \n1 1 1" ] ]
In the first set of input data, the algorithm will select the cell $(1, 1)$ twice in a row and then terminate. ![](CDN_BASE_URL/ce7b57ad25f59927de6d90880ce5a2ba) In the second set of input data, there is no cell whose value is strictly greater than the values of all neighboring cells. In the third set of input data, the algorithm will select the cell $(2, 2)$ and then terminate. ![](CDN_BASE_URL/e6f79376ac190defeb9657662c825d80) In the fourth set of input data, the algorithm will select the cell $(1, 1)$ three times and then the cell $(2, 3)$ twice. ![](CDN_BASE_URL/7651bb6af57f6669236e3dd32a7c1293)
Title: Matrix Stabilization time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given a matrix of size $n \times m$, where the rows are numbered from $1$ to $n$ from top to bottom, and the columns are numbered from $1$ to $m$ from left to right. The element at the intersection of the $i$-th row and the $j$-th column is denoted by $a_{ij}$. Consider the algorithm for stabilizing matrix $a$: 1. Find the cell $(i, j)$ such that its value is strictly greater than the values of all its neighboring cells. If there is no such cell, terminate the algorithm. If there are multiple such cells, choose the cell with the smallest value of $i$, and if there are still multiple cells, choose the one with the smallest value of $j$. 2. Set $a_{ij} = a_{ij} - 1$. 3. Go to step $1$. In this problem, cells $(a, b)$ and $(c, d)$ are considered neighbors if they share a common side, i.e., $|a - c| + |b - d| = 1$. Your task is to output the matrix $a$ after the stabilization algorithm has been executed. It can be shown that this algorithm cannot run for an infinite number of iterations. Input: Each test consists of multiple sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of sets of input data. This is followed by their description. The first line of each set of input data contains two integers $n$ and $m$ ($1 \leq n, m \leq 100, n \cdot m > 1$) β€” the number of rows and columns of matrix $a$. The next $n$ lines describe the corresponding rows of the matrix. The $i$-th line contains $m$ integers $a_{i1}, a_{i2}, \ldots, a_{im}$ ($1 \leq a_{ij} \leq 10^9$). It is guaranteed that the sum of $n \cdot m$ over all sets of input data does not exceed $2 \cdot 10^5$. Output: For each set of input data, output $n$ lines with $m$ numbers in each line β€” the values of the cells of matrix $a$ after the stabilization algorithm. Examples: input: 6 1 2 3 1 2 1 1 1 2 2 1 2 3 4 2 3 7 4 5 1 8 10 5 4 92 74 31 74 74 92 17 7 31 17 92 3 74 7 3 92 7 31 1 1 3 3 1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000 output: 1 1 1 1 1 2 3 3 4 4 5 1 8 8 74 74 31 31 74 74 17 7 31 17 17 3 31 7 3 3 7 7 1 1 1 1 1 1 1 1 1 1 1 Note: In the first set of input data, the algorithm will select the cell $(1, 1)$ twice in a row and then terminate. ![](CDN_BASE_URL/ce7b57ad25f59927de6d90880ce5a2ba) In the second set of input data, there is no cell whose value is strictly greater than the values of all neighboring cells. In the third set of input data, the algorithm will select the cell $(2, 2)$ and then terminate. ![](CDN_BASE_URL/e6f79376ac190defeb9657662c825d80) In the fourth set of input data, the algorithm will select the cell $(1, 1)$ three times and then the cell $(2, 3)$ twice. ![](CDN_BASE_URL/7651bb6af57f6669236e3dd32a7c1293)
1994E
https://codeforces.com/problemset/problem/1994/E
Wooden Game
2,000
[ "bitmasks", "greedy", "math", "trees" ]
Div. 1 + 2
2,000
256
You are given a forest of $k$ rooted trees$^{\text{βˆ—}}$. Lumberjack Timofey wants to cut down the entire forest by applying the following operation: * Select a subtree$^{\text{†}}$ of any vertex of one of the trees and remove it from the tree. Timofey loves bitwise operations, so he wants the [bitwise OR](https://en.wikipedia.org/wiki/Bitwise_operation#OR) of the sizes of the subtrees he removed to be maximum. Help him and find the maximum result he can obtain. $^{\text{βˆ—}}$ A tree is a connected graph without cycles, loops, or multiple edges. In a rooted tree, a selected vertex is called a root. A forest is a collection of one or more trees. $^{\text{†}}$ The subtree of a vertex $v$ is the set of vertices for which $v$ lies on the shortest path from this vertex to the root, including $v$ itself.
Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. Then follows the description of the test cases. The first line of each test case contains a single integer $k$ ($1 \leq k \leq 10^6$) β€” the number of trees in the forest. This is followed by a description of each of the $k$ trees: The first line contains a single integer $n$ ($1 \leq n \leq 10^6$) β€” the size of the tree. The vertices of the tree are numbered with integers from $1$ to $n$. The root of the tree is vertex number $1$. The second line contains $n - 1$ integers $p_2, p_3, \ldots p_n$ ($1 \leq p_i < i$), where $p_i$ β€” the parent of vertex $i$. It is guaranteed that the sum of $k$ and $n$ for all sets of input data does not exceed $10^6$.
For each test case, output a single integer β€” the maximum result that can be obtained.
[ [ "3\n1\n1\n\n\n2\n4\n1 2 2\n6\n1 1 3 1 3\n1\n10\n1 2 2 1 1 5 7 6 4", "1\n7\n10" ] ]
In the second test case, the trees look like this: ![](CDN_BASE_URL/149fbfb0b4a0bcf29a8a6b8b997b79b2) The first operation removes the entire second tree. ![](CDN_BASE_URL/3a1651525e7c5087a7d88d51824c16d6) The second operation removes vertex $4$ from the first tree. ![](CDN_BASE_URL/c5e219384aa686ba75274781503be59f) The third operation removes the first tree. The result is $6|1|3 = 7$ ($|$ denotes bitwise OR). In the third test case, the entire tree needs to be removed.
Title: Wooden Game time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given a forest of $k$ rooted trees$^{\text{βˆ—}}$. Lumberjack Timofey wants to cut down the entire forest by applying the following operation: * Select a subtree$^{\text{†}}$ of any vertex of one of the trees and remove it from the tree. Timofey loves bitwise operations, so he wants the [bitwise OR](https://en.wikipedia.org/wiki/Bitwise_operation#OR) of the sizes of the subtrees he removed to be maximum. Help him and find the maximum result he can obtain. $^{\text{βˆ—}}$ A tree is a connected graph without cycles, loops, or multiple edges. In a rooted tree, a selected vertex is called a root. A forest is a collection of one or more trees. $^{\text{†}}$ The subtree of a vertex $v$ is the set of vertices for which $v$ lies on the shortest path from this vertex to the root, including $v$ itself. Input: Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. Then follows the description of the test cases. The first line of each test case contains a single integer $k$ ($1 \leq k \leq 10^6$) β€” the number of trees in the forest. This is followed by a description of each of the $k$ trees: The first line contains a single integer $n$ ($1 \leq n \leq 10^6$) β€” the size of the tree. The vertices of the tree are numbered with integers from $1$ to $n$. The root of the tree is vertex number $1$. The second line contains $n - 1$ integers $p_2, p_3, \ldots p_n$ ($1 \leq p_i < i$), where $p_i$ β€” the parent of vertex $i$. It is guaranteed that the sum of $k$ and $n$ for all sets of input data does not exceed $10^6$. Output: For each test case, output a single integer β€” the maximum result that can be obtained. Examples: input: 3 1 1 2 4 1 2 2 6 1 1 3 1 3 1 10 1 2 2 1 1 5 7 6 4 output: 1 7 10 Note: In the second test case, the trees look like this: ![](CDN_BASE_URL/149fbfb0b4a0bcf29a8a6b8b997b79b2) The first operation removes the entire second tree. ![](CDN_BASE_URL/3a1651525e7c5087a7d88d51824c16d6) The second operation removes vertex $4$ from the first tree. ![](CDN_BASE_URL/c5e219384aa686ba75274781503be59f) The third operation removes the first tree. The result is $6|1|3 = 7$ ($|$ denotes bitwise OR). In the third test case, the entire tree needs to be removed.
2008D
https://codeforces.com/problemset/problem/2008/D
Sakurako's Hobby
1,100
[ "dp", "dsu", "graphs", "math" ]
Div. 3
2,000
256
For a certain permutation $p$$^{\text{βˆ—}}$ Sakurako calls an integer $j$ reachable from an integer $i$ if it is possible to make $i$ equal to $j$ by assigning $i=p_i$ a certain number of times. If $p=[3,5,6,1,2,4]$, then, for example, $4$ is reachable from $1$, because: $i=1$ $\rightarrow$ $i=p_1=3$ $\rightarrow$ $i=p_3=6$ $\rightarrow$ $i=p_6=4$. Now $i=4$, so $4$ is reachable from $1$. Each number in the permutation is colored either black or white. Sakurako defines the function $F(i)$ as the number of black integers that are reachable from $i$. Sakurako is interested in $F(i)$ for each $1\le i\le n$, but calculating all values becomes very difficult, so she asks you, as her good friend, to compute this. $^{\text{βˆ—}}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation (the number $2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$, but the array contains $4$).
The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1\le n\le 2\cdot 10^5$) β€” the number of elements in the array. The second line of each test case contains $n$ integers $p_1, p_2, \dots, p_n$ ($1\le p_i\le n$) β€” the elements of the permutation. The third line of each test case contains a string $s$ of length $n$, consisting of '0' and '1'. If $s_i=0$, then the number $p_i$ is colored black; if $s_i=1$, then the number $p_i$ is colored white. It is guaranteed that the sum of $n$ across all test cases does not exceed $2\cdot 10^5$.
For each test case, output $n$ integers $F(1), F(2), \dots, F(n)$.
[ [ "5\n1\n1\n0\n5\n1 2 4 5 3\n10101\n5\n5 4 1 3 2\n10011\n6\n3 5 6 1 2 4\n010000\n6\n1 2 3 4 5 6\n100110", "1 \n0 1 1 1 1 \n2 2 2 2 2 \n4 1 4 4 1 4 \n0 1 1 0 0 1" ] ]
Title: Sakurako's Hobby time_limit_ms: 2000 memory_limit_mb: 256 Description: For a certain permutation $p$$^{\text{βˆ—}}$ Sakurako calls an integer $j$ reachable from an integer $i$ if it is possible to make $i$ equal to $j$ by assigning $i=p_i$ a certain number of times. If $p=[3,5,6,1,2,4]$, then, for example, $4$ is reachable from $1$, because: $i=1$ $\rightarrow$ $i=p_1=3$ $\rightarrow$ $i=p_3=6$ $\rightarrow$ $i=p_6=4$. Now $i=4$, so $4$ is reachable from $1$. Each number in the permutation is colored either black or white. Sakurako defines the function $F(i)$ as the number of black integers that are reachable from $i$. Sakurako is interested in $F(i)$ for each $1\le i\le n$, but calculating all values becomes very difficult, so she asks you, as her good friend, to compute this. $^{\text{βˆ—}}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation (the number $2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$, but the array contains $4$). 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$ ($1\le n\le 2\cdot 10^5$) β€” the number of elements in the array. The second line of each test case contains $n$ integers $p_1, p_2, \dots, p_n$ ($1\le p_i\le n$) β€” the elements of the permutation. The third line of each test case contains a string $s$ of length $n$, consisting of '0' and '1'. If $s_i=0$, then the number $p_i$ is colored black; if $s_i=1$, then the number $p_i$ is colored white. It is guaranteed that the sum of $n$ across all test cases does not exceed $2\cdot 10^5$. Output: For each test case, output $n$ integers $F(1), F(2), \dots, F(n)$. Examples: input: 5 1 1 0 5 1 2 4 5 3 10101 5 5 4 1 3 2 10011 6 3 5 6 1 2 4 010000 6 1 2 3 4 5 6 100110 output: 1 0 1 1 1 1 2 2 2 2 2 4 1 4 4 1 4 0 1 1 0 0 1 Note:
2024B
https://codeforces.com/problemset/problem/2024/B
Buying Lemonade
1,100
[ "binary search", "constructive algorithms", "sortings" ]
Div. 2
1,000
256
There is a vending machine that sells lemonade. The machine has a total of $n$ slots. You know that initially, the $i$-th slot contains $a_i$ cans of lemonade. There are also $n$ buttons on the machine, each button corresponds to a slot, with exactly one button corresponding to each slot. Unfortunately, the labels on the buttons have worn off, so you do not know which button corresponds to which slot. When you press the button corresponding to the $i$-th slot, one of two events occurs: * If there is a can of lemonade in the $i$-th slot, it will drop out and you will take it. At this point, the number of cans in the $i$-th slot decreases by $1$. * If there are no cans of lemonade left in the $i$-th slot, nothing will drop out. After pressing, the can drops out so quickly that it is impossible to track from which slot it fell. The contents of the slots are hidden from your view, so you cannot see how many cans are left in each slot. The only thing you know is the initial number of cans in the slots: $a_1, a_2, \ldots, a_n$. Determine the minimum number of button presses needed to guarantee that you receive at least $k$ cans of lemonade. Note that you can adapt your strategy during the button presses based on whether you received a can or not. It is guaranteed that there are at least $k$ cans of lemonade in total in the machine. In other words, $k \leq a_1 + a_2 + \ldots + a_n$.
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $1 \leq k \leq 10^9$) β€” the number of slots in the machine and the required number of cans of lemonade. 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 number of cans in the slots. It is guaranteed that $k \leq a_1 + a_2 + \ldots + a_n$, meaning there are at least $k$ cans of lemonade in the machine. 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 minimum number of button presses needed to guarantee that you receive at least $k$ cans of lemonade.
[ [ "5\n2 1\n1 1\n2 2\n1 2\n3 4\n2 1 3\n10 50\n1 1 3 8 8 9 12 13 27 27\n2 1000000000\n1000000000 500000000", "1\n2\n5\n53\n1000000000" ] ]
In the first test case, we can simply press the first button and receive one can of lemonade. In the second test case, we can press each button once and guarantee that we receive $2$ cans of lemonade. Note that if we simply press one button twice, we might not be lucky, and that button could correspond to the first slot, in which case we would only receive $1$ can of lemonade for two presses. In the third test case, one of the optimal strategies is as follows: Press the first button twice. After the first press, a can of lemonade will definitely drop out. Then there are two options: * If no can of lemonade drops after the second press, we know that this button must correspond to the second slot, since $a_2 = 1$ and $a_1, a_3 > 1$. Then we can press the second button twice and the third button once. Since $a_1, a_3 \geq 2$, we will definitely receive three cans of lemonade for these three presses. Thus, after $5$ presses, we will have $4$ cans of lemonade. * If a can of lemonade drops after the second press, we can make one press on the second button and one press on the third button. After each of these presses, we will definitely receive a can of lemonade. Thus, after $4$ presses, we will have $4$ cans of lemonade. It can be shown that it is impossible to guarantee receiving $4$ cans of lemonade with only $4$ presses, so the answer is $5$.
Title: Buying Lemonade time_limit_ms: 1000 memory_limit_mb: 256 Description: There is a vending machine that sells lemonade. The machine has a total of $n$ slots. You know that initially, the $i$-th slot contains $a_i$ cans of lemonade. There are also $n$ buttons on the machine, each button corresponds to a slot, with exactly one button corresponding to each slot. Unfortunately, the labels on the buttons have worn off, so you do not know which button corresponds to which slot. When you press the button corresponding to the $i$-th slot, one of two events occurs: * If there is a can of lemonade in the $i$-th slot, it will drop out and you will take it. At this point, the number of cans in the $i$-th slot decreases by $1$. * If there are no cans of lemonade left in the $i$-th slot, nothing will drop out. After pressing, the can drops out so quickly that it is impossible to track from which slot it fell. The contents of the slots are hidden from your view, so you cannot see how many cans are left in each slot. The only thing you know is the initial number of cans in the slots: $a_1, a_2, \ldots, a_n$. Determine the minimum number of button presses needed to guarantee that you receive at least $k$ cans of lemonade. Note that you can adapt your strategy during the button presses based on whether you received a can or not. It is guaranteed that there are at least $k$ cans of lemonade in total in the machine. In other words, $k \leq a_1 + a_2 + \ldots + a_n$. 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$ ($1 \le n \le 2 \cdot 10^5$, $1 \leq k \leq 10^9$) β€” the number of slots in the machine and the required number of cans of lemonade. 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 number of cans in the slots. It is guaranteed that $k \leq a_1 + a_2 + \ldots + a_n$, meaning there are at least $k$ cans of lemonade in the machine. 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 minimum number of button presses needed to guarantee that you receive at least $k$ cans of lemonade. Examples: input: 5 2 1 1 1 2 2 1 2 3 4 2 1 3 10 50 1 1 3 8 8 9 12 13 27 27 2 1000000000 1000000000 500000000 output: 1 2 5 53 1000000000 Note: In the first test case, we can simply press the first button and receive one can of lemonade. In the second test case, we can press each button once and guarantee that we receive $2$ cans of lemonade. Note that if we simply press one button twice, we might not be lucky, and that button could correspond to the first slot, in which case we would only receive $1$ can of lemonade for two presses. In the third test case, one of the optimal strategies is as follows: Press the first button twice. After the first press, a can of lemonade will definitely drop out. Then there are two options: * If no can of lemonade drops after the second press, we know that this button must correspond to the second slot, since $a_2 = 1$ and $a_1, a_3 > 1$. Then we can press the second button twice and the third button once. Since $a_1, a_3 \geq 2$, we will definitely receive three cans of lemonade for these three presses. Thus, after $5$ presses, we will have $4$ cans of lemonade. * If a can of lemonade drops after the second press, we can make one press on the second button and one press on the third button. After each of these presses, we will definitely receive a can of lemonade. Thus, after $4$ presses, we will have $4$ cans of lemonade. It can be shown that it is impossible to guarantee receiving $4$ cans of lemonade with only $4$ presses, so the answer is $5$.
1974A
https://codeforces.com/problemset/problem/1974/A
Phone Desktop
800
[ "greedy", "math" ]
Div. 3
1,000
256
Little Rosie has a phone with a desktop (or launcher, as it is also called). The desktop can consist of several screens. Each screen is represented as a grid of size $5 \times 3$, i.e., five rows and three columns. There are $x$ applications with an icon size of $1 \times 1$ cells; such an icon occupies only one cell of the screen. There are also $y$ applications with an icon size of $2 \times 2$ cells; such an icon occupies a square of $4$ cells on the screen. Each cell of each screen can be occupied by no more than one icon. Rosie wants to place the application icons on the minimum number of screens. Help her find the minimum number of screens needed.
The first line of the input contains $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first and only line of each test case contains two integers $x$ and $y$ ($0 \leq x, y \leq 99$) β€” the number of applications with a $1 \times 1$ icon and the number of applications with a $2 \times 2$ icon, respectively.
For each test case, output the minimal number of required screens on a separate line.
[ [ "11\n1 1\n7 2\n12 4\n0 3\n1 0\n8 1\n0 0\n2 0\n15 0\n8 2\n0 9", "1\n1\n2\n2\n1\n1\n0\n1\n1\n2\n5" ] ]
The solution for the first test case can look as follows: ![](CDN_BASE_URL/c1f8e47d264264fb303b101863bef14b) Blue squares represent empty spaces for icons, green squares represent $1 \times 1$ icons, red squares represent $2 \times 2$ icons The solution for the third test case can look as follows: ![](CDN_BASE_URL/05b392e954bd162bfc00b3f026bcba0d)
Title: Phone Desktop time_limit_ms: 1000 memory_limit_mb: 256 Description: Little Rosie has a phone with a desktop (or launcher, as it is also called). The desktop can consist of several screens. Each screen is represented as a grid of size $5 \times 3$, i.e., five rows and three columns. There are $x$ applications with an icon size of $1 \times 1$ cells; such an icon occupies only one cell of the screen. There are also $y$ applications with an icon size of $2 \times 2$ cells; such an icon occupies a square of $4$ cells on the screen. Each cell of each screen can be occupied by no more than one icon. Rosie wants to place the application icons on the minimum number of screens. Help her find the minimum number of screens needed. Input: The first line of the input contains $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first and only line of each test case contains two integers $x$ and $y$ ($0 \leq x, y \leq 99$) β€” the number of applications with a $1 \times 1$ icon and the number of applications with a $2 \times 2$ icon, respectively. Output: For each test case, output the minimal number of required screens on a separate line. Examples: input: 11 1 1 7 2 12 4 0 3 1 0 8 1 0 0 2 0 15 0 8 2 0 9 output: 1 1 2 2 1 1 0 1 1 2 5 Note: The solution for the first test case can look as follows: ![](CDN_BASE_URL/c1f8e47d264264fb303b101863bef14b) Blue squares represent empty spaces for icons, green squares represent $1 \times 1$ icons, red squares represent $2 \times 2$ icons The solution for the third test case can look as follows: ![](CDN_BASE_URL/05b392e954bd162bfc00b3f026bcba0d)
1982A
https://codeforces.com/problemset/problem/1982/A
Soccer
800
[ "greedy", "implementation", "math", "sortings" ]
Div. 2
1,000
256
Dima loves watching soccer. In such a game, the score on the scoreboard is represented as $x$ : $y$, where $x$ is the number of goals of the first team, and $y$ is the number of goals of the second team. At any given time, only one team can score a goal, so the score $x$ : $y$ can change to either $(x + 1)$ : $y$, or $x$ : $(y + 1)$. While watching a soccer game, Dima was distracted by very important matters, and after some time, he returned to watching the game. Dima remembers the score right before he was distracted, and the score right after he returned. Given these two scores, he wonders the following question. Is it possible that, while Dima was not watching the game, the teams never had an equal score? It is guaranteed that at neither of the two time points Dima remembers the teams had equal scores. However, it is possible that the score did not change during his absence. Help Dima and answer the question!
Each test consists of several test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) β€” the number of test cases. Then follows the description of the test cases. The first line of each test case contains two integers $x_{1}, y_{1}$ ($0 \le x_{1}, y_{1} \le 10^{9}$, $x_{1} \neq y_{1}$) β€” the score before Dima was distracted. The second line of each test case contains two integers $x_{2}, y_{2}$ ($x_{1} \le x_{2} \le 10^{9}$, $y_{1} \le y_{2} \le 10^{9}$, $x_{2} \neq y_{2}$) β€” the score when Dima returned.
For each test case, output "YES" without quotes if it is possible, that the teams never had a tie while Dima was away, otherwise output "NO" without quotes. You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
[ [ "6\n1 0\n5 0\n1 2\n3 2\n1 2\n4 5\n1 2\n4 3\n1 2\n1 2\n998244353 0\n1000000000 999999999", "YES\nNO\nYES\nNO\nYES\nYES" ] ]
In the first test case, the score before Dima left was $1$ : $0$. When he leaves, the first team scores several goals in a row until the score becomes $5$ : $0$, so the answer is YES. In the second test case, the score could only change as follows: * $1$ : $2$ * $2$ : $2$ * $3$ : $2$ In this scenario, there is a moment when the teams have an equal score, so the answer is NO. In the third test case, one of the possible developments is: * $1$ : $2$ * $1$ : $3$ * $2$ : $3$ * $2$ : $4$ * $2$ : $5$ * $3$ : $5$ * $4$ : $5$ In this scenario, there was no time when the score was equal, so the answer is YES.
Title: Soccer time_limit_ms: 1000 memory_limit_mb: 256 Description: Dima loves watching soccer. In such a game, the score on the scoreboard is represented as $x$ : $y$, where $x$ is the number of goals of the first team, and $y$ is the number of goals of the second team. At any given time, only one team can score a goal, so the score $x$ : $y$ can change to either $(x + 1)$ : $y$, or $x$ : $(y + 1)$. While watching a soccer game, Dima was distracted by very important matters, and after some time, he returned to watching the game. Dima remembers the score right before he was distracted, and the score right after he returned. Given these two scores, he wonders the following question. Is it possible that, while Dima was not watching the game, the teams never had an equal score? It is guaranteed that at neither of the two time points Dima remembers the teams had equal scores. However, it is possible that the score did not change during his absence. Help Dima and answer the question! Input: Each test consists of several test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) β€” the number of test cases. Then follows the description of the test cases. The first line of each test case contains two integers $x_{1}, y_{1}$ ($0 \le x_{1}, y_{1} \le 10^{9}$, $x_{1} \neq y_{1}$) β€” the score before Dima was distracted. The second line of each test case contains two integers $x_{2}, y_{2}$ ($x_{1} \le x_{2} \le 10^{9}$, $y_{1} \le y_{2} \le 10^{9}$, $x_{2} \neq y_{2}$) β€” the score when Dima returned. Output: For each test case, output "YES" without quotes if it is possible, that the teams never had a tie while Dima was away, otherwise output "NO" without quotes. You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer). Examples: input: 6 1 0 5 0 1 2 3 2 1 2 4 5 1 2 4 3 1 2 1 2 998244353 0 1000000000 999999999 output: YES NO YES NO YES YES Note: In the first test case, the score before Dima left was $1$ : $0$. When he leaves, the first team scores several goals in a row until the score becomes $5$ : $0$, so the answer is YES. In the second test case, the score could only change as follows: * $1$ : $2$ * $2$ : $2$ * $3$ : $2$ In this scenario, there is a moment when the teams have an equal score, so the answer is NO. In the third test case, one of the possible developments is: * $1$ : $2$ * $1$ : $3$ * $2$ : $3$ * $2$ : $4$ * $2$ : $5$ * $3$ : $5$ * $4$ : $5$ In this scenario, there was no time when the score was equal, so the answer is YES.
2033C
https://codeforces.com/problemset/problem/2033/C
Sakurako's Field Trip
1,400
[ "dp", "greedy", "two pointers" ]
Div. 3
2,000
256
Even in university, students need to relax. That is why Sakurakos teacher decided to go on a field trip. It is known that all of the students will be walking in one line. The student with index $i$ has some topic of interest which is described as $a_i$. As a teacher, you want to minimise the disturbance of the line of students. The disturbance of the line is defined as the number of neighbouring people with the same topic of interest. In other words, disturbance is the number of indices $j$ ($1 \le j < n$) such that $a_j = a_{j + 1}$. In order to do this, you can choose index $i$ ($1\le i\le n$) and swap students at positions $i$ and $n-i+1$. You can perform any number of swaps. Your task is to determine the minimal amount of disturbance that you can achieve by doing the operation described above any number of times.
The first line contains one integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Each test case is described by two lines. * The first line contains one integer $n$ ($2 \le n \le 10^5$) β€” the length of the line of students. * The second line contains $n$ integers $a_i$ ($1\le a_i\le n$) β€” the topics of interest of students in line. It is guaranteed that the sum of $n$ across all test cases does not exceed $2\cdot 10^5$.
For each test case, output the minimal possible disturbance of the line that you can achieve.
[ [ "9\n5\n1 1 1 2 3\n6\n2 1 2 2 1 1\n4\n1 2 1 1\n6\n2 1 1 2 2 4\n4\n2 1 2 3\n6\n1 2 2 1 2 1\n5\n4 5 5 1 5\n7\n1 4 3 5 1 1 3\n7\n3 1 3 2 2 3 3", "1\n2\n1\n0\n0\n1\n1\n0\n2" ] ]
In the first example, it is necessary to apply the operation to $i=2$, thus the array will become $[1, \textbf{2}, 1, \textbf{1}, 3]$, with the bold elements indicating those that have swapped places. The disturbance of this array is equal to $1$. In the fourth example, it is sufficient to apply the operation to $i=3$, thus the array will become $[2, 1, \textbf{2}, \textbf{1}, 2, 4]$. The disturbance of this array is equal to $0$. In the eighth example, it is sufficient to apply the operation to $i=3$, thus the array will become $[1, 4, \textbf{1}, 5, \textbf{3}, 1, 3]$. The disturbance of this array is equal to $0$.
Title: Sakurako's Field Trip time_limit_ms: 2000 memory_limit_mb: 256 Description: Even in university, students need to relax. That is why Sakurakos teacher decided to go on a field trip. It is known that all of the students will be walking in one line. The student with index $i$ has some topic of interest which is described as $a_i$. As a teacher, you want to minimise the disturbance of the line of students. The disturbance of the line is defined as the number of neighbouring people with the same topic of interest. In other words, disturbance is the number of indices $j$ ($1 \le j < n$) such that $a_j = a_{j + 1}$. In order to do this, you can choose index $i$ ($1\le i\le n$) and swap students at positions $i$ and $n-i+1$. You can perform any number of swaps. Your task is to determine the minimal amount of disturbance that you can achieve by doing the operation described above any number of times. Input: The first line contains one integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Each test case is described by two lines. * The first line contains one integer $n$ ($2 \le n \le 10^5$) β€” the length of the line of students. * The second line contains $n$ integers $a_i$ ($1\le a_i\le n$) β€” the topics of interest of students in line. It is guaranteed that the sum of $n$ across all test cases does not exceed $2\cdot 10^5$. Output: For each test case, output the minimal possible disturbance of the line that you can achieve. Examples: input: 9 5 1 1 1 2 3 6 2 1 2 2 1 1 4 1 2 1 1 6 2 1 1 2 2 4 4 2 1 2 3 6 1 2 2 1 2 1 5 4 5 5 1 5 7 1 4 3 5 1 1 3 7 3 1 3 2 2 3 3 output: 1 2 1 0 0 1 1 0 2 Note: In the first example, it is necessary to apply the operation to $i=2$, thus the array will become $[1, \textbf{2}, 1, \textbf{1}, 3]$, with the bold elements indicating those that have swapped places. The disturbance of this array is equal to $1$. In the fourth example, it is sufficient to apply the operation to $i=3$, thus the array will become $[2, 1, \textbf{2}, \textbf{1}, 2, 4]$. The disturbance of this array is equal to $0$. In the eighth example, it is sufficient to apply the operation to $i=3$, thus the array will become $[1, 4, \textbf{1}, 5, \textbf{3}, 1, 3]$. The disturbance of this array is equal to $0$.
2013E
https://codeforces.com/problemset/problem/2013/E
Prefix GCD
2,200
[ "brute force", "dp", "greedy", "math", "number theory" ]
Div. 2
2,000
256
Since Mansur is tired of making legends, there will be no legends for this task. You are given an array of positive integer numbers $a_1, a_2, \ldots, a_n$. The elements of the array can be rearranged in any order. You need to find the smallest possible value of the expression $$\gcd(a_1) + \gcd(a_1, a_2) + \ldots + \gcd(a_1, a_2, \ldots, a_n),$$ where $\gcd(a_1, a_2, \ldots, a_n)$ denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of $a_1, a_2, \ldots, a_n$.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first line of each test case contains a single number $n$ ($1 \le n \le 10^5$) β€” the size of the array. The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$) β€” the initial array. The sum of $n$ over all test cases does not exceed $10^5$. The sum of $\max(a_1, a_2, \ldots, a_n)$ over all test cases does not exceed $10^5$.
For each test case, output a single number on a separate line β€” the answer to the problem.
[ [ "5\n3\n4 2 2\n2\n6 3\n3\n10 15 6\n5\n6 42 12 52 20\n4\n42 154 231 66", "6\n6\n9\n14\n51" ] ]
In the first test case, the elements can be rearranged as follows: $[2, 4, 2]$. Then the answer will be $\gcd(2) + \gcd(2, 4) + \gcd(2, 4, 2) = 2 + 2 + 2 = 6$. In the third test case, the elements can be rearranged as follows: $[6, 10, 15]$. Then the answer will be $\gcd(6) + \gcd(6, 10) + \gcd(6, 10, 15) = 6 + 2 + 1 = 9$.
Title: Prefix GCD time_limit_ms: 2000 memory_limit_mb: 256 Description: Since Mansur is tired of making legends, there will be no legends for this task. You are given an array of positive integer numbers $a_1, a_2, \ldots, a_n$. The elements of the array can be rearranged in any order. You need to find the smallest possible value of the expression $$\gcd(a_1) + \gcd(a_1, a_2) + \ldots + \gcd(a_1, a_2, \ldots, a_n),$$ where $\gcd(a_1, a_2, \ldots, a_n)$ denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of $a_1, a_2, \ldots, a_n$. 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 number $n$ ($1 \le n \le 10^5$) β€” the size of the array. The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$) β€” the initial array. The sum of $n$ over all test cases does not exceed $10^5$. The sum of $\max(a_1, a_2, \ldots, a_n)$ over all test cases does not exceed $10^5$. Output: For each test case, output a single number on a separate line β€” the answer to the problem. Examples: input: 5 3 4 2 2 2 6 3 3 10 15 6 5 6 42 12 52 20 4 42 154 231 66 output: 6 6 9 14 51 Note: In the first test case, the elements can be rearranged as follows: $[2, 4, 2]$. Then the answer will be $\gcd(2) + \gcd(2, 4) + \gcd(2, 4, 2) = 2 + 2 + 2 = 6$. In the third test case, the elements can be rearranged as follows: $[6, 10, 15]$. Then the answer will be $\gcd(6) + \gcd(6, 10) + \gcd(6, 10, 15) = 6 + 2 + 1 = 9$.
2014G
https://codeforces.com/problemset/problem/2014/G
Milky Days
2,200
[ "brute force", "data structures", "greedy", "implementation" ]
Div. 3
2,000
256
What is done is done, and the spoilt milk cannot be helped. Little John is as little as night is day β€” he was known to be a giant, at possibly $2.1$ metres tall. It has everything to do with his love for milk. His dairy diary has $n$ entries, showing that he acquired $a_i$ pints of fresh milk on day $d_i$. Milk declines in freshness with time and stays drinkable for a maximum of $k$ days. In other words, fresh milk acquired on day $d_i$ will be drinkable between days $d_i$ and $d_i+k-1$ inclusive. Every day, Little John drinks drinkable milk, up to a maximum of $m$ pints. In other words, if there are less than $m$ pints of milk, he will drink them all and not be satisfied; if there are at least $m$ pints of milk, he will drink exactly $m$ pints and be satisfied, and it's a milk satisfaction day. Little John always drinks the freshest drinkable milk first. Determine the number of milk satisfaction days for Little John.
The first line of the input contains a single integer $t$ ($1\leq t \leq 10^4$), the number of test cases. The first line of each test case consists of three integers $n$, $m$, $k$ ($1\le n$, $m$, $k \le 10^5$), the number of diary entries, the maximum pints needed for a milk satisfaction day, and the duration of milk's freshness. Then follow $n$ lines of each test case, each with two integers $d_i$ and $a_i$ ($1\le d_i$, $a_i \le 10^6$), the day on which the milk was acquired and the number of pints acquired. They are sorted in increasing values of $d_i$, and all values of $d_i$ are distinct. 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 milk satisfaction days.
[ [ "6\n1 1 3\n1 5\n2 3 3\n1 5\n2 7\n4 5 2\n1 9\n2 6\n4 9\n5 6\n5 2 4\n4 7\n5 3\n7 1\n11 2\n12 1\n4 1 3\n5 10\n9 4\n14 8\n15 3\n5 5 5\n8 9\n10 7\n16 10\n21 5\n28 9", "3\n3\n4\n5\n10\n6" ] ]
In the first test case, $5$ pints of milk are good for $3$ days before spoiling. In the second test case, the following will happen: * On day $1$, he will receive $5$ pints of milk and drink $3$ of them (leaving $2$ pints from day $1$); * On day $2$, he will receive $7$ pints of milk and drink $3$ of them (leaving $2$ pints from day $1$ and $4$ pints from day $2$); * On day $3$, he will drink $3$ pints from day $2$ (leaving $2$ pints from day $1$ and $1$ pint from day $2$); * On day $4$, the milk acquired on day $1$ will spoil, and he will drink $1$ pint from day $2$ (no more milk is left).
Title: Milky Days time_limit_ms: 2000 memory_limit_mb: 256 Description: What is done is done, and the spoilt milk cannot be helped. Little John is as little as night is day β€” he was known to be a giant, at possibly $2.1$ metres tall. It has everything to do with his love for milk. His dairy diary has $n$ entries, showing that he acquired $a_i$ pints of fresh milk on day $d_i$. Milk declines in freshness with time and stays drinkable for a maximum of $k$ days. In other words, fresh milk acquired on day $d_i$ will be drinkable between days $d_i$ and $d_i+k-1$ inclusive. Every day, Little John drinks drinkable milk, up to a maximum of $m$ pints. In other words, if there are less than $m$ pints of milk, he will drink them all and not be satisfied; if there are at least $m$ pints of milk, he will drink exactly $m$ pints and be satisfied, and it's a milk satisfaction day. Little John always drinks the freshest drinkable milk first. Determine the number of milk satisfaction days for Little John. Input: The first line of the input contains a single integer $t$ ($1\leq t \leq 10^4$), the number of test cases. The first line of each test case consists of three integers $n$, $m$, $k$ ($1\le n$, $m$, $k \le 10^5$), the number of diary entries, the maximum pints needed for a milk satisfaction day, and the duration of milk's freshness. Then follow $n$ lines of each test case, each with two integers $d_i$ and $a_i$ ($1\le d_i$, $a_i \le 10^6$), the day on which the milk was acquired and the number of pints acquired. They are sorted in increasing values of $d_i$, and all values of $d_i$ are distinct. 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 milk satisfaction days. Examples: input: 6 1 1 3 1 5 2 3 3 1 5 2 7 4 5 2 1 9 2 6 4 9 5 6 5 2 4 4 7 5 3 7 1 11 2 12 1 4 1 3 5 10 9 4 14 8 15 3 5 5 5 8 9 10 7 16 10 21 5 28 9 output: 3 3 4 5 10 6 Note: In the first test case, $5$ pints of milk are good for $3$ days before spoiling. In the second test case, the following will happen: * On day $1$, he will receive $5$ pints of milk and drink $3$ of them (leaving $2$ pints from day $1$); * On day $2$, he will receive $7$ pints of milk and drink $3$ of them (leaving $2$ pints from day $1$ and $4$ pints from day $2$); * On day $3$, he will drink $3$ pints from day $2$ (leaving $2$ pints from day $1$ and $1$ pint from day $2$); * On day $4$, the milk acquired on day $1$ will spoil, and he will drink $1$ pint from day $2$ (no more milk is left).
1975C
https://codeforces.com/problemset/problem/1975/C
Chamo and Mocha's Array
1,200
[ "binary search", "brute force", "greedy" ]
Div. 1 + 2
2,000
256
Mocha likes arrays, so before her departure, Chamo gave her an array $a$ consisting of $n$ positive integers as a gift. Mocha doesn't like arrays containing different numbers, so Mocha decides to use magic to change the array. Mocha can perform the following three-step operation some (possibly, zero) times: 1. Choose indices $l$ and $r$ ($1 \leq l < r \leq n$) 2. Let $x$ be the median$^\dagger$ of the subarray $[a_l, a_{l+1},\ldots, a_r]$ 3. Set all values $a_l, a_{l+1},\ldots, a_r$ to $x$ Suppose $a=[1,2,3,4,5]$ initially: * If Mocha chooses $(l,r)=(3,4)$ in the first operation, then $x=3$, the array will be changed into $a=[1,2,3,3,5]$. * If Mocha chooses $(l,r)=(1,3)$ in the first operation, then $x=2$, the array will be changed into $a=[2,2,2,4,5]$. Mocha will perform the operation until the array contains only the same number. Mocha wants to know what is the maximum possible value of this number. $^\dagger$ The median in an array $b$ of length $m$ is an element that occupies position number $\lfloor \frac{m+1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example, the median of $[3,1,4,1,5]$ is $3$ and the median of $[5,25,20,24]$ is $20$.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\leq t\leq 500$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2\leq n\leq 10^5$) β€” the length of the array $a$. The second line of each test case contains $n$ 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 does not exceed $10^5$.
For each test case, output the maximum value of the number.
[ [ "2\n2\n1 2\n5\n1 2 3 4 5", "1\n4" ] ]
In the first test case, $a=[1,2]$. Mocha can only choose the interval $(l,r)=(1,2)$. The array will be changed to $a=[1,1]$. Therefore, the answer is $1$. In the second test case, Mocha can perform the following operations: * Choose the interval $(l,r)=(4,5)$, then $a=[1,2,3,4,4]$. * Choose the interval $(l,r)=(3,5)$, then $a=[1,2,4,4,4]$. * Choose the interval $(l,r)=(1,5)$, then $a=[4,4,4,4,4]$. The array contains only the same number, which is $4$. It can be proven that the maximum value of the final number cannot be greater than $4$.
Title: Chamo and Mocha's Array time_limit_ms: 2000 memory_limit_mb: 256 Description: Mocha likes arrays, so before her departure, Chamo gave her an array $a$ consisting of $n$ positive integers as a gift. Mocha doesn't like arrays containing different numbers, so Mocha decides to use magic to change the array. Mocha can perform the following three-step operation some (possibly, zero) times: 1. Choose indices $l$ and $r$ ($1 \leq l < r \leq n$) 2. Let $x$ be the median$^\dagger$ of the subarray $[a_l, a_{l+1},\ldots, a_r]$ 3. Set all values $a_l, a_{l+1},\ldots, a_r$ to $x$ Suppose $a=[1,2,3,4,5]$ initially: * If Mocha chooses $(l,r)=(3,4)$ in the first operation, then $x=3$, the array will be changed into $a=[1,2,3,3,5]$. * If Mocha chooses $(l,r)=(1,3)$ in the first operation, then $x=2$, the array will be changed into $a=[2,2,2,4,5]$. Mocha will perform the operation until the array contains only the same number. Mocha wants to know what is the maximum possible value of this number. $^\dagger$ The median in an array $b$ of length $m$ is an element that occupies position number $\lfloor \frac{m+1}{2} \rfloor$ after we sort the elements in non-decreasing order. For example, the median of $[3,1,4,1,5]$ is $3$ and the median of $[5,25,20,24]$ is $20$. Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\leq t\leq 500$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2\leq n\leq 10^5$) β€” the length of the array $a$. The second line of each test case contains $n$ 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 does not exceed $10^5$. Output: For each test case, output the maximum value of the number. Examples: input: 2 2 1 2 5 1 2 3 4 5 output: 1 4 Note: In the first test case, $a=[1,2]$. Mocha can only choose the interval $(l,r)=(1,2)$. The array will be changed to $a=[1,1]$. Therefore, the answer is $1$. In the second test case, Mocha can perform the following operations: * Choose the interval $(l,r)=(4,5)$, then $a=[1,2,3,4,4]$. * Choose the interval $(l,r)=(3,5)$, then $a=[1,2,4,4,4]$. * Choose the interval $(l,r)=(1,5)$, then $a=[4,4,4,4,4]$. The array contains only the same number, which is $4$. It can be proven that the maximum value of the final number cannot be greater than $4$.
2008C
https://codeforces.com/problemset/problem/2008/C
Longest Good Array
800
[ "binary search", "brute force", "math" ]
Div. 3
2,000
256
Today, Sakurako was studying arrays. An array $a$ of length $n$ is considered good if and only if: * the array $a$ is increasing, meaning $a_{i - 1} < a_i$ for all $2 \le i \le n$; * the differences between adjacent elements are increasing, meaning $a_i - a_{i-1} < a_{i+1} - a_i$ for all $2 \le i < n$. Sakurako has come up with boundaries $l$ and $r$ and wants to construct a good array of maximum length, where $l \le a_i \le r$ for all $a_i$. Help Sakurako find the maximum length of a good array for the given $l$ and $r$.
The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The only line of each test case contains two integers $l$ and $r$ ($1\le l\le r\le 10^9$).
For each test case, output a single integer β€” the length of the longest good array Sakurako can form given $l$ and $r$.
[ [ "5\n1 2\n1 5\n2 2\n10 20\n1 1000000000", "2\n3\n1\n5\n44721" ] ]
For $l=1$ and $r=5$, one possible array could be $(1,2,5)$. It can be proven that an array of length $4$ does not exist for the given $l$ and $r$. For $l=2$ and $r=2$, the only possible array is $(2)$. For $l=10$ and $r=20$, the only possible array is $(10,11,13,16,20)$.
Title: Longest Good Array time_limit_ms: 2000 memory_limit_mb: 256 Description: Today, Sakurako was studying arrays. An array $a$ of length $n$ is considered good if and only if: * the array $a$ is increasing, meaning $a_{i - 1} < a_i$ for all $2 \le i \le n$; * the differences between adjacent elements are increasing, meaning $a_i - a_{i-1} < a_{i+1} - a_i$ for all $2 \le i < n$. Sakurako has come up with boundaries $l$ and $r$ and wants to construct a good array of maximum length, where $l \le a_i \le r$ for all $a_i$. Help Sakurako find the maximum length of a good array for the given $l$ and $r$. 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 two integers $l$ and $r$ ($1\le l\le r\le 10^9$). Output: For each test case, output a single integer β€” the length of the longest good array Sakurako can form given $l$ and $r$. Examples: input: 5 1 2 1 5 2 2 10 20 1 1000000000 output: 2 3 1 5 44721 Note: For $l=1$ and $r=5$, one possible array could be $(1,2,5)$. It can be proven that an array of length $4$ does not exist for the given $l$ and $r$. For $l=2$ and $r=2$, the only possible array is $(2)$. For $l=10$ and $r=20$, the only possible array is $(10,11,13,16,20)$.
1999B
https://codeforces.com/problemset/problem/1999/B
Card Game
1,000
[ "brute force", "constructive algorithms", "implementation" ]
Div. 4
2,000
256
Suneet and Slavic play a card game. The rules of the game are as follows: * Each card has an integer value between $1$ and $10$. * Each player receives $2$ cards which are face-down (so a player doesn't know their cards). * The game is turn-based and consists exactly of two turns. In a round, both players pick a random unflipped card and flip it. The player who flipped a card with a strictly greater number wins the round. In case of equality, no one wins the round. * A player wins a game if he wins the most number of rounds (i.e. strictly greater than the other player). In case of equality, no one wins the game. Since Suneet and Slavic aren't best friends, you need to calculate the number of ways the game could happen that Suneet would end up as the winner. For a better understanding, please check the notes section.
The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first and only line of each test case contains $4$ integers $a_1$, $a_2$, $b_1$, $b_2$ ($1 \leq a_1, a_2, b_1, b_2 \leq 10$) where $a_1$ and $a_2$ represent the cards Suneet has, and $b_1$ and $b_2$ represent the cards Slavic has, respectively.
For each test case, output a single integer β€” the number of games Suneet would win considering all possible games.
[ [ "5\n3 8 2 6\n1 1 1 1\n10 10 2 2\n1 1 10 10\n3 8 7 2", "2\n0\n4\n0\n2" ] ]
Consider the first test case when Slavic starts with the cards that have the values $2$ and $6$, and Suneet starts with cards that have the values $3$ and $8$. The game could happen in $4$ different ways: * Suneet flips $3$ and Slavic flips $2$. Suneet wins the first round. Then, Suneet flips $8$ and Slavic flips $6$. Suneet wins the second round as well. Since Suneet won $2$ rounds, he wins the game. * Suneet flips $3$ and Slavic flips $6$. Slavic wins the first round. Then, Suneet flips $8$ and Slavic flips $2$. Suneet wins the second round. Nobody wins since both players won an equal amount of rounds. * Suneet flips $8$ and Slavic flips $6$. Suneet wins the first round. Then, Suneet flips $3$ and Slavic flips $2$. Suneet wins the second round as well. Since Suneet won $2$ rounds, he wins the game. * Suneet flips $8$ and Slavic flips $2$. Suneet wins the first round. Then, Suneet flips $3$ and Slavic flips $6$. Slavic wins the round. Nobody wins since both players won an equal amount of rounds.
Title: Card Game time_limit_ms: 2000 memory_limit_mb: 256 Description: Suneet and Slavic play a card game. The rules of the game are as follows: * Each card has an integer value between $1$ and $10$. * Each player receives $2$ cards which are face-down (so a player doesn't know their cards). * The game is turn-based and consists exactly of two turns. In a round, both players pick a random unflipped card and flip it. The player who flipped a card with a strictly greater number wins the round. In case of equality, no one wins the round. * A player wins a game if he wins the most number of rounds (i.e. strictly greater than the other player). In case of equality, no one wins the game. Since Suneet and Slavic aren't best friends, you need to calculate the number of ways the game could happen that Suneet would end up as the winner. For a better understanding, please check the notes section. Input: The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first and only line of each test case contains $4$ integers $a_1$, $a_2$, $b_1$, $b_2$ ($1 \leq a_1, a_2, b_1, b_2 \leq 10$) where $a_1$ and $a_2$ represent the cards Suneet has, and $b_1$ and $b_2$ represent the cards Slavic has, respectively. Output: For each test case, output a single integer β€” the number of games Suneet would win considering all possible games. Examples: input: 5 3 8 2 6 1 1 1 1 10 10 2 2 1 1 10 10 3 8 7 2 output: 2 0 4 0 2 Note: Consider the first test case when Slavic starts with the cards that have the values $2$ and $6$, and Suneet starts with cards that have the values $3$ and $8$. The game could happen in $4$ different ways: * Suneet flips $3$ and Slavic flips $2$. Suneet wins the first round. Then, Suneet flips $8$ and Slavic flips $6$. Suneet wins the second round as well. Since Suneet won $2$ rounds, he wins the game. * Suneet flips $3$ and Slavic flips $6$. Slavic wins the first round. Then, Suneet flips $8$ and Slavic flips $2$. Suneet wins the second round. Nobody wins since both players won an equal amount of rounds. * Suneet flips $8$ and Slavic flips $6$. Suneet wins the first round. Then, Suneet flips $3$ and Slavic flips $2$. Suneet wins the second round as well. Since Suneet won $2$ rounds, he wins the game. * Suneet flips $8$ and Slavic flips $2$. Suneet wins the first round. Then, Suneet flips $3$ and Slavic flips $6$. Slavic wins the round. Nobody wins since both players won an equal amount of rounds.
2021E2
https://codeforces.com/problemset/problem/2021/E2
Digital Village (Hard Version)
2,500
[ "data structures", "dp", "dsu", "graphs", "math", "trees" ]
Div. 2
2,000
256
This is the hard version of the problem. In the three versions, the constraints on $n$ and $m$ are different. You can make hacks only if all the versions of the problem are solved. Pak Chanek is setting up internet connections for the village of Khuntien. The village can be represented as a connected simple graph with $n$ houses and $m$ internet cables connecting house $u_i$ and house $v_i$, each with a latency of $w_i$. There are $p$ houses that require internet. Pak Chanek can install servers in at most $k$ of the houses. The houses that need internet will then be connected to one of the servers. However, since each cable has its latency, the latency experienced by house $s_i$ requiring internet will be the maximum latency of the cables between that house and the server it is connected to. For each $k = 1,2,\ldots,n$, help Pak Chanek determine the minimum total latency that can be achieved for all the houses requiring internet.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2000$). The description of the test cases follows. The first line of each test case contains three integers $n$, $m$, $p$ ($2 \le n \le 5000$; $n-1 \le m \le 5000$; $1 \le p \le n$) β€” the number of houses, the number of cables, and the number of houses that need internet. The second line of each test case contains $p$ integers $s_1, s_2, \ldots, s_p$ ($1 \le s_i \le n$) β€” the houses that need internet. It is guaranteed that all elements of $s$ are distinct. The $i$-th of the next $m$ lines of each test case contains three integers $u_i$, $v_i$, and $w_i$ ($1 \le u_i < v_i \le n$; $1 \le w_i \le 10^9$) β€” the internet cable connecting house $u_i$ and house $v_i$ with latency of $w_i$. It is guaranteed that the given edges form a connected simple graph. It is guaranteed that the sum of $n$ and the sum of $m$ do not exceed $5000$.
For each test case, output $n$ integers: the minimum total latency that can be achieved for all the houses requiring internet for each $k = 1,2,\ldots,n$.
[ [ "2\n9 8 5\n2 5 6 8 9\n1 2 1\n1 3 2\n3 4 10\n4 5 3\n4 6 5\n1 7 10\n7 8 4\n7 9 2\n3 3 2\n3 1\n1 2 1\n2 3 3\n1 3 2", "34 19 9 4 0 0 0 0 0\n2 0 0" ] ]
In the first test case for $k=3$, a possible optimal solution is to install servers at vertices $2$, $6$ and $8$ and obtain the following latency: * $\text{latency}(2) = 0$ * $\text{latency}(5) = \max(3, 5) = 5$ * $\text{latency}(6) = 0$ * $\text{latency}(8) = 0$ * $\text{latency}(9) = \max(2, 4) = 4$ So the total latency is $9$.
Title: Digital Village (Hard Version) time_limit_ms: 2000 memory_limit_mb: 256 Description: This is the hard version of the problem. In the three versions, the constraints on $n$ and $m$ are different. You can make hacks only if all the versions of the problem are solved. Pak Chanek is setting up internet connections for the village of Khuntien. The village can be represented as a connected simple graph with $n$ houses and $m$ internet cables connecting house $u_i$ and house $v_i$, each with a latency of $w_i$. There are $p$ houses that require internet. Pak Chanek can install servers in at most $k$ of the houses. The houses that need internet will then be connected to one of the servers. However, since each cable has its latency, the latency experienced by house $s_i$ requiring internet will be the maximum latency of the cables between that house and the server it is connected to. For each $k = 1,2,\ldots,n$, help Pak Chanek determine the minimum total latency that can be achieved for all the houses requiring internet. Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2000$). The description of the test cases follows. The first line of each test case contains three integers $n$, $m$, $p$ ($2 \le n \le 5000$; $n-1 \le m \le 5000$; $1 \le p \le n$) β€” the number of houses, the number of cables, and the number of houses that need internet. The second line of each test case contains $p$ integers $s_1, s_2, \ldots, s_p$ ($1 \le s_i \le n$) β€” the houses that need internet. It is guaranteed that all elements of $s$ are distinct. The $i$-th of the next $m$ lines of each test case contains three integers $u_i$, $v_i$, and $w_i$ ($1 \le u_i < v_i \le n$; $1 \le w_i \le 10^9$) β€” the internet cable connecting house $u_i$ and house $v_i$ with latency of $w_i$. It is guaranteed that the given edges form a connected simple graph. It is guaranteed that the sum of $n$ and the sum of $m$ do not exceed $5000$. Output: For each test case, output $n$ integers: the minimum total latency that can be achieved for all the houses requiring internet for each $k = 1,2,\ldots,n$. Examples: input: 2 9 8 5 2 5 6 8 9 1 2 1 1 3 2 3 4 10 4 5 3 4 6 5 1 7 10 7 8 4 7 9 2 3 3 2 3 1 1 2 1 2 3 3 1 3 2 output: 34 19 9 4 0 0 0 0 0 2 0 0 Note: In the first test case for $k=3$, a possible optimal solution is to install servers at vertices $2$, $6$ and $8$ and obtain the following latency: * $\text{latency}(2) = 0$ * $\text{latency}(5) = \max(3, 5) = 5$ * $\text{latency}(6) = 0$ * $\text{latency}(8) = 0$ * $\text{latency}(9) = \max(2, 4) = 4$ So the total latency is $9$.
1984H
https://codeforces.com/problemset/problem/1984/H
Tower Capturing
3,300
[ "combinatorics", "dp", "geometry" ]
Div. 1 + 2
2,000
256
There are $n$ towers at $n$ distinct points $(x_1, y_1), (x_2, y_2), \ldots, (x_n, y_n)$, such that no three are collinear and no four are concyclic. Initially, you own towers $(x_1, y_1)$ and $(x_2, y_2)$, and you want to capture all of them. To do this, you can do the following operation any number of times: * Pick two towers $P$ and $Q$ you own and one tower $R$ you don't own, such that the circle through $P$, $Q$, and $R$ contains all $n$ towers inside of it. * Afterwards, capture all towers in or on triangle $\triangle PQR$, including $R$ itself. An attack plan is a series of choices of $R$ ($R_1, R_2, \ldots, R_k$) using the above operations after which you capture all towers. Note that two attack plans are considered different only if they differ in their choice of $R$ in some operation; in particular, two attack plans using the same choices of $R$ but different choices of $P$ and $Q$ are considered the same. Count the number of attack plans of minimal length. Note that it might not be possible to capture all towers, in which case the answer is $0$. Since the answer may be large, output it modulo $998\,244\,353$.
The first line contains a single integer $t$ ($1 \leq t \leq 250$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($4 \leq n \leq 100$) β€” the number of towers. The $i$-th of the next $n$ lines contains two integers $x_i$ and $y_i$ ($-10^4 \leq x_i, y_i \leq 10^4$) β€” the location of the $i$-th tower. Initially, you own towers $(x_1, y_1)$ and $(x_2, y_2)$. All towers are at distinct locations, no three towers are collinear, and no four towers are concyclic. The sum of $n$ over all test cases does not exceed $1000$.
For each test case, output a single integer β€” the number of attack plans of minimal length after which you capture all towers, modulo $998\,244\,353$.
[ [ "3\n5\n1 1\n2 5\n3 3\n4 2\n5 4\n6\n1 1\n3 3\n1 2\n2 1\n3 10000\n19 84\n7\n2 7\n-4 -3\n-3 6\n3 1\n-5 2\n1 -4\n-1 7", "1\n0\n10" ] ]
In the first test case, there is only one possible attack plan of shortest length, shown below. ![](CDN_BASE_URL/fa2c55e855095283a306e017f03d06af) * Use the operation with $P =$ tower $1$, $Q =$ tower $2$, and $R =$ tower $5$. The circle through these three towers contains all towers inside of it, and as a result towers $3$ and $5$ are captured. * Use the operation with $P =$ tower $5$, $Q =$ tower $1$, and $R =$ tower $4$. The circle through these three towers contains all towers inside of it, and as a result tower $4$ is captured. In the second case, for example, you can never capture the tower at $(3, 10\,000)$.
Title: Tower Capturing time_limit_ms: 2000 memory_limit_mb: 256 Description: There are $n$ towers at $n$ distinct points $(x_1, y_1), (x_2, y_2), \ldots, (x_n, y_n)$, such that no three are collinear and no four are concyclic. Initially, you own towers $(x_1, y_1)$ and $(x_2, y_2)$, and you want to capture all of them. To do this, you can do the following operation any number of times: * Pick two towers $P$ and $Q$ you own and one tower $R$ you don't own, such that the circle through $P$, $Q$, and $R$ contains all $n$ towers inside of it. * Afterwards, capture all towers in or on triangle $\triangle PQR$, including $R$ itself. An attack plan is a series of choices of $R$ ($R_1, R_2, \ldots, R_k$) using the above operations after which you capture all towers. Note that two attack plans are considered different only if they differ in their choice of $R$ in some operation; in particular, two attack plans using the same choices of $R$ but different choices of $P$ and $Q$ are considered the same. Count the number of attack plans of minimal length. Note that it might not be possible to capture all towers, in which case the answer is $0$. 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 250$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($4 \leq n \leq 100$) β€” the number of towers. The $i$-th of the next $n$ lines contains two integers $x_i$ and $y_i$ ($-10^4 \leq x_i, y_i \leq 10^4$) β€” the location of the $i$-th tower. Initially, you own towers $(x_1, y_1)$ and $(x_2, y_2)$. All towers are at distinct locations, no three towers are collinear, and no four towers are concyclic. The sum of $n$ over all test cases does not exceed $1000$. Output: For each test case, output a single integer β€” the number of attack plans of minimal length after which you capture all towers, modulo $998\,244\,353$. Examples: input: 3 5 1 1 2 5 3 3 4 2 5 4 6 1 1 3 3 1 2 2 1 3 10000 19 84 7 2 7 -4 -3 -3 6 3 1 -5 2 1 -4 -1 7 output: 1 0 10 Note: In the first test case, there is only one possible attack plan of shortest length, shown below. ![](CDN_BASE_URL/fa2c55e855095283a306e017f03d06af) * Use the operation with $P =$ tower $1$, $Q =$ tower $2$, and $R =$ tower $5$. The circle through these three towers contains all towers inside of it, and as a result towers $3$ and $5$ are captured. * Use the operation with $P =$ tower $5$, $Q =$ tower $1$, and $R =$ tower $4$. The circle through these three towers contains all towers inside of it, and as a result tower $4$ is captured. In the second case, for example, you can never capture the tower at $(3, 10\,000)$.
2021E3
https://codeforces.com/problemset/problem/2021/E3
Digital Village (Extreme Version)
2,800
[ "data structures", "dfs and similar", "dp", "dsu", "graphs", "greedy", "math", "trees" ]
Div. 2
2,000
256
This is the extreme version of the problem. In the three versions, the constraints on $n$ and $m$ are different. You can make hacks only if all the versions of the problem are solved. Pak Chanek is setting up internet connections for the village of Khuntien. The village can be represented as a connected simple graph with $n$ houses and $m$ internet cables connecting house $u_i$ and house $v_i$, each with a latency of $w_i$. There are $p$ houses that require internet. Pak Chanek can install servers in at most $k$ of the houses. The houses that need internet will then be connected to one of the servers. However, since each cable has its latency, the latency experienced by house $s_i$ requiring internet will be the maximum latency of the cables between that house and the server it is connected to. For each $k = 1,2,\ldots,n$, help Pak Chanek determine the minimum total latency that can be achieved for all the houses requiring internet.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first line of each test case contains 3 integers $n$, $m$, $p$ ($2 \le n \le 2 \cdot 10^5$; $n-1 \le m \le 2 \cdot 10^5$; $1 \le p \le n$) β€” the number of houses, the number of cables, and the number of houses that need internet. The second line of each test case contains $p$ integers $s_1, s_2, \ldots, s_p$ ($1 \le s_i \le n$) β€” the houses that need internet. It is guaranteed that all elements of $s$ are distinct. The $i$-th of the next $m$ lines of each test case contains three integers $u_i$, $v_i$, and $w_i$ ($1 \le u_i < v_i \le n$; $1 \le w_i \le 10^9$) β€” the internet cable connecting house $u_i$ and house $v_i$ with latency of $w_i$. It is guaranteed that the given edges form a connected simple graph. It is guaranteed that the sum of $n$ and the sum of $m$ do not exceed $2 \cdot 10^5$.
For each test case, output $n$ integers: the minimum total latency that can be achieved for all the houses requiring internet for each $k = 1,2,\ldots,n$.
[ [ "2\n9 8 5\n2 5 6 8 9\n1 2 1\n1 3 2\n3 4 10\n4 5 3\n4 6 5\n1 7 10\n7 8 4\n7 9 2\n3 3 2\n3 1\n1 2 1\n2 3 3\n1 3 2", "34 19 9 4 0 0 0 0 0\n2 0 0" ] ]
In the first test case for $k=3$, a possible optimal solution is to install servers at vertices $2$, $6$ and $8$ and obtain the following latency: * $\text{latency}(2) = 0$ * $\text{latency}(5) = \max(3, 5) = 5$ * $\text{latency}(6) = 0$ * $\text{latency}(8) = 0$ * $\text{latency}(9) = \max(2, 4) = 4$ So the total latency is $9$.
Title: Digital Village (Extreme Version) time_limit_ms: 2000 memory_limit_mb: 256 Description: This is the extreme version of the problem. In the three versions, the constraints on $n$ and $m$ are different. You can make hacks only if all the versions of the problem are solved. Pak Chanek is setting up internet connections for the village of Khuntien. The village can be represented as a connected simple graph with $n$ houses and $m$ internet cables connecting house $u_i$ and house $v_i$, each with a latency of $w_i$. There are $p$ houses that require internet. Pak Chanek can install servers in at most $k$ of the houses. The houses that need internet will then be connected to one of the servers. However, since each cable has its latency, the latency experienced by house $s_i$ requiring internet will be the maximum latency of the cables between that house and the server it is connected to. For each $k = 1,2,\ldots,n$, help Pak Chanek determine the minimum total latency that can be achieved for all the houses requiring internet. 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 3 integers $n$, $m$, $p$ ($2 \le n \le 2 \cdot 10^5$; $n-1 \le m \le 2 \cdot 10^5$; $1 \le p \le n$) β€” the number of houses, the number of cables, and the number of houses that need internet. The second line of each test case contains $p$ integers $s_1, s_2, \ldots, s_p$ ($1 \le s_i \le n$) β€” the houses that need internet. It is guaranteed that all elements of $s$ are distinct. The $i$-th of the next $m$ lines of each test case contains three integers $u_i$, $v_i$, and $w_i$ ($1 \le u_i < v_i \le n$; $1 \le w_i \le 10^9$) β€” the internet cable connecting house $u_i$ and house $v_i$ with latency of $w_i$. It is guaranteed that the given edges form a connected simple graph. It is guaranteed that the sum of $n$ and the sum of $m$ do not exceed $2 \cdot 10^5$. Output: For each test case, output $n$ integers: the minimum total latency that can be achieved for all the houses requiring internet for each $k = 1,2,\ldots,n$. Examples: input: 2 9 8 5 2 5 6 8 9 1 2 1 1 3 2 3 4 10 4 5 3 4 6 5 1 7 10 7 8 4 7 9 2 3 3 2 3 1 1 2 1 2 3 3 1 3 2 output: 34 19 9 4 0 0 0 0 0 2 0 0 Note: In the first test case for $k=3$, a possible optimal solution is to install servers at vertices $2$, $6$ and $8$ and obtain the following latency: * $\text{latency}(2) = 0$ * $\text{latency}(5) = \max(3, 5) = 5$ * $\text{latency}(6) = 0$ * $\text{latency}(8) = 0$ * $\text{latency}(9) = \max(2, 4) = 4$ So the total latency is $9$.
1975A
https://codeforces.com/problemset/problem/1975/A
Bazoka and Mocha's Array
800
[ "brute force", "greedy", "implementation", "sortings" ]
Div. 1 + 2
1,000
256
Mocha likes arrays, so before her departure, Bazoka gave her an array $a$ consisting of $n$ positive integers as a gift. Now Mocha wants to know whether array $a$ could become sorted in non- decreasing order after performing the following operation some (possibly, zero) times: * Split the array into two parts β€” a prefix and a suffix, then swap these two parts. In other words, let $a=x+y$. Then, we can set $a:= y+x$. Here $+$ denotes the array concatenation operation. For example, if $a=[3,1,4,1,5]$, we can choose $x=[3,1]$ and $y=[4,1,5]$, satisfying $a=x+y$. Then, we can set $a:= y + x = [4,1,5,3,1]$. We can also choose $x=[3,1,4,1,5]$ and $y=[\,]$, satisfying $a=x+y$. Then, we can set $a := y+x = [3,1,4,1,5]$. Note that we are not allowed to choose $x=[3,1,1]$ and $y=[4,5]$, neither are we allowed to choose $x=[1,3]$ and $y=[5,1,4]$, as both these choices do not satisfy $a=x+y$.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\leq t\leq 1000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2\leq n\leq 50$) β€” the length of the array $a$. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq a_i \leq 10^6$) β€” the elements of array $a$.
For each test case, output "Yes" if $a$ could become non-decreasing after performing the operation any number of times, and output "No" if not. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
[ [ "3\n6\n1 1 4 5 1 4\n5\n7 9 2 2 3\n3\n1 2 3", "No\nYes\nYes" ] ]
In the first test case, it can be proven that $a$ cannot become non- decreasing after performing the operation any number of times. In the second test case, we can perform the following operations to make $a$ sorted in non-decreasing order: * Split the array into two parts: $x=[7]$ and $y=[9,2,2,3]$, then swap these two parts. The array will become $y+x = [9,2,2,3,7]$. * Split the array into two parts: $x=[9]$ and $y=[2,2,3,7]$, then swap these two parts. The array will become $y+x=[2,2,3,7,9]$, which is non-decreasing.
Title: Bazoka and Mocha's Array time_limit_ms: 1000 memory_limit_mb: 256 Description: Mocha likes arrays, so before her departure, Bazoka gave her an array $a$ consisting of $n$ positive integers as a gift. Now Mocha wants to know whether array $a$ could become sorted in non- decreasing order after performing the following operation some (possibly, zero) times: * Split the array into two parts β€” a prefix and a suffix, then swap these two parts. In other words, let $a=x+y$. Then, we can set $a:= y+x$. Here $+$ denotes the array concatenation operation. For example, if $a=[3,1,4,1,5]$, we can choose $x=[3,1]$ and $y=[4,1,5]$, satisfying $a=x+y$. Then, we can set $a:= y + x = [4,1,5,3,1]$. We can also choose $x=[3,1,4,1,5]$ and $y=[\,]$, satisfying $a=x+y$. Then, we can set $a := y+x = [3,1,4,1,5]$. Note that we are not allowed to choose $x=[3,1,1]$ and $y=[4,5]$, neither are we allowed to choose $x=[1,3]$ and $y=[5,1,4]$, as both these choices do not satisfy $a=x+y$. Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\leq t\leq 1000$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2\leq n\leq 50$) β€” the length of the array $a$. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq a_i \leq 10^6$) β€” the elements of array $a$. Output: For each test case, output "Yes" if $a$ could become non-decreasing after performing the operation any number of times, and output "No" if not. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response). Examples: input: 3 6 1 1 4 5 1 4 5 7 9 2 2 3 3 1 2 3 output: No Yes Yes Note: In the first test case, it can be proven that $a$ cannot become non- decreasing after performing the operation any number of times. In the second test case, we can perform the following operations to make $a$ sorted in non-decreasing order: * Split the array into two parts: $x=[7]$ and $y=[9,2,2,3]$, then swap these two parts. The array will become $y+x = [9,2,2,3,7]$. * Split the array into two parts: $x=[9]$ and $y=[2,2,3,7]$, then swap these two parts. The array will become $y+x=[2,2,3,7,9]$, which is non-decreasing.
1981B
https://codeforces.com/problemset/problem/1981/B
Turtle and an Infinite Sequence
1,300
[ "bitmasks", "math" ]
Div. 2
1,000
256
There is a sequence $a_0, a_1, a_2, \ldots$ of infinite length. Initially $a_i = i$ for every non-negative integer $i$. After every second, each element of the sequence will simultaneously change. $a_i$ will change to $a_{i - 1} \mid a_i \mid a_{i + 1}$ for every positive integer $i$. $a_0$ will change to $a_0 \mid a_1$. Here, $|$ denotes [bitwise OR](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Turtle is asked to find the value of $a_n$ after $m$ seconds. In particular, if $m = 0$, then he needs to find the initial value of $a_n$. He is tired of calculating so many values, so please help him!
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first line of each test case contains two integers $n, m$ ($0 \le n, m \le 10^9$).
For each test case, output a single integer β€” the value of $a_n$ after $m$ seconds.
[ [ "9\n0 0\n0 1\n0 2\n1 0\n5 2\n10 1\n20 3\n1145 14\n19198 10", "0\n1\n3\n1\n7\n11\n23\n1279\n19455" ] ]
After $1$ second, $[a_0, a_1, a_2, a_3, a_4, a_5]$ will become $[1, 3, 3, 7, 7, 7]$. After $2$ seconds, $[a_0, a_1, a_2, a_3, a_4, a_5]$ will become $[3, 3, 7, 7, 7, 7]$.
Title: Turtle and an Infinite Sequence time_limit_ms: 1000 memory_limit_mb: 256 Description: There is a sequence $a_0, a_1, a_2, \ldots$ of infinite length. Initially $a_i = i$ for every non-negative integer $i$. After every second, each element of the sequence will simultaneously change. $a_i$ will change to $a_{i - 1} \mid a_i \mid a_{i + 1}$ for every positive integer $i$. $a_0$ will change to $a_0 \mid a_1$. Here, $|$ denotes [bitwise OR](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Turtle is asked to find the value of $a_n$ after $m$ seconds. In particular, if $m = 0$, then he needs to find the initial value of $a_n$. He is tired of calculating so many values, so please help him! 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, m$ ($0 \le n, m \le 10^9$). Output: For each test case, output a single integer β€” the value of $a_n$ after $m$ seconds. Examples: input: 9 0 0 0 1 0 2 1 0 5 2 10 1 20 3 1145 14 19198 10 output: 0 1 3 1 7 11 23 1279 19455 Note: After $1$ second, $[a_0, a_1, a_2, a_3, a_4, a_5]$ will become $[1, 3, 3, 7, 7, 7]$. After $2$ seconds, $[a_0, a_1, a_2, a_3, a_4, a_5]$ will become $[3, 3, 7, 7, 7, 7]$.
2005E2
https://codeforces.com/problemset/problem/2005/E2
Subtangle Game (Hard Version)
2,500
[ "data structures", "dp", "games", "greedy", "implementation" ]
Div. 2
2,000
256
This is the hard version of the problem. The differences between the two versions are the constraints on all the variables. You can make hacks only if both versions of the problem are solved. Tsovak and Narek are playing a game. They have an array $a$ and a matrix $b$ of integers with $n$ rows and $m$ columns, numbered from $1$. The cell in the $i$-th row and the $j$-th column is $(i, j)$. They are looking for the elements of $a$ in turns; Tsovak starts first. Each time a player looks for a cell in the matrix containing the current element of $a$ (Tsovak looks for the first, then Narek looks for the second, etc.). Let's say a player has chosen the cell $(r, c)$. The next player has to choose his cell in the submatrix starting at $(r + 1, c + 1)$ and ending in $(n, m)$ (the submatrix can be empty if $r=n$ or $c=m$). If a player cannot find such a cell (or the remaining submatrix is empty) or the array ends (the previous player has chosen the last element), then he loses. Your task is to determine the winner if the players play optimally. Note: since the input is large, you may need to optimize input/output for this problem. For example, in C++, it is enough to use the following lines at the start of the main() function: int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); }
The first line of the input contains $t$ ($1 \le t \le 1500$) – the number of test cases. The first line of each test case contains three integers $l$, $n$, and $m$ ($1 \le l, n, m \le 1500$) – the size of the array and the sizes of the matrix. The second line contains $l$ integers $a_1, a_2, a_3, \ldots a_l$ ($1 \le a_i \le n \cdot m$) – the elements of the array $a$. The $i$-th of the last $n$ lines contains $m$ integers $b_{i,1}, b_{i,2}, b_{i,3}, \ldots b_{i,m}$ ($1 \le b_{i,j} \le n \cdot m$) – representing the $i$-th row of the matrix. It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $3 \cdot 10^6$. It is guaranteed that the sum of $l$ over all test cases does not exceed $1500$.
You should output $t$ lines, the $i$-th of them containing a character representing the answer of the $i$-th test case: "T" if Tsovak wins or "N", otherwise (without quotes).
[ [ "3\n2 2 3\n1 2\n1 3 6\n4 6 2\n2 2 4\n1 2\n1 1 3 2\n4 2 5 1\n2 4 2\n1 2\n3 4\n5 6\n7 8\n8 8", "N\nT\nN" ] ]
In the first example, Tsovak starts by looking for $1$. There is only one occurrence of $1$ at $(1,1)$, so he chooses it. Then Narek needs to look for $2$ in the submatrix of $(2, 2)$, which consists of just the last two elements: $6$ and $2$. He chooses $2$, and then Tsovak loses since the array has ended. In the second example, Tsovak needs to choose $1$. There is a $1$ at the cell $(n,m)$, so he chooses that cell. Then, since the submatrix of $(n + 1, m + 1)$ is empty, Narek cannot find $2$, so he loses.
Title: Subtangle Game (Hard Version) time_limit_ms: 2000 memory_limit_mb: 256 Description: This is the hard version of the problem. The differences between the two versions are the constraints on all the variables. You can make hacks only if both versions of the problem are solved. Tsovak and Narek are playing a game. They have an array $a$ and a matrix $b$ of integers with $n$ rows and $m$ columns, numbered from $1$. The cell in the $i$-th row and the $j$-th column is $(i, j)$. They are looking for the elements of $a$ in turns; Tsovak starts first. Each time a player looks for a cell in the matrix containing the current element of $a$ (Tsovak looks for the first, then Narek looks for the second, etc.). Let's say a player has chosen the cell $(r, c)$. The next player has to choose his cell in the submatrix starting at $(r + 1, c + 1)$ and ending in $(n, m)$ (the submatrix can be empty if $r=n$ or $c=m$). If a player cannot find such a cell (or the remaining submatrix is empty) or the array ends (the previous player has chosen the last element), then he loses. Your task is to determine the winner if the players play optimally. Note: since the input is large, you may need to optimize input/output for this problem. For example, in C++, it is enough to use the following lines at the start of the main() function: int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } Input: The first line of the input contains $t$ ($1 \le t \le 1500$) – the number of test cases. The first line of each test case contains three integers $l$, $n$, and $m$ ($1 \le l, n, m \le 1500$) – the size of the array and the sizes of the matrix. The second line contains $l$ integers $a_1, a_2, a_3, \ldots a_l$ ($1 \le a_i \le n \cdot m$) – the elements of the array $a$. The $i$-th of the last $n$ lines contains $m$ integers $b_{i,1}, b_{i,2}, b_{i,3}, \ldots b_{i,m}$ ($1 \le b_{i,j} \le n \cdot m$) – representing the $i$-th row of the matrix. It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $3 \cdot 10^6$. It is guaranteed that the sum of $l$ over all test cases does not exceed $1500$. Output: You should output $t$ lines, the $i$-th of them containing a character representing the answer of the $i$-th test case: "T" if Tsovak wins or "N", otherwise (without quotes). Examples: input: 3 2 2 3 1 2 1 3 6 4 6 2 2 2 4 1 2 1 1 3 2 4 2 5 1 2 4 2 1 2 3 4 5 6 7 8 8 8 output: N T N Note: In the first example, Tsovak starts by looking for $1$. There is only one occurrence of $1$ at $(1,1)$, so he chooses it. Then Narek needs to look for $2$ in the submatrix of $(2, 2)$, which consists of just the last two elements: $6$ and $2$. He chooses $2$, and then Tsovak loses since the array has ended. In the second example, Tsovak needs to choose $1$. There is a $1$ at the cell $(n,m)$, so he chooses that cell. Then, since the submatrix of $(n + 1, m + 1)$ is empty, Narek cannot find $2$, so he loses.
1981E
https://codeforces.com/problemset/problem/1981/E
Turtle and Intersected Segments
2,600
[ "data structures", "dsu", "graphs", "greedy" ]
Div. 2
5,000
512
Turtle just received $n$ segments and a sequence $a_1, a_2, \ldots, a_n$. The $i$-th segment is $[l_i, r_i]$. Turtle will create an undirected graph $G$. If segment $i$ and segment $j$ intersect, then Turtle will add an undirected edge between $i$ and $j$ with a weight of $|a_i - a_j|$, for every $i \ne j$. Turtle wants you to calculate the sum of the weights of the edges of the minimum spanning tree of the graph $G$, or report that the graph $G$ has no spanning tree. We say two segments $[l_1, r_1]$ and $[l_2, r_2]$ intersect if and only if $\max(l_1, l_2) \le \min(r_1, r_2)$.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \le n \le 5 \cdot 10^5$) β€” the number of segments. The $i$-th of the following $n$ lines contains three integers $l_i, r_i, a_i$ ($1 \le l_i \le r_i \le 10^9, 1 \le a_i \le 10^9$) β€” the $i$-th segment and the $i$-th element of the sequence. 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 sum of the weights of the edges of the minimum spanning tree of the graph $G$. If the graph $G$ has no spanning tree, output $-1$.
[ [ "4\n5\n1 7 3\n2 4 6\n3 5 5\n6 7 9\n3 4 4\n5\n2 7 3\n1 3 6\n4 5 5\n6 7 9\n1 1 4\n4\n1 4 3\n1 2 1\n3 4 5\n1 4 4\n3\n1 3 1\n2 3 3\n4 5 8", "9\n13\n4\n-1" ] ]
In the first test case, the graph $G$ is as follows: ![](CDN_BASE_URL/7b0a32f8f1d54a5dbbaffee1efd8ece1) One of the minimum spanning trees of $G$ is as follows: ![](CDN_BASE_URL/7a035af2d5227648781d0c24b81c076e) The sum of the weights of the edges of the minimum spanning tree is $9$. In the second test case, the graph $G$ is as follows: ![](CDN_BASE_URL/62be880a08fa85071b491d6badd5f58e) $G$ is already a tree, and the sum of the weights of the tree is $13$. In the third test case, the graph $G$ is as follows: ![](CDN_BASE_URL/b667aa771d019e3a8a3a4c4372200f03) In the fourth test case, the graph $G$ is as follows: ![](CDN_BASE_URL/45bc2d052677dbf032ff37fd1d723d83) It's easy to see that $G$ is not connected, so $G$ has no spanning tree.
Title: Turtle and Intersected Segments time_limit_ms: 5000 memory_limit_mb: 512 Description: Turtle just received $n$ segments and a sequence $a_1, a_2, \ldots, a_n$. The $i$-th segment is $[l_i, r_i]$. Turtle will create an undirected graph $G$. If segment $i$ and segment $j$ intersect, then Turtle will add an undirected edge between $i$ and $j$ with a weight of $|a_i - a_j|$, for every $i \ne j$. Turtle wants you to calculate the sum of the weights of the edges of the minimum spanning tree of the graph $G$, or report that the graph $G$ has no spanning tree. We say two segments $[l_1, r_1]$ and $[l_2, r_2]$ intersect if and only if $\max(l_1, l_2) \le \min(r_1, r_2)$. Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \le n \le 5 \cdot 10^5$) β€” the number of segments. The $i$-th of the following $n$ lines contains three integers $l_i, r_i, a_i$ ($1 \le l_i \le r_i \le 10^9, 1 \le a_i \le 10^9$) β€” the $i$-th segment and the $i$-th element of the sequence. 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 sum of the weights of the edges of the minimum spanning tree of the graph $G$. If the graph $G$ has no spanning tree, output $-1$. Examples: input: 4 5 1 7 3 2 4 6 3 5 5 6 7 9 3 4 4 5 2 7 3 1 3 6 4 5 5 6 7 9 1 1 4 4 1 4 3 1 2 1 3 4 5 1 4 4 3 1 3 1 2 3 3 4 5 8 output: 9 13 4 -1 Note: In the first test case, the graph $G$ is as follows: ![](CDN_BASE_URL/7b0a32f8f1d54a5dbbaffee1efd8ece1) One of the minimum spanning trees of $G$ is as follows: ![](CDN_BASE_URL/7a035af2d5227648781d0c24b81c076e) The sum of the weights of the edges of the minimum spanning tree is $9$. In the second test case, the graph $G$ is as follows: ![](CDN_BASE_URL/62be880a08fa85071b491d6badd5f58e) $G$ is already a tree, and the sum of the weights of the tree is $13$. In the third test case, the graph $G$ is as follows: ![](CDN_BASE_URL/b667aa771d019e3a8a3a4c4372200f03) In the fourth test case, the graph $G$ is as follows: ![](CDN_BASE_URL/45bc2d052677dbf032ff37fd1d723d83) It's easy to see that $G$ is not connected, so $G$ has no spanning tree.
1993D
https://codeforces.com/problemset/problem/1993/D
Med-imize
2,200
[ "binary search", "dp", "greedy" ]
Div. 2
2,000
256
Given two positive integers $n$ and $k$, and another array $a$ of $n$ integers. In one operation, you can select any subarray of size $k$ of $a$, then remove it from the array without changing the order of other elements. More formally, let $(l, r)$ be an operation on subarray $a_l, a_{l+1}, \ldots, a_r$ such that $r-l+1=k$, then performing this operation means replacing $a$ with $[a_1, \ldots, a_{l-1}, a_{r+1}, \ldots, a_n]$. For example, if $a=[1,2,3,4,5]$ and we perform operation $(3,5)$ on this array, it will become $a=[1,2]$. Moreover, operation $(2, 4)$ results in $a=[1,5]$, and operation $(1,3)$ results in $a=[4,5]$. You have to repeat the operation while the length of $a$ is greater than $k$ (which means $|a| \gt k$). What is the largest possible median$^\dagger$ of all remaining elements of the array $a$ after the process? $^\dagger$The median of an array of length $n$ is the element whose index is $\left \lfloor (n+1)/2 \right \rfloor$ after we sort the elements in non-decreasing order. For example: $median([2,1,5,4,3]) = 3$, $median([5]) = 5$, and $median([6,8,2,4]) = 4$.
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 5 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.
For each test case, print a single integer β€” the largest median possible after performing the operations.
[ [ "5\n4 3\n3 9 9 2\n5 3\n3 2 5 6 4\n7 1\n5 9 2 6 5 4 6\n8 2\n7 1 2 6 8 3 4 5\n4 5\n3 4 5 6", "3\n4\n9\n6\n4" ] ]
In the first test case, you can select a subarray $(l, r)$ which can be either $(1, 3)$ or $(2, 4)$. Thus, two obtainable final arrays are $[3]$ and $[2]$. The former one has the larger median ($3 > 2$) so the answer is $3$. In the second test case, three obtainable final arrays are $[6, 4]$, $[3, 4]$, and $[3, 2]$. Their medians are $4$, $3$, and $2$ respectively. The answer is $4$. In the third test case, only one element is left in the final array and it can be any element of the initial array. The largest one among them is $9$, so the answer is $9$.
Title: Med-imize time_limit_ms: 2000 memory_limit_mb: 256 Description: Given two positive integers $n$ and $k$, and another array $a$ of $n$ integers. In one operation, you can select any subarray of size $k$ of $a$, then remove it from the array without changing the order of other elements. More formally, let $(l, r)$ be an operation on subarray $a_l, a_{l+1}, \ldots, a_r$ such that $r-l+1=k$, then performing this operation means replacing $a$ with $[a_1, \ldots, a_{l-1}, a_{r+1}, \ldots, a_n]$. For example, if $a=[1,2,3,4,5]$ and we perform operation $(3,5)$ on this array, it will become $a=[1,2]$. Moreover, operation $(2, 4)$ results in $a=[1,5]$, and operation $(1,3)$ results in $a=[4,5]$. You have to repeat the operation while the length of $a$ is greater than $k$ (which means $|a| \gt k$). What is the largest possible median$^\dagger$ of all remaining elements of the array $a$ after the process? $^\dagger$The median of an array of length $n$ is the element whose index is $\left \lfloor (n+1)/2 \right \rfloor$ after we sort the elements in non-decreasing order. For example: $median([2,1,5,4,3]) = 3$, $median([5]) = 5$, and $median([6,8,2,4]) = 4$. 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 two integers $n$ and $k$ ($1 \le n, k \le 5 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$. Output: For each test case, print a single integer β€” the largest median possible after performing the operations. Examples: input: 5 4 3 3 9 9 2 5 3 3 2 5 6 4 7 1 5 9 2 6 5 4 6 8 2 7 1 2 6 8 3 4 5 4 5 3 4 5 6 output: 3 4 9 6 4 Note: In the first test case, you can select a subarray $(l, r)$ which can be either $(1, 3)$ or $(2, 4)$. Thus, two obtainable final arrays are $[3]$ and $[2]$. The former one has the larger median ($3 > 2$) so the answer is $3$. In the second test case, three obtainable final arrays are $[6, 4]$, $[3, 4]$, and $[3, 2]$. Their medians are $4$, $3$, and $2$ respectively. The answer is $4$. In the third test case, only one element is left in the final array and it can be any element of the initial array. The largest one among them is $9$, so the answer is $9$.
1984E
https://codeforces.com/problemset/problem/1984/E
Shuffle
2,400
[ "dp", "greedy", "trees" ]
Div. 1 + 2
2,000
256
Two hungry red pandas, Oscar and Lura, have a tree $T$ with $n$ nodes. They are willing to perform the following shuffle procedure on the whole tree $T$ exactly once. With this shuffle procedure, they will create a new tree out of the nodes of the old tree. 1. Choose any node $V$ from the original tree $T$. Create a new tree $T_2$, with $V$ as the root. 2. Remove $V$ from $T$, such that the original tree is split into one or more subtrees (or zero subtrees, if $V$ is the only node in $T$). 3. Shuffle each subtree with the same procedure (again choosing any node as the root), then connect all shuffled subtrees' roots back to $V$ to finish constructing $T_2$. After this, Oscar and Lura are left with a new tree $T_2$. They can only eat leaves and are very hungry, so please find the maximum number of leaves over all trees that can be created in exactly one shuffle. Note that leaves are all nodes with degree $1$. Thus, the root may be considered as a leaf if it has only one child.
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first line of every test case contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the number of nodes within the original tree $T$. The next $n - 1$ lines each contain two integers $u$ and $v$ ($1 \leq u, v \leq n$) β€” an edge within the original tree $T$. The given edges form a tree. The sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
For each test case, output a single integer β€” the maximum number of leaves achievable with exactly one shuffle procedure on the whole tree.
[ [ "4\n5\n1 2\n1 3\n2 4\n2 5\n5\n1 2\n2 3\n3 4\n4 5\n6\n1 2\n1 3\n1 4\n1 5\n1 6\n10\n9 3\n8 1\n10 6\n8 5\n7 8\n4 6\n1 3\n10 1\n2 7", "4\n3\n5\n6" ] ]
In the first test case, it can be shown that the maximum number of leaves is $4$. To accomplish this, we can start our shuffle with selecting node $3$ as the new root. ![](CDN_BASE_URL/038cb83999c75e319cd6897cdfe03b7b) Next, we are left only with one subtree, in which we can select node $2$ to be the new root of that subtree. ![](CDN_BASE_URL/85eb09dd4b3b98aa44c0450fc1904edf) This will force all $3$ remaining nodes to be leaves, and once we connect them back to our new root, the shuffled subtree looks like this: ![](CDN_BASE_URL/71ffcc64012b118124db081f8a9493b2) We connect the shuffled subtree back to our new root of our new tree. Our final tree has four leaves (including the root), and looks like this: ![](CDN_BASE_URL/882e22ac95139c8917e71dee39d6a32c) In our second test case, we have a line of five nodes. It can be shown that the maximum number of leaves after one shuffle is $3$. We can start off with node $2$, which forces node $1$ to become a leaf. Then, if we select node $4$ on the right side, we will also have nodes $3$ and $5$ as leaves. The third test case is a star graph with six nodes. The number of leaves cannot increase, thus our answer will be $5$ (if we start the shuffling with the original root node).
Title: Shuffle time_limit_ms: 2000 memory_limit_mb: 256 Description: Two hungry red pandas, Oscar and Lura, have a tree $T$ with $n$ nodes. They are willing to perform the following shuffle procedure on the whole tree $T$ exactly once. With this shuffle procedure, they will create a new tree out of the nodes of the old tree. 1. Choose any node $V$ from the original tree $T$. Create a new tree $T_2$, with $V$ as the root. 2. Remove $V$ from $T$, such that the original tree is split into one or more subtrees (or zero subtrees, if $V$ is the only node in $T$). 3. Shuffle each subtree with the same procedure (again choosing any node as the root), then connect all shuffled subtrees' roots back to $V$ to finish constructing $T_2$. After this, Oscar and Lura are left with a new tree $T_2$. They can only eat leaves and are very hungry, so please find the maximum number of leaves over all trees that can be created in exactly one shuffle. Note that leaves are all nodes with degree $1$. Thus, the root may be considered as a leaf if it has only one child. Input: The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first line of every test case contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the number of nodes within the original tree $T$. The next $n - 1$ lines each contain two integers $u$ and $v$ ($1 \leq u, v \leq n$) β€” an edge within the original tree $T$. The given edges form a tree. 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 maximum number of leaves achievable with exactly one shuffle procedure on the whole tree. Examples: input: 4 5 1 2 1 3 2 4 2 5 5 1 2 2 3 3 4 4 5 6 1 2 1 3 1 4 1 5 1 6 10 9 3 8 1 10 6 8 5 7 8 4 6 1 3 10 1 2 7 output: 4 3 5 6 Note: In the first test case, it can be shown that the maximum number of leaves is $4$. To accomplish this, we can start our shuffle with selecting node $3$ as the new root. ![](CDN_BASE_URL/038cb83999c75e319cd6897cdfe03b7b) Next, we are left only with one subtree, in which we can select node $2$ to be the new root of that subtree. ![](CDN_BASE_URL/85eb09dd4b3b98aa44c0450fc1904edf) This will force all $3$ remaining nodes to be leaves, and once we connect them back to our new root, the shuffled subtree looks like this: ![](CDN_BASE_URL/71ffcc64012b118124db081f8a9493b2) We connect the shuffled subtree back to our new root of our new tree. Our final tree has four leaves (including the root), and looks like this: ![](CDN_BASE_URL/882e22ac95139c8917e71dee39d6a32c) In our second test case, we have a line of five nodes. It can be shown that the maximum number of leaves after one shuffle is $3$. We can start off with node $2$, which forces node $1$ to become a leaf. Then, if we select node $4$ on the right side, we will also have nodes $3$ and $5$ as leaves. The third test case is a star graph with six nodes. The number of leaves cannot increase, thus our answer will be $5$ (if we start the shuffling with the original root node).
2004A
https://codeforces.com/problemset/problem/2004/A
Closest Point
800
[ "implementation", "math" ]
Div. 2
2,000
512
Consider a set of points on a line. The distance between two points $i$ and $j$ is $|i - j|$. The point $i$ from the set is the closest to the point $j$ from the set, if there is no other point $k$ in the set such that the distance from $j$ to $k$ is strictly less than the distance from $j$ to $i$. In other words, all other points from the set have distance to $j$ greater or equal to $|i - j|$. For example, consider a set of points $\\{1, 3, 5, 8\\}$: * for the point $1$, the closest point is $3$ (other points have distance greater than $|1-3| = 2$); * for the point $3$, there are two closest points: $1$ and $5$; * for the point $5$, the closest point is $3$ (but not $8$, since its distance is greater than $|3-5|$); * for the point $8$, the closest point is $5$. You are given a set of points. You have to add an integer point into this set in such a way that it is different from every existing point in the set, and it becomes the closest point to every point in the set. Is it possible?
The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. Each test case consists of two lines: * the first line contains one integer $n$ ($2 \le n \le 40$) β€” the number of points in the set; * the second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_1 < x_2 < \dots < x_n \le 100$) β€” the points from the set.
For each test case, print YES if it is possible to add a new point according to the conditions from the statement. Otherwise, print NO.
[ [ "3\n2\n3 8\n2\n5 6\n6\n1 2 3 4 5 10", "YES\nNO\nNO" ] ]
In the first example, the point $7$ will be the closest to both $3$ and $8$. In the second example, it is impossible to add an integer point so that it becomes the closest to both $5$ and $6$, and is different from both of them.
Title: Closest Point time_limit_ms: 2000 memory_limit_mb: 512 Description: Consider a set of points on a line. The distance between two points $i$ and $j$ is $|i - j|$. The point $i$ from the set is the closest to the point $j$ from the set, if there is no other point $k$ in the set such that the distance from $j$ to $k$ is strictly less than the distance from $j$ to $i$. In other words, all other points from the set have distance to $j$ greater or equal to $|i - j|$. For example, consider a set of points $\\{1, 3, 5, 8\\}$: * for the point $1$, the closest point is $3$ (other points have distance greater than $|1-3| = 2$); * for the point $3$, there are two closest points: $1$ and $5$; * for the point $5$, the closest point is $3$ (but not $8$, since its distance is greater than $|3-5|$); * for the point $8$, the closest point is $5$. You are given a set of points. You have to add an integer point into this set in such a way that it is different from every existing point in the set, and it becomes the closest point to every point in the set. Is it possible? Input: The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. Each test case consists of two lines: * the first line contains one integer $n$ ($2 \le n \le 40$) β€” the number of points in the set; * the second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_1 < x_2 < \dots < x_n \le 100$) β€” the points from the set. Output: For each test case, print YES if it is possible to add a new point according to the conditions from the statement. Otherwise, print NO. Examples: input: 3 2 3 8 2 5 6 6 1 2 3 4 5 10 output: YES NO NO Note: In the first example, the point $7$ will be the closest to both $3$ and $8$. In the second example, it is impossible to add an integer point so that it becomes the closest to both $5$ and $6$, and is different from both of them.
1980F2
https://codeforces.com/problemset/problem/1980/F2
Field Division (hard version)
2,400
[ "math", "sortings" ]
Div. 3
3,000
256
This is a hard version of the problem; it differs from the easy version only by the question. The easy version only needs you to print whether some values are non-zero or not. The hard version needs you to print the exact values. Alice and Bob are dividing the field. The field is a rectangle of size $n \times m$ ($2 \le n, m \le 10^9$); the rows are numbered from $1$ to $n$ from top to bottom, and the columns are numbered from $1$ to $m$ from left to right. The cell at the intersection of row $r$ and column $c$ is denoted as ($r, c$). Bob has $k$ ($2 \le k \le 2 \cdot 10^5$) fountains, all of them are located in different cells of the field. Alice is responsible for dividing the field, but she must meet several conditions: * To divide the field, Alice will start her path in any free (without a fountain) cell on the left or top side of the field and will move, each time moving to the adjacent cell down or right. Her path will end on the right or bottom side of the field. * Alice's path will divide the field into two parts β€” one part will belong to Alice (this part includes the cells of her path), the other part β€” to Bob. * Alice will own the part that includes the cell ($n, 1$). * Bob will own the part that includes the cell ($1, m$). Alice wants to divide the field in such a way as to get as many cells as possible. Bob wants to keep ownership of all the fountains, but he can give one of them to Alice. First, output the integer $\alpha$ β€” the maximum possible size of Alice's plot, if Bob does not give her any fountain (i.e., all fountains will remain on Bob's plot). Then output $k$ non-negative integers $a_1, a_2, \dots, a_k$, where $a_i$ is a value such that after Bob gives Alice the $i$-th fountain, the maximum size of her plot will be $\alpha + a_i$.
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains three integers $n$, $m$, and $k$ ($2 \le n, m \le 10^9$, $2 \le k \le 2 \cdot 10^5$) β€” the field sizes and the number of fountains, respectively. Then follow $k$ lines, each containing two numbers $r_i$ and $c_i$ ($1 \le r_i \le n$, $1 \le c_i \le m$) β€” the coordinates of the cell with the $i$-th fountain. It is guaranteed that all cells are distinct and none of them is ($n, 1$). It is guaranteed that the sum of $k$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, first output $\alpha$ β€” the maximum size of the plot that can belong to Alice if Bob does not give her any of the fountains. Then output $k$ non-negative integers $a_1, a_2, \dots, a_k$, where $a_i$ is a value such that after Bob gives Alice the $i$-th fountain, the maximum size of her plot will be $\alpha + a_i$.
[ [ "5\n2 2 3\n1 1\n1 2\n2 2\n5 5 4\n1 2\n2 2\n3 4\n4 3\n2 5 9\n1 2\n1 5\n1 1\n2 2\n2 4\n2 5\n1 4\n2 3\n1 3\n6 4 4\n6 2\n1 3\n1 4\n1 2\n3 4 5\n2 1\n3 2\n1 4\n1 3\n2 4", "1\n1 0 1 \n11\n0 1 0 4 \n1\n0 0 1 1 0 0 0 0 0 \n6\n15 0 0 0 \n1\n2 3 0 0 0" ] ]
Below are the images for the second example: ![](CDN_BASE_URL/0e3fee1fe74ac4348d869fc256da57a3) The indices of the fountains are labeled in green. The cells belonging to Alice are marked in blue. Note that if Bob gives Alice fountain $1$ or fountain $3$, then that fountain cannot be on Alice's plot.
Title: Field Division (hard version) time_limit_ms: 3000 memory_limit_mb: 256 Description: This is a hard version of the problem; it differs from the easy version only by the question. The easy version only needs you to print whether some values are non-zero or not. The hard version needs you to print the exact values. Alice and Bob are dividing the field. The field is a rectangle of size $n \times m$ ($2 \le n, m \le 10^9$); the rows are numbered from $1$ to $n$ from top to bottom, and the columns are numbered from $1$ to $m$ from left to right. The cell at the intersection of row $r$ and column $c$ is denoted as ($r, c$). Bob has $k$ ($2 \le k \le 2 \cdot 10^5$) fountains, all of them are located in different cells of the field. Alice is responsible for dividing the field, but she must meet several conditions: * To divide the field, Alice will start her path in any free (without a fountain) cell on the left or top side of the field and will move, each time moving to the adjacent cell down or right. Her path will end on the right or bottom side of the field. * Alice's path will divide the field into two parts β€” one part will belong to Alice (this part includes the cells of her path), the other part β€” to Bob. * Alice will own the part that includes the cell ($n, 1$). * Bob will own the part that includes the cell ($1, m$). Alice wants to divide the field in such a way as to get as many cells as possible. Bob wants to keep ownership of all the fountains, but he can give one of them to Alice. First, output the integer $\alpha$ β€” the maximum possible size of Alice's plot, if Bob does not give her any fountain (i.e., all fountains will remain on Bob's plot). Then output $k$ non-negative integers $a_1, a_2, \dots, a_k$, where $a_i$ is a value such that after Bob gives Alice the $i$-th fountain, the maximum size of her plot will be $\alpha + a_i$. 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 three integers $n$, $m$, and $k$ ($2 \le n, m \le 10^9$, $2 \le k \le 2 \cdot 10^5$) β€” the field sizes and the number of fountains, respectively. Then follow $k$ lines, each containing two numbers $r_i$ and $c_i$ ($1 \le r_i \le n$, $1 \le c_i \le m$) β€” the coordinates of the cell with the $i$-th fountain. It is guaranteed that all cells are distinct and none of them is ($n, 1$). It is guaranteed that the sum of $k$ over all test cases does not exceed $2 \cdot 10^5$. Output: For each test case, first output $\alpha$ β€” the maximum size of the plot that can belong to Alice if Bob does not give her any of the fountains. Then output $k$ non-negative integers $a_1, a_2, \dots, a_k$, where $a_i$ is a value such that after Bob gives Alice the $i$-th fountain, the maximum size of her plot will be $\alpha + a_i$. Examples: input: 5 2 2 3 1 1 1 2 2 2 5 5 4 1 2 2 2 3 4 4 3 2 5 9 1 2 1 5 1 1 2 2 2 4 2 5 1 4 2 3 1 3 6 4 4 6 2 1 3 1 4 1 2 3 4 5 2 1 3 2 1 4 1 3 2 4 output: 1 1 0 1 11 0 1 0 4 1 0 0 1 1 0 0 0 0 0 6 15 0 0 0 1 2 3 0 0 0 Note: Below are the images for the second example: ![](CDN_BASE_URL/0e3fee1fe74ac4348d869fc256da57a3) The indices of the fountains are labeled in green. The cells belonging to Alice are marked in blue. Note that if Bob gives Alice fountain $1$ or fountain $3$, then that fountain cannot be on Alice's plot.
2008B
https://codeforces.com/problemset/problem/2008/B
Square or Not
800
[ "brute force", "math", "strings" ]
Div. 3
2,000
256
A beautiful binary matrix is a matrix that has ones on its edges and zeros inside. ![](CDN_BASE_URL/5df31946486165887b31c82158ed558d) Examples of four beautiful binary matrices. Today, Sakurako was playing with a beautiful binary matrix of size $r \times c$ and created a binary string $s$ by writing down all the rows of the matrix, starting from the first and ending with the $r$-th. More formally, the element from the matrix in the $i$-th row and $j$-th column corresponds to the $((i-1)*c+j)$-th element of the string. You need to check whether the beautiful matrix from which the string $s$ was obtained could be squared. In other words, you need to check whether the string $s$ could have been build from a square beautiful binary matrix (i.e., one where $r=c$).
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the length of the string. The second line of each test case contains the string $s$ of length $n$. The string is always the result of writing out the strings of a beautiful matrix. It is guaranteed that the sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
Print "Yes", if the original matrix could have been square, and "No" otherwise.
[ [ "5\n2\n11\n4\n1111\n9\n111101111\n9\n111111111\n12\n111110011111", "No\nYes\nYes\nNo\nNo" ] ]
For the second test case, string 1111 can be obtained from the matrix: $1$| $1$ ---|--- $1$| $1$ For the third test case, string 111101111 can be obtained from the matrix: $1$| $1$| $1$ ---|---|--- $1$| $0$| $1$ $1$| $1$| $1$ There is no square matrix in the fourth case, such that the string can be obtained from it.
Title: Square or Not time_limit_ms: 2000 memory_limit_mb: 256 Description: A beautiful binary matrix is a matrix that has ones on its edges and zeros inside. ![](CDN_BASE_URL/5df31946486165887b31c82158ed558d) Examples of four beautiful binary matrices. Today, Sakurako was playing with a beautiful binary matrix of size $r \times c$ and created a binary string $s$ by writing down all the rows of the matrix, starting from the first and ending with the $r$-th. More formally, the element from the matrix in the $i$-th row and $j$-th column corresponds to the $((i-1)*c+j)$-th element of the string. You need to check whether the beautiful matrix from which the string $s$ was obtained could be squared. In other words, you need to check whether the string $s$ could have been build from a square beautiful binary matrix (i.e., one where $r=c$). 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$ ($2 \le n \le 2 \cdot 10^5$) β€” the length of the string. The second line of each test case contains the string $s$ of length $n$. The string is always the result of writing out the strings of a beautiful matrix. It is guaranteed that the sum of $n$ across all test cases does not exceed $2 \cdot 10^5$. Output: Print "Yes", if the original matrix could have been square, and "No" otherwise. Examples: input: 5 2 11 4 1111 9 111101111 9 111111111 12 111110011111 output: No Yes Yes No No Note: For the second test case, string 1111 can be obtained from the matrix: $1$| $1$ ---|--- $1$| $1$ For the third test case, string 111101111 can be obtained from the matrix: $1$| $1$| $1$ ---|---|--- $1$| $0$| $1$ $1$| $1$| $1$ There is no square matrix in the fourth case, such that the string can be obtained from it.
1976C
https://codeforces.com/problemset/problem/1976/C
Job Interview
1,600
[ "binary search", "dp", "greedy", "implementation", "two pointers" ]
Div. 2
2,000
256
Monocarp is opening his own IT company. He wants to hire $n$ programmers and $m$ testers. There are $n+m+1$ candidates, numbered from $1$ to $n+m+1$ in chronological order of their arriving time. The $i$-th candidate has programming skill $a_i$ and testing skill $b_i$ (a person's programming skill is different from their testing skill). The skill of the team is the sum of the programming skills of all candidates hired as programmers, and the sum of the testing skills of all candidates hired as testers. When a candidate arrives to interview, Monocarp tries to assign them to the most suitable position for them (if their programming skill is higher, then he hires them as a programmer, otherwise as a tester). If all slots for that position are filled, Monocarp assigns them to the other position. Your task is, for each candidate, calculate the skill of the team if everyone except them comes to interview. Note that it means that exactly $n+m$ candidates will arrive, so all $n+m$ positions in the company will be filled.
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Each test case consists of three lines: * the first line contains two integers $n$ and $m$ ($0 \le n, m \le 2 \cdot 10^5$; $2 \le n + m + 1 \le 2 \cdot 10^5$) β€” the number of programmers and the number of testers Monocarp wants to hire, respectively; * the second line contains $n + m + 1$ integers $a_1, a_2, \dots, a_{n+m+1}$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th candidate; * the third line contains $n + m + 1$ integers $b_1, b_2, \dots, b_{n+m+1}$ ($1 \le b_i \le 10^9$; $b_i \ne a_i$), where $b_i$ is the testing skill of the $i$-th candidate. Additional constraint on the input: the sum of $(n + m + 1)$ over all test cases doesn't exceed $2 \cdot 10^5$.
For each test case, print $n + m + 1$ integers, where the $i$-th integer should be equal to the skill of the team if everyone except the $i$-th candidate comes to interview.
[ [ "4\n1 0\n2 1\n1 2\n0 2\n4 5 5\n5 4 1\n1 2\n2 1 5 4\n5 2 3 1\n3 1\n4 3 3 4 1\n5 5 4 5 2", "1 2 \n5 6 9 \n8 11 11 12 \n13 13 13 12 15" ] ]
Let's consider the third test case of the example: * if the $1$-st candidate does not arrive, the $2$-nd candidate gets hired as a tester, the $3$-rd candidate gets hired as a programmer, the $4$-th candidate gets hired as a tester. The total skill of the team will be $2 + 5 + 1 = 8$; * if the $2$-nd candidate does not arrive, the $1$-st candidate gets hired as a tester, the $3$-rd candidate gets hired as a programmer, the $4$-th candidate gets hired as a tester. The total skill of the team will be $5 + 5 + 1 = 11$; * if the $3$-rd candidate does not arrive, the $1$-st candidate gets hired as a tester, the $2$-nd candidate gets hired as a tester, the $4$-th candidate gets hired as a programmer. The total skill of the team will be $5 + 2 + 4 = 11$; * if the $4$-th candidate does not arrive, the $1$-st candidate gets hired as a tester, the $2$-nd candidate gets hired as a tester, the $3$-rd candidate gets hired as a programmer. The total skill of the team will be $5 + 2 + 5 = 12$.
Title: Job Interview time_limit_ms: 2000 memory_limit_mb: 256 Description: Monocarp is opening his own IT company. He wants to hire $n$ programmers and $m$ testers. There are $n+m+1$ candidates, numbered from $1$ to $n+m+1$ in chronological order of their arriving time. The $i$-th candidate has programming skill $a_i$ and testing skill $b_i$ (a person's programming skill is different from their testing skill). The skill of the team is the sum of the programming skills of all candidates hired as programmers, and the sum of the testing skills of all candidates hired as testers. When a candidate arrives to interview, Monocarp tries to assign them to the most suitable position for them (if their programming skill is higher, then he hires them as a programmer, otherwise as a tester). If all slots for that position are filled, Monocarp assigns them to the other position. Your task is, for each candidate, calculate the skill of the team if everyone except them comes to interview. Note that it means that exactly $n+m$ candidates will arrive, so all $n+m$ positions in the company will be filled. Input: The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Each test case consists of three lines: * the first line contains two integers $n$ and $m$ ($0 \le n, m \le 2 \cdot 10^5$; $2 \le n + m + 1 \le 2 \cdot 10^5$) β€” the number of programmers and the number of testers Monocarp wants to hire, respectively; * the second line contains $n + m + 1$ integers $a_1, a_2, \dots, a_{n+m+1}$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th candidate; * the third line contains $n + m + 1$ integers $b_1, b_2, \dots, b_{n+m+1}$ ($1 \le b_i \le 10^9$; $b_i \ne a_i$), where $b_i$ is the testing skill of the $i$-th candidate. Additional constraint on the input: the sum of $(n + m + 1)$ over all test cases doesn't exceed $2 \cdot 10^5$. Output: For each test case, print $n + m + 1$ integers, where the $i$-th integer should be equal to the skill of the team if everyone except the $i$-th candidate comes to interview. Examples: input: 4 1 0 2 1 1 2 0 2 4 5 5 5 4 1 1 2 2 1 5 4 5 2 3 1 3 1 4 3 3 4 1 5 5 4 5 2 output: 1 2 5 6 9 8 11 11 12 13 13 13 12 15 Note: Let's consider the third test case of the example: * if the $1$-st candidate does not arrive, the $2$-nd candidate gets hired as a tester, the $3$-rd candidate gets hired as a programmer, the $4$-th candidate gets hired as a tester. The total skill of the team will be $2 + 5 + 1 = 8$; * if the $2$-nd candidate does not arrive, the $1$-st candidate gets hired as a tester, the $3$-rd candidate gets hired as a programmer, the $4$-th candidate gets hired as a tester. The total skill of the team will be $5 + 5 + 1 = 11$; * if the $3$-rd candidate does not arrive, the $1$-st candidate gets hired as a tester, the $2$-nd candidate gets hired as a tester, the $4$-th candidate gets hired as a programmer. The total skill of the team will be $5 + 2 + 4 = 11$; * if the $4$-th candidate does not arrive, the $1$-st candidate gets hired as a tester, the $2$-nd candidate gets hired as a tester, the $3$-rd candidate gets hired as a programmer. The total skill of the team will be $5 + 2 + 5 = 12$.
2033A
https://codeforces.com/problemset/problem/2033/A
Sakurako and Kosuke
800
[ "constructive algorithms", "implementation", "math" ]
Div. 3
1,000
256
Sakurako and Kosuke decided to play some games with a dot on a coordinate line. The dot is currently located in position $x=0$. They will be taking turns, and Sakurako will be the one to start. On the $i$-th move, the current player will move the dot in some direction by $2\cdot i-1$ units. Sakurako will always be moving the dot in the negative direction, whereas Kosuke will always move it in the positive direction. In other words, the following will happen: 1. Sakurako will change the position of the dot by $-1$, $x = -1$ now 2. Kosuke will change the position of the dot by $3$, $x = 2$ now 3. Sakurako will change the position of the dot by $-5$, $x = -3$ now 4. $\cdots$ They will keep on playing while the absolute value of the coordinate of the dot does not exceed $n$. More formally, the game continues while $-n\le x\le n$. It can be proven that the game will always end. Your task is to determine who will be the one who makes the last turn.
The first line contains one integer $t$ ($1\le t\le 100$) β€” the number of games that Sakurako and Kosuke played. Each game is described by one number $n$ ($1 \le n\le 100$) β€” the number that defines the condition when the game ends.
For each of the $t$ games, output a line with the result of that game. If Sakurako makes the last turn, output "Sakurako" (without quotes); else output "Kosuke".
[ [ "4\n1\n6\n3\n98", "Kosuke\nSakurako\nKosuke\nSakurako" ] ]
Title: Sakurako and Kosuke time_limit_ms: 1000 memory_limit_mb: 256 Description: Sakurako and Kosuke decided to play some games with a dot on a coordinate line. The dot is currently located in position $x=0$. They will be taking turns, and Sakurako will be the one to start. On the $i$-th move, the current player will move the dot in some direction by $2\cdot i-1$ units. Sakurako will always be moving the dot in the negative direction, whereas Kosuke will always move it in the positive direction. In other words, the following will happen: 1. Sakurako will change the position of the dot by $-1$, $x = -1$ now 2. Kosuke will change the position of the dot by $3$, $x = 2$ now 3. Sakurako will change the position of the dot by $-5$, $x = -3$ now 4. $\cdots$ They will keep on playing while the absolute value of the coordinate of the dot does not exceed $n$. More formally, the game continues while $-n\le x\le n$. It can be proven that the game will always end. Your task is to determine who will be the one who makes the last turn. Input: The first line contains one integer $t$ ($1\le t\le 100$) β€” the number of games that Sakurako and Kosuke played. Each game is described by one number $n$ ($1 \le n\le 100$) β€” the number that defines the condition when the game ends. Output: For each of the $t$ games, output a line with the result of that game. If Sakurako makes the last turn, output "Sakurako" (without quotes); else output "Kosuke". Examples: input: 4 1 6 3 98 output: Kosuke Sakurako Kosuke Sakurako Note:
1998E2
https://codeforces.com/problemset/problem/1998/E2
Eliminating Balls With Merging (Hard Version)
2,500
[ "binary search", "brute force", "data structures", "divide and conquer", "greedy", "implementation" ]
Div. 2
4,000
512
Drink water. β€” Sun Tzu, The Art of Becoming a Healthy Programmer This is the hard version of the problem. The only difference is that $x=1$ in this version. You must solve both versions to be able to hack. You are given two integers $n$ and $x$ ($x=1$). There are $n$ balls lined up in a row, numbered from $1$ to $n$ from left to right. Initially, there is a value $a_i$ written on the $i$-th ball. For each integer $i$ from $1$ to $n$, we define a function $f(i)$ as follows: * Suppose you have a set $S = \\{1, 2, \ldots, i\\}$. * In each operation, you have to select an integer $l$ ($1 \leq l < i$) from $S$ such that $l$ is not the largest element of $S$. Suppose $r$ is the smallest element in $S$ which is greater than $l$. * If $a_l > a_r$, you set $a_l = a_l + a_r$ and remove $r$ from $S$. * If $a_l < a_r$, you set $a_r = a_l + a_r$ and remove $l$ from $S$. * If $a_l = a_r$, you choose either the integer $l$ or $r$ to remove from $S$: * If you choose to remove $l$ from $S$, you set $a_r = a_l + a_r$ and remove $l$ from $S$. * If you choose to remove $r$ from $S$, you set $a_l = a_l + a_r$ and remove $r$ from $S$. * $f(i)$ denotes the number of integers $j$ ($1 \le j \le i$) such that it is possible to obtain $S = \\{j\\}$ after performing the above operations exactly $i - 1$ times. For each integer $i$ from $x$ to $n$, you need to find $f(i)$.
The first line contains $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $x$ ($1 \leq n \leq 2 \cdot 10^5; x = 1$) β€” the number of balls and the smallest index $i$ for which you need to find $f(i)$. The second line of each test case contains $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) β€” the initial number written on each ball. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output $n-x+1$ space separated integers on a new line, where the $j$-th integer should represent $f(x+j-1)$.
[ [ "3\n5 1\n1 2 3 2 1\n7 1\n4 5 1 2 1 4 5\n11 1\n1 2 3 1 1 9 3 2 4 1 3", "1 1 2 2 3\n1 1 1 1 1 3 4\n1 1 2 2 2 1 1 1 3 3 4" ] ]
In the first test case, below are the possible values of $j$ for each $f(i)$ from $1$ to $n$. * For $f(1)$, the only possible value of $j$ is $1$. * For $f(2)$, the only possible value of $j$ is $2$. * For $f(3)$, the possible values of $j$ are $2$ and $3$. * For $f(4)$, the possible values of $j$ are $2$ and $3$. * For $f(5)$, the possible values of $j$ are $2$, $3$, and $4$.
Title: Eliminating Balls With Merging (Hard Version) time_limit_ms: 4000 memory_limit_mb: 512 Description: Drink water. β€” Sun Tzu, The Art of Becoming a Healthy Programmer This is the hard version of the problem. The only difference is that $x=1$ in this version. You must solve both versions to be able to hack. You are given two integers $n$ and $x$ ($x=1$). There are $n$ balls lined up in a row, numbered from $1$ to $n$ from left to right. Initially, there is a value $a_i$ written on the $i$-th ball. For each integer $i$ from $1$ to $n$, we define a function $f(i)$ as follows: * Suppose you have a set $S = \\{1, 2, \ldots, i\\}$. * In each operation, you have to select an integer $l$ ($1 \leq l < i$) from $S$ such that $l$ is not the largest element of $S$. Suppose $r$ is the smallest element in $S$ which is greater than $l$. * If $a_l > a_r$, you set $a_l = a_l + a_r$ and remove $r$ from $S$. * If $a_l < a_r$, you set $a_r = a_l + a_r$ and remove $l$ from $S$. * If $a_l = a_r$, you choose either the integer $l$ or $r$ to remove from $S$: * If you choose to remove $l$ from $S$, you set $a_r = a_l + a_r$ and remove $l$ from $S$. * If you choose to remove $r$ from $S$, you set $a_l = a_l + a_r$ and remove $r$ from $S$. * $f(i)$ denotes the number of integers $j$ ($1 \le j \le i$) such that it is possible to obtain $S = \\{j\\}$ after performing the above operations exactly $i - 1$ times. For each integer $i$ from $x$ to $n$, you need to find $f(i)$. 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 $n$ and $x$ ($1 \leq n \leq 2 \cdot 10^5; x = 1$) β€” the number of balls and the smallest index $i$ for which you need to find $f(i)$. The second line of each test case contains $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) β€” the initial number written on each ball. 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 $n-x+1$ space separated integers on a new line, where the $j$-th integer should represent $f(x+j-1)$. Examples: input: 3 5 1 1 2 3 2 1 7 1 4 5 1 2 1 4 5 11 1 1 2 3 1 1 9 3 2 4 1 3 output: 1 1 2 2 3 1 1 1 1 1 3 4 1 1 2 2 2 1 1 1 3 3 4 Note: In the first test case, below are the possible values of $j$ for each $f(i)$ from $1$ to $n$. * For $f(1)$, the only possible value of $j$ is $1$. * For $f(2)$, the only possible value of $j$ is $2$. * For $f(3)$, the possible values of $j$ are $2$ and $3$. * For $f(4)$, the possible values of $j$ are $2$ and $3$. * For $f(5)$, the possible values of $j$ are $2$, $3$, and $4$.
2036E
https://codeforces.com/problemset/problem/2036/E
Reverse the Rivers
1,600
[ "binary search", "constructive algorithms", "data structures", "greedy" ]
Div. 3
2,000
256
A conspiracy of ancient sages, who decided to redirect rivers for their own convenience, has put the world on the brink. But before implementing their grand plan, they decided to carefully think through their strategy β€” that's what sages do. There are $n$ countries, each with exactly $k$ regions. For the $j$-th region of the $i$-th country, they calculated the value $a_{i,j}$, which reflects the amount of water in it. The sages intend to create channels between the $j$-th region of the $i$-th country and the $j$-th region of the $(i + 1)$-th country for all $1 \leq i \leq (n - 1)$ and for all $1 \leq j \leq k$. Since all $n$ countries are on a large slope, water flows towards the country with the highest number. According to the sages' predictions, after the channel system is created, the new value of the $j$-th region of the $i$-th country will be $b_{i,j} = a_{1,j} | a_{2,j} | ... | a_{i,j}$, where $|$ denotes the [bitwise "OR"](http://tiny.cc/bitwise_or) operation. After the redistribution of water, the sages aim to choose the most suitable country for living, so they will send you $q$ queries for consideration. Each query will contain $m$ requirements. Each requirement contains three parameters: the region number $r$, the sign $o$ (either "$<$" or "$>$"), and the value $c$. If $o$ = "$<$", then in the $r$-th region of the country you choose, the new value must be strictly less than the limit $c$, and if $o$ = "$>$", it must be strictly greater. In other words, the chosen country $i$ must satisfy all $m$ requirements. If in the current requirement $o$ = "$<$", then it must hold that $b_{i,r} < c$, and if $o$ = "$>$", then $b_{i,r} > c$. In response to each query, you should output a single integer β€” the number of the suitable country. If there are multiple such countries, output the smallest one. If no such country exists, output $-1$.
The first line contains three integers $n$, $k$, and $q$ ($1 \leq n, k, q \leq 10^5$) β€” the number of countries, regions, and queries, respectively. Next, there are $n$ lines, where the $i$-th line contains $k$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,k}$ ($1 \leq a_{i,j} \leq 10^9$), where $a_{i,j}$ is the value of the $j$-th region of the $i$-th country. Then, $q$ queries are described. The first line of each query contains a single integer $m$ ($1 \leq m \leq 10^5$) β€” the number of requirements. Then follow $m$ lines, each containing an integer $r$, a character $o$, and an integer $c$ ($1 \leq r \leq k$, $0 \leq c \leq 2 \cdot 10^9$), where $r$ and $c$ are the region number and the value, and $o$ is either "$<$" or "$>$" β€” the sign. It is guaranteed that $n \cdot k$ does not exceed $10^5$ and that the sum of $m$ across all queries also does not exceed $10^5$.
For each query, output a single integer on a new line β€” the smallest number of the suitable country, or $-1$ if no such country exists.
[ [ "3 4 4\n1 3 5 9\n4 6 5 3\n2 1 2 7\n3\n1 > 4\n2 < 8\n1 < 6\n2\n1 < 8\n2 > 8\n1\n3 > 5\n2\n4 > 8\n1 < 8", "2\n-1\n3\n1" ] ]
In the example, the initial values of the regions are as follows: $1$| $3$| $5$| $9$ ---|---|---|--- $4$| $6$| $5$| $3$ $2$| $1$| $2$| $7$ After creating the channels, the new values will look like this: $1$| $3$| $5$| $9$ ---|---|---|--- $1 | 4$| $3 | 6$| $5 | 5$| $9 | 3$ $1 | 4 | 2$| $3 | 6 | 1$| $5 | 5 | 2$| $9 | 3 | 7$ $\downarrow$ $1$| $3$| $5$| $9$ ---|---|---|--- $5$| $7$| $5$| $11$ $7$| $7$| $7$| $15$ In the first query, it is necessary to output the minimum country number (i.e., row) where, after the redistribution of water in the first region (i.e., column), the new value will be greater than four and less than six, and in the second region it will be less than eight. Only the country with number $2$ meets these requirements. In the second query, there are no countries that meet the specified requirements. In the third query, only the country with number $3$ is suitable. In the fourth query, all three countries meet the conditions, so the answer is the smallest number $1$.
Title: Reverse the Rivers time_limit_ms: 2000 memory_limit_mb: 256 Description: A conspiracy of ancient sages, who decided to redirect rivers for their own convenience, has put the world on the brink. But before implementing their grand plan, they decided to carefully think through their strategy β€” that's what sages do. There are $n$ countries, each with exactly $k$ regions. For the $j$-th region of the $i$-th country, they calculated the value $a_{i,j}$, which reflects the amount of water in it. The sages intend to create channels between the $j$-th region of the $i$-th country and the $j$-th region of the $(i + 1)$-th country for all $1 \leq i \leq (n - 1)$ and for all $1 \leq j \leq k$. Since all $n$ countries are on a large slope, water flows towards the country with the highest number. According to the sages' predictions, after the channel system is created, the new value of the $j$-th region of the $i$-th country will be $b_{i,j} = a_{1,j} | a_{2,j} | ... | a_{i,j}$, where $|$ denotes the [bitwise "OR"](http://tiny.cc/bitwise_or) operation. After the redistribution of water, the sages aim to choose the most suitable country for living, so they will send you $q$ queries for consideration. Each query will contain $m$ requirements. Each requirement contains three parameters: the region number $r$, the sign $o$ (either "$<$" or "$>$"), and the value $c$. If $o$ = "$<$", then in the $r$-th region of the country you choose, the new value must be strictly less than the limit $c$, and if $o$ = "$>$", it must be strictly greater. In other words, the chosen country $i$ must satisfy all $m$ requirements. If in the current requirement $o$ = "$<$", then it must hold that $b_{i,r} < c$, and if $o$ = "$>$", then $b_{i,r} > c$. In response to each query, you should output a single integer β€” the number of the suitable country. If there are multiple such countries, output the smallest one. If no such country exists, output $-1$. Input: The first line contains three integers $n$, $k$, and $q$ ($1 \leq n, k, q \leq 10^5$) β€” the number of countries, regions, and queries, respectively. Next, there are $n$ lines, where the $i$-th line contains $k$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,k}$ ($1 \leq a_{i,j} \leq 10^9$), where $a_{i,j}$ is the value of the $j$-th region of the $i$-th country. Then, $q$ queries are described. The first line of each query contains a single integer $m$ ($1 \leq m \leq 10^5$) β€” the number of requirements. Then follow $m$ lines, each containing an integer $r$, a character $o$, and an integer $c$ ($1 \leq r \leq k$, $0 \leq c \leq 2 \cdot 10^9$), where $r$ and $c$ are the region number and the value, and $o$ is either "$<$" or "$>$" β€” the sign. It is guaranteed that $n \cdot k$ does not exceed $10^5$ and that the sum of $m$ across all queries also does not exceed $10^5$. Output: For each query, output a single integer on a new line β€” the smallest number of the suitable country, or $-1$ if no such country exists. Examples: input: 3 4 4 1 3 5 9 4 6 5 3 2 1 2 7 3 1 > 4 2 < 8 1 < 6 2 1 < 8 2 > 8 1 3 > 5 2 4 > 8 1 < 8 output: 2 -1 3 1 Note: In the example, the initial values of the regions are as follows: $1$| $3$| $5$| $9$ ---|---|---|--- $4$| $6$| $5$| $3$ $2$| $1$| $2$| $7$ After creating the channels, the new values will look like this: $1$| $3$| $5$| $9$ ---|---|---|--- $1 | 4$| $3 | 6$| $5 | 5$| $9 | 3$ $1 | 4 | 2$| $3 | 6 | 1$| $5 | 5 | 2$| $9 | 3 | 7$ $\downarrow$ $1$| $3$| $5$| $9$ ---|---|---|--- $5$| $7$| $5$| $11$ $7$| $7$| $7$| $15$ In the first query, it is necessary to output the minimum country number (i.e., row) where, after the redistribution of water in the first region (i.e., column), the new value will be greater than four and less than six, and in the second region it will be less than eight. Only the country with number $2$ meets these requirements. In the second query, there are no countries that meet the specified requirements. In the third query, only the country with number $3$ is suitable. In the fourth query, all three countries meet the conditions, so the answer is the smallest number $1$.
2000B
https://codeforces.com/problemset/problem/2000/B
Seating in a Bus
800
[ "two pointers" ]
Div. 3
2,000
256
In Berland, a bus consists of a row of $n$ seats numbered from $1$ to $n$. Passengers are advised to always board the bus following these rules: * If there are no occupied seats in the bus, a passenger can sit in any free seat; * Otherwise, a passenger should sit in any free seat that has at least one occupied neighboring seat. In other words, a passenger can sit in a seat with index $i$ ($1 \le i \le n$) only if at least one of the seats with indices $i-1$ or $i+1$ is occupied. Today, $n$ passengers boarded the bus. The array $a$ chronologically records the seat numbers they occupied. That is, $a_1$ contains the seat number where the first passenger sat, $a_2$ β€” the seat number where the second passenger sat, and so on. You know the contents of the array $a$. Determine whether all passengers followed the recommendations. For example, if $n = 5$, and $a$ = [$5, 4, 2, 1, 3$], then the recommendations were not followed, as the $3$-rd passenger sat in seat number $2$, while the neighboring seats with numbers $1$ and $3$ were free.
The first line of input contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The following describes the input test cases. The first line of each test case contains exactly one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of seats in the bus and the number of passengers who boarded the bus. The second line of each test case contains $n$ distinct integers $a_i$ ($1 \le a_i \le n$) β€” the seats that the passengers occupied in chronological order. It is guaranteed that the sum of $n$ values across all test cases does not exceed $2 \cdot 10^5$, and that no passenger sits in an already occupied seat.
For each test case, output on a separate line: * "YES", if all passengers followed the recommendations; * "NO" otherwise. You may output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
[ [ "4\n5\n5 4 2 1 3\n3\n2 3 1\n4\n2 3 1 4\n5\n1 2 3 5 4", "NO\nYES\nYES\nNO" ] ]
The first test case is explained in the problem statement.
Title: Seating in a Bus time_limit_ms: 2000 memory_limit_mb: 256 Description: In Berland, a bus consists of a row of $n$ seats numbered from $1$ to $n$. Passengers are advised to always board the bus following these rules: * If there are no occupied seats in the bus, a passenger can sit in any free seat; * Otherwise, a passenger should sit in any free seat that has at least one occupied neighboring seat. In other words, a passenger can sit in a seat with index $i$ ($1 \le i \le n$) only if at least one of the seats with indices $i-1$ or $i+1$ is occupied. Today, $n$ passengers boarded the bus. The array $a$ chronologically records the seat numbers they occupied. That is, $a_1$ contains the seat number where the first passenger sat, $a_2$ β€” the seat number where the second passenger sat, and so on. You know the contents of the array $a$. Determine whether all passengers followed the recommendations. For example, if $n = 5$, and $a$ = [$5, 4, 2, 1, 3$], then the recommendations were not followed, as the $3$-rd passenger sat in seat number $2$, while the neighboring seats with numbers $1$ and $3$ were free. Input: The first line of input contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The following describes the input test cases. The first line of each test case contains exactly one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of seats in the bus and the number of passengers who boarded the bus. The second line of each test case contains $n$ distinct integers $a_i$ ($1 \le a_i \le n$) β€” the seats that the passengers occupied in chronological order. It is guaranteed that the sum of $n$ values across all test cases does not exceed $2 \cdot 10^5$, and that no passenger sits in an already occupied seat. Output: For each test case, output on a separate line: * "YES", if all passengers followed the recommendations; * "NO" otherwise. You may output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer). Examples: input: 4 5 5 4 2 1 3 3 2 3 1 4 2 3 1 4 5 1 2 3 5 4 output: NO YES YES NO Note: The first test case is explained in the problem statement.
2002B
https://codeforces.com/problemset/problem/2002/B
Removals Game
1,000
[ "constructive algorithms", "games" ]
Div. 1 + 2
1,000
256
Alice got a permutation $a_1, a_2, \ldots, a_n$ of $[1,2,\ldots,n]$, and Bob got another permutation $b_1, b_2, \ldots, b_n$ of $[1,2,\ldots,n]$. They are going to play a game with these arrays. In each turn, the following events happen in order: * Alice chooses either the first or the last element of her array and removes it from the array; * Bob chooses either the first or the last element of his array and removes it from the array. The game continues for $n-1$ turns, after which both arrays will have exactly one remaining element: $x$ in the array $a$ and $y$ in the array $b$. If $x=y$, Bob wins; otherwise, Alice wins. Find which player will win if both players play optimally.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^4$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1\le n\le 3\cdot 10^5$). The next line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le n$, all $a_i$ are distinct) β€” the permutation of Alice. The next line contains $n$ integers $b_1,b_2,\ldots,b_n$ ($1\le b_i\le n$, all $b_i$ are distinct) β€” the permutation of Bob. It is guaranteed that the sum of all $n$ does not exceed $3\cdot 10^5$.
For each test case, print a single line with the name of the winner, assuming both players play optimally. If Alice wins, print $\texttt{Alice}$; otherwise, print $\texttt{Bob}$.
[ [ "2\n2\n1 2\n1 2\n3\n1 2 3\n2 3 1", "Bob\nAlice" ] ]
In the first test case, Bob can win the game by deleting the same element as Alice did. In the second test case, Alice can delete $3$ in the first turn, and then in the second turn, delete the element that is different from the one Bob deleted in the first turn to win the game.
Title: Removals Game time_limit_ms: 1000 memory_limit_mb: 256 Description: Alice got a permutation $a_1, a_2, \ldots, a_n$ of $[1,2,\ldots,n]$, and Bob got another permutation $b_1, b_2, \ldots, b_n$ of $[1,2,\ldots,n]$. They are going to play a game with these arrays. In each turn, the following events happen in order: * Alice chooses either the first or the last element of her array and removes it from the array; * Bob chooses either the first or the last element of his array and removes it from the array. The game continues for $n-1$ turns, after which both arrays will have exactly one remaining element: $x$ in the array $a$ and $y$ in the array $b$. If $x=y$, Bob wins; otherwise, Alice wins. Find which player will win if both players play optimally. Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^4$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1\le n\le 3\cdot 10^5$). The next line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le n$, all $a_i$ are distinct) β€” the permutation of Alice. The next line contains $n$ integers $b_1,b_2,\ldots,b_n$ ($1\le b_i\le n$, all $b_i$ are distinct) β€” the permutation of Bob. It is guaranteed that the sum of all $n$ does not exceed $3\cdot 10^5$. Output: For each test case, print a single line with the name of the winner, assuming both players play optimally. If Alice wins, print $\texttt{Alice}$; otherwise, print $\texttt{Bob}$. Examples: input: 2 2 1 2 1 2 3 1 2 3 2 3 1 output: Bob Alice Note: In the first test case, Bob can win the game by deleting the same element as Alice did. In the second test case, Alice can delete $3$ in the first turn, and then in the second turn, delete the element that is different from the one Bob deleted in the first turn to win the game.
2033G
https://codeforces.com/problemset/problem/2033/G
Sakurako and Chefir
2,200
[ "data structures", "dfs and similar", "dp", "greedy", "trees" ]
Div. 3
4,000
256
Given a tree with $n$ vertices rooted at vertex $1$. While walking through it with her cat Chefir, Sakurako got distracted, and Chefir ran away. To help Sakurako, Kosuke recorded his $q$ guesses. In the $i$-th guess, he assumes that Chefir got lost at vertex $v_i$ and had $k_i$ stamina. Also, for each guess, Kosuke assumes that Chefir could move along the edges an arbitrary number of times: * from vertex $a$ to vertex $b$, if $a$ is an ancestor$^{\text{βˆ—}}$ of $b$, the stamina will not change; * from vertex $a$ to vertex $b$, if $a$ is not an ancestor of $b$, then Chefir's stamina decreases by $1$. If Chefir's stamina is $0$, he cannot make a move of the second type. For each assumption, your task is to find the distance to the farthest vertex that Chefir could reach from vertex $v_i$, having $k_i$ stamina. $^{\text{βˆ—}}$Vertex $a$ is an ancestor of vertex $b$ if the shortest path from $b$ to the root passes through $a$.
The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Each test case is described as follows: * The first line contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of vertices in the tree. * The next $n-1$ lines contain the edges of the tree. It is guaranteed that the given edges form a tree. * The next line consists of a single integer $q$ $(1\le q\le 2 \cdot 10^5)$, which denotes the number of guesses made by Kosuke. * The next $q$ lines describe the guesses made by Kosuke, with two integers $v_i$, $k_i$ $(1\le v_i \le n, 0 \le k_i\le n)$. It is guaranteed that the sum of $n$ and the sum of $q$ across all test cases does not exceed $2\cdot 10^5$.
For each test case and for each guess, output the maximum distance to the farthest vertex that Chefir could reach from the starting point $v_i$ having $k_i$ stamina.
[ [ "3\n5\n1 2\n2 3\n3 4\n3 5\n3\n5 1\n3 1\n2 0\n9\n8 1\n1 7\n1 4\n7 3\n4 9\n3 2\n1 5\n3 6\n7\n6 0\n2 3\n6 2\n8 2\n2 4\n9 2\n6 3\n6\n2 1\n2 5\n2 4\n5 6\n4 3\n3\n3 1\n1 3\n6 5", "2 1 2 \n0 5 2 4 5 5 5 \n1 3 4" ] ]
In the first example: * In the first query, you can go from vertex $5$ to vertex $3$ (after which your stamina will decrease by $1$ and become $0$), and then you can go to vertex $4$; * In the second query, from vertex $3$ with $1$ stamina, you can only reach vertices $2$, $3$, $4$, and $5$; * In the third query, from vertex $2$ with $0$ stamina, you can only reach vertices $2$, $3$, $4$, and $5$;
Title: Sakurako and Chefir time_limit_ms: 4000 memory_limit_mb: 256 Description: Given a tree with $n$ vertices rooted at vertex $1$. While walking through it with her cat Chefir, Sakurako got distracted, and Chefir ran away. To help Sakurako, Kosuke recorded his $q$ guesses. In the $i$-th guess, he assumes that Chefir got lost at vertex $v_i$ and had $k_i$ stamina. Also, for each guess, Kosuke assumes that Chefir could move along the edges an arbitrary number of times: * from vertex $a$ to vertex $b$, if $a$ is an ancestor$^{\text{βˆ—}}$ of $b$, the stamina will not change; * from vertex $a$ to vertex $b$, if $a$ is not an ancestor of $b$, then Chefir's stamina decreases by $1$. If Chefir's stamina is $0$, he cannot make a move of the second type. For each assumption, your task is to find the distance to the farthest vertex that Chefir could reach from vertex $v_i$, having $k_i$ stamina. $^{\text{βˆ—}}$Vertex $a$ is an ancestor of vertex $b$ if the shortest path from $b$ to the root passes through $a$. Input: The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Each test case is described as follows: * The first line contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of vertices in the tree. * The next $n-1$ lines contain the edges of the tree. It is guaranteed that the given edges form a tree. * The next line consists of a single integer $q$ $(1\le q\le 2 \cdot 10^5)$, which denotes the number of guesses made by Kosuke. * The next $q$ lines describe the guesses made by Kosuke, with two integers $v_i$, $k_i$ $(1\le v_i \le n, 0 \le k_i\le n)$. It is guaranteed that the sum of $n$ and the sum of $q$ across all test cases does not exceed $2\cdot 10^5$. Output: For each test case and for each guess, output the maximum distance to the farthest vertex that Chefir could reach from the starting point $v_i$ having $k_i$ stamina. Examples: input: 3 5 1 2 2 3 3 4 3 5 3 5 1 3 1 2 0 9 8 1 1 7 1 4 7 3 4 9 3 2 1 5 3 6 7 6 0 2 3 6 2 8 2 2 4 9 2 6 3 6 2 1 2 5 2 4 5 6 4 3 3 3 1 1 3 6 5 output: 2 1 2 0 5 2 4 5 5 5 1 3 4 Note: In the first example: * In the first query, you can go from vertex $5$ to vertex $3$ (after which your stamina will decrease by $1$ and become $0$), and then you can go to vertex $4$; * In the second query, from vertex $3$ with $1$ stamina, you can only reach vertices $2$, $3$, $4$, and $5$; * In the third query, from vertex $2$ with $0$ stamina, you can only reach vertices $2$, $3$, $4$, and $5$;
2035D
https://codeforces.com/problemset/problem/2035/D
Yet Another Real Number Problem
1,800
[ "binary search", "data structures", "divide and conquer", "greedy", "implementation", "math" ]
Div. 1 + 2
2,000
256
Three r there are's in strawberry. You are given an array $b$ of length $m$. You can perform the following operation any number of times (possibly zero): * Choose two distinct indices $i$ and $j$ where $\bf{1\le i < j\le m}$ and $b_i$ is even, divide $b_i$ by $2$ and multiply $b_j$ by $2$. Your task is to maximize the sum of the array after performing any number of such operations. Since it could be large, output this sum modulo $10^9+7$. Since this problem is too easy, you are given an array $a$ of length $n$ and need to solve the problem for each prefix of $a$. In other words, denoting the maximum sum of $b$ after performing any number of such operations as $f(b)$, you need to output $f([a_1])$, $f([a_1,a_2])$, $\ldots$, $f([a_1,a_2,\ldots,a_n])$ modulo $10^9+7$ respectively.
The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the length of $a$. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the starting values of array $a$. It is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
For each test case, output $n$ integers representing the answer for each prefix of $a$ modulo $10^9+7$.
[ [ "3\n10\n1 2 3 4 5 6 7 8 9 10\n11\n1 6 9 4 7 4 4 10 3 2 3\n4\n527792568 502211460 850237282 374773208", "1 3 8 13 46 59 126 149 1174 1311 \n1 7 22 26 70 74 150 1303 1306 1308 1568 \n527792568 83665723 399119771 773892979" ] ]
For each prefix in the first example, a possible array after operations is: * $[1]$ and the sum is $1$; * $[1, 2]$ and the sum is $3$; * $[1, 1, 6]$ and the sum is $8$; * $[1, 1, 3, 8]$ and the sum is $13$; * $[1, 1, 3, 1, 40]$ and the sum is $46$; * $[1, 1, 3, 1, 5, 48]$ and the sum is $59$; * $[1, 1, 3, 1, 5, 3, 112]$ and the sum is $126$; * $[1, 1, 3, 1, 5, 3, 7, 128]$ and the sum is $149$; * $[1, 1, 3, 1, 5, 3, 7, 1, 1152]$ and the sum is $1174$; * $[1, 1, 3, 1, 5, 3, 7, 1, 9, 1280]$ and the sum is $1311$.
Title: Yet Another Real Number Problem time_limit_ms: 2000 memory_limit_mb: 256 Description: Three r there are's in strawberry. You are given an array $b$ of length $m$. You can perform the following operation any number of times (possibly zero): * Choose two distinct indices $i$ and $j$ where $\bf{1\le i < j\le m}$ and $b_i$ is even, divide $b_i$ by $2$ and multiply $b_j$ by $2$. Your task is to maximize the sum of the array after performing any number of such operations. Since it could be large, output this sum modulo $10^9+7$. Since this problem is too easy, you are given an array $a$ of length $n$ and need to solve the problem for each prefix of $a$. In other words, denoting the maximum sum of $b$ after performing any number of such operations as $f(b)$, you need to output $f([a_1])$, $f([a_1,a_2])$, $\ldots$, $f([a_1,a_2,\ldots,a_n])$ modulo $10^9+7$ respectively. 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$ ($1 \le n \le 2 \cdot 10^5$) β€” the length of $a$. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the starting values of array $a$. 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 $n$ integers representing the answer for each prefix of $a$ modulo $10^9+7$. Examples: input: 3 10 1 2 3 4 5 6 7 8 9 10 11 1 6 9 4 7 4 4 10 3 2 3 4 527792568 502211460 850237282 374773208 output: 1 3 8 13 46 59 126 149 1174 1311 1 7 22 26 70 74 150 1303 1306 1308 1568 527792568 83665723 399119771 773892979 Note: For each prefix in the first example, a possible array after operations is: * $[1]$ and the sum is $1$; * $[1, 2]$ and the sum is $3$; * $[1, 1, 6]$ and the sum is $8$; * $[1, 1, 3, 8]$ and the sum is $13$; * $[1, 1, 3, 1, 40]$ and the sum is $46$; * $[1, 1, 3, 1, 5, 48]$ and the sum is $59$; * $[1, 1, 3, 1, 5, 3, 112]$ and the sum is $126$; * $[1, 1, 3, 1, 5, 3, 7, 128]$ and the sum is $149$; * $[1, 1, 3, 1, 5, 3, 7, 1, 1152]$ and the sum is $1174$; * $[1, 1, 3, 1, 5, 3, 7, 1, 9, 1280]$ and the sum is $1311$.
2026E
https://codeforces.com/problemset/problem/2026/E
Best Subsequence
2,500
[ "bitmasks", "dfs and similar", "flows", "graph matchings", "graphs" ]
Div. 2
2,000
256
Given an integer array $a$ of size $n$. Let's define the value of the array as its size minus the number of set bits in the bitwise OR of all elements of the array. For example, for the array $[1, 0, 1, 2]$, the bitwise OR is $3$ (which contains $2$ set bits), and the value of the array is $4-2=2$. Your task is to calculate the maximum possible value of some subsequence of the given array.
The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$). The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < 2^{60}$).
For each test case, print the maximum possible value of some subsequence of the given array.
[ [ "4\n3\n0 0 0\n4\n1 0 1 2\n1\n5\n8\n7 1 48 14 13 8 7 6", "3\n2\n0\n3" ] ]
Title: Best Subsequence time_limit_ms: 2000 memory_limit_mb: 256 Description: Given an integer array $a$ of size $n$. Let's define the value of the array as its size minus the number of set bits in the bitwise OR of all elements of the array. For example, for the array $[1, 0, 1, 2]$, the bitwise OR is $3$ (which contains $2$ set bits), and the value of the array is $4-2=2$. Your task is to calculate the maximum possible value of some subsequence of the given array. Input: The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$). The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < 2^{60}$). Output: For each test case, print the maximum possible value of some subsequence of the given array. Examples: input: 4 3 0 0 0 4 1 0 1 2 1 5 8 7 1 48 14 13 8 7 6 output: 3 2 0 3 Note:
2002E
https://codeforces.com/problemset/problem/2002/E
Cosmic Rays
2,300
[ "brute force", "data structures", "dp" ]
Div. 1 + 2
2,000
512
Given an array of integers $s_1, s_2, \ldots, s_l$, every second, cosmic rays will cause all $s_i$ such that $i=1$ or $s_i\neq s_{i-1}$ to be deleted simultaneously, and the remaining parts will be concatenated together in order to form the new array $s_1, s_2, \ldots, s_{l'}$. Define the strength of an array as the number of seconds it takes to become empty. You are given an array of integers compressed in the form of $n$ pairs that describe the array left to right. Each pair $(a_i,b_i)$ represents $a_i$ copies of $b_i$, i.e. $\underbrace{b_i,b_i,\cdots,b_i}_{a_i\textrm{ times}}$. For each $i=1,2,\dots,n$, please find the strength of the sequence described by the first $i$ pairs.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^4$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1\le n\le3\cdot10^5$) β€” the length of sequence $a$. The next $n$ lines contain two integers each $a_i$, $b_i$ ($1\le a_i\le10^9,0\le b_i\le n$) β€” the pairs which describe the sequence. It is guaranteed that the sum of all $n$ does not exceed $3\cdot10^5$. It is guaranteed that for all $1\le i<n$, $b_i\neq b_{i+1}$ holds.
For each test case, print one line containing $n$ integers β€” the answer for each prefix of pairs.
[ [ "4\n4\n2 0\n1 1\n3 0\n5 1\n6\n4 6\n1 3\n4 6\n4 0\n7 6\n6 3\n7\n9 0\n7 1\n5 0\n7 1\n9 0\n1 1\n2 0\n10\n10 7\n4 9\n2 2\n7 9\n2 8\n8 5\n11 7\n15 5\n12 7\n4 0", "2 2 4 5 \n4 4 7 7 10 10 \n9 9 9 9 9 9 10 \n10 10 10 10 10 10 12 15 15 15" ] ]
In the first test case, for the prefix of length $4$, the changes will be $[0,0,1,0,0,0,1,1,1,1,1]\rightarrow[0,0,0,1,1,1,1]\rightarrow[0,0,1,1,1]\rightarrow[0,1,1]\rightarrow[1]\rightarrow[]$, so the array becomes empty after $5$ seconds. In the second test case, for the prefix of length $4$, the changes will be $[6,6,6,6,3,6,6,6,6,0,0,0,0]\rightarrow[6,6,6,6,6,6,0,0,0]\rightarrow[6,6,6,6,6,0,0]\rightarrow[6,6,6,6,0]\rightarrow[6,6,6]\rightarrow[6,6]\rightarrow[6]\rightarrow[]$, so the array becomes empty after $7$ seconds.
Title: Cosmic Rays time_limit_ms: 2000 memory_limit_mb: 512 Description: Given an array of integers $s_1, s_2, \ldots, s_l$, every second, cosmic rays will cause all $s_i$ such that $i=1$ or $s_i\neq s_{i-1}$ to be deleted simultaneously, and the remaining parts will be concatenated together in order to form the new array $s_1, s_2, \ldots, s_{l'}$. Define the strength of an array as the number of seconds it takes to become empty. You are given an array of integers compressed in the form of $n$ pairs that describe the array left to right. Each pair $(a_i,b_i)$ represents $a_i$ copies of $b_i$, i.e. $\underbrace{b_i,b_i,\cdots,b_i}_{a_i\textrm{ times}}$. For each $i=1,2,\dots,n$, please find the strength of the sequence described by the first $i$ pairs. Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^4$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1\le n\le3\cdot10^5$) β€” the length of sequence $a$. The next $n$ lines contain two integers each $a_i$, $b_i$ ($1\le a_i\le10^9,0\le b_i\le n$) β€” the pairs which describe the sequence. It is guaranteed that the sum of all $n$ does not exceed $3\cdot10^5$. It is guaranteed that for all $1\le i<n$, $b_i\neq b_{i+1}$ holds. Output: For each test case, print one line containing $n$ integers β€” the answer for each prefix of pairs. Examples: input: 4 4 2 0 1 1 3 0 5 1 6 4 6 1 3 4 6 4 0 7 6 6 3 7 9 0 7 1 5 0 7 1 9 0 1 1 2 0 10 10 7 4 9 2 2 7 9 2 8 8 5 11 7 15 5 12 7 4 0 output: 2 2 4 5 4 4 7 7 10 10 9 9 9 9 9 9 10 10 10 10 10 10 10 12 15 15 15 Note: In the first test case, for the prefix of length $4$, the changes will be $[0,0,1,0,0,0,1,1,1,1,1]\rightarrow[0,0,0,1,1,1,1]\rightarrow[0,0,1,1,1]\rightarrow[0,1,1]\rightarrow[1]\rightarrow[]$, so the array becomes empty after $5$ seconds. In the second test case, for the prefix of length $4$, the changes will be $[6,6,6,6,3,6,6,6,6,0,0,0,0]\rightarrow[6,6,6,6,6,6,0,0,0]\rightarrow[6,6,6,6,6,0,0]\rightarrow[6,6,6,6,0]\rightarrow[6,6,6]\rightarrow[6,6]\rightarrow[6]\rightarrow[]$, so the array becomes empty after $7$ seconds.
1986D
https://codeforces.com/problemset/problem/1986/D
Mathematical Problem
1,400
[ "brute force", "dp", "greedy", "implementation", "math", "two pointers" ]
Div. 3
2,000
256
You are given a string $s$ of length $n > 1$, consisting of digits from $0$ to $9$. You must insert exactly $n - 2$ symbols $+$ (addition) or $\times$ (multiplication) into this string to form a valid arithmetic expression. In this problem, the symbols cannot be placed before the first or after the last character of the string $s$, and two symbols cannot be written consecutively. Also, note that the order of the digits in the string cannot be changed. Let's consider $s = 987009$: * From this string, the following arithmetic expressions can be obtained: $9 \times 8 + 70 \times 0 + 9 = 81$, $98 \times 7 \times 0 + 0 \times 9 = 0$, $9 + 8 + 7 + 0 + 09 = 9 + 8 + 7 + 0 + 9 = 33$. Note that the number $09$ is considered valid and is simply transformed into $9$. * From this string, the following arithmetic expressions cannot be obtained: $+9 \times 8 \times 70 + 09$ (symbols should only be placed between digits), $98 \times 70 + 0 + 9$ (since there are $6$ digits, there must be exactly $4$ symbols). The result of the arithmetic expression is calculated according to the rules of mathematics β€” first all multiplication operations are performed, then addition. You need to find the minimum result that can be obtained by inserting exactly $n - 2$ addition or multiplication symbols into the given string $s$.
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. Then follows their description. The first line of each test case contains a single integer $n$ ($2 \leq n \leq 20$) β€” the length of the string $s$. The second line of each test case contains a string $s$ of length $n$, consisting of digits from $0$ to $9$.
For each test case, output the minimum result of the arithmetic expression that can be obtained by inserting exactly $n - 2$ addition or multiplication symbols into the given string.
[ [ "18\n2\n10\n2\n74\n2\n00\n2\n01\n3\n901\n3\n101\n5\n23311\n6\n987009\n7\n1111111\n20\n99999999999999999999\n20\n00000000000000000000\n4\n0212\n18\n057235283621345395\n4\n1112\n20\n19811678487321784121\n4\n1121\n4\n2221\n3\n011", "10\n74\n0\n1\n9\n1\n19\n0\n11\n261\n0\n0\n0\n12\n93\n12\n24\n0" ] ]
In the first four test cases, we cannot add symbols, so the answer will be the original number. In the fifth test case, the optimal answer looks as follows: $9 \times 01 = 9 \times 1 = 9$. In the sixth test case, the optimal answer looks as follows: $1 \times 01 = 1 \times 1 = 1$. In the seventh test case, the optimal answer looks as follows: $2 + 3 + 3 + 11 = 19$. In the eighth test case, one of the optimal answers looks as follows: $98 \times 7 \times 0 + 0 \times 9 = 0$.
Title: Mathematical Problem time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given a string $s$ of length $n > 1$, consisting of digits from $0$ to $9$. You must insert exactly $n - 2$ symbols $+$ (addition) or $\times$ (multiplication) into this string to form a valid arithmetic expression. In this problem, the symbols cannot be placed before the first or after the last character of the string $s$, and two symbols cannot be written consecutively. Also, note that the order of the digits in the string cannot be changed. Let's consider $s = 987009$: * From this string, the following arithmetic expressions can be obtained: $9 \times 8 + 70 \times 0 + 9 = 81$, $98 \times 7 \times 0 + 0 \times 9 = 0$, $9 + 8 + 7 + 0 + 09 = 9 + 8 + 7 + 0 + 9 = 33$. Note that the number $09$ is considered valid and is simply transformed into $9$. * From this string, the following arithmetic expressions cannot be obtained: $+9 \times 8 \times 70 + 09$ (symbols should only be placed between digits), $98 \times 70 + 0 + 9$ (since there are $6$ digits, there must be exactly $4$ symbols). The result of the arithmetic expression is calculated according to the rules of mathematics β€” first all multiplication operations are performed, then addition. You need to find the minimum result that can be obtained by inserting exactly $n - 2$ addition or multiplication symbols into the given string $s$. 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. Then follows their description. The first line of each test case contains a single integer $n$ ($2 \leq n \leq 20$) β€” the length of the string $s$. The second line of each test case contains a string $s$ of length $n$, consisting of digits from $0$ to $9$. Output: For each test case, output the minimum result of the arithmetic expression that can be obtained by inserting exactly $n - 2$ addition or multiplication symbols into the given string. Examples: input: 18 2 10 2 74 2 00 2 01 3 901 3 101 5 23311 6 987009 7 1111111 20 99999999999999999999 20 00000000000000000000 4 0212 18 057235283621345395 4 1112 20 19811678487321784121 4 1121 4 2221 3 011 output: 10 74 0 1 9 1 19 0 11 261 0 0 0 12 93 12 24 0 Note: In the first four test cases, we cannot add symbols, so the answer will be the original number. In the fifth test case, the optimal answer looks as follows: $9 \times 01 = 9 \times 1 = 9$. In the sixth test case, the optimal answer looks as follows: $1 \times 01 = 1 \times 1 = 1$. In the seventh test case, the optimal answer looks as follows: $2 + 3 + 3 + 11 = 19$. In the eighth test case, one of the optimal answers looks as follows: $98 \times 7 \times 0 + 0 \times 9 = 0$.
2008A
https://codeforces.com/problemset/problem/2008/A
Sakurako's Exam
800
[ "brute force", "constructive algorithms", "greedy", "math" ]
Div. 3
1,000
256
Today, Sakurako has a math exam. The teacher gave the array, consisting of $a$ ones and $b$ twos. In an array, Sakurako must place either a '+' or a '-' in front of each element so that the sum of all elements in the array equals $0$. Sakurako is not sure if it is possible to solve this problem, so determine whether there is a way to assign signs such that the sum of all elements in the array equals $0$.
The first line contains a single integer $t$ ($1\le t\le 100$) β€” the number of test cases. The only line of each test case contains two integers $a$ and $b$ ($0\le a,b<10$) β€” the number of '1's and the number of '2's in the array.
For each test case, output "Yes" if you can make the sum of the entire array equal to $0$, and "No" otherwise. You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.
[ [ "5\n0 1\n0 3\n2 0\n2 3\n3 1", "NO\nNO\nYES\nYES\nNO" ] ]
1. $a=0$, $b=1$: This means the array is $[2]$ β€” it is impossible to add the signs '+' or '-' to get $0$ as a result; 2. $a=0$, $b=3$: This means the array is $[2, 2, 2]$ β€” it is impossible to add the signs '+' or '-' to get $0$ as a result; 3. $a=2$, $b=0$: This means the array is $[1, 1]$ β€” it is possible to add the signs '+' or '-' to get $0$ as a result ($+1-1=0$); 4. $a=2$, $b=3$: This means the array is $[1, 1, 2, 2, 2]$ β€” it is possible to add the signs '+' or '-' to get $0$ as a result ($+1+1-2-2+2=0$);
Title: Sakurako's Exam time_limit_ms: 1000 memory_limit_mb: 256 Description: Today, Sakurako has a math exam. The teacher gave the array, consisting of $a$ ones and $b$ twos. In an array, Sakurako must place either a '+' or a '-' in front of each element so that the sum of all elements in the array equals $0$. Sakurako is not sure if it is possible to solve this problem, so determine whether there is a way to assign signs such that the sum of all elements in the array equals $0$. Input: The first line contains a single integer $t$ ($1\le t\le 100$) β€” the number of test cases. The only line of each test case contains two integers $a$ and $b$ ($0\le a,b<10$) β€” the number of '1's and the number of '2's in the array. Output: For each test case, output "Yes" if you can make the sum of the entire array equal to $0$, and "No" otherwise. You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer. Examples: input: 5 0 1 0 3 2 0 2 3 3 1 output: NO NO YES YES NO Note: 1. $a=0$, $b=1$: This means the array is $[2]$ β€” it is impossible to add the signs '+' or '-' to get $0$ as a result; 2. $a=0$, $b=3$: This means the array is $[2, 2, 2]$ β€” it is impossible to add the signs '+' or '-' to get $0$ as a result; 3. $a=2$, $b=0$: This means the array is $[1, 1]$ β€” it is possible to add the signs '+' or '-' to get $0$ as a result ($+1-1=0$); 4. $a=2$, $b=3$: This means the array is $[1, 1, 2, 2, 2]$ β€” it is possible to add the signs '+' or '-' to get $0$ as a result ($+1+1-2-2+2=0$);
2002D1
https://codeforces.com/problemset/problem/2002/D1
DFS Checker (Easy Version)
1,900
[ "brute force", "data structures", "dfs and similar", "graphs", "hashing", "trees" ]
Div. 1 + 2
2,000
512
This is the easy version of the problem. In this version, the given tree is a perfect binary tree and the constraints on $n$ and $q$ are lower. You can make hacks only if both versions of the problem are solved. You are given a perfect binary tree$^\dagger$ consisting of $n$ vertices. The vertices are numbered from $1$ to $n$, and the root is the vertex $1$. You are also given a permutation $p_1, p_2, \ldots, p_n$ of $[1,2,\ldots,n]$. You need to answer $q$ queries. For each query, you are given two integers $x$, $y$; you need to swap $p_x$ and $p_y$ and determine if $p_1, p_2, \ldots, p_n$ is a valid DFS order$^\ddagger$ of the given tree. Please note that the swaps are persistent through queries. $^\dagger$ A perfect binary tree is a tree with vertex $1$ as its root, with size $n=2^k-1$ for a positive integer $k$, and where the parent of each vertex $i$ ($1<i\le n$) is $\left\lfloor\frac{i}{2}\right\rfloor$. Thus, all leaves of this tree are at a distance $k - 1$ from the root. $^\ddagger$ A DFS order is found by calling the following $\texttt{dfs}$ function on the given tree. dfs_order = [] function dfs(v): append v to the back of dfs_order pick an arbitrary permutation s of children of v for child in s: dfs(child) dfs(1) Note that the DFS order is not unique.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^4$). The description of the test cases follows. The first line of each test case contains two integers $n$, $q$ ($3\le n\le 65\,535$, $2\le q\le 5 \cdot 10^4$) β€” the number of vertices in the tree and the number of queries. It is guaranteed that $n=2^k-1$ for a positive integer $k$. The next line contains $n-1$ integers $a_2,a_3,\ldots,a_n$ ($1\le a_i<i$) β€” the parent of each vertex in the given tree. It is guaranteed that $a_i=\left\lfloor\frac{i}{2}\right\rfloor$. The next line contains $n$ integers $p_1,p_2,\ldots,p_n$ ($1\le p_i\le n$, all $p_i$ are distinct) β€” the initial permutation $p$. The next $q$ lines each contain two integers $x$, $y$ ($1\le x,y\le n,x\neq y$) β€” the positions of the elements to swap in the permutation. It is guaranteed that the sum of all $n$ does not exceed $65\,535$, and the sum of all $q$ does not exceed $5 \cdot 10^4$.
For each test case, print $q$ lines corresponding to the $q$ queries. For each query, output $\texttt{YES}$ if there is a DFS order that exactly equals the current permutation, and output $\texttt{NO}$ otherwise. You can output $\texttt{Yes}$ and $\texttt{No}$ in any case (for example, strings $\texttt{yEs}$, $\texttt{yes}$, $\texttt{Yes}$ and $\texttt{YES}$ will be recognized as a positive response).
[ [ "2\n3 3\n1 1\n1 2 3\n2 3\n3 2\n1 3\n7 4\n1 1 2 2 3 3\n1 2 3 4 5 6 7\n3 5\n2 5\n3 7\n4 6", "YES\nYES\nNO\nYES\nNO\nNO\nYES" ] ]
In the first test case, the permutation $p_1, p_2, \ldots, p_n$ after each modification is $[1,3,2],[1,2,3],[3,2,1]$, respectively. The first two permutations are valid DFS orders; the third is not a DFS order. In the second test case, the permutation $p_1, p_2, \ldots, p_n$ after each modification is $[1,2,5,4,3,6,7],[1,3,5,4,2,6,7],[1,3,7,4,2,6,5],[1,3,7,6,2,4,5]$, respectively.
Title: DFS Checker (Easy Version) time_limit_ms: 2000 memory_limit_mb: 512 Description: This is the easy version of the problem. In this version, the given tree is a perfect binary tree and the constraints on $n$ and $q$ are lower. You can make hacks only if both versions of the problem are solved. You are given a perfect binary tree$^\dagger$ consisting of $n$ vertices. The vertices are numbered from $1$ to $n$, and the root is the vertex $1$. You are also given a permutation $p_1, p_2, \ldots, p_n$ of $[1,2,\ldots,n]$. You need to answer $q$ queries. For each query, you are given two integers $x$, $y$; you need to swap $p_x$ and $p_y$ and determine if $p_1, p_2, \ldots, p_n$ is a valid DFS order$^\ddagger$ of the given tree. Please note that the swaps are persistent through queries. $^\dagger$ A perfect binary tree is a tree with vertex $1$ as its root, with size $n=2^k-1$ for a positive integer $k$, and where the parent of each vertex $i$ ($1<i\le n$) is $\left\lfloor\frac{i}{2}\right\rfloor$. Thus, all leaves of this tree are at a distance $k - 1$ from the root. $^\ddagger$ A DFS order is found by calling the following $\texttt{dfs}$ function on the given tree. dfs_order = [] function dfs(v): append v to the back of dfs_order pick an arbitrary permutation s of children of v for child in s: dfs(child) dfs(1) Note that the DFS order is not unique. Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^4$). The description of the test cases follows. The first line of each test case contains two integers $n$, $q$ ($3\le n\le 65\,535$, $2\le q\le 5 \cdot 10^4$) β€” the number of vertices in the tree and the number of queries. It is guaranteed that $n=2^k-1$ for a positive integer $k$. The next line contains $n-1$ integers $a_2,a_3,\ldots,a_n$ ($1\le a_i<i$) β€” the parent of each vertex in the given tree. It is guaranteed that $a_i=\left\lfloor\frac{i}{2}\right\rfloor$. The next line contains $n$ integers $p_1,p_2,\ldots,p_n$ ($1\le p_i\le n$, all $p_i$ are distinct) β€” the initial permutation $p$. The next $q$ lines each contain two integers $x$, $y$ ($1\le x,y\le n,x\neq y$) β€” the positions of the elements to swap in the permutation. It is guaranteed that the sum of all $n$ does not exceed $65\,535$, and the sum of all $q$ does not exceed $5 \cdot 10^4$. Output: For each test case, print $q$ lines corresponding to the $q$ queries. For each query, output $\texttt{YES}$ if there is a DFS order that exactly equals the current permutation, and output $\texttt{NO}$ otherwise. You can output $\texttt{Yes}$ and $\texttt{No}$ in any case (for example, strings $\texttt{yEs}$, $\texttt{yes}$, $\texttt{Yes}$ and $\texttt{YES}$ will be recognized as a positive response). Examples: input: 2 3 3 1 1 1 2 3 2 3 3 2 1 3 7 4 1 1 2 2 3 3 1 2 3 4 5 6 7 3 5 2 5 3 7 4 6 output: YES YES NO YES NO NO YES Note: In the first test case, the permutation $p_1, p_2, \ldots, p_n$ after each modification is $[1,3,2],[1,2,3],[3,2,1]$, respectively. The first two permutations are valid DFS orders; the third is not a DFS order. In the second test case, the permutation $p_1, p_2, \ldots, p_n$ after each modification is $[1,2,5,4,3,6,7],[1,3,5,4,2,6,7],[1,3,7,4,2,6,5],[1,3,7,6,2,4,5]$, respectively.
2005D
https://codeforces.com/problemset/problem/2005/D
Alter the GCD
2,400
[ "binary search", "brute force", "data structures", "divide and conquer", "implementation", "number theory" ]
Div. 2
4,000
256
You are given two arrays $a_1, a_2, \ldots, a_n$ and $b_1, b_2, \ldots, b_n$. You must perform the following operation exactly once: * choose any indices $l$ and $r$ such that $1 \le l \le r \le n$; * swap $a_i$ and $b_i$ for all $i$ such that $l \leq i \leq r$. Find the maximum possible value of $\text{gcd}(a_1, a_2, \ldots, a_n) + \text{gcd}(b_1, b_2, \ldots, b_n)$ after performing the operation exactly once. Also find the number of distinct pairs $(l, r)$ which achieve the maximum value.
In the first line of the input, you are given a single integer $t$ ($1 \le t \le 10^5$), the number of test cases. Then the description of each test case follows. In the first line of each test case, you are given a single integer $n$ ($1 \le n \le 2 \cdot 10^5$), representing the number of integers in each array. In the next line, you are given $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the elements of the array $a$. In the last line, you are given $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 10^9$) β€” the elements of the array $b$. The sum of values of $n$ over all test cases does not exceed $5 \cdot 10^5$.
For each test case, output a line with two integers: the maximum value of $\text{gcd}(a_1, a_2, \ldots, a_n) + \text{gcd}(b_1, b_2, \ldots, b_n)$ after performing the operation exactly once, and the number of ways.
[ [ "5\n8\n11 4 16 17 3 24 25 8\n8 10 4 21 17 18 25 21\n4\n6 4 24 13\n15 3 1 14\n2\n13 14\n5 8\n8\n20 17 15 11 21 10 3 7\n9 9 4 20 14 9 13 1\n2\n18 13\n15 20", "2 36\n3 2\n2 3\n2 36\n6 1" ] ]
In the first, third, and fourth test cases, there's no way to achieve a higher GCD than $1$ in any of the arrays, so the answer is $1 + 1 = 2$. Any pair $(l, r)$ achieves the same result; for example, in the first test case there are $36$ such pairs. In the last test case, you must choose $l = 1$, $r = 2$ to maximize the answer. In this case, the GCD of the first array is $5$, and the GCD of the second array is $1$, so the answer is $5 + 1 = 6$, and the number of ways is $1$.
Title: Alter the GCD time_limit_ms: 4000 memory_limit_mb: 256 Description: You are given two arrays $a_1, a_2, \ldots, a_n$ and $b_1, b_2, \ldots, b_n$. You must perform the following operation exactly once: * choose any indices $l$ and $r$ such that $1 \le l \le r \le n$; * swap $a_i$ and $b_i$ for all $i$ such that $l \leq i \leq r$. Find the maximum possible value of $\text{gcd}(a_1, a_2, \ldots, a_n) + \text{gcd}(b_1, b_2, \ldots, b_n)$ after performing the operation exactly once. Also find the number of distinct pairs $(l, r)$ which achieve the maximum value. Input: In the first line of the input, you are given a single integer $t$ ($1 \le t \le 10^5$), the number of test cases. Then the description of each test case follows. In the first line of each test case, you are given a single integer $n$ ($1 \le n \le 2 \cdot 10^5$), representing the number of integers in each array. In the next line, you are given $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the elements of the array $a$. In the last line, you are given $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 10^9$) β€” the elements of the array $b$. The sum of values of $n$ over all test cases does not exceed $5 \cdot 10^5$. Output: For each test case, output a line with two integers: the maximum value of $\text{gcd}(a_1, a_2, \ldots, a_n) + \text{gcd}(b_1, b_2, \ldots, b_n)$ after performing the operation exactly once, and the number of ways. Examples: input: 5 8 11 4 16 17 3 24 25 8 8 10 4 21 17 18 25 21 4 6 4 24 13 15 3 1 14 2 13 14 5 8 8 20 17 15 11 21 10 3 7 9 9 4 20 14 9 13 1 2 18 13 15 20 output: 2 36 3 2 2 3 2 36 6 1 Note: In the first, third, and fourth test cases, there's no way to achieve a higher GCD than $1$ in any of the arrays, so the answer is $1 + 1 = 2$. Any pair $(l, r)$ achieves the same result; for example, in the first test case there are $36$ such pairs. In the last test case, you must choose $l = 1$, $r = 2$ to maximize the answer. In this case, the GCD of the first array is $5$, and the GCD of the second array is $1$, so the answer is $5 + 1 = 6$, and the number of ways is $1$.
1976A
https://codeforces.com/problemset/problem/1976/A
Verify Password
800
[ "implementation", "sortings", "strings" ]
Div. 2
2,000
256
Monocarp is working on his new site, and the current challenge is to make the users pick strong passwords. Monocarp decided that strong passwords should satisfy the following conditions: * password should consist only of lowercase Latin letters and digits; * there should be no digit that comes after a letter (so, after each letter, there is either another letter or the string ends); * all digits should be sorted in the non-decreasing order; * all letters should be sorted in the non-decreasing order. Note that it's allowed for the password to have only letters or only digits. Monocarp managed to implement the first condition, but he struggles with the remaining ones. Can you help him to verify the passwords?
The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of testcases. The first line of each testcase contains a single integer $n$ ($1 \le n \le 20$) β€” the length of the password. The second line contains a string, consisting of exactly $n$ characters. Each character is either a lowercase Latin letter or a digit.
For each testcase, print "YES" if the given password is strong and "NO" otherwise.
[ [ "5\n4\n12ac\n5\n123wa\n9\nallllmost\n5\nac123\n6\n011679", "YES\nNO\nYES\nNO\nYES" ] ]
In the second testcase, the letters are not sorted in the non-decreasing order. In the fourth testcase, there is a digit that comes after a letter β€” digit '1' after a letter 'c'.
Title: Verify Password time_limit_ms: 2000 memory_limit_mb: 256 Description: Monocarp is working on his new site, and the current challenge is to make the users pick strong passwords. Monocarp decided that strong passwords should satisfy the following conditions: * password should consist only of lowercase Latin letters and digits; * there should be no digit that comes after a letter (so, after each letter, there is either another letter or the string ends); * all digits should be sorted in the non-decreasing order; * all letters should be sorted in the non-decreasing order. Note that it's allowed for the password to have only letters or only digits. Monocarp managed to implement the first condition, but he struggles with the remaining ones. Can you help him to verify the passwords? Input: The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of testcases. The first line of each testcase contains a single integer $n$ ($1 \le n \le 20$) β€” the length of the password. The second line contains a string, consisting of exactly $n$ characters. Each character is either a lowercase Latin letter or a digit. Output: For each testcase, print "YES" if the given password is strong and "NO" otherwise. Examples: input: 5 4 12ac 5 123wa 9 allllmost 5 ac123 6 011679 output: YES NO YES NO YES Note: In the second testcase, the letters are not sorted in the non-decreasing order. In the fourth testcase, there is a digit that comes after a letter β€” digit '1' after a letter 'c'.
1986E
https://codeforces.com/problemset/problem/1986/E
Beautiful Array
1,700
[ "greedy", "math", "number theory", "sortings" ]
Div. 3
2,000
256
You are given an array of integers $a_1, a_2, \ldots, a_n$ and an integer $k$. You need to make it beautiful with the least amount of operations. Before applying operations, you can shuffle the array elements as you like. For one operation, you can do the following: * Choose an index $1 \leq i \leq n$, * Make $a_i = a_i + k$. The array $b_1, b_2, \ldots, b_n$ is beautiful if $b_i = b_{n - i + 1}$ for all $1 \leq i \leq n$. Find the minimum number of operations needed to make the array beautiful, or report that it is impossible.
Each test consists of several sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of sets of input data. Then follows their description. The first line of each set of input data contains two integers $n$ and $k$ ($1 \leq n \leq 10^5$, $1 \leq k \leq 10^9$) β€” the size of the array $a$ and the number $k$ from the problem statement. The second line of each set of input data 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 sets of input data does not exceed $2 \cdot 10^5$.
For each set of input data, output the minimum number of operations needed to make the array beautiful, or $-1$ if it is impossible.
[ [ "11\n1 1000000000\n1\n2 1\n624323799 708290323\n3 1\n3 2 1\n4 1\n7 1 5 3\n5 1\n11 2 15 7 10\n7 1\n1 8 2 16 8 16 31\n13 1\n2 1 1 3 3 11 12 22 45 777 777 1500 74\n10 2\n1 2 1 2 1 2 1 2 1 2\n11 2\n1 2 1 2 1 2 1 2 1 2 1\n13 3\n2 3 9 14 17 10 22 20 18 30 1 4 28\n5 1\n2 3 5 3 5", "0\n83966524\n1\n4\n6\n1\n48\n-1\n0\n14\n0" ] ]
In the first set of input data, the array is already beautiful. In the second set of input data, you can shuffle the array before the operations and perform the operation with index $i = 1$ for $83966524$ times. In the third set of input data, you can shuffle the array $a$ and make it equal to $[2, 3, 1]$. Then apply the operation with index $i = 3$ to get the array $[2, 3, 2]$, which is beautiful. In the eighth set of input data, there is no set of operations and no way to shuffle the elements to make the array beautiful. In the ninth set of input data, the array is already beautiful.
Title: Beautiful Array time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given an array of integers $a_1, a_2, \ldots, a_n$ and an integer $k$. You need to make it beautiful with the least amount of operations. Before applying operations, you can shuffle the array elements as you like. For one operation, you can do the following: * Choose an index $1 \leq i \leq n$, * Make $a_i = a_i + k$. The array $b_1, b_2, \ldots, b_n$ is beautiful if $b_i = b_{n - i + 1}$ for all $1 \leq i \leq n$. Find the minimum number of operations needed to make the array beautiful, or report that it is impossible. 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 $k$ ($1 \leq n \leq 10^5$, $1 \leq k \leq 10^9$) β€” the size of the array $a$ and the number $k$ from the problem statement. The second line of each set of input data 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 sets of input data does not exceed $2 \cdot 10^5$. Output: For each set of input data, output the minimum number of operations needed to make the array beautiful, or $-1$ if it is impossible. Examples: input: 11 1 1000000000 1 2 1 624323799 708290323 3 1 3 2 1 4 1 7 1 5 3 5 1 11 2 15 7 10 7 1 1 8 2 16 8 16 31 13 1 2 1 1 3 3 11 12 22 45 777 777 1500 74 10 2 1 2 1 2 1 2 1 2 1 2 11 2 1 2 1 2 1 2 1 2 1 2 1 13 3 2 3 9 14 17 10 22 20 18 30 1 4 28 5 1 2 3 5 3 5 output: 0 83966524 1 4 6 1 48 -1 0 14 0 Note: In the first set of input data, the array is already beautiful. In the second set of input data, you can shuffle the array before the operations and perform the operation with index $i = 1$ for $83966524$ times. In the third set of input data, you can shuffle the array $a$ and make it equal to $[2, 3, 1]$. Then apply the operation with index $i = 3$ to get the array $[2, 3, 2]$, which is beautiful. In the eighth set of input data, there is no set of operations and no way to shuffle the elements to make the array beautiful. In the ninth set of input data, the array is already beautiful.
1987G2
https://codeforces.com/problemset/problem/1987/G2
Spinning Round (Hard Version)
3,500
[ "divide and conquer", "dp", "trees" ]
Div. 1 + 2
7,000
1,024
This is the hard version of the problem. The only difference between the two versions are the allowed characters in $s$. You can make hacks only if both versions of the problem are solved. You are given a permutation $p$ of length $n$. You are also given a string $s$ of length $n$, where each character is either L, R, or ?. For each $i$ from $1$ to $n$: * Define $l_i$ as the largest index $j < i$ such that $p_j > p_i$. If there is no such index, $l_i := i$. * Define $r_i$ as the smallest index $j > i$ such that $p_j > p_i$. If there is no such index, $r_i := i$. Initially, you have an undirected graph with $n$ vertices (numbered from $1$ to $n$) and no edges. Then, for each $i$ from $1$ to $n$, add one edge to the graph: * If $s_i =$ L, add the edge $(i, l_i)$ to the graph. * If $s_i =$ R, add the edge $(i, r_i)$ to the graph. * If $s_i =$ ?, either add the edge $(i, l_i)$ or the edge $(i, r_i)$ to the graph at your choice. Find the maximum possible diameter over all connected$^{\text{βˆ—}}$ graphs that you can form. Output $-1$ if it is not possible to form any connected graphs. $^{\text{βˆ—}}$ Let $d(s, t)$ denote the smallest number of edges on any path between $s$ and $t$. The diameter of the graph is defined as the maximum value of $d(s, t)$ over all pairs of vertices $s$ and $t$.
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \le n \le 4 \cdot 10^5$) β€” the length of the permutation $p$. The second line of each test case contains $n$ integers $p_1,p_2,\ldots, p_n$ ($1 \le p_i \le n$) β€” the elements of $p$, which are guaranteed to form a permutation. The third line of each test case contains a string $s$ of length $n$. It is guaranteed that it consists only of the characters L, R, and ?. It is guaranteed that the sum of $n$ over all test cases does not exceed $4 \cdot 10^5$.
For each test case, output the maximum possible diameter over all connected graphs that you form, or $-1$ if it is not possible to form any connected graphs.
[ [ "8\n5\n2 1 4 3 5\nR?RL?\n2\n1 2\nLR\n3\n3 1 2\nL?R\n7\n5 3 1 6 4 2 7\n?R?R?R?\n5\n5 2 1 3 4\n?????\n6\n6 2 3 4 5 1\n?LLRLL\n8\n1 7 5 6 2 8 4 3\n?R??????\n12\n6 10 7 1 8 5 12 2 11 3 4 9\n????????????", "3\n-1\n-1\n4\n4\n3\n5\n8" ] ]
In the first test case, there are two connected graphs (the labels are indices): ![](CDN_BASE_URL/4561c9025a4357785c7aff0bb1bbff9a)| ![](CDN_BASE_URL/a680f9e3bffa38fd90dd7bbc4c3e5f3b) ---|--- The graph on the left has a diameter of $2$, while the graph on the right has a diameter of $3$, so the answer is $3$. In the second test case, there are no connected graphs, so the answer is $-1$.
Title: Spinning Round (Hard Version) time_limit_ms: 7000 memory_limit_mb: 1024 Description: This is the hard version of the problem. The only difference between the two versions are the allowed characters in $s$. You can make hacks only if both versions of the problem are solved. You are given a permutation $p$ of length $n$. You are also given a string $s$ of length $n$, where each character is either L, R, or ?. For each $i$ from $1$ to $n$: * Define $l_i$ as the largest index $j < i$ such that $p_j > p_i$. If there is no such index, $l_i := i$. * Define $r_i$ as the smallest index $j > i$ such that $p_j > p_i$. If there is no such index, $r_i := i$. Initially, you have an undirected graph with $n$ vertices (numbered from $1$ to $n$) and no edges. Then, for each $i$ from $1$ to $n$, add one edge to the graph: * If $s_i =$ L, add the edge $(i, l_i)$ to the graph. * If $s_i =$ R, add the edge $(i, r_i)$ to the graph. * If $s_i =$ ?, either add the edge $(i, l_i)$ or the edge $(i, r_i)$ to the graph at your choice. Find the maximum possible diameter over all connected$^{\text{βˆ—}}$ graphs that you can form. Output $-1$ if it is not possible to form any connected graphs. $^{\text{βˆ—}}$ Let $d(s, t)$ denote the smallest number of edges on any path between $s$ and $t$. The diameter of the graph is defined as the maximum value of $d(s, t)$ over all pairs of vertices $s$ and $t$. Input: Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \le n \le 4 \cdot 10^5$) β€” the length of the permutation $p$. The second line of each test case contains $n$ integers $p_1,p_2,\ldots, p_n$ ($1 \le p_i \le n$) β€” the elements of $p$, which are guaranteed to form a permutation. The third line of each test case contains a string $s$ of length $n$. It is guaranteed that it consists only of the characters L, R, and ?. 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 the maximum possible diameter over all connected graphs that you form, or $-1$ if it is not possible to form any connected graphs. Examples: input: 8 5 2 1 4 3 5 R?RL? 2 1 2 LR 3 3 1 2 L?R 7 5 3 1 6 4 2 7 ?R?R?R? 5 5 2 1 3 4 ????? 6 6 2 3 4 5 1 ?LLRLL 8 1 7 5 6 2 8 4 3 ?R?????? 12 6 10 7 1 8 5 12 2 11 3 4 9 ???????????? output: 3 -1 -1 4 4 3 5 8 Note: In the first test case, there are two connected graphs (the labels are indices): ![](CDN_BASE_URL/4561c9025a4357785c7aff0bb1bbff9a)| ![](CDN_BASE_URL/a680f9e3bffa38fd90dd7bbc4c3e5f3b) ---|--- The graph on the left has a diameter of $2$, while the graph on the right has a diameter of $3$, so the answer is $3$. In the second test case, there are no connected graphs, so the answer is $-1$.
2033E
https://codeforces.com/problemset/problem/2033/E
Sakurako, Kosuke, and the Permutation
1,400
[ "brute force", "data structures", "dfs and similar", "dsu", "graphs", "greedy", "math" ]
Div. 3
2,000
256
Sakurako's exams are over, and she did excellently. As a reward, she received a permutation $p$. Kosuke was not entirely satisfied because he failed one exam and did not receive a gift. He decided to sneak into her room (thanks to the code for her lock) and spoil the permutation so that it becomes simple. A permutation $p$ is considered simple if for every $i$ $(1\le i \le n)$ one of the following conditions holds: * $p_i=i$ * $p_{p_i}=i$ For example, the permutations $[1, 2, 3, 4]$, $[5, 2, 4, 3, 1]$, and $[2, 1]$ are simple, while $[2, 3, 1]$ and $[5, 2, 1, 4, 3]$ are not. In one operation, Kosuke can choose indices $i,j$ $(1\le i,j\le n)$ and swap the elements $p_i$ and $p_j$. Sakurako is about to return home. Your task is to calculate the minimum number of operations that Kosuke needs to perform to make the permutation simple.
The first line contains one integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Each test case is described by two lines. * The first line contains one integer $n$ ($1\le n \le 10^6$) β€” the length of the permutation $p$. * The second line contains $n$ integers $p_i$ ($1\le p_i\le n$) β€” the elements of the permutation $p$. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^6$. It is guaranteed that $p$ is a permutation.
For each test case, output the minimum number of operations that Kosuke needs to perform to make the permutation simple.
[ [ "6\n5\n1 2 3 4 5\n5\n5 4 3 2 1\n5\n2 3 4 5 1\n4\n2 3 4 1\n3\n1 3 2\n7\n2 3 1 5 6 7 4", "0\n0\n2\n1\n0\n2" ] ]
In the first and second examples, the permutations are already simple. In the fourth example, it is sufficient to swap $p_2$ and $p_4$. Thus, the permutation will become $[2, 1, 4, 3]$ in $1$ operation.
Title: Sakurako, Kosuke, and the Permutation time_limit_ms: 2000 memory_limit_mb: 256 Description: Sakurako's exams are over, and she did excellently. As a reward, she received a permutation $p$. Kosuke was not entirely satisfied because he failed one exam and did not receive a gift. He decided to sneak into her room (thanks to the code for her lock) and spoil the permutation so that it becomes simple. A permutation $p$ is considered simple if for every $i$ $(1\le i \le n)$ one of the following conditions holds: * $p_i=i$ * $p_{p_i}=i$ For example, the permutations $[1, 2, 3, 4]$, $[5, 2, 4, 3, 1]$, and $[2, 1]$ are simple, while $[2, 3, 1]$ and $[5, 2, 1, 4, 3]$ are not. In one operation, Kosuke can choose indices $i,j$ $(1\le i,j\le n)$ and swap the elements $p_i$ and $p_j$. Sakurako is about to return home. Your task is to calculate the minimum number of operations that Kosuke needs to perform to make the permutation simple. Input: The first line contains one integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. Each test case is described by two lines. * The first line contains one integer $n$ ($1\le n \le 10^6$) β€” the length of the permutation $p$. * The second line contains $n$ integers $p_i$ ($1\le p_i\le n$) β€” the elements of the permutation $p$. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^6$. It is guaranteed that $p$ is a permutation. Output: For each test case, output the minimum number of operations that Kosuke needs to perform to make the permutation simple. Examples: input: 6 5 1 2 3 4 5 5 5 4 3 2 1 5 2 3 4 5 1 4 2 3 4 1 3 1 3 2 7 2 3 1 5 6 7 4 output: 0 0 2 1 0 2 Note: In the first and second examples, the permutations are already simple. In the fourth example, it is sufficient to swap $p_2$ and $p_4$. Thus, the permutation will become $[2, 1, 4, 3]$ in $1$ operation.