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
|
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2001E1
|
https://codeforces.com/problemset/problem/2001/E1
|
Deterministic Heap (Easy Version)
| 2,400
|
[
"combinatorics",
"dp",
"math",
"trees"
] |
Div. 2
| 3,000
| 512
|
This is the easy version of the problem. The difference between the two versions is the definition of deterministic max-heap, time limit, and constraints on $n$ and $t$. You can make hacks only if both versions of the problem are solved.
Consider a perfect binary tree with size $2^n - 1$, with nodes numbered from $1$ to $2^n-1$ and rooted at $1$. For each vertex $v$ ($1 \le v \le 2^{n - 1} - 1$), vertex $2v$ is its left child and vertex $2v + 1$ is its right child. Each node $v$ also has a value $a_v$ assigned to it.
Define the operation $\mathrm{pop}$ as follows:
1. initialize variable $v$ as $1$; 2. repeat the following process until vertex $v$ is a leaf (i.e. until $2^{n - 1} \le v \le 2^n - 1$); 1. among the children of $v$, choose the one with the larger value on it and denote such vertex as $x$; if the values on them are equal (i.e. $a_{2v} = a_{2v + 1}$), you can choose any of them; 2. assign $a_x$ to $a_v$ (i.e. $a_v := a_x$); 3. assign $x$ to $v$ (i.e. $v := x$); 3. assign $-1$ to $a_v$ (i.e. $a_v := -1$).
Then we say the $\mathrm{pop}$ operation is deterministic if there is a unique way to do such operation. In other words, $a_{2v} \neq a_{2v + 1}$ would hold whenever choosing between them.
A binary tree is called a max-heap if for every vertex $v$ ($1 \le v \le 2^{n - 1} - 1$), both $a_v \ge a_{2v}$ and $a_v \ge a_{2v + 1}$ hold.
A max-heap is deterministic if the $\mathrm{pop}$ operation is deterministic to the heap when we do it for the first time.
Initially, $a_v := 0$ for every vertex $v$ ($1 \le v \le 2^n - 1$), and your goal is to count the number of different deterministic max- heaps produced by applying the following operation $\mathrm{add}$ exactly $k$ times:
* Choose an integer $v$ ($1 \le v \le 2^n - 1$) and, for every vertex $x$ on the path between $1$ and $v$, add $1$ to $a_x$.
Two heaps are considered different if there is a node which has different values in the heaps.
Since the answer might be large, print it modulo $p$.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.
The first line of each test case contains three integers $n, k, p$ ($1 \le n, k \le 500$, $10^8 \le p \le 10^9$, $p$ is a prime).
It is guaranteed that the sum of $n$ and the sum of $k$ over all test cases does not exceed $500$.
|
For each test case, output a single line containing an integer: the number of different deterministic max-heaps produced by applying the aforementioned operation $\mathrm{add}$ exactly $k$ times, modulo $p$.
|
[
[
"7\n1 13 998244353\n2 1 998244353\n3 2 998244853\n3 3 998244353\n3 4 100000037\n4 2 100000039\n4 3 100000037",
"1\n2\n12\n52\n124\n32\n304"
],
[
"1\n500 500 100000007",
"76297230"
],
[
"6\n87 63 100000037\n77 77 100000039\n100 200 998244353\n200 100 998244353\n32 59 998244853\n1 1 998244353",
"26831232\n94573603\n37147649\n847564946\n727060898\n1"
]
] |
For the first testcase, there is only one way to generate $a$, and such sequence is a deterministic max-heap, so the answer is $1$.
For the second testcase, if we choose $v = 1$ and do the operation, we would have $a = [1, 0, 0]$, and since $a_2 = a_3$, we can choose either of them when doing the first $\mathrm{pop}$ operation, so such heap is not a deterministic max-heap.
And if we choose $v = 2$, we would have $a = [1, 1, 0]$, during the first $\mathrm{pop}$, the following would happen:
* initialize $v$ as $1$ * since $a_{2v} > a_{2v + 1}$, choose $2v$ as $x$, then $x = 2$ * assign $a_x$ to $a_v$, then $a = [1, 1, 0]$ * assign $x$ to $v$, then $v = 2$ * since $v$ is a leaf, assign $-1$ to $a_v$, then $a = [1, -1, 0]$
Since the first $\mathrm{pop}$ operation is deterministic, this is a deterministic max-heap. Also, if we choose $v = 3$, $a$ would be a deterministic max-heap, so the answer is $2$.
|
Title: Deterministic Heap (Easy Version)
time_limit_ms: 3000
memory_limit_mb: 512
Description: This is the easy version of the problem. The difference between the two versions is the definition of deterministic max-heap, time limit, and constraints on $n$ and $t$. You can make hacks only if both versions of the problem are solved.
Consider a perfect binary tree with size $2^n - 1$, with nodes numbered from $1$ to $2^n-1$ and rooted at $1$. For each vertex $v$ ($1 \le v \le 2^{n - 1} - 1$), vertex $2v$ is its left child and vertex $2v + 1$ is its right child. Each node $v$ also has a value $a_v$ assigned to it.
Define the operation $\mathrm{pop}$ as follows:
1. initialize variable $v$ as $1$; 2. repeat the following process until vertex $v$ is a leaf (i.e. until $2^{n - 1} \le v \le 2^n - 1$); 1. among the children of $v$, choose the one with the larger value on it and denote such vertex as $x$; if the values on them are equal (i.e. $a_{2v} = a_{2v + 1}$), you can choose any of them; 2. assign $a_x$ to $a_v$ (i.e. $a_v := a_x$); 3. assign $x$ to $v$ (i.e. $v := x$); 3. assign $-1$ to $a_v$ (i.e. $a_v := -1$).
Then we say the $\mathrm{pop}$ operation is deterministic if there is a unique way to do such operation. In other words, $a_{2v} \neq a_{2v + 1}$ would hold whenever choosing between them.
A binary tree is called a max-heap if for every vertex $v$ ($1 \le v \le 2^{n - 1} - 1$), both $a_v \ge a_{2v}$ and $a_v \ge a_{2v + 1}$ hold.
A max-heap is deterministic if the $\mathrm{pop}$ operation is deterministic to the heap when we do it for the first time.
Initially, $a_v := 0$ for every vertex $v$ ($1 \le v \le 2^n - 1$), and your goal is to count the number of different deterministic max- heaps produced by applying the following operation $\mathrm{add}$ exactly $k$ times:
* Choose an integer $v$ ($1 \le v \le 2^n - 1$) and, for every vertex $x$ on the path between $1$ and $v$, add $1$ to $a_x$.
Two heaps are considered different if there is a node which has different values in the heaps.
Since the answer might be large, print it modulo $p$.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.
The first line of each test case contains three integers $n, k, p$ ($1 \le n, k \le 500$, $10^8 \le p \le 10^9$, $p$ is a prime).
It is guaranteed that the sum of $n$ and the sum of $k$ over all test cases does not exceed $500$.
Output: For each test case, output a single line containing an integer: the number of different deterministic max-heaps produced by applying the aforementioned operation $\mathrm{add}$ exactly $k$ times, modulo $p$.
Examples:
input:
7
1 13 998244353
2 1 998244353
3 2 998244853
3 3 998244353
3 4 100000037
4 2 100000039
4 3 100000037
output:
1
2
12
52
124
32
304
input:
1
500 500 100000007
output:
76297230
input:
6
87 63 100000037
77 77 100000039
100 200 998244353
200 100 998244353
32 59 998244853
1 1 998244353
output:
26831232
94573603
37147649
847564946
727060898
1
Note: For the first testcase, there is only one way to generate $a$, and such sequence is a deterministic max-heap, so the answer is $1$.
For the second testcase, if we choose $v = 1$ and do the operation, we would have $a = [1, 0, 0]$, and since $a_2 = a_3$, we can choose either of them when doing the first $\mathrm{pop}$ operation, so such heap is not a deterministic max-heap.
And if we choose $v = 2$, we would have $a = [1, 1, 0]$, during the first $\mathrm{pop}$, the following would happen:
* initialize $v$ as $1$ * since $a_{2v} > a_{2v + 1}$, choose $2v$ as $x$, then $x = 2$ * assign $a_x$ to $a_v$, then $a = [1, 1, 0]$ * assign $x$ to $v$, then $v = 2$ * since $v$ is a leaf, assign $-1$ to $a_v$, then $a = [1, -1, 0]$
Since the first $\mathrm{pop}$ operation is deterministic, this is a deterministic max-heap. Also, if we choose $v = 3$, $a$ would be a deterministic max-heap, so the answer is $2$.
|
1972B
|
https://codeforces.com/problemset/problem/1972/B
|
Coin Games
| 900
|
[
"games"
] |
Div. 2
| 1,000
| 256
|
There are $n$ coins on the table forming a circle, and each coin is either facing up or facing down. Alice and Bob take turns to play the following game, and Alice goes first.
In each operation, the player chooses a facing-up coin, removes the coin, and flips the two coins that are adjacent to it. If (before the operation) there are only two coins left, then one will be removed and the other won't be flipped (as it would be flipped twice). If (before the operation) there is only one coin left, no coins will be flipped. If (before the operation) there are no facing-up coins, the player loses.
Decide who will win the game if they both play optimally. It can be proved that the game will end in a finite number of operations, and one of them will win.
|
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 only one positive integer $n$ ($1 \leq n \leq 100$), representing the number of the coins.
A string $s$ of length $n$ follows on the second line of each test case, containing only "U" and "D", representing that each coin is facing up or facing down.
|
For each test case, print "YES" if Alice will win the game, and "NO" otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
|
[
[
"3\n5\nUUDUD\n5\nUDDUD\n2\nUU",
"YES\nNO\nNO"
]
] |
In the first test case, the game may go as follows.
* Alice chooses the first coin and $s$ becomes "DDUU". * Bob chooses the last coin and $s$ becomes "UDD". * Alice chooses the first coin and $s$ becomes "UU". * Bob chooses the first coin and $s$ becomes "U". * Alice chooses the only coin and $s$ becomes empty. * Bob can't choose any coin now, and he loses the game.
It can be proved that Bob will always lose if they both play optimally.
|
Title: Coin Games
time_limit_ms: 1000
memory_limit_mb: 256
Description: There are $n$ coins on the table forming a circle, and each coin is either facing up or facing down. Alice and Bob take turns to play the following game, and Alice goes first.
In each operation, the player chooses a facing-up coin, removes the coin, and flips the two coins that are adjacent to it. If (before the operation) there are only two coins left, then one will be removed and the other won't be flipped (as it would be flipped twice). If (before the operation) there is only one coin left, no coins will be flipped. If (before the operation) there are no facing-up coins, the player loses.
Decide who will win the game if they both play optimally. It can be proved that the game will end in a finite number of operations, and one of them will win.
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 only one positive integer $n$ ($1 \leq n \leq 100$), representing the number of the coins.
A string $s$ of length $n$ follows on the second line of each test case, containing only "U" and "D", representing that each coin is facing up or facing down.
Output: For each test case, print "YES" if Alice will win the game, and "NO" otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
Examples:
input:
3
5
UUDUD
5
UDDUD
2
UU
output:
YES
NO
NO
Note: In the first test case, the game may go as follows.
* Alice chooses the first coin and $s$ becomes "DDUU". * Bob chooses the last coin and $s$ becomes "UDD". * Alice chooses the first coin and $s$ becomes "UU". * Bob chooses the first coin and $s$ becomes "U". * Alice chooses the only coin and $s$ becomes empty. * Bob can't choose any coin now, and he loses the game.
It can be proved that Bob will always lose if they both play optimally.
|
1986G1
|
https://codeforces.com/problemset/problem/1986/G1
|
Permutation Problem (Simple Version)
| 2,200
|
[
"binary search",
"brute force",
"combinatorics",
"data structures",
"math",
"number theory"
] |
Div. 3
| 3,000
| 128
|
This is a simple version of the problem. The only difference is that in this version $n \leq 10^5$ and the sum of $n$ for all sets of input data does not exceed $10^5$.
You are given a permutation $p$ of length $n$. Calculate the number of index pairs $1 \leq i < j \leq n$ such that $p_i \cdot p_j$ is divisible by $i \cdot j$ without remainder.
A permutation is a sequence of $n$ integers, where each integer from $1$ to $n$ occurs exactly once. For example, $[1]$, $[3,5,2,1,4]$, $[1,3,2]$ are permutations, while $[2,3,2]$, $[4,3,1]$, $[0]$ are not.
|
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. Then follows their description.
The first line of each set of input data contains a single integer $n$ ($1 \leq n \leq 10^5$) β the length of the permutation $p$.
The second line of each set of input data contains $n$ distinct integers $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq n$) β the permutation $p$.
It is guaranteed that the sum of $n$ for all sets of input data does not exceed $10^5$.
|
For each set of input data, output the number of index pairs $1 \leq i < j \leq n$ such that $p_i \cdot p_j$ is divisible by $i \cdot j$ without remainder.
|
[
[
"6\n1\n1\n2\n1 2\n3\n2 3 1\n5\n2 4 1 3 5\n12\n8 9 7 12 1 10 6 3 2 4 11 5\n15\n1 2 4 6 8 10 12 14 3 9 15 5 7 11 13",
"0\n1\n1\n3\n9\n3"
]
] |
In the first set of input data, there are no index pairs, as the size of the permutation is $1$.
In the second set of input data, there is one index pair $(1, 2)$ and it is valid.
In the third set of input data, the index pair $(1, 2)$ is valid.
In the fourth set of input data, the index pairs $(1, 2)$, $(1, 5)$, and $(2, 5)$ are valid.
|
Title: Permutation Problem (Simple Version)
time_limit_ms: 3000
memory_limit_mb: 128
Description: This is a simple version of the problem. The only difference is that in this version $n \leq 10^5$ and the sum of $n$ for all sets of input data does not exceed $10^5$.
You are given a permutation $p$ of length $n$. Calculate the number of index pairs $1 \leq i < j \leq n$ such that $p_i \cdot p_j$ is divisible by $i \cdot j$ without remainder.
A permutation is a sequence of $n$ integers, where each integer from $1$ to $n$ occurs exactly once. For example, $[1]$, $[3,5,2,1,4]$, $[1,3,2]$ are permutations, while $[2,3,2]$, $[4,3,1]$, $[0]$ are not.
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. Then follows their description.
The first line of each set of input data contains a single integer $n$ ($1 \leq n \leq 10^5$) β the length of the permutation $p$.
The second line of each set of input data contains $n$ distinct integers $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq n$) β the permutation $p$.
It is guaranteed that the sum of $n$ for all sets of input data does not exceed $10^5$.
Output: For each set of input data, output the number of index pairs $1 \leq i < j \leq n$ such that $p_i \cdot p_j$ is divisible by $i \cdot j$ without remainder.
Examples:
input:
6
1
1
2
1 2
3
2 3 1
5
2 4 1 3 5
12
8 9 7 12 1 10 6 3 2 4 11 5
15
1 2 4 6 8 10 12 14 3 9 15 5 7 11 13
output:
0
1
1
3
9
3
Note: In the first set of input data, there are no index pairs, as the size of the permutation is $1$.
In the second set of input data, there is one index pair $(1, 2)$ and it is valid.
In the third set of input data, the index pair $(1, 2)$ is valid.
In the fourth set of input data, the index pairs $(1, 2)$, $(1, 5)$, and $(2, 5)$ are valid.
|
2030G2
|
https://codeforces.com/problemset/problem/2030/G2
|
The Destruction of the Universe (Hard Version)
| 3,100
|
[
"combinatorics",
"math"
] |
Div. 2
| 4,000
| 512
|
This is the hard version of the problem. In this version, $n \leq 10^6$. You can only make hacks if both versions of the problem are solved.
Orangutans are powerful beingsβso powerful that they only need $1$ unit of time to destroy every vulnerable planet in the universe!
There are $n$ planets in the universe. Each planet has an interval of vulnerability $[l, r]$, during which it will be exposed to destruction by orangutans. Orangutans can also expand the interval of vulnerability of any planet by $1$ unit.
Specifically, suppose the expansion is performed on planet $p$ with interval of vulnerability $[l_p, r_p]$. Then, the resulting interval of vulnerability may be either $[l_p - 1, r_p]$ or $[l_p, r_p + 1]$.
Given a set of planets, orangutans can destroy all planets if the intervals of vulnerability of all planets in the set intersect at least one common point. Let the score of such a set denote the minimum number of expansions that must be performed.
Orangutans are interested in the sum of scores of all non-empty subsets of the planets in the universe. As the answer can be large, output it modulo $998\,244\,353$.
|
The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The first line of each test case contains an integer $n$ ($1 \leq n \leq 10^6$) β the number of planets in the universe.
The following $n$ lines contain two integers $l_i$ and $r_i$ ($1 \leq l_i \leq r_i \leq n$) β the initial interval of vulnerability of the $i$-th planet.
It is guaranteed that the sum of $n$ does not exceed $10^6$ over all test cases.
|
For each test case, output an integer β the sum of scores to destroy all non- empty subsets of the planets in the universe, modulo $998\,244\,353$.
|
[
[
"3\n3\n1 1\n2 3\n3 3\n4\n1 4\n2 3\n2 4\n1 1\n5\n1 2\n2 3\n3 4\n4 5\n1 5",
"5\n6\n24"
]
] |
In the first testcase, there are seven non-empty subsets of planets we must consider:
* For each of the subsets $\\{[1,1]\\}, \\{[2,3]\\}, \\{[3,3]\\}$, the score is $0$. * For the subset $\\{[2,3], [3,3]\\}$, the score is $0$, because the point $3$ is already contained in both planets' interval of vulnerability. * For the subset $\\{[1,1], [2,3]\\}$, the score is $1$. By using one operation on changing the interval of vulnerability of the second planet to be $[1,3]$, the two planets now both have the point $1$ in their interval. * For the subset $\\{[1,1], [3,3]\\}$, the score is $2$. By using two operations on changing the interval of vulnerability of the first planet to be $[1,3]$, the two planets now both have the point $3$ in their interval. * For the subset $\\{[1,1], [2,3], [3,3]\\}$, the score is $2$. By using one operation on changing the interval of vulnerability of the first planet to be $[1,2]$ and one operation on changing the interval of vulnerability of the third planet to $[2,3]$, all three planets will have the point $2$ in their interval.
The sum of scores of all non-empty subsets of the first testcase is $0 \cdot 4 + 1 \cdot 1 + 2\cdot2 = 5$.
|
Title: The Destruction of the Universe (Hard Version)
time_limit_ms: 4000
memory_limit_mb: 512
Description: This is the hard version of the problem. In this version, $n \leq 10^6$. You can only make hacks if both versions of the problem are solved.
Orangutans are powerful beingsβso powerful that they only need $1$ unit of time to destroy every vulnerable planet in the universe!
There are $n$ planets in the universe. Each planet has an interval of vulnerability $[l, r]$, during which it will be exposed to destruction by orangutans. Orangutans can also expand the interval of vulnerability of any planet by $1$ unit.
Specifically, suppose the expansion is performed on planet $p$ with interval of vulnerability $[l_p, r_p]$. Then, the resulting interval of vulnerability may be either $[l_p - 1, r_p]$ or $[l_p, r_p + 1]$.
Given a set of planets, orangutans can destroy all planets if the intervals of vulnerability of all planets in the set intersect at least one common point. Let the score of such a set denote the minimum number of expansions that must be performed.
Orangutans are interested in the sum of scores of all non-empty subsets of the planets in the universe. As the answer can be large, output it modulo $998\,244\,353$.
Input: The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The first line of each test case contains an integer $n$ ($1 \leq n \leq 10^6$) β the number of planets in the universe.
The following $n$ lines contain two integers $l_i$ and $r_i$ ($1 \leq l_i \leq r_i \leq n$) β the initial interval of vulnerability of the $i$-th planet.
It is guaranteed that the sum of $n$ does not exceed $10^6$ over all test cases.
Output: For each test case, output an integer β the sum of scores to destroy all non- empty subsets of the planets in the universe, modulo $998\,244\,353$.
Examples:
input:
3
3
1 1
2 3
3 3
4
1 4
2 3
2 4
1 1
5
1 2
2 3
3 4
4 5
1 5
output:
5
6
24
Note: In the first testcase, there are seven non-empty subsets of planets we must consider:
* For each of the subsets $\\{[1,1]\\}, \\{[2,3]\\}, \\{[3,3]\\}$, the score is $0$. * For the subset $\\{[2,3], [3,3]\\}$, the score is $0$, because the point $3$ is already contained in both planets' interval of vulnerability. * For the subset $\\{[1,1], [2,3]\\}$, the score is $1$. By using one operation on changing the interval of vulnerability of the second planet to be $[1,3]$, the two planets now both have the point $1$ in their interval. * For the subset $\\{[1,1], [3,3]\\}$, the score is $2$. By using two operations on changing the interval of vulnerability of the first planet to be $[1,3]$, the two planets now both have the point $3$ in their interval. * For the subset $\\{[1,1], [2,3], [3,3]\\}$, the score is $2$. By using one operation on changing the interval of vulnerability of the first planet to be $[1,2]$ and one operation on changing the interval of vulnerability of the third planet to $[2,3]$, all three planets will have the point $2$ in their interval.
The sum of scores of all non-empty subsets of the first testcase is $0 \cdot 4 + 1 \cdot 1 + 2\cdot2 = 5$.
|
2004C
|
https://codeforces.com/problemset/problem/2004/C
|
Splitting Items
| 1,100
|
[
"games",
"greedy",
"sortings"
] |
Div. 2
| 2,000
| 256
|
Alice and Bob have $n$ items they'd like to split between them, so they decided to play a game. All items have a cost, and the $i$-th item costs $a_i$. Players move in turns starting from Alice.
In each turn, the player chooses one of the remaining items and takes it. The game goes on until no items are left.
Let's say that $A$ is the total cost of items taken by Alice and $B$ is the total cost of Bob's items. The resulting score of the game then will be equal to $A - B$.
Alice wants to maximize the score, while Bob wants to minimize it. Both Alice and Bob will play optimally.
But the game will take place tomorrow, so today Bob can modify the costs a little. He can increase the costs $a_i$ of several (possibly none or all) items by an integer value (possibly, by the same value or by different values for each item). However, the total increase must be less than or equal to $k$. Otherwise, Alice may suspect something. Note that Bob can't decrease costs, only increase.
What is the minimum possible score Bob can achieve?
|
The first line contains a single integer $t$ ($1 \le t \le 5000$) β the number of test cases. Then $t$ cases follow.
The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $0 \le k \le 10^9$) β the number of items and the maximum total increase Bob can make.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the initial costs of the items.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
|
For each test case, print a single integer β the minimum possible score $A - B$ after Bob increases the costs of several (possibly none or all) items.
|
[
[
"4\n2 5\n1 10\n3 0\n10 15 12\n4 6\n3 1 2 4\n2 4\n6 9",
"4\n13\n0\n0"
]
] |
In the first test case, Bob can increase $a_1$ by $5$, making costs equal to $[6, 10]$. Tomorrow, Alice will take $10$ and Bob will take $6$. The total score will be equal to $10 - 6 = 4$, and it's the minimum possible.
In the second test case, Bob can't change costs. So the score will be equal to $(15 + 10) - 12 = 13$, since Alice will take $15$, Bob will take $12$, and Alice β $10$.
In the third test case, Bob, for example, can increase $a_1$ by $1$, $a_2$ by $3$, and $a_3$ by $2$. The total change is equal to $1 + 3 + 2 \le 6$ and costs will be equal to $[4, 4, 4, 4]$. Obviously, the score will be equal to $(4 + 4) - (4 + 4) = 0$.
In the fourth test case, Bob can increase $a_1$ by $3$, making costs equal to $[9, 9]$. The score will be equal to $9 - 9 = 0$.
|
Title: Splitting Items
time_limit_ms: 2000
memory_limit_mb: 256
Description: Alice and Bob have $n$ items they'd like to split between them, so they decided to play a game. All items have a cost, and the $i$-th item costs $a_i$. Players move in turns starting from Alice.
In each turn, the player chooses one of the remaining items and takes it. The game goes on until no items are left.
Let's say that $A$ is the total cost of items taken by Alice and $B$ is the total cost of Bob's items. The resulting score of the game then will be equal to $A - B$.
Alice wants to maximize the score, while Bob wants to minimize it. Both Alice and Bob will play optimally.
But the game will take place tomorrow, so today Bob can modify the costs a little. He can increase the costs $a_i$ of several (possibly none or all) items by an integer value (possibly, by the same value or by different values for each item). However, the total increase must be less than or equal to $k$. Otherwise, Alice may suspect something. Note that Bob can't decrease costs, only increase.
What is the minimum possible score Bob can achieve?
Input: The first line contains a single integer $t$ ($1 \le t \le 5000$) β the number of test cases. Then $t$ cases follow.
The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $0 \le k \le 10^9$) β the number of items and the maximum total increase Bob can make.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the initial costs of the items.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
Output: For each test case, print a single integer β the minimum possible score $A - B$ after Bob increases the costs of several (possibly none or all) items.
Examples:
input:
4
2 5
1 10
3 0
10 15 12
4 6
3 1 2 4
2 4
6 9
output:
4
13
0
0
Note: In the first test case, Bob can increase $a_1$ by $5$, making costs equal to $[6, 10]$. Tomorrow, Alice will take $10$ and Bob will take $6$. The total score will be equal to $10 - 6 = 4$, and it's the minimum possible.
In the second test case, Bob can't change costs. So the score will be equal to $(15 + 10) - 12 = 13$, since Alice will take $15$, Bob will take $12$, and Alice β $10$.
In the third test case, Bob, for example, can increase $a_1$ by $1$, $a_2$ by $3$, and $a_3$ by $2$. The total change is equal to $1 + 3 + 2 \le 6$ and costs will be equal to $[4, 4, 4, 4]$. Obviously, the score will be equal to $(4 + 4) - (4 + 4) = 0$.
In the fourth test case, Bob can increase $a_1$ by $3$, making costs equal to $[9, 9]$. The score will be equal to $9 - 9 = 0$.
|
1986A
|
https://codeforces.com/problemset/problem/1986/A
|
X Axis
| 800
|
[
"brute force",
"geometry",
"math",
"sortings"
] |
Div. 3
| 2,000
| 256
|
You are given three points with integer coordinates $x_1$, $x_2$, and $x_3$ on the $X$ axis ($1 \leq x_i \leq 10$). You can choose any point with an integer coordinate $a$ on the $X$ axis. Note that the point $a$ may coincide with $x_1$, $x_2$, or $x_3$. Let $f(a)$ be the total distance from the given points to the point $a$. Find the smallest value of $f(a)$.
The distance between points $a$ and $b$ is equal to $|a - b|$. For example, the distance between points $a = 5$ and $b = 2$ is $3$.
|
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^3$) β the number of test cases. Then follows their descriptions.
The single line of each test case contains three integers $x_1$, $x_2$, and $x_3$ ($1 \leq x_i \leq 10$) β the coordinates of the points.
|
For each test case, output the smallest value of $f(a)$.
|
[
[
"8\n1 1 1\n1 5 9\n8 2 8\n10 9 3\n2 1 1\n2 4 1\n7 3 5\n1 9 4",
"0\n8\n6\n7\n1\n3\n4\n8"
]
] |
In the first test case, the smallest value of $f(a)$ is achieved when $a = 1$: $f(1) = |1 - 1| + |1 - 1| + |1 - 1| = 0$.
In the second test case, the smallest value of $f(a)$ is achieved when $a = 5$: $f(5) = |1 - 5| + |5 - 5| + |9 - 5| = 8$.
In the third test case, the smallest value of $f(a)$ is achieved when $a = 8$: $f(8) = |8 - 8| + |2 - 8| + |8 - 8| = 6$.
In the fourth test case, the smallest value of $f(a)$ is achieved when $a = 9$: $f(10) = |10 - 9| + |9 - 9| + |3 - 9| = 7$.
|
Title: X Axis
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given three points with integer coordinates $x_1$, $x_2$, and $x_3$ on the $X$ axis ($1 \leq x_i \leq 10$). You can choose any point with an integer coordinate $a$ on the $X$ axis. Note that the point $a$ may coincide with $x_1$, $x_2$, or $x_3$. Let $f(a)$ be the total distance from the given points to the point $a$. Find the smallest value of $f(a)$.
The distance between points $a$ and $b$ is equal to $|a - b|$. For example, the distance between points $a = 5$ and $b = 2$ is $3$.
Input: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^3$) β the number of test cases. Then follows their descriptions.
The single line of each test case contains three integers $x_1$, $x_2$, and $x_3$ ($1 \leq x_i \leq 10$) β the coordinates of the points.
Output: For each test case, output the smallest value of $f(a)$.
Examples:
input:
8
1 1 1
1 5 9
8 2 8
10 9 3
2 1 1
2 4 1
7 3 5
1 9 4
output:
0
8
6
7
1
3
4
8
Note: In the first test case, the smallest value of $f(a)$ is achieved when $a = 1$: $f(1) = |1 - 1| + |1 - 1| + |1 - 1| = 0$.
In the second test case, the smallest value of $f(a)$ is achieved when $a = 5$: $f(5) = |1 - 5| + |5 - 5| + |9 - 5| = 8$.
In the third test case, the smallest value of $f(a)$ is achieved when $a = 8$: $f(8) = |8 - 8| + |2 - 8| + |8 - 8| = 6$.
In the fourth test case, the smallest value of $f(a)$ is achieved when $a = 9$: $f(10) = |10 - 9| + |9 - 9| + |3 - 9| = 7$.
|
1995E2
|
https://codeforces.com/problemset/problem/1995/E2
|
Let Me Teach You a Lesson (Hard Version)
| 2,900
|
[
"data structures",
"dp",
"matrices",
"two pointers"
] |
Div. 2
| 2,000
| 256
|
This is the hard version of a problem. The only difference between an easy and a hard version is the constraints on $t$ and $n$. You can make hacks only if both versions of the problem are solved.
Arthur is giving a lesson to his famous $2 n$ knights. Like any other students, they're sitting at the desks in pairs, but out of habit in a circle. The knight $2 i - 1$ is sitting at the desk with the knight $2 i$.
Each knight has intelligence, which can be measured by an integer. Let's denote the intelligence of the $i$-th knight as $a_i$. Arthur wants the maximal difference in total intelligence over all pairs of desks to be as small as possible. More formally, he wants to minimize $\max\limits_{1 \le i \le n} (a_{2 i - 1} + a_{2 i}) - \min\limits_{1 \le i \le n} (a_{2 i - 1} + a_{2 i})$.
However, the Code of Chivalry only allows swapping the opposite knights in the circle, i.e., Arthur can simultaneously perform $a_i := a_{i + n}$, $a_{i + n} := a_i$ for any $1 \le i \le n$. Arthur can make any number of such swaps. What is the best result he can achieve?
|
Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 10\,000$) β the number of test cases. It is followed by descriptions of the test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 100\,000$) β the number of desks.
The second line consists of $2n$ integers $a_1, a_2, \ldots, a_{2 n}$ ($1 \le a_i \le 10^9$) β the intelligence values of the knights.
It is guaranteed that the sum of $n$ over all test cases does not exceed $100\,000$.
|
For each test case, output a single line containing one integer β the minimal difference Arthur can achieve.
|
[
[
"5\n2\n6 6 4 4\n1\n10 17\n3\n1 10 1 10 1 10\n3\n3 3 4 5 5 4\n5\n1 2 3 4 5 6 7 8 9 10",
"0\n0\n0\n2\n4"
]
] |
In the first test case, Arthur can swap the second and the fourth knights. Then the total intelligence at both desks will be $10$.
In the third test case, Arthur can make $0$ operations, which will result in the total intelligence of $11$ at each of the desks.
In the fourth test case, Arthur can swap knights with indices $2$ and $5$ and achieve the difference of $2$. It can be proven that he cannot improve his result any further.
|
Title: Let Me Teach You a Lesson (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: This is the hard version of a problem. The only difference between an easy and a hard version is the constraints on $t$ and $n$. You can make hacks only if both versions of the problem are solved.
Arthur is giving a lesson to his famous $2 n$ knights. Like any other students, they're sitting at the desks in pairs, but out of habit in a circle. The knight $2 i - 1$ is sitting at the desk with the knight $2 i$.
Each knight has intelligence, which can be measured by an integer. Let's denote the intelligence of the $i$-th knight as $a_i$. Arthur wants the maximal difference in total intelligence over all pairs of desks to be as small as possible. More formally, he wants to minimize $\max\limits_{1 \le i \le n} (a_{2 i - 1} + a_{2 i}) - \min\limits_{1 \le i \le n} (a_{2 i - 1} + a_{2 i})$.
However, the Code of Chivalry only allows swapping the opposite knights in the circle, i.e., Arthur can simultaneously perform $a_i := a_{i + n}$, $a_{i + n} := a_i$ for any $1 \le i \le n$. Arthur can make any number of such swaps. What is the best result he can achieve?
Input: Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 10\,000$) β the number of test cases. It is followed by descriptions of the test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 100\,000$) β the number of desks.
The second line consists of $2n$ integers $a_1, a_2, \ldots, a_{2 n}$ ($1 \le a_i \le 10^9$) β the intelligence values of the knights.
It is guaranteed that the sum of $n$ over all test cases does not exceed $100\,000$.
Output: For each test case, output a single line containing one integer β the minimal difference Arthur can achieve.
Examples:
input:
5
2
6 6 4 4
1
10 17
3
1 10 1 10 1 10
3
3 3 4 5 5 4
5
1 2 3 4 5 6 7 8 9 10
output:
0
0
0
2
4
Note: In the first test case, Arthur can swap the second and the fourth knights. Then the total intelligence at both desks will be $10$.
In the third test case, Arthur can make $0$ operations, which will result in the total intelligence of $11$ at each of the desks.
In the fourth test case, Arthur can swap knights with indices $2$ and $5$ and achieve the difference of $2$. It can be proven that he cannot improve his result any further.
|
2005B1
|
https://codeforces.com/problemset/problem/2005/B1
|
The Strict Teacher (Easy Version)
| 1,000
|
[
"greedy",
"math",
"sortings"
] |
Div. 2
| 1,500
| 256
|
This is the easy version of the problem. The only differences between the two versions are the constraints on $m$ and $q$. In this version, $m=2$ and $q=1$. You can make hacks only if both versions of the problem are solved.
Narek and Tsovak were busy preparing this round, so they have not managed to do their homework and decided to steal David's homework. Their strict teacher noticed that David has no homework and now wants to punish him. She hires other teachers to help her catch David. And now $m$ teachers together are chasing him. Luckily, the classroom is big, so David has many places to hide.
The classroom can be represented as a one-dimensional line with cells from $1$ to $n$, inclusive.
At the start, all $m$ teachers and David are in distinct cells. Then they make moves. During each move
* David goes to an adjacent cell or stays at the current one. * Then, each of the $m$ teachers simultaneously goes to an adjacent cell or stays at the current one.
This continues until David is caught. David is caught if any of the teachers (possibly more than one) is located in the same cell as David. Everyone sees others' moves, so they all act optimally.
Your task is to find how many moves it will take for the teachers to catch David if they all act optimally.
Acting optimally means the student makes his moves in a way that maximizes the number of moves the teachers need to catch him; and the teachers coordinate with each other to make their moves in a way that minimizes the number of moves they need to catch the student.
Also, as Narek and Tsovak think this task is easy, they decided to give you $q$ queries on David's position. Note: this is the easy version, and you are given only one query.
|
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. The description of each test case follows.
In the first line of each test case, you are given three integers $n$, $m$, and $q$ ($3 \le n \le 10^9$, $m=2$, $q=1$) β the number of cells on the line, the number of teachers, and the number of queries.
In the second line of each test case, you are given $m$ distinct integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le n$) β the cell numbers of the teachers.
In the third line of each test case, you are given $q$ integers $a_1, a_2, \ldots, a_q$ ($1 \le a_i \le n$) β David's cell number for every query.
It is guaranteed that for any $i$, $j$ such that $1 \le i \le m$ and $1 \le j \le q$, $b_i \neq a_j$.
|
For each test case, output $q$ lines, the $i$-th of them containing the answer of the $i$-th query.
|
[
[
"3\n10 2 1\n1 4\n2\n8 2 1\n3 6\n1\n8 2 1\n3 6\n8",
"1\n2\n2"
]
] |
In the first example, the student can just stay at cell $2$. The teacher, initially located in cell $1$, can reach cell $2$ in one move. Therefore, the answer is $1$.
In the second example, the student should just stay at cell $1$. The teacher, initially located in cell $3$, can reach cell $1$ in two moves. Therefore, the answer is $2$.
|
Title: The Strict Teacher (Easy Version)
time_limit_ms: 1500
memory_limit_mb: 256
Description: This is the easy version of the problem. The only differences between the two versions are the constraints on $m$ and $q$. In this version, $m=2$ and $q=1$. You can make hacks only if both versions of the problem are solved.
Narek and Tsovak were busy preparing this round, so they have not managed to do their homework and decided to steal David's homework. Their strict teacher noticed that David has no homework and now wants to punish him. She hires other teachers to help her catch David. And now $m$ teachers together are chasing him. Luckily, the classroom is big, so David has many places to hide.
The classroom can be represented as a one-dimensional line with cells from $1$ to $n$, inclusive.
At the start, all $m$ teachers and David are in distinct cells. Then they make moves. During each move
* David goes to an adjacent cell or stays at the current one. * Then, each of the $m$ teachers simultaneously goes to an adjacent cell or stays at the current one.
This continues until David is caught. David is caught if any of the teachers (possibly more than one) is located in the same cell as David. Everyone sees others' moves, so they all act optimally.
Your task is to find how many moves it will take for the teachers to catch David if they all act optimally.
Acting optimally means the student makes his moves in a way that maximizes the number of moves the teachers need to catch him; and the teachers coordinate with each other to make their moves in a way that minimizes the number of moves they need to catch the student.
Also, as Narek and Tsovak think this task is easy, they decided to give you $q$ queries on David's position. Note: this is the easy version, and you are given only one query.
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. The description of each test case follows.
In the first line of each test case, you are given three integers $n$, $m$, and $q$ ($3 \le n \le 10^9$, $m=2$, $q=1$) β the number of cells on the line, the number of teachers, and the number of queries.
In the second line of each test case, you are given $m$ distinct integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le n$) β the cell numbers of the teachers.
In the third line of each test case, you are given $q$ integers $a_1, a_2, \ldots, a_q$ ($1 \le a_i \le n$) β David's cell number for every query.
It is guaranteed that for any $i$, $j$ such that $1 \le i \le m$ and $1 \le j \le q$, $b_i \neq a_j$.
Output: For each test case, output $q$ lines, the $i$-th of them containing the answer of the $i$-th query.
Examples:
input:
3
10 2 1
1 4
2
8 2 1
3 6
1
8 2 1
3 6
8
output:
1
2
2
Note: In the first example, the student can just stay at cell $2$. The teacher, initially located in cell $1$, can reach cell $2$ in one move. Therefore, the answer is $1$.
In the second example, the student should just stay at cell $1$. The teacher, initially located in cell $3$, can reach cell $1$ in two moves. Therefore, the answer is $2$.
|
2033D
|
https://codeforces.com/problemset/problem/2033/D
|
Kousuke's Assignment
| 1,300
|
[
"data structures",
"dp",
"dsu",
"greedy",
"math"
] |
Div. 3
| 2,000
| 256
|
After a trip with Sakurako, Kousuke was very scared because he forgot about his programming assignment. In this assignment, the teacher gave him an array $a$ of $n$ integers and asked him to calculate the number of non- overlapping segments of the array $a$, such that each segment is considered beautiful.
A segment $[l,r]$ is considered beautiful if $a_l + a_{l+1} + \dots + a_{r-1} + a_r=0$.
For a fixed array $a$, your task is to compute the maximum number of non- overlapping beautiful segments.
|
The first line of input contains the number $t$ ($1 \le t \le 10^4$) β the number of test cases. Each test case consists of $2$ lines.
* The first line contains one integer $n$ ($1 \le n \le 10^5$) β the length of the array. * The second line contains $n$ integers $a_i$ ($-10^5 \le a_i \le 10^5$) β the elements of the array $a$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $3\cdot 10^5$.
|
For each test case, output a single integer: the maximum number of non- overlapping beautiful segments.
|
[
[
"3\n5\n2 1 -3 2 1\n7\n12 -4 4 43 -3 -5 8\n6\n0 -4 0 3 0 1",
"1\n2\n3"
]
] |
Title: Kousuke's Assignment
time_limit_ms: 2000
memory_limit_mb: 256
Description: After a trip with Sakurako, Kousuke was very scared because he forgot about his programming assignment. In this assignment, the teacher gave him an array $a$ of $n$ integers and asked him to calculate the number of non- overlapping segments of the array $a$, such that each segment is considered beautiful.
A segment $[l,r]$ is considered beautiful if $a_l + a_{l+1} + \dots + a_{r-1} + a_r=0$.
For a fixed array $a$, your task is to compute the maximum number of non- overlapping beautiful segments.
Input: The first line of input contains the number $t$ ($1 \le t \le 10^4$) β the number of test cases. Each test case consists of $2$ lines.
* The first line contains one integer $n$ ($1 \le n \le 10^5$) β the length of the array. * The second line contains $n$ integers $a_i$ ($-10^5 \le a_i \le 10^5$) β the elements of the array $a$.
It is guaranteed that the sum of $n$ across all test cases does not exceed $3\cdot 10^5$.
Output: For each test case, output a single integer: the maximum number of non- overlapping beautiful segments.
Examples:
input:
3
5
2 1 -3 2 1
7
12 -4 4 43 -3 -5 8
6
0 -4 0 3 0 1
output:
1
2
3
Note:
|
|
2022E2
|
https://codeforces.com/problemset/problem/2022/E2
|
Billetes MX (Hard Version)
| 2,600
|
[
"binary search",
"combinatorics",
"data structures",
"dsu",
"graphs"
] |
Div. 2
| 2,000
| 512
|
This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved.
An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:
* All elements of the grid are integers between $0$ and $2^{30}-1$, and * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.
However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.
For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.
|
The first line contains $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) β the number of rows, the number of columns, the number of fixed cells, and the number of updates.
The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.
The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.
It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.
It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.
|
For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.
|
[
[
"3\n3 3 8 1\n2 1 6\n3 2 12\n1 2 6\n2 2 0\n1 3 10\n1 1 0\n2 3 12\n3 1 10\n3 3 1\n2 5 2 0\n1 1 10\n1 2 30\n2 5 0 2\n1 1 10\n1 2 30",
"1\n0\n489373567\n651321892\n769740174\n489373567"
]
] |
In the first test case of the example, we initially have the following grid:
$0$| $6$| $10$ ---|---|--- $6$| $0$| $12$ $10$| $12$| $?$ It can be proven that the only valid value for tile $(3, 3)$ is $0$, so the first answer is $1$. For the second query, the grid does not satisfy the condition, and thus the answer is $0$.
|
Title: Billetes MX (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved.
An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:
* All elements of the grid are integers between $0$ and $2^{30}-1$, and * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.
However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.
For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.
Input: The first line contains $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) β the number of rows, the number of columns, the number of fixed cells, and the number of updates.
The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.
The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.
It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.
It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.
Output: For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.
Examples:
input:
3
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
2 5 2 0
1 1 10
1 2 30
2 5 0 2
1 1 10
1 2 30
output:
1
0
489373567
651321892
769740174
489373567
Note: In the first test case of the example, we initially have the following grid:
$0$| $6$| $10$ ---|---|--- $6$| $0$| $12$ $10$| $12$| $?$ It can be proven that the only valid value for tile $(3, 3)$ is $0$, so the first answer is $1$. For the second query, the grid does not satisfy the condition, and thus the answer is $0$.
|
2020D
|
https://codeforces.com/problemset/problem/2020/D
|
Connect the Dots
| 1,800
|
[
"brute force",
"dp",
"dsu",
"graphs",
"math",
"trees"
] |
Div. 2
| 2,000
| 512
|
One fine evening, Alice sat down to play the classic game "Connect the Dots", but with a twist.
To play the game, Alice draws a straight line and marks $n$ points on it, indexed from $1$ to $n$. Initially, there are no arcs between the points, so they are all disjoint. After that, Alice performs $m$ operations of the following type:
* She picks three integers $a_i$, $d_i$ ($1 \le d_i \le 10$), and $k_i$. * She selects points $a_i, a_i+d_i, a_i+2d_i, a_i+3d_i, \ldots, a_i+k_i\cdot d_i$ and connects each pair of these points with arcs.
After performing all $m$ operations, she wants to know the number of connected components$^\dagger$ these points form. Please help her find this number.
$^\dagger$ Two points are said to be in one connected component if there is a path between them via several (possibly zero) arcs and other points.
|
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 two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 2 \cdot 10^5$).
The $i$-th of the following $m$ lines contains three integers $a_i$, $d_i$, and $k_i$ ($1 \le a_i \le a_i + k_i\cdot d_i \le n$, $1 \le d_i \le 10$, $0 \le k_i \le n$).
It is guaranteed that both the sum of $n$ and the sum of $m$ over all test cases do not exceed $2 \cdot 10^5$.
|
For each test case, output the number of connected components.
|
[
[
"3\n10 2\n1 2 4\n2 2 4\n100 1\n19 2 4\n100 3\n1 2 5\n7 2 6\n17 2 31",
"2\n96\n61"
]
] |
In the first test case, there are $n = 10$ points. The first operation joins the points $1$, $3$, $5$, $7$, and $9$. The second operation joins the points $2$, $4$, $6$, $8$, and $10$. There are thus two connected components: $\\{1, 3, 5, 7, 9\\}$ and $\\{2, 4, 6, 8, 10\\}$.
In the second test case, there are $n = 100$ points. The only operation joins the points $19$, $21$, $23$, $25$, and $27$. Now all of them form a single connected component of size $5$. The other $95$ points form single-point connected components. Thus, the answer is $1 + 95 = 96$.
In the third test case, there are $n = 100$ points. After the operations, all odd points from $1$ to $79$ will be in one connected component of size $40$. The other $60$ points form single-point connected components. Thus, the answer is $1 + 60 = 61$.
|
Title: Connect the Dots
time_limit_ms: 2000
memory_limit_mb: 512
Description: One fine evening, Alice sat down to play the classic game "Connect the Dots", but with a twist.
To play the game, Alice draws a straight line and marks $n$ points on it, indexed from $1$ to $n$. Initially, there are no arcs between the points, so they are all disjoint. After that, Alice performs $m$ operations of the following type:
* She picks three integers $a_i$, $d_i$ ($1 \le d_i \le 10$), and $k_i$. * She selects points $a_i, a_i+d_i, a_i+2d_i, a_i+3d_i, \ldots, a_i+k_i\cdot d_i$ and connects each pair of these points with arcs.
After performing all $m$ operations, she wants to know the number of connected components$^\dagger$ these points form. Please help her find this number.
$^\dagger$ Two points are said to be in one connected component if there is a path between them via several (possibly zero) arcs and other points.
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 two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 2 \cdot 10^5$).
The $i$-th of the following $m$ lines contains three integers $a_i$, $d_i$, and $k_i$ ($1 \le a_i \le a_i + k_i\cdot d_i \le n$, $1 \le d_i \le 10$, $0 \le k_i \le n$).
It is guaranteed that both the sum of $n$ and the sum of $m$ over all test cases do not exceed $2 \cdot 10^5$.
Output: For each test case, output the number of connected components.
Examples:
input:
3
10 2
1 2 4
2 2 4
100 1
19 2 4
100 3
1 2 5
7 2 6
17 2 31
output:
2
96
61
Note: In the first test case, there are $n = 10$ points. The first operation joins the points $1$, $3$, $5$, $7$, and $9$. The second operation joins the points $2$, $4$, $6$, $8$, and $10$. There are thus two connected components: $\\{1, 3, 5, 7, 9\\}$ and $\\{2, 4, 6, 8, 10\\}$.
In the second test case, there are $n = 100$ points. The only operation joins the points $19$, $21$, $23$, $25$, and $27$. Now all of them form a single connected component of size $5$. The other $95$ points form single-point connected components. Thus, the answer is $1 + 95 = 96$.
In the third test case, there are $n = 100$ points. After the operations, all odd points from $1$ to $79$ will be in one connected component of size $40$. The other $60$ points form single-point connected components. Thus, the answer is $1 + 60 = 61$.
|
2003A
|
https://codeforces.com/problemset/problem/2003/A
|
Turtle and Good Strings
| 800
|
[
"greedy",
"strings"
] |
Div. 2
| 1,000
| 256
|
Turtle thinks a string $s$ is a good string if there exists a sequence of strings $t_1, t_2, \ldots, t_k$ ($k$ is an arbitrary integer) such that:
* $k \ge 2$. * $s = t_1 + t_2 + \ldots + t_k$, where $+$ represents the concatenation operation. For example, $\texttt{abc} = \texttt{a} + \texttt{bc}$. * For all $1 \le i < j \le k$, the first character of $t_i$ isn't equal to the last character of $t_j$.
Turtle is given a string $s$ consisting of lowercase Latin letters. Please tell him whether the string $s$ is a good string!
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 100$) β the length of the string.
The second line of each test case contains a string $s$ of length $n$, consisting of lowercase Latin letters.
|
For each test case, output "YES" if the string $s$ is a good string, and "NO" otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
|
[
[
"4\n2\naa\n3\naba\n4\nabcb\n12\nabcabcabcabc",
"No\nnO\nYes\nYES"
]
] |
In the first test case, the sequence of strings $\texttt{a}, \texttt{a}$ satisfies the condition $s = t_1 + t_2 + \ldots + t_k$, but the first character of $t_1$ is equal to the last character of $t_2$. It can be seen that there doesn't exist any sequence of strings which satisfies all of the conditions, so the answer is "NO".
In the third test case, the sequence of strings $\texttt{ab}, \texttt{cb}$ satisfies all of the conditions.
In the fourth test case, the sequence of strings $\texttt{abca}, \texttt{bcab}, \texttt{cabc}$ satisfies all of the conditions.
|
Title: Turtle and Good Strings
time_limit_ms: 1000
memory_limit_mb: 256
Description: Turtle thinks a string $s$ is a good string if there exists a sequence of strings $t_1, t_2, \ldots, t_k$ ($k$ is an arbitrary integer) such that:
* $k \ge 2$. * $s = t_1 + t_2 + \ldots + t_k$, where $+$ represents the concatenation operation. For example, $\texttt{abc} = \texttt{a} + \texttt{bc}$. * For all $1 \le i < j \le k$, the first character of $t_i$ isn't equal to the last character of $t_j$.
Turtle is given a string $s$ consisting of lowercase Latin letters. Please tell him whether the string $s$ is a good string!
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 100$) β the length of the string.
The second line of each test case contains a string $s$ of length $n$, consisting of lowercase Latin letters.
Output: For each test case, output "YES" if the string $s$ is a good string, and "NO" otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
Examples:
input:
4
2
aa
3
aba
4
abcb
12
abcabcabcabc
output:
No
nO
Yes
YES
Note: In the first test case, the sequence of strings $\texttt{a}, \texttt{a}$ satisfies the condition $s = t_1 + t_2 + \ldots + t_k$, but the first character of $t_1$ is equal to the last character of $t_2$. It can be seen that there doesn't exist any sequence of strings which satisfies all of the conditions, so the answer is "NO".
In the third test case, the sequence of strings $\texttt{ab}, \texttt{cb}$ satisfies all of the conditions.
In the fourth test case, the sequence of strings $\texttt{abca}, \texttt{bcab}, \texttt{cabc}$ satisfies all of the conditions.
|
1993C
|
https://codeforces.com/problemset/problem/1993/C
|
Light Switches
| 1,400
|
[
"implementation",
"math"
] |
Div. 2
| 2,000
| 256
|
There is an apartment consisting of $n$ rooms, each with its light initially turned off.
To control the lights in these rooms, the owner of the apartment decided to install chips in the rooms so that each room has exactly one chip, and the chips are installed at different times. Specifically, these times are represented by the array $a_1, a_2, \ldots, a_n$, where $a_i$ is the time (in minutes) at which a chip is installed in the $i$-th room.
As soon as a chip is installed, it changes the room's light status every $k$ minutes β it turns on the light for $k$ minutes, then turns it off for the next $k$ minutes, then turns it back on for the next $k$ minutes, and so on. In other words, the light status is changed by the chip at minute $a_i$, $a_i + k$, $a_i + 2k$, $a_i + 3k$, $\ldots$ for the $i$-th room.
What is the earliest moment when all rooms in the apartment have their lights turned on?
|
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 k \le n \le 2 \cdot 10^5$) β the number of rooms in the apartment and the period of the chips.
The second line contains $n$ distinct integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β the moments when the chips are installed.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, print a single integer β the answer to the question (in minutes). If there is no such moment that the lights are turned on in all the rooms, print $-1$ instead.
|
[
[
"9\n4 4\n2 3 4 5\n4 3\n2 3 4 5\n4 3\n3 4 8 9\n3 3\n6 2 1\n1 1\n1\n7 5\n14 34 6 25 46 7 17\n6 5\n40 80 99 60 90 50\n6 5\n64 40 50 68 70 10\n2 1\n1 1000000000",
"5\n-1\n10\n8\n1\n47\n100\n-1\n-1"
]
] |
In the first test case, all lights will be on by the minute $5$ without any of them being turned off by the chips. The answer is $5$.
In the second test case, due to $k=3$, the $1$-st light will be on at minutes $2, 3, 4, 8, 9, 10, 14, \ldots$; meanwhile, the $4$-th light will be on at minutes $5, 6, 7, 11, 12, 13, 17, \ldots$. These two sequences don't have any number in common, so they will never be on at the same time.
In the third test case, it can be seen that the $1$-st and $2$-nd lights will be turned off at minutes $6$ and $7$, but the chips will turn them back on at minutes $9$ and $10$. The $3$-rd and $4$-th lights will also be on at minute $10$, so the answer is $10$.
|
Title: Light Switches
time_limit_ms: 2000
memory_limit_mb: 256
Description: There is an apartment consisting of $n$ rooms, each with its light initially turned off.
To control the lights in these rooms, the owner of the apartment decided to install chips in the rooms so that each room has exactly one chip, and the chips are installed at different times. Specifically, these times are represented by the array $a_1, a_2, \ldots, a_n$, where $a_i$ is the time (in minutes) at which a chip is installed in the $i$-th room.
As soon as a chip is installed, it changes the room's light status every $k$ minutes β it turns on the light for $k$ minutes, then turns it off for the next $k$ minutes, then turns it back on for the next $k$ minutes, and so on. In other words, the light status is changed by the chip at minute $a_i$, $a_i + k$, $a_i + 2k$, $a_i + 3k$, $\ldots$ for the $i$-th room.
What is the earliest moment when all rooms in the apartment have their lights turned on?
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 k \le n \le 2 \cdot 10^5$) β the number of rooms in the apartment and the period of the chips.
The second line contains $n$ distinct integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β the moments when the chips are installed.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, print a single integer β the answer to the question (in minutes). If there is no such moment that the lights are turned on in all the rooms, print $-1$ instead.
Examples:
input:
9
4 4
2 3 4 5
4 3
2 3 4 5
4 3
3 4 8 9
3 3
6 2 1
1 1
1
7 5
14 34 6 25 46 7 17
6 5
40 80 99 60 90 50
6 5
64 40 50 68 70 10
2 1
1 1000000000
output:
5
-1
10
8
1
47
100
-1
-1
Note: In the first test case, all lights will be on by the minute $5$ without any of them being turned off by the chips. The answer is $5$.
In the second test case, due to $k=3$, the $1$-st light will be on at minutes $2, 3, 4, 8, 9, 10, 14, \ldots$; meanwhile, the $4$-th light will be on at minutes $5, 6, 7, 11, 12, 13, 17, \ldots$. These two sequences don't have any number in common, so they will never be on at the same time.
In the third test case, it can be seen that the $1$-st and $2$-nd lights will be turned off at minutes $6$ and $7$, but the chips will turn them back on at minutes $9$ and $10$. The $3$-rd and $4$-th lights will also be on at minute $10$, so the answer is $10$.
|
1971H
|
https://codeforces.com/problemset/problem/1971/H
|
Β±1
| 2,100
|
[
"2-sat",
"dfs and similar",
"graphs"
] |
Div. 4
| 2,000
| 256
|
Bob has a grid with $3$ rows and $n$ columns, each of which contains either $a_i$ or $-a_i$ for some integer $1 \leq i \leq n$. For example, one possible grid for $n=4$ is shown below:
$$\begin{bmatrix} a_1 & -a_2 & -a_3 & -a_2 \\\ -a_4 & a_4 & -a_1 & -a_3 \\\ a_1 & a_2 & -a_2 & a_4 \end{bmatrix}$$
Alice and Bob play a game as follows:
* Bob shows Alice his grid. * Alice gives Bob an array $a_1, a_2, \dots, a_n$ of her choosing, whose elements are all $\mathbf{-1}$ or $\mathbf{1}$. * Bob substitutes these values into his grid to make a grid of $-1$s and $1$s. * Bob sorts the elements of each column in non-decreasing order. * Alice wins if all the elements in the middle row are $1$; otherwise, Bob wins.
For example, suppose Alice gives Bob the array $[1, -1, -1, 1]$ for the grid above. Then the following will happen (colors are added for clarity):
$$\begin{bmatrix} \color{red}{a_1} & \color{green}{-a_2} & \color{blue}{-a_3} & \color{green}{-a_2} \\\ -a_4 & a_4 & \color{red}{-a_1} & \color{blue}{-a_3} \\\ \color{red}{a_1} & \color{green}{a_2} & \color{green}{-a_2} & a_4 \end{bmatrix} \xrightarrow{[\color{red}{1},\color{green}{-1},\color{blue}{-1},1]} \begin{bmatrix} \color{red}{1} & \color{green}{1} & \color{blue}{1} & \color{green}{1} \\\ -1 & 1 & \color{red}{-1} & \color{blue}{1} \\\ \color{red}{1} & \color{green}{-1} & \color{green}{1} & 1 \end{bmatrix} \xrightarrow{\text{sort each column}} \begin{bmatrix} -1 & -1 & -1 & 1 \\\ \mathbf{1} & \mathbf{1} & \mathbf{1} & \mathbf{1} \\\ 1 & 1 & 1 & 1 \\\ \end{bmatrix}\,. $$ Since the middle row is all $1$, Alice wins.
Given Bob's grid, determine whether or not Alice can choose the array $a$ to win the game.
|
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 500$) β the number of columns of Bob's grid.
The next three lines each contain $n$ integers, the $i$-th of which contains $g_{i,1}, g_{i,2}, \dots, g_{i,n}$ ($-n \leq g_{i,j} \leq n$, $g_{i,j} \neq 0$), representing Bob's grid.
If cell $x > 0$ is in the input, that cell in Bob's grid should contain $a_x$; if $x < 0$ is in the input, that cell in Bob's grid should contain $-a_{-x}$. See the sample input and notes for a better understanding.
|
For each test case, output "YES" (without quotes) if Alice can win, and "NO" (without quotes) otherwise.
You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", and "Yes" will be recognized as a positive response).
|
[
[
"4\n4\n1 -2 -3 -2\n-4 4 -1 -3\n1 2 -2 4\n2\n1 2\n-1 -2\n2 -2\n5\n1 2 3 4 5\n-2 3 -4 -5 -1\n3 -5 1 2 2\n6\n1 3 -6 2 5 2\n1 3 -2 -3 -6 -5\n-2 -1 -3 2 3 1",
"YES\nNO\nYES\nNO"
]
] |
The first test case is described in the statement.
In the second test case, Bob's grid is as follows:
$$\begin{bmatrix} a_1 & a_2 \\\ -a_1 & -a_2 \\\ a_2 & -a_2 \end{bmatrix}$$
For the last column to have $1$ in the middle row when sorted, Alice must pick $a_2 = -1$. However, it is then impossible to choose $a_1$ such that the first column has $1$ in the middle when sorted. Thus, Alice cannot win.
In the third test case, Alice can pick $a = [1,1,1,1,1]$.
|
Title: Β±1
time_limit_ms: 2000
memory_limit_mb: 256
Description: Bob has a grid with $3$ rows and $n$ columns, each of which contains either $a_i$ or $-a_i$ for some integer $1 \leq i \leq n$. For example, one possible grid for $n=4$ is shown below:
$$\begin{bmatrix} a_1 & -a_2 & -a_3 & -a_2 \\\ -a_4 & a_4 & -a_1 & -a_3 \\\ a_1 & a_2 & -a_2 & a_4 \end{bmatrix}$$
Alice and Bob play a game as follows:
* Bob shows Alice his grid. * Alice gives Bob an array $a_1, a_2, \dots, a_n$ of her choosing, whose elements are all $\mathbf{-1}$ or $\mathbf{1}$. * Bob substitutes these values into his grid to make a grid of $-1$s and $1$s. * Bob sorts the elements of each column in non-decreasing order. * Alice wins if all the elements in the middle row are $1$; otherwise, Bob wins.
For example, suppose Alice gives Bob the array $[1, -1, -1, 1]$ for the grid above. Then the following will happen (colors are added for clarity):
$$\begin{bmatrix} \color{red}{a_1} & \color{green}{-a_2} & \color{blue}{-a_3} & \color{green}{-a_2} \\\ -a_4 & a_4 & \color{red}{-a_1} & \color{blue}{-a_3} \\\ \color{red}{a_1} & \color{green}{a_2} & \color{green}{-a_2} & a_4 \end{bmatrix} \xrightarrow{[\color{red}{1},\color{green}{-1},\color{blue}{-1},1]} \begin{bmatrix} \color{red}{1} & \color{green}{1} & \color{blue}{1} & \color{green}{1} \\\ -1 & 1 & \color{red}{-1} & \color{blue}{1} \\\ \color{red}{1} & \color{green}{-1} & \color{green}{1} & 1 \end{bmatrix} \xrightarrow{\text{sort each column}} \begin{bmatrix} -1 & -1 & -1 & 1 \\\ \mathbf{1} & \mathbf{1} & \mathbf{1} & \mathbf{1} \\\ 1 & 1 & 1 & 1 \\\ \end{bmatrix}\,. $$ Since the middle row is all $1$, Alice wins.
Given Bob's grid, determine whether or not Alice can choose the array $a$ to win the game.
Input: The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 500$) β the number of columns of Bob's grid.
The next three lines each contain $n$ integers, the $i$-th of which contains $g_{i,1}, g_{i,2}, \dots, g_{i,n}$ ($-n \leq g_{i,j} \leq n$, $g_{i,j} \neq 0$), representing Bob's grid.
If cell $x > 0$ is in the input, that cell in Bob's grid should contain $a_x$; if $x < 0$ is in the input, that cell in Bob's grid should contain $-a_{-x}$. See the sample input and notes for a better understanding.
Output: For each test case, output "YES" (without quotes) if Alice can win, and "NO" (without quotes) otherwise.
You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", and "Yes" will be recognized as a positive response).
Examples:
input:
4
4
1 -2 -3 -2
-4 4 -1 -3
1 2 -2 4
2
1 2
-1 -2
2 -2
5
1 2 3 4 5
-2 3 -4 -5 -1
3 -5 1 2 2
6
1 3 -6 2 5 2
1 3 -2 -3 -6 -5
-2 -1 -3 2 3 1
output:
YES
NO
YES
NO
Note: The first test case is described in the statement.
In the second test case, Bob's grid is as follows:
$$\begin{bmatrix} a_1 & a_2 \\\ -a_1 & -a_2 \\\ a_2 & -a_2 \end{bmatrix}$$
For the last column to have $1$ in the middle row when sorted, Alice must pick $a_2 = -1$. However, it is then impossible to choose $a_1$ such that the first column has $1$ in the middle when sorted. Thus, Alice cannot win.
In the third test case, Alice can pick $a = [1,1,1,1,1]$.
|
1980E
|
https://codeforces.com/problemset/problem/1980/E
|
Permutation of Rows and Columns
| 1,600
|
[
"constructive algorithms",
"data structures",
"greedy",
"hashing",
"implementation",
"math",
"matrices",
"sortings"
] |
Div. 3
| 3,000
| 256
|
You have been given a matrix $a$ of size $n$ by $m$, containing a permutation of integers from $1$ to $n \cdot m$.
A permutation of $n$ integers is an array containing all numbers from $1$ to $n$ exactly once. For example, the arrays $[1]$, $[2, 1, 3]$, $[5, 4, 3, 2, 1]$ are permutations, while the arrays $[1, 1]$, $[100]$, $[1, 2, 4, 5]$ are not.
A matrix contains a permutation if, when all its elements are written out, the resulting array is a permutation. Matrices $[[1, 2], [3, 4]]$, $[[1]]$, $[[1, 5, 3], [2, 6, 4]]$ contain permutations, while matrices $[[2]]$, $[[1, 1], [2, 2]]$, $[[1, 2], [100, 200]]$ do not.
You can perform one of the following two actions in one operation:
* choose columns $c$ and $d$ ($1 \le c, d \le m$, $c \ne d$) and swap these columns; * choose rows $c$ and $d$ ($1 \le c, d \le n$, $c \ne d$) and swap these rows.
You can perform any number of operations.
You are given the original matrix $a$ and the matrix $b$. Your task is to determine whether it is possible to transform matrix $a$ into matrix $b$ using the given operations.
|
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The descriptions of the test cases follow.
The first line of each test case description contains $2$ integers $n$ and $m$ ($1 \le n, m \le n \cdot m \le 2 \cdot 10^5$) β the sizes of the matrix.
The next $n$ lines contain $m$ integers $a_{ij}$ each ($1 \le a_{ij} \le n \cdot m$). It is guaranteed that matrix $a$ is a permutation.
The next $n$ lines contain $m$ integers $b_{ij}$ each ($1 \le b_{ij} \le n \cdot m$). It is guaranteed that matrix $b$ is a permutation.
It is guaranteed that the sum of the values $n \cdot m$ for all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output "YES" if the second matrix can be obtained from the first, 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.
|
[
[
"7\n1 1\n1\n1\n2 2\n1 2\n3 4\n4 3\n2 1\n2 2\n1 2\n3 4\n4 3\n1 2\n3 4\n1 5 9 6\n12 10 4 8\n7 11 3 2\n1 5 9 6\n12 10 4 8\n7 11 3 2\n3 3\n1 5 9\n6 4 2\n3 8 7\n9 5 1\n2 4 6\n7 8 3\n2 3\n1 2 6\n5 4 3\n6 1 2\n3 4 5\n1 5\n5 1 2 3 4\n4 2 5 1 3",
"YES\nYES\nNO\nYES\nYES\nNO\nYES"
]
] |
In the second example, the original matrix looks like this:
$ \begin{pmatrix} 1 & 2 \\\ 3 & 4 \end{pmatrix} $
By swapping rows $1$ and $2$, it becomes:
$ \begin{pmatrix} 3 & 4 \\\ 1 & 2 \end{pmatrix} $
By swapping columns $1$ and $2$, it becomes equal to matrix $b$:
$ \begin{pmatrix} 4 & 3 \\\ 2 & 1 \end{pmatrix} $
|
Title: Permutation of Rows and Columns
time_limit_ms: 3000
memory_limit_mb: 256
Description: You have been given a matrix $a$ of size $n$ by $m$, containing a permutation of integers from $1$ to $n \cdot m$.
A permutation of $n$ integers is an array containing all numbers from $1$ to $n$ exactly once. For example, the arrays $[1]$, $[2, 1, 3]$, $[5, 4, 3, 2, 1]$ are permutations, while the arrays $[1, 1]$, $[100]$, $[1, 2, 4, 5]$ are not.
A matrix contains a permutation if, when all its elements are written out, the resulting array is a permutation. Matrices $[[1, 2], [3, 4]]$, $[[1]]$, $[[1, 5, 3], [2, 6, 4]]$ contain permutations, while matrices $[[2]]$, $[[1, 1], [2, 2]]$, $[[1, 2], [100, 200]]$ do not.
You can perform one of the following two actions in one operation:
* choose columns $c$ and $d$ ($1 \le c, d \le m$, $c \ne d$) and swap these columns; * choose rows $c$ and $d$ ($1 \le c, d \le n$, $c \ne d$) and swap these rows.
You can perform any number of operations.
You are given the original matrix $a$ and the matrix $b$. Your task is to determine whether it is possible to transform matrix $a$ into matrix $b$ using the given operations.
Input: The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The descriptions of the test cases follow.
The first line of each test case description contains $2$ integers $n$ and $m$ ($1 \le n, m \le n \cdot m \le 2 \cdot 10^5$) β the sizes of the matrix.
The next $n$ lines contain $m$ integers $a_{ij}$ each ($1 \le a_{ij} \le n \cdot m$). It is guaranteed that matrix $a$ is a permutation.
The next $n$ lines contain $m$ integers $b_{ij}$ each ($1 \le b_{ij} \le n \cdot m$). It is guaranteed that matrix $b$ is a permutation.
It is guaranteed that the sum of the values $n \cdot m$ for all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output "YES" if the second matrix can be obtained from the first, 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:
7
1 1
1
1
2 2
1 2
3 4
4 3
2 1
2 2
1 2
3 4
4 3
1 2
3 4
1 5 9 6
12 10 4 8
7 11 3 2
1 5 9 6
12 10 4 8
7 11 3 2
3 3
1 5 9
6 4 2
3 8 7
9 5 1
2 4 6
7 8 3
2 3
1 2 6
5 4 3
6 1 2
3 4 5
1 5
5 1 2 3 4
4 2 5 1 3
output:
YES
YES
NO
YES
YES
NO
YES
Note: In the second example, the original matrix looks like this:
$ \begin{pmatrix} 1 & 2 \\\ 3 & 4 \end{pmatrix} $
By swapping rows $1$ and $2$, it becomes:
$ \begin{pmatrix} 3 & 4 \\\ 1 & 2 \end{pmatrix} $
By swapping columns $1$ and $2$, it becomes equal to matrix $b$:
$ \begin{pmatrix} 4 & 3 \\\ 2 & 1 \end{pmatrix} $
|
2014F
|
https://codeforces.com/problemset/problem/2014/F
|
Sheriff's Defense
| 2,000
|
[
"dfs and similar",
"dp",
"greedy",
"trees"
] |
Div. 3
| 2,000
| 256
|
"Why, master," quoth Little John, taking the bags and weighing them in his hand, "here is the chink of gold."
The folk hero Robin Hood has been troubling Sheriff of Nottingham greatly. Sheriff knows that Robin Hood is about to attack his camps and he wants to be prepared.
Sheriff of Nottingham built the camps with strategy in mind and thus there are exactly $n$ camps numbered from $1$ to $n$ and $n-1$ trails, each connecting two camps. Any camp can be reached from any other camp. Each camp $i$ has initially $a_i$ gold.
As it is now, all camps would be destroyed by Robin. Sheriff can strengthen a camp by subtracting exactly $c$ gold from each of its neighboring camps and use it to build better defenses for that camp. Strengthening a camp doesn't change its gold, only its neighbors' gold. A camp can have negative gold.
After Robin Hood's attack, all camps that have been strengthened survive the attack, all others are destroyed.
What's the maximum gold Sheriff can keep in his surviving camps after Robin Hood's attack if he strengthens his camps optimally?
Camp $a$ is neighboring camp $b$ if and only if there exists a trail connecting $a$ and $b$. Only strengthened camps count towards the answer, as others are destroyed.
|
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
Each test case begins with two integers $n$, $c$ ($1 \le n \le 2\cdot10^5, 1 \le c \le 10^9$) β the number of camps and the gold taken from each neighboring camp for strengthening.
The second line of each test case contains $n$ integers $a_1,a_2,\dots,a_n$ ($-10^9 \le a_i \le 10^9$) β the initial gold of each camp.
Then follow $n-1$ lines, each with integers $u$, $v$ ($1 \le u, v \le n$, $u \ne v$) β meaning that there is a trail between $u$ and $v$.
The sum of $n$ over all test cases doesn't exceed $2\cdot10^5$.
It is guaranteed that any camp is reachable from any other camp.
|
Output a single integer, the maximum gold Sheriff of Nottingham can keep in his surviving camps after Robin Hood's attack.
|
[
[
"5\n3 1\n2 3 1\n1 2\n2 3\n3 1\n3 6 3\n1 2\n2 3\n3 1\n-2 -3 -1\n1 2\n2 3\n6 1\n5 -4 3 6 7 3\n4 1\n5 1\n3 5\n3 6\n1 2\n8 1\n3 5 2 7 8 5 -3 -4\n7 3\n1 8\n4 3\n3 5\n7 6\n8 7\n2 1",
"3\n8\n0\n17\n26"
]
] |
In the first test case, it is optimal to strengthen the second base. The final gold at each base is $[1,3,0]$.
In the second test case, it is optimal to strengthen all bases. The final gold at each base is $[2,4,2]$.
In the third test case, it is optimal to not strengthen any base.
|
Title: Sheriff's Defense
time_limit_ms: 2000
memory_limit_mb: 256
Description: "Why, master," quoth Little John, taking the bags and weighing them in his hand, "here is the chink of gold."
The folk hero Robin Hood has been troubling Sheriff of Nottingham greatly. Sheriff knows that Robin Hood is about to attack his camps and he wants to be prepared.
Sheriff of Nottingham built the camps with strategy in mind and thus there are exactly $n$ camps numbered from $1$ to $n$ and $n-1$ trails, each connecting two camps. Any camp can be reached from any other camp. Each camp $i$ has initially $a_i$ gold.
As it is now, all camps would be destroyed by Robin. Sheriff can strengthen a camp by subtracting exactly $c$ gold from each of its neighboring camps and use it to build better defenses for that camp. Strengthening a camp doesn't change its gold, only its neighbors' gold. A camp can have negative gold.
After Robin Hood's attack, all camps that have been strengthened survive the attack, all others are destroyed.
What's the maximum gold Sheriff can keep in his surviving camps after Robin Hood's attack if he strengthens his camps optimally?
Camp $a$ is neighboring camp $b$ if and only if there exists a trail connecting $a$ and $b$. Only strengthened camps count towards the answer, as others are destroyed.
Input: The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
Each test case begins with two integers $n$, $c$ ($1 \le n \le 2\cdot10^5, 1 \le c \le 10^9$) β the number of camps and the gold taken from each neighboring camp for strengthening.
The second line of each test case contains $n$ integers $a_1,a_2,\dots,a_n$ ($-10^9 \le a_i \le 10^9$) β the initial gold of each camp.
Then follow $n-1$ lines, each with integers $u$, $v$ ($1 \le u, v \le n$, $u \ne v$) β meaning that there is a trail between $u$ and $v$.
The sum of $n$ over all test cases doesn't exceed $2\cdot10^5$.
It is guaranteed that any camp is reachable from any other camp.
Output: Output a single integer, the maximum gold Sheriff of Nottingham can keep in his surviving camps after Robin Hood's attack.
Examples:
input:
5
3 1
2 3 1
1 2
2 3
3 1
3 6 3
1 2
2 3
3 1
-2 -3 -1
1 2
2 3
6 1
5 -4 3 6 7 3
4 1
5 1
3 5
3 6
1 2
8 1
3 5 2 7 8 5 -3 -4
7 3
1 8
4 3
3 5
7 6
8 7
2 1
output:
3
8
0
17
26
Note: In the first test case, it is optimal to strengthen the second base. The final gold at each base is $[1,3,0]$.
In the second test case, it is optimal to strengthen all bases. The final gold at each base is $[2,4,2]$.
In the third test case, it is optimal to not strengthen any base.
|
1971G
|
https://codeforces.com/problemset/problem/1971/G
|
XOUR
| 1,700
|
[
"data structures",
"dsu",
"sortings"
] |
Div. 4
| 2,000
| 256
|
You are given an array $a$ consisting of $n$ nonnegative integers.
You can swap the elements at positions $i$ and $j$ if $a_i~\mathsf{XOR}~a_j < 4$, where $\mathsf{XOR}$ is the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Find the lexicographically smallest array that can be made with any number of swaps.
An array $x$ is lexicographically smaller than an array $y$ if in the first position where $x$ and $y$ differ, $x_i < y_i$.
|
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2\cdot10^5$) β the length of the array.
The second line of each test case contains $n$ integers $a_i$ ($0 \leq a_i \leq 10^9$) β the elements of the 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 $n$ integers β the lexicographically smallest array that can be made with any number of swaps.
|
[
[
"4\n4\n1 0 3 2\n5\n2 7 1 5 6\n8\n1 2 1 2 1 2 1 2\n4\n16 4 1 64",
"0 1 2 3 \n1 5 2 6 7 \n1 1 1 1 2 2 2 2 \n16 4 1 64"
]
] |
For the first test case, you can swap any two elements, so we can produce the sorted array.
For the second test case, you can swap $2$ and $1$ (their $\mathsf{XOR}$ is $3$), $7$ and $5$ (their $\mathsf{XOR}$ is $2$), and $7$ and $6$ (their $\mathsf{XOR}$ is $1$) to get the lexicographically smallest array.
|
Title: XOUR
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given an array $a$ consisting of $n$ nonnegative integers.
You can swap the elements at positions $i$ and $j$ if $a_i~\mathsf{XOR}~a_j < 4$, where $\mathsf{XOR}$ is the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Find the lexicographically smallest array that can be made with any number of swaps.
An array $x$ is lexicographically smaller than an array $y$ if in the first position where $x$ and $y$ differ, $x_i < y_i$.
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$ ($1 \leq n \leq 2\cdot10^5$) β the length of the array.
The second line of each test case contains $n$ integers $a_i$ ($0 \leq a_i \leq 10^9$) β the elements of the 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 $n$ integers β the lexicographically smallest array that can be made with any number of swaps.
Examples:
input:
4
4
1 0 3 2
5
2 7 1 5 6
8
1 2 1 2 1 2 1 2
4
16 4 1 64
output:
0 1 2 3
1 5 2 6 7
1 1 1 1 2 2 2 2
16 4 1 64
Note: For the first test case, you can swap any two elements, so we can produce the sorted array.
For the second test case, you can swap $2$ and $1$ (their $\mathsf{XOR}$ is $3$), $7$ and $5$ (their $\mathsf{XOR}$ is $2$), and $7$ and $6$ (their $\mathsf{XOR}$ is $1$) to get the lexicographically smallest array.
|
1976F
|
https://codeforces.com/problemset/problem/1976/F
|
Remove Bridges
| 2,800
|
[
"data structures",
"dfs and similar",
"dp",
"greedy",
"sortings",
"trees"
] |
Div. 2
| 2,000
| 256
|
You are given a rooted tree, consisting of $n$ vertices, numbered from $1$ to $n$. Vertex $1$ is the root. Additionally, the root only has one child.
You are asked to add exactly $k$ edges to the tree (possibly, multiple edges and/or edges already existing in the tree).
Recall that a bridge is such an edge that, after you remove it, the number of connected components in the graph increases. So, initially, all edges of the tree are bridges.
After $k$ edges are added, some original edges of the tree are still bridges and some are not anymore. You want to satisfy two conditions:
* for every bridge, all tree edges in the subtree of the lower vertex of that bridge should also be bridges; * the number of bridges is as small as possible.
Solve the task for all values of $k$ from $1$ to $n - 1$ and output the smallest number of bridges.
|
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($2 \le n \le 3 \cdot 10^5$) β the number of vertices of the tree.
Each of the next $n - 1$ lines contain two integers $v$ and $u$ ($1 \le v, u \le n$) β the description of the edges of the tree. It's guaranteed that the given edges form a valid tree.
Additional constraint on the input: the root (vertex $1$) has exactly one child.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
|
For each testcase, print $n - 1$ integers. For each $k$ from $1$ to $n - 1$ print the smallest number of bridges that can be left after you add $k$ edges to the tree.
|
[
[
"4\n2\n1 2\n12\n4 10\n5 12\n12 11\n3 6\n9 6\n1 6\n12 7\n11 6\n2 11\n10 9\n10 8\n8\n1 2\n2 3\n2 4\n3 5\n3 6\n4 7\n4 8\n5\n1 2\n2 3\n3 4\n4 5",
"0 \n7 3 1 0 0 0 0 0 0 0 0 \n4 1 0 0 0 0 0 \n0 0 0 0"
]
] |
Title: Remove Bridges
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given a rooted tree, consisting of $n$ vertices, numbered from $1$ to $n$. Vertex $1$ is the root. Additionally, the root only has one child.
You are asked to add exactly $k$ edges to the tree (possibly, multiple edges and/or edges already existing in the tree).
Recall that a bridge is such an edge that, after you remove it, the number of connected components in the graph increases. So, initially, all edges of the tree are bridges.
After $k$ edges are added, some original edges of the tree are still bridges and some are not anymore. You want to satisfy two conditions:
* for every bridge, all tree edges in the subtree of the lower vertex of that bridge should also be bridges; * the number of bridges is as small as possible.
Solve the task for all values of $k$ from $1$ to $n - 1$ and output the smallest number of bridges.
Input: The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of testcases.
The first line of each testcase contains a single integer $n$ ($2 \le n \le 3 \cdot 10^5$) β the number of vertices of the tree.
Each of the next $n - 1$ lines contain two integers $v$ and $u$ ($1 \le v, u \le n$) β the description of the edges of the tree. It's guaranteed that the given edges form a valid tree.
Additional constraint on the input: the root (vertex $1$) has exactly one child.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
Output: For each testcase, print $n - 1$ integers. For each $k$ from $1$ to $n - 1$ print the smallest number of bridges that can be left after you add $k$ edges to the tree.
Examples:
input:
4
2
1 2
12
4 10
5 12
12 11
3 6
9 6
1 6
12 7
11 6
2 11
10 9
10 8
8
1 2
2 3
2 4
3 5
3 6
4 7
4 8
5
1 2
2 3
3 4
4 5
output:
0
7 3 1 0 0 0 0 0 0 0 0
4 1 0 0 0 0 0
0 0 0 0
Note:
|
|
2027E1
|
https://codeforces.com/problemset/problem/2027/E1
|
Bit Game (Easy Version)
| 2,800
|
[
"bitmasks",
"brute force",
"games",
"math"
] |
Div. 2
| 2,000
| 256
|
This is the easy version of this problem. The only difference is that you need to output the winner of the game in this version, and the number of stones in each pile are fixed. You must solve both versions to be able to hack.
Alice and Bob are playing a familiar game where they take turns removing stones from $n$ piles. Initially, there are $x_i$ stones in the $i$-th pile, and it has an associated value $a_i$. A player can take $d$ stones away from the $i$-th pile if and only if both of the following conditions are met:
* $1 \le d \le a_i$, and * $x \, \& \, d = d$, where $x$ is the current number of stones in the $i$-th pile and $\&$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
The player who cannot make a move loses, and Alice goes first.
You're given the $a_i$ and $x_i$ values for each pile, please determine who will win the game if both players play optimally.
|
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 $n$ ($1 \le n \le 10^4$) β the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i < 2^{30}$).
The third line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \le x_i < 2^{30}$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$.
|
Print a single line with the winner's name. If Alice wins, print "Alice", otherwise print "Bob" (without quotes).
|
[
[
"7\n2\n1 6\n10 7\n3\n10 8 15\n25 4 14\n4\n8 32 65 64\n7 45 126 94\n3\n20 40 1\n23 55 1\n5\n12345 9876 86419 8641 1\n6789 54321 7532 97532 1\n2\n20 64\n44 61\n3\n57 109 55\n69 90 85",
"Bob\nBob\nBob\nBob\nBob\nAlice\nAlice"
]
] |
In the first test case, neither player can take any stones from the first pile since there is no value of $d$ satisfying the conditions. For the second pile, to begin with, Alice can remove between $1$ and $6$ stones. No matter which move Alice performs, Bob can remove the rest of the stones on his turn. After Bob's move, there are no more moves that Alice can perform, so Bob wins.
In the second test case, here is one example of how the game might go. Alice moves first, and she decides to remove from the first pile. She cannot take $17$ stones, because $17 > 10$, which fails the first condition. She cannot take $10$ stones, because $25 \, \& \, 10 = 8$ which fails the second condition. One option is to take $9$ stones; now the pile has $16$ stones left. On Bob's turn he decides to take stones from the second pile; the only option here is to take all $4$. Now, no more stones can be taken from either of the first two piles, so Alice must take some stones from the last pile. She decides to take $12$ stones, and Bob then follows by taking the last $2$ stones on that pile. Since Alice now has no legal moves left, Bob wins. It can be shown that no matter which strategy Alice follows, Bob will always be able to win if he plays optimally.
|
Title: Bit Game (Easy Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: This is the easy version of this problem. The only difference is that you need to output the winner of the game in this version, and the number of stones in each pile are fixed. You must solve both versions to be able to hack.
Alice and Bob are playing a familiar game where they take turns removing stones from $n$ piles. Initially, there are $x_i$ stones in the $i$-th pile, and it has an associated value $a_i$. A player can take $d$ stones away from the $i$-th pile if and only if both of the following conditions are met:
* $1 \le d \le a_i$, and * $x \, \& \, d = d$, where $x$ is the current number of stones in the $i$-th pile and $\&$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
The player who cannot make a move loses, and Alice goes first.
You're given the $a_i$ and $x_i$ values for each pile, please determine who will win the game 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 \le 1000$). The description of the test cases follows.
The first line of each test case contains $n$ ($1 \le n \le 10^4$) β the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i < 2^{30}$).
The third line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \le x_i < 2^{30}$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$.
Output: Print a single line with the winner's name. If Alice wins, print "Alice", otherwise print "Bob" (without quotes).
Examples:
input:
7
2
1 6
10 7
3
10 8 15
25 4 14
4
8 32 65 64
7 45 126 94
3
20 40 1
23 55 1
5
12345 9876 86419 8641 1
6789 54321 7532 97532 1
2
20 64
44 61
3
57 109 55
69 90 85
output:
Bob
Bob
Bob
Bob
Bob
Alice
Alice
Note: In the first test case, neither player can take any stones from the first pile since there is no value of $d$ satisfying the conditions. For the second pile, to begin with, Alice can remove between $1$ and $6$ stones. No matter which move Alice performs, Bob can remove the rest of the stones on his turn. After Bob's move, there are no more moves that Alice can perform, so Bob wins.
In the second test case, here is one example of how the game might go. Alice moves first, and she decides to remove from the first pile. She cannot take $17$ stones, because $17 > 10$, which fails the first condition. She cannot take $10$ stones, because $25 \, \& \, 10 = 8$ which fails the second condition. One option is to take $9$ stones; now the pile has $16$ stones left. On Bob's turn he decides to take stones from the second pile; the only option here is to take all $4$. Now, no more stones can be taken from either of the first two piles, so Alice must take some stones from the last pile. She decides to take $12$ stones, and Bob then follows by taking the last $2$ stones on that pile. Since Alice now has no legal moves left, Bob wins. It can be shown that no matter which strategy Alice follows, Bob will always be able to win if he plays optimally.
|
2029C
|
https://codeforces.com/problemset/problem/2029/C
|
New Rating
| 1,700
|
[
"binary search",
"data structures",
"dp",
"greedy"
] |
Div. 1 + 2
| 2,000
| 512
|
Hello, Codeforces Forcescode!
Kevin used to be a participant of Codeforces. Recently, the KDOI Team has developed a new Online Judge called Forcescode.
Kevin has participated in $n$ contests on Forcescode. In the $i$-th contest, his performance rating is $a_i$.
Now he has hacked into the backend of Forcescode and will select an interval $[l,r]$ ($1\le l\le r\le n$), then skip all of the contests in this interval. After that, his rating will be recalculated in the following way:
* Initially, his rating is $x=0$; * For each $1\le i\le n$, after the $i$-th contest, * If $l\le i\le r$, this contest will be skipped, and the rating will remain unchanged; * Otherwise, his rating will be updated according to the following rules: * If $a_i>x$, his rating $x$ will increase by $1$; * If $a_i=x$, his rating $x$ will remain unchanged; * If $a_i<x$, his rating $x$ will decrease by $1$.
You have to help Kevin to find his maximum possible rating after the recalculation if he chooses the interval $[l,r]$ optimally. Note that Kevin has to skip at least one contest.
|
Each test contains multiple test cases. The first line of the input contains a single integer $t$ ($1\le t\le 5\cdot 10^4$) β the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ ($1\le n\le 3\cdot 10^5$) β the number of contests.
The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le n$) β the performance ratings in the contests.
It is guaranteed that 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 possible rating after the recalculation if Kevin chooses the interval optimally.
|
[
[
"5\n6\n1 2 3 4 5 6\n7\n1 2 1 1 1 3 4\n1\n1\n9\n9 9 8 2 4 4 3 5 3\n10\n1 2 3 4 1 3 2 1 1 10",
"5\n4\n0\n4\n5"
]
] |
In the first test case, Kevin must skip at least one contest. If he chooses any interval of length $1$, his rating after the recalculation will be equal to $5$.
In the second test case, Kevin's optimal choice is to select the interval $[3,5]$. During the recalculation, his rating changes as follows:
$$ 0 \xrightarrow{a_1=1} 1 \xrightarrow{a_2=2} 2 \xrightarrow{\mathtt{skip}} 2 \xrightarrow{\mathtt{skip}} 2 \xrightarrow{\mathtt{skip}} 2 \xrightarrow{a_6=3} 3 \xrightarrow{a_7=4} 4 $$
In the third test case, Kevin must skip the only contest, so his rating will remain at the initial value of $0$.
In the fourth test case, Kevin's optimal choice is to select the interval $[7,9]$. During the recalculation, his rating changes as follows:
$$ 0 \xrightarrow{a_1=9} 1 \xrightarrow{a_2=9} 2 \xrightarrow{a_3=8} 3 \xrightarrow{a_4=2} 2 \xrightarrow{a_5=4} 3 \xrightarrow{a_6=4} 4 \xrightarrow{\mathtt{skip}} 4 \xrightarrow{\mathtt{skip}} 4 \xrightarrow{\mathtt{skip}} 4 $$
In the fifth test case, Kevin's optimal choice is to select the interval $[5,9]$.
|
Title: New Rating
time_limit_ms: 2000
memory_limit_mb: 512
Description: Hello, Codeforces Forcescode!
Kevin used to be a participant of Codeforces. Recently, the KDOI Team has developed a new Online Judge called Forcescode.
Kevin has participated in $n$ contests on Forcescode. In the $i$-th contest, his performance rating is $a_i$.
Now he has hacked into the backend of Forcescode and will select an interval $[l,r]$ ($1\le l\le r\le n$), then skip all of the contests in this interval. After that, his rating will be recalculated in the following way:
* Initially, his rating is $x=0$; * For each $1\le i\le n$, after the $i$-th contest, * If $l\le i\le r$, this contest will be skipped, and the rating will remain unchanged; * Otherwise, his rating will be updated according to the following rules: * If $a_i>x$, his rating $x$ will increase by $1$; * If $a_i=x$, his rating $x$ will remain unchanged; * If $a_i<x$, his rating $x$ will decrease by $1$.
You have to help Kevin to find his maximum possible rating after the recalculation if he chooses the interval $[l,r]$ optimally. Note that Kevin has to skip at least one contest.
Input: Each test contains multiple test cases. The first line of the input contains a single integer $t$ ($1\le t\le 5\cdot 10^4$) β the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ ($1\le n\le 3\cdot 10^5$) β the number of contests.
The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le n$) β the performance ratings in the contests.
It is guaranteed that 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 possible rating after the recalculation if Kevin chooses the interval optimally.
Examples:
input:
5
6
1 2 3 4 5 6
7
1 2 1 1 1 3 4
1
1
9
9 9 8 2 4 4 3 5 3
10
1 2 3 4 1 3 2 1 1 10
output:
5
4
0
4
5
Note: In the first test case, Kevin must skip at least one contest. If he chooses any interval of length $1$, his rating after the recalculation will be equal to $5$.
In the second test case, Kevin's optimal choice is to select the interval $[3,5]$. During the recalculation, his rating changes as follows:
$$ 0 \xrightarrow{a_1=1} 1 \xrightarrow{a_2=2} 2 \xrightarrow{\mathtt{skip}} 2 \xrightarrow{\mathtt{skip}} 2 \xrightarrow{\mathtt{skip}} 2 \xrightarrow{a_6=3} 3 \xrightarrow{a_7=4} 4 $$
In the third test case, Kevin must skip the only contest, so his rating will remain at the initial value of $0$.
In the fourth test case, Kevin's optimal choice is to select the interval $[7,9]$. During the recalculation, his rating changes as follows:
$$ 0 \xrightarrow{a_1=9} 1 \xrightarrow{a_2=9} 2 \xrightarrow{a_3=8} 3 \xrightarrow{a_4=2} 2 \xrightarrow{a_5=4} 3 \xrightarrow{a_6=4} 4 \xrightarrow{\mathtt{skip}} 4 \xrightarrow{\mathtt{skip}} 4 \xrightarrow{\mathtt{skip}} 4 $$
In the fifth test case, Kevin's optimal choice is to select the interval $[5,9]$.
|
1991F
|
https://codeforces.com/problemset/problem/1991/F
|
Triangle Formation
| 2,200
|
[
"brute force",
"greedy",
"implementation",
"math",
"sortings"
] |
Div. 1 + 2
| 5,000
| 256
|
You are given $n$ sticks, numbered from $1$ to $n$. The length of the $i$-th stick is $a_i$.
You need to answer $q$ queries. In each query, you are given two integers $l$ and $r$ ($1 \le l < r \le n$, $r - l + 1 \ge 6$). Determine whether it is possible to choose $6$ distinct sticks from the sticks numbered $l$ to $r$, to form $2$ non-degenerate triangles$^{\text{β}}$.
$^{\text{β}}$A triangle with side lengths $a$, $b$, and $c$ is called non-degenerate if:
* $a < b + c$, * $b < a + c$, and * $c < a + b$.
|
The first line contains two integers $n$ and $q$ ($6 \le n \le 10^5$, $1 \le q \le 10^5$) β the number of sticks and the number of queries respectively.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β $a_i$ denotes the length of the $i$-th stick.
Each of the following $q$ lines contains two integers $l$ and $r$ ($1 \le l < r \le n$, $r - l + 1 \ge 6$) β the parameters of each query.
|
For each query, output "YES" (without quotes) if it is possible to form $2$ triangles, and "NO" (without quotes) otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
|
[
[
"10 5\n5 2 2 10 4 10 6 1 5 3\n1 6\n2 7\n2 8\n5 10\n4 10",
"YES\nNO\nYES\nNO\nYES"
]
] |
In the first query, the lengths of the sticks are $[5, 2, 2, 10, 4, 10]$. Two sets of sticks $[2, 4, 5]$ and $[2, 10, 10]$ can be selected to form $2$ non-degenerate triangles.
In the second query, the lengths of the sticks are $[2, 2, 10, 4, 10, 6]$. It can be shown that it is impossible to form $2$ non-degenerate triangles.
In the third query, the lengths of the sticks are $[2, 2, 10, 4, 10, 6, 1]$. Two sets of sticks $[1, 2, 2]$ and $[4, 10, 10]$ can be selected to form $2$ non-degenerate triangles.
In the fourth query, the lengths of the sticks are $[4, 10, 6, 1, 5, 3]$. It can be shown that it is impossible to form $2$ non-degenerate triangles.
In the fifth query, the lengths of the sticks are $[10, 4, 10, 6, 1, 5, 3]$. Two sets of sticks $[1, 10, 10]$ and $[3, 4, 5]$ can be selected to form $2$ non-degenerate triangles.
|
Title: Triangle Formation
time_limit_ms: 5000
memory_limit_mb: 256
Description: You are given $n$ sticks, numbered from $1$ to $n$. The length of the $i$-th stick is $a_i$.
You need to answer $q$ queries. In each query, you are given two integers $l$ and $r$ ($1 \le l < r \le n$, $r - l + 1 \ge 6$). Determine whether it is possible to choose $6$ distinct sticks from the sticks numbered $l$ to $r$, to form $2$ non-degenerate triangles$^{\text{β}}$.
$^{\text{β}}$A triangle with side lengths $a$, $b$, and $c$ is called non-degenerate if:
* $a < b + c$, * $b < a + c$, and * $c < a + b$.
Input: The first line contains two integers $n$ and $q$ ($6 \le n \le 10^5$, $1 \le q \le 10^5$) β the number of sticks and the number of queries respectively.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β $a_i$ denotes the length of the $i$-th stick.
Each of the following $q$ lines contains two integers $l$ and $r$ ($1 \le l < r \le n$, $r - l + 1 \ge 6$) β the parameters of each query.
Output: For each query, output "YES" (without quotes) if it is possible to form $2$ triangles, and "NO" (without quotes) otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
Examples:
input:
10 5
5 2 2 10 4 10 6 1 5 3
1 6
2 7
2 8
5 10
4 10
output:
YES
NO
YES
NO
YES
Note: In the first query, the lengths of the sticks are $[5, 2, 2, 10, 4, 10]$. Two sets of sticks $[2, 4, 5]$ and $[2, 10, 10]$ can be selected to form $2$ non-degenerate triangles.
In the second query, the lengths of the sticks are $[2, 2, 10, 4, 10, 6]$. It can be shown that it is impossible to form $2$ non-degenerate triangles.
In the third query, the lengths of the sticks are $[2, 2, 10, 4, 10, 6, 1]$. Two sets of sticks $[1, 2, 2]$ and $[4, 10, 10]$ can be selected to form $2$ non-degenerate triangles.
In the fourth query, the lengths of the sticks are $[4, 10, 6, 1, 5, 3]$. It can be shown that it is impossible to form $2$ non-degenerate triangles.
In the fifth query, the lengths of the sticks are $[10, 4, 10, 6, 1, 5, 3]$. Two sets of sticks $[1, 10, 10]$ and $[3, 4, 5]$ can be selected to form $2$ non-degenerate triangles.
|
1984B
|
https://codeforces.com/problemset/problem/1984/B
|
Large Addition
| 1,100
|
[
"implementation",
"math"
] |
Div. 1 + 2
| 1,000
| 256
|
A digit is large if it is between $5$ and $9$, inclusive. A positive integer is large if all of its digits are large.
You are given an integer $x$. Can it be the sum of two large positive integers with the same number of digits?
|
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The only line of each test case contains a single integer $x$ ($10 \leq x \leq 10^{18}$).
|
For each test case, output $\texttt{YES}$ if $x$ satisfies the condition, and $\texttt{NO}$ otherwise.
You can output $\texttt{YES}$ and $\texttt{NO}$ in any case (for example, strings $\texttt{yES}$, $\texttt{yes}$, and $\texttt{Yes}$ will be recognized as a positive response).
|
[
[
"11\n1337\n200\n1393938\n1434\n98765432123456789\n11111111111111111\n420\n1984\n10\n69\n119",
"YES\nNO\nYES\nYES\nNO\nYES\nNO\nYES\nYES\nNO\nNO"
]
] |
In the first test case, we can have $658 + 679 = 1337$.
In the second test case, it can be shown that no numbers of equal length and only consisting of large digits can add to $200$.
In the third test case, we can have $696\,969 + 696\,969 = 1\,393\,938$.
In the fourth test case, we can have $777 + 657 = 1434$.
|
Title: Large Addition
time_limit_ms: 1000
memory_limit_mb: 256
Description: A digit is large if it is between $5$ and $9$, inclusive. A positive integer is large if all of its digits are large.
You are given an integer $x$. Can it be the sum of two large positive integers with the same number of digits?
Input: The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The only line of each test case contains a single integer $x$ ($10 \leq x \leq 10^{18}$).
Output: For each test case, output $\texttt{YES}$ if $x$ satisfies the condition, and $\texttt{NO}$ otherwise.
You can output $\texttt{YES}$ and $\texttt{NO}$ in any case (for example, strings $\texttt{yES}$, $\texttt{yes}$, and $\texttt{Yes}$ will be recognized as a positive response).
Examples:
input:
11
1337
200
1393938
1434
98765432123456789
11111111111111111
420
1984
10
69
119
output:
YES
NO
YES
YES
NO
YES
NO
YES
YES
NO
NO
Note: In the first test case, we can have $658 + 679 = 1337$.
In the second test case, it can be shown that no numbers of equal length and only consisting of large digits can add to $200$.
In the third test case, we can have $696\,969 + 696\,969 = 1\,393\,938$.
In the fourth test case, we can have $777 + 657 = 1434$.
|
1997B
|
https://codeforces.com/problemset/problem/1997/B
|
Make Three Regions
| 1,100
|
[
"constructive algorithms",
"two pointers"
] |
Div. 2
| 2,000
| 256
|
There is a grid, consisting of $2$ rows and $n$ columns. Each cell of the grid is either free or blocked.
A free cell $y$ is reachable from a free cell $x$ if at least one of these conditions holds:
* $x$ and $y$ share a side; * there exists a free cell $z$ such that $z$ is reachable from $x$ and $y$ is reachable from $z$.
A connected region is a set of free cells of the grid such that all cells in it are reachable from one another, but adding any other free cell to the set violates this rule.
For example, consider the following layout, where white cells are free, and dark grey cells are blocked:

There are $3$ regions in it, denoted with red, green and blue color respectively:

The given grid contains at most $1$ connected region. Your task is to calculate the number of free cells meeting the following constraint:
* if this cell is blocked, the number of connected regions becomes exactly $3$.
|
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 columns.
The $i$-th of the next two lines contains a description of the $i$-th row of the grid β the string $s_i$, consisting of $n$ characters. Each character is either . (denoting a free cell) or x (denoting a blocked cell).
Additional constraint on the input:
* the given grid contains at most $1$ connected region; * the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
|
For each test case, print a single integer β the number of cells such that the number of connected regions becomes $3$ if this cell is blocked.
|
[
[
"4\n8\n.......x\n.x.xx...\n2\n..\n..\n3\nxxx\nxxx\n9\n..x.x.x.x\nx.......x",
"1\n0\n0\n2"
]
] |
In the first test case, if the cell $(1, 3)$ is blocked, the number of connected regions becomes $3$ (as shown in the picture from the statement).
|
Title: Make Three Regions
time_limit_ms: 2000
memory_limit_mb: 256
Description: There is a grid, consisting of $2$ rows and $n$ columns. Each cell of the grid is either free or blocked.
A free cell $y$ is reachable from a free cell $x$ if at least one of these conditions holds:
* $x$ and $y$ share a side; * there exists a free cell $z$ such that $z$ is reachable from $x$ and $y$ is reachable from $z$.
A connected region is a set of free cells of the grid such that all cells in it are reachable from one another, but adding any other free cell to the set violates this rule.
For example, consider the following layout, where white cells are free, and dark grey cells are blocked:

There are $3$ regions in it, denoted with red, green and blue color respectively:

The given grid contains at most $1$ connected region. Your task is to calculate the number of free cells meeting the following constraint:
* if this cell is blocked, the number of connected regions becomes exactly $3$.
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 columns.
The $i$-th of the next two lines contains a description of the $i$-th row of the grid β the string $s_i$, consisting of $n$ characters. Each character is either . (denoting a free cell) or x (denoting a blocked cell).
Additional constraint on the input:
* the given grid contains at most $1$ connected region; * the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
Output: For each test case, print a single integer β the number of cells such that the number of connected regions becomes $3$ if this cell is blocked.
Examples:
input:
4
8
.......x
.x.xx...
2
..
..
3
xxx
xxx
9
..x.x.x.x
x.......x
output:
1
0
0
2
Note: In the first test case, if the cell $(1, 3)$ is blocked, the number of connected regions becomes $3$ (as shown in the picture from the statement).
|
1993B
|
https://codeforces.com/problemset/problem/1993/B
|
Parity and Sum
| 1,100
|
[
"constructive algorithms",
"greedy"
] |
Div. 2
| 1,000
| 256
|
Given an array $a$ of $n$ positive integers.
In one operation, you can pick any pair of indexes $(i, j)$ such that $a_i$ and $a_j$ have distinct parity, then replace the smaller one with the sum of them. More formally:
* If $a_i < a_j$, replace $a_i$ with $a_i + a_j$; * Otherwise, replace $a_j$ with $a_i + a_j$.
Find the minimum number of operations needed to make all elements of the array have the same parity.
|
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 second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β the elements of 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.
|
[
[
"7\n5\n1 3 5 7 9\n4\n4 4 4 4\n3\n2 3 4\n4\n3 2 2 8\n6\n4 3 6 1 2 1\n6\n3 6 1 2 1 2\n5\n999999996 999999997 999999998 999999999 1000000000",
"0\n0\n2\n4\n3\n3\n3"
]
] |
In the first test case, all integers already have the same parity. Therefore, no operation is needed.
In the third test case, we can perform two operations $(1, 2)$ and $(1, 3)$. The array $a$ transforms as follows: $a = [\color{red}2, \color{red}3, 4] \longrightarrow [\color{red}5, 3, \color{red}4] \longrightarrow [5, 3, 9]$.
In the fourth test case, an example of an optimal sequence of operations is $(1, 2)$, $(1, 3)$, $(1, 4)$, and $(1, 4)$. The array $a$ transforms as follows: $a = [\color{red}3, \color{red}2, 2, 8] \longrightarrow [\color{red}3, 5, \color{red}2, 8] \longrightarrow [\color{red}3, 5, 5, \color{red}8] \longrightarrow [\color{red}{11}, 5, 5, \color{red}8] \longrightarrow [11, 5, 5, 19]$.
|
Title: Parity and Sum
time_limit_ms: 1000
memory_limit_mb: 256
Description: Given an array $a$ of $n$ positive integers.
In one operation, you can pick any pair of indexes $(i, j)$ such that $a_i$ and $a_j$ have distinct parity, then replace the smaller one with the sum of them. More formally:
* If $a_i < a_j$, replace $a_i$ with $a_i + a_j$; * Otherwise, replace $a_j$ with $a_i + a_j$.
Find the minimum number of operations needed to make all elements of the array have the same parity.
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 second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β the elements of 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:
7
5
1 3 5 7 9
4
4 4 4 4
3
2 3 4
4
3 2 2 8
6
4 3 6 1 2 1
6
3 6 1 2 1 2
5
999999996 999999997 999999998 999999999 1000000000
output:
0
0
2
4
3
3
3
Note: In the first test case, all integers already have the same parity. Therefore, no operation is needed.
In the third test case, we can perform two operations $(1, 2)$ and $(1, 3)$. The array $a$ transforms as follows: $a = [\color{red}2, \color{red}3, 4] \longrightarrow [\color{red}5, 3, \color{red}4] \longrightarrow [5, 3, 9]$.
In the fourth test case, an example of an optimal sequence of operations is $(1, 2)$, $(1, 3)$, $(1, 4)$, and $(1, 4)$. The array $a$ transforms as follows: $a = [\color{red}3, \color{red}2, 2, 8] \longrightarrow [\color{red}3, 5, \color{red}2, 8] \longrightarrow [\color{red}3, 5, 5, \color{red}8] \longrightarrow [\color{red}{11}, 5, 5, \color{red}8] \longrightarrow [11, 5, 5, 19]$.
|
2014D
|
https://codeforces.com/problemset/problem/2014/D
|
Robert Hood and Mrs Hood
| 1,400
|
[
"brute force",
"data structures",
"greedy",
"sortings"
] |
Div. 3
| 2,000
| 256
|
Impress thy brother, yet fret not thy mother.
Robin's brother and mother are visiting, and Robin gets to choose the start day for each visitor.
All days are numbered from $1$ to $n$. Visitors stay for $d$ continuous days, all of those $d$ days must be between day $1$ and $n$ inclusive.
Robin has a total of $k$ risky 'jobs' planned. The $i$-th job takes place between days $l_i$ and $r_i$ inclusive, for $1 \le i \le k$. If a job takes place on any of the $d$ days, the visit overlaps with this job (the length of overlap is unimportant).
Robin wants his brother's visit to overlap with the maximum number of distinct jobs, and his mother's the minimum.
Find suitable start days for the visits of Robin's brother and mother. If there are multiple suitable days, choose the earliest one.
|
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$, $d$, $k$ ($1 \le n \le 10^5, 1 \le d, k \le n$) β the number of total days, duration of the visits, and the number of jobs.
Then follow $k$ lines of each test case, each with two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β the start and end day of each job.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output two integers, the best starting days of Robin's brother and mother respectively. Both visits must fit between day $1$ and $n$ inclusive.
|
[
[
"6\n2 1 1\n1 2\n4 1 2\n1 2\n2 4\n7 2 3\n1 2\n1 3\n6 7\n5 1 2\n1 2\n3 5\n9 2 1\n2 8\n9 2 4\n7 9\n4 8\n1 3\n2 3",
"1 1\n2 1\n1 4\n1 1\n1 1\n3 4"
]
] |
In the first test case, the only job fills all $2$ days, both should visit on day $1$.
In the second test case, day $2$ overlaps with $2$ jobs and day $1$ overlaps with only $1$.
In the third test case, Robert visits for days $[1,2]$, Mrs. Hood visits for days $[4,5]$.
|
Title: Robert Hood and Mrs Hood
time_limit_ms: 2000
memory_limit_mb: 256
Description: Impress thy brother, yet fret not thy mother.
Robin's brother and mother are visiting, and Robin gets to choose the start day for each visitor.
All days are numbered from $1$ to $n$. Visitors stay for $d$ continuous days, all of those $d$ days must be between day $1$ and $n$ inclusive.
Robin has a total of $k$ risky 'jobs' planned. The $i$-th job takes place between days $l_i$ and $r_i$ inclusive, for $1 \le i \le k$. If a job takes place on any of the $d$ days, the visit overlaps with this job (the length of overlap is unimportant).
Robin wants his brother's visit to overlap with the maximum number of distinct jobs, and his mother's the minimum.
Find suitable start days for the visits of Robin's brother and mother. If there are multiple suitable days, choose the earliest one.
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$, $d$, $k$ ($1 \le n \le 10^5, 1 \le d, k \le n$) β the number of total days, duration of the visits, and the number of jobs.
Then follow $k$ lines of each test case, each with two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β the start and end day of each job.
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 two integers, the best starting days of Robin's brother and mother respectively. Both visits must fit between day $1$ and $n$ inclusive.
Examples:
input:
6
2 1 1
1 2
4 1 2
1 2
2 4
7 2 3
1 2
1 3
6 7
5 1 2
1 2
3 5
9 2 1
2 8
9 2 4
7 9
4 8
1 3
2 3
output:
1 1
2 1
1 4
1 1
1 1
3 4
Note: In the first test case, the only job fills all $2$ days, both should visit on day $1$.
In the second test case, day $2$ overlaps with $2$ jobs and day $1$ overlaps with only $1$.
In the third test case, Robert visits for days $[1,2]$, Mrs. Hood visits for days $[4,5]$.
|
1979A
|
https://codeforces.com/problemset/problem/1979/A
|
Guess the Maximum
| 800
|
[
"brute force",
"greedy",
"implementation"
] |
Div. 2
| 1,000
| 256
|
Alice and Bob came up with a rather strange game. They have an array of integers $a_1, a_2,\ldots, a_n$. Alice chooses a certain integer $k$ and tells it to Bob, then the following happens:
* Bob chooses two integers $i$ and $j$ ($1 \le i < j \le n$), and then finds the maximum among the integers $a_i, a_{i + 1},\ldots, a_j$; * If the obtained maximum is strictly greater than $k$, Alice wins, otherwise Bob wins.
Help Alice find the maximum $k$ at which she is guaranteed to win.
|
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$ ($2 \le n \le 5 \cdot 10^4$) β the number of elements in 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 elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^4$.
|
For each test case, output one integer β the maximum integer $k$ at which Alice is guaranteed to win.
|
[
[
"6\n4\n2 4 1 7\n5\n1 2 3 4 5\n2\n1 1\n3\n37 8 16\n5\n10 10 10 10 9\n10\n3 12 9 5 2 3 2 9 8 2",
"3\n1\n0\n15\n9\n2"
]
] |
In the first test case, all possible subsegments that Bob can choose look as follows: $[2, 4], [2, 4, 1], [2, 4, 1, 7], [4, 1], [4, 1, 7], [1, 7]$. The maximums on the subsegments are respectively equal to $4, 4, 7, 4, 7, 7$. It can be shown that $3$ is the largest integer such that any of the maximums will be strictly greater than it.
In the third test case, the only segment that Bob can choose is $[1, 1]$. So the answer is $0$.
|
Title: Guess the Maximum
time_limit_ms: 1000
memory_limit_mb: 256
Description: Alice and Bob came up with a rather strange game. They have an array of integers $a_1, a_2,\ldots, a_n$. Alice chooses a certain integer $k$ and tells it to Bob, then the following happens:
* Bob chooses two integers $i$ and $j$ ($1 \le i < j \le n$), and then finds the maximum among the integers $a_i, a_{i + 1},\ldots, a_j$; * If the obtained maximum is strictly greater than $k$, Alice wins, otherwise Bob wins.
Help Alice find the maximum $k$ at which she is guaranteed to win.
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$ ($2 \le n \le 5 \cdot 10^4$) β the number of elements in 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 elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^4$.
Output: For each test case, output one integer β the maximum integer $k$ at which Alice is guaranteed to win.
Examples:
input:
6
4
2 4 1 7
5
1 2 3 4 5
2
1 1
3
37 8 16
5
10 10 10 10 9
10
3 12 9 5 2 3 2 9 8 2
output:
3
1
0
15
9
2
Note: In the first test case, all possible subsegments that Bob can choose look as follows: $[2, 4], [2, 4, 1], [2, 4, 1, 7], [4, 1], [4, 1, 7], [1, 7]$. The maximums on the subsegments are respectively equal to $4, 4, 7, 4, 7, 7$. It can be shown that $3$ is the largest integer such that any of the maximums will be strictly greater than it.
In the third test case, the only segment that Bob can choose is $[1, 1]$. So the answer is $0$.
|
1976B
|
https://codeforces.com/problemset/problem/1976/B
|
Increase/Decrease/Copy
| 1,100
|
[
"greedy",
"implementation"
] |
Div. 2
| 2,000
| 256
|
You are given two integer arrays: array $a$ of length $n$ and array $b$ of length $n+1$.
You can perform the following operations any number of times in any order:
* choose any element of the array $a$ and increase it by $1$; * choose any element of the array $a$ and decrease it by $1$; * choose any element of the array $a$, copy it and append the copy to the end of the array $a$.
Your task is to calculate the minimum number of aforementioned operations (possibly zero) required to transform the array $a$ into the array $b$. It can be shown that under the constraints of the problem, it is always possible.
|
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 a single integer $n$ ($1 \le n \le 2 \cdot 10^5$); * the second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$); * the third line contains $n + 1$ integers $b_1, b_2, \dots, b_{n + 1}$ ($1 \le b_i \le 10^9$).
Additional constraint on the input: the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
|
For each test case, print a single integer β the minimum number of operations (possibly zero) required to transform the array $a$ into the array $b$.
|
[
[
"3\n1\n2\n1 3\n2\n3 3\n3 3 3\n4\n4 2 1 2\n2 1 5 2 3",
"3\n1\n8"
]
] |
In the first example, you can transform $a$ into $b$ as follows: $[2] \rightarrow [2, 2] \rightarrow [1, 2] \rightarrow [1, 3]$.
|
Title: Increase/Decrease/Copy
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given two integer arrays: array $a$ of length $n$ and array $b$ of length $n+1$.
You can perform the following operations any number of times in any order:
* choose any element of the array $a$ and increase it by $1$; * choose any element of the array $a$ and decrease it by $1$; * choose any element of the array $a$, copy it and append the copy to the end of the array $a$.
Your task is to calculate the minimum number of aforementioned operations (possibly zero) required to transform the array $a$ into the array $b$. It can be shown that under the constraints of the problem, it is always possible.
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 a single integer $n$ ($1 \le n \le 2 \cdot 10^5$); * the second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$); * the third line contains $n + 1$ integers $b_1, b_2, \dots, b_{n + 1}$ ($1 \le b_i \le 10^9$).
Additional constraint on the input: the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
Output: For each test case, print a single integer β the minimum number of operations (possibly zero) required to transform the array $a$ into the array $b$.
Examples:
input:
3
1
2
1 3
2
3 3
3 3 3
4
4 2 1 2
2 1 5 2 3
output:
3
1
8
Note: In the first example, you can transform $a$ into $b$ as follows: $[2] \rightarrow [2, 2] \rightarrow [1, 2] \rightarrow [1, 3]$.
|
2027A
|
https://codeforces.com/problemset/problem/2027/A
|
Rectangle Arrangement
| 800
|
[
"geometry",
"implementation",
"math"
] |
Div. 2
| 1,000
| 256
|
You are coloring an infinite square grid, in which all cells are initially white. To do this, you are given $n$ stamps. Each stamp is a rectangle of width $w_i$ and height $h_i$.
You will use each stamp exactly once to color a rectangle of the same size as the stamp on the grid in black. You cannot rotate the stamp, and for each cell, the stamp must either cover it fully or not cover it at all. You can use the stamp at any position on the grid, even if some or all of the cells covered by the stamping area are already black.
What is the minimum sum of the perimeters of the connected regions of black squares you can obtain after all the stamps have been used?
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 100$).
The $i$-th of the next $n$ lines contains two integers $w_i$ and $h_i$ ($1 \le w_i, h_i \le 100$).
|
For each test case, output a single integer β the minimum sum of the perimeters of the connected regions of black squares you can obtain after all the stamps have been used.
|
[
[
"5\n5\n1 5\n2 4\n3 3\n4 2\n5 1\n3\n2 2\n1 1\n1 2\n1\n3 2\n3\n100 100\n100 100\n100 100\n4\n1 4\n2 3\n1 5\n3 2",
"20\n8\n10\n400\n16"
]
] |
In the first test case, the stamps can be used as shown on the left. Each stamp is highlighted in its own color for clarity.

After all these stamps are used, there is one black region (as shown on the right), and its perimeter is $20$. It can be shown that there is no way of using the stamps that yields a lower total perimeter.
In the second test case, the second and third stamps can be used entirely inside the first one, so the minimum perimeter is equal to $8$.
|
Title: Rectangle Arrangement
time_limit_ms: 1000
memory_limit_mb: 256
Description: You are coloring an infinite square grid, in which all cells are initially white. To do this, you are given $n$ stamps. Each stamp is a rectangle of width $w_i$ and height $h_i$.
You will use each stamp exactly once to color a rectangle of the same size as the stamp on the grid in black. You cannot rotate the stamp, and for each cell, the stamp must either cover it fully or not cover it at all. You can use the stamp at any position on the grid, even if some or all of the cells covered by the stamping area are already black.
What is the minimum sum of the perimeters of the connected regions of black squares you can obtain after all the stamps have been used?
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 100$).
The $i$-th of the next $n$ lines contains two integers $w_i$ and $h_i$ ($1 \le w_i, h_i \le 100$).
Output: For each test case, output a single integer β the minimum sum of the perimeters of the connected regions of black squares you can obtain after all the stamps have been used.
Examples:
input:
5
5
1 5
2 4
3 3
4 2
5 1
3
2 2
1 1
1 2
1
3 2
3
100 100
100 100
100 100
4
1 4
2 3
1 5
3 2
output:
20
8
10
400
16
Note: In the first test case, the stamps can be used as shown on the left. Each stamp is highlighted in its own color for clarity.

After all these stamps are used, there is one black region (as shown on the right), and its perimeter is $20$. It can be shown that there is no way of using the stamps that yields a lower total perimeter.
In the second test case, the second and third stamps can be used entirely inside the first one, so the minimum perimeter is equal to $8$.
|
2020F
|
https://codeforces.com/problemset/problem/2020/F
|
Count Leaves
| 2,900
|
[
"dp",
"math",
"number theory"
] |
Div. 2
| 4,000
| 256
|
Let $n$ and $d$ be positive integers. We build the the divisor tree $T_{n,d}$ as follows:
* The root of the tree is a node marked with number $n$. This is the $0$-th layer of the tree. * For each $i$ from $0$ to $d - 1$, for each vertex of the $i$-th layer, do the following. If the current vertex is marked with $x$, create its children and mark them with all possible distinct divisors$^\dagger$ of $x$. These children will be in the $(i+1)$-st layer. * The vertices on the $d$-th layer are the leaves of the tree.
For example, $T_{6,2}$ (the divisor tree for $n = 6$ and $d = 2$) looks like this:

Define $f(n,d)$ as the number of leaves in $T_{n,d}$.
Given integers $n$, $k$, and $d$, please compute $\sum\limits_{i=1}^{n} f(i^k,d)$, modulo $10^9+7$.
$^\dagger$ In this problem, we say that an integer $y$ is a divisor of $x$ if $y \ge 1$ and there exists an integer $z$ such that $x = y \cdot z$.
|
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 only line of each test case contains three integers $n$, $k$, and $d$ ($1 \le n \le 10^9$, $1 \le k,d \le 10^5$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^9$.
|
For each test case, output $\sum\limits_{i=1}^{n} f(i^k,d)$, modulo $10^9+7$.
|
[
[
"3\n6 1 1\n1 3 3\n10 1 2",
"14\n1\n53"
]
] |
In the first test case, $n = 6$, $k = 1$, and $d = 1$. Thus, we need to find the total number of leaves in the divisor trees $T_{1,1}$, $T_{2,1}$, $T_{3,1}$, $T_{4,1}$, $T_{5,1}$, $T_{6,1}$.
* $T_{1,1}$ has only one leaf, which is marked with $1$. * $T_{2,1}$ has two leaves, marked with $1$ and $2$. * $T_{3,1}$ has two leaves, marked with $1$ and $3$. * $T_{4,1}$ has three leaves, marked with $1$, $2$, and $4$. * $T_{5,1}$ has two leaves, marked with $1$ and $5$. * $T_{6,1}$ has four leaves, marked with $1$, $2$, $3$, and $6$.
The total number of leaves is $1 + 2 + 2 + 3 + 2 + 4 = 14$.
In the second test case, $n = 1$, $k = 3$, $d = 3$. Thus, we need to find the number of leaves in $T_{1,3}$, because $1^3 = 1$. This tree has only one leaf, so the answer is $1$.
|
Title: Count Leaves
time_limit_ms: 4000
memory_limit_mb: 256
Description: Let $n$ and $d$ be positive integers. We build the the divisor tree $T_{n,d}$ as follows:
* The root of the tree is a node marked with number $n$. This is the $0$-th layer of the tree. * For each $i$ from $0$ to $d - 1$, for each vertex of the $i$-th layer, do the following. If the current vertex is marked with $x$, create its children and mark them with all possible distinct divisors$^\dagger$ of $x$. These children will be in the $(i+1)$-st layer. * The vertices on the $d$-th layer are the leaves of the tree.
For example, $T_{6,2}$ (the divisor tree for $n = 6$ and $d = 2$) looks like this:

Define $f(n,d)$ as the number of leaves in $T_{n,d}$.
Given integers $n$, $k$, and $d$, please compute $\sum\limits_{i=1}^{n} f(i^k,d)$, modulo $10^9+7$.
$^\dagger$ In this problem, we say that an integer $y$ is a divisor of $x$ if $y \ge 1$ and there exists an integer $z$ such that $x = y \cdot z$.
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 only line of each test case contains three integers $n$, $k$, and $d$ ($1 \le n \le 10^9$, $1 \le k,d \le 10^5$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^9$.
Output: For each test case, output $\sum\limits_{i=1}^{n} f(i^k,d)$, modulo $10^9+7$.
Examples:
input:
3
6 1 1
1 3 3
10 1 2
output:
14
1
53
Note: In the first test case, $n = 6$, $k = 1$, and $d = 1$. Thus, we need to find the total number of leaves in the divisor trees $T_{1,1}$, $T_{2,1}$, $T_{3,1}$, $T_{4,1}$, $T_{5,1}$, $T_{6,1}$.
* $T_{1,1}$ has only one leaf, which is marked with $1$. * $T_{2,1}$ has two leaves, marked with $1$ and $2$. * $T_{3,1}$ has two leaves, marked with $1$ and $3$. * $T_{4,1}$ has three leaves, marked with $1$, $2$, and $4$. * $T_{5,1}$ has two leaves, marked with $1$ and $5$. * $T_{6,1}$ has four leaves, marked with $1$, $2$, $3$, and $6$.
The total number of leaves is $1 + 2 + 2 + 3 + 2 + 4 = 14$.
In the second test case, $n = 1$, $k = 3$, $d = 3$. Thus, we need to find the number of leaves in $T_{1,3}$, because $1^3 = 1$. This tree has only one leaf, so the answer is $1$.
|
2022B
|
https://codeforces.com/problemset/problem/2022/B
|
Kar Salesman
| 1,300
|
[
"binary search",
"greedy",
"math"
] |
Div. 2
| 1,000
| 256
|
Karel is a salesman in a car dealership. The dealership has $n$ different models of cars. There are $a_i$ cars of the $i$-th model. Karel is an excellent salesperson and can convince customers to buy up to $x$ cars (of Karel's choice), as long as the cars are from different models. Determine the minimum number of customers Karel has to bring in to sell all the cars.
|
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$ and $x$ ($1 \le n \le 5 \cdot 10^5$; $1 \le x \le 10$) β the number of different models of cars and the maximum number of cars Karel can convince a customer to buy.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β the number of cars of each model.
It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.
|
For each test case, output the minimum possible number of customers needed to sell all the cars.
|
[
[
"4\n3 2\n3 1 2\n3 3\n2 1 3\n5 3\n2 2 1 9 2\n7 4\n2 5 3 3 5 2 5",
"3\n3\n9\n7"
]
] |
For the first case, Karel only needs to lure in $3$ customers. He will convince the customers to buy the following models of cars:
* Customer $1$ buys $2$ cars with model $1$ and $3$. * Customer $2$ buys $2$ cars with model $1$ and $2$. * Customer $3$ buys $2$ cars with model $1$ and $3$.
For the second case, Karel only needs to lure in $3$ customers. He will convince the customers to buy the following models of cars:
* Customer $1$ buys $2$ cars with model $1$ and $3$. * Customer $2$ buys $3$ cars with model $1$, $2$ and $3$. * Customer $3$ buys $1$ car with model $3$.
|
Title: Kar Salesman
time_limit_ms: 1000
memory_limit_mb: 256
Description: Karel is a salesman in a car dealership. The dealership has $n$ different models of cars. There are $a_i$ cars of the $i$-th model. Karel is an excellent salesperson and can convince customers to buy up to $x$ cars (of Karel's choice), as long as the cars are from different models. Determine the minimum number of customers Karel has to bring in to sell all the cars.
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$ and $x$ ($1 \le n \le 5 \cdot 10^5$; $1 \le x \le 10$) β the number of different models of cars and the maximum number of cars Karel can convince a customer to buy.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β the number of cars of each model.
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 the minimum possible number of customers needed to sell all the cars.
Examples:
input:
4
3 2
3 1 2
3 3
2 1 3
5 3
2 2 1 9 2
7 4
2 5 3 3 5 2 5
output:
3
3
9
7
Note: For the first case, Karel only needs to lure in $3$ customers. He will convince the customers to buy the following models of cars:
* Customer $1$ buys $2$ cars with model $1$ and $3$. * Customer $2$ buys $2$ cars with model $1$ and $2$. * Customer $3$ buys $2$ cars with model $1$ and $3$.
For the second case, Karel only needs to lure in $3$ customers. He will convince the customers to buy the following models of cars:
* Customer $1$ buys $2$ cars with model $1$ and $3$. * Customer $2$ buys $3$ cars with model $1$, $2$ and $3$. * Customer $3$ buys $1$ car with model $3$.
|
1997C
|
https://codeforces.com/problemset/problem/1997/C
|
Even Positions
| 1,100
|
[
"constructive algorithms",
"data structures",
"greedy"
] |
Div. 2
| 2,000
| 256
|
Monocarp had a regular bracket sequence $s$ of length $n$ ($n$ is even). He even came up with his own way to calculate its cost.
He knows that in a regular bracket sequence (RBS), each opening bracket is paired up with the corresponding closing bracket. So he decided to calculate the cost of RBS as the sum of distances between pairs of corresponding bracket pairs.
For example, let's look at RBS (())(). It has three pairs of brackets:
* (__)__: the distance between brackets at position $1$ and at $4$ is $4 - 1 = 3$; * _()___: the distance is $3 - 2 = 1$; * ____(): the distance is $6 - 5 = 1$.
So the cost of (())() is $3 + 1 + 1 = 5$.
Unfortunately, due to data corruption, Monocarp lost all characters on odd positions $s_1, s_3, \dots, s_{n-1}$. Only characters on even positions ($s_2, s_4, \dots, s_{n}$) remain. For example, (())() turned to _(_)_).
Monocarp wants to restore his RBS by placing brackets on the odd positions. But since the restored RBS may not be unique, he wants to choose one with minimum cost. It's too hard to do for Monocarp alone, so can you help him?
Reminder: A regular bracket sequence is a string consisting of only brackets, such that this sequence, when inserted 1-s and +-s, gives a valid mathematical expression. For example, (), (()) or (()())() are RBS, while ), ()( or ())(() are not.
|
The first line contains a single integer $t$ ($1 \le t \le 5000$) β the number of test cases. Next $t$ cases follow.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$; $n$ is even) β the length of string $s$.
The second line of each test case contains a string $s$ of length $n$, where all characters on the odd positions are '_' and all characters on the even positions are either '(' or ')'.
Additional constraints:
* $s$ can be restored to at least one regular bracket sequence; * the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
|
For each test case, print one integer β the minimum cost of the regular bracket sequence that can be obtained from $s$ by replacing '_'-s with brackets.
|
[
[
"4\n6\n_(_)_)\n2\n_)\n8\n_)_)_)_)\n8\n_(_)_(_)",
"5\n1\n4\n8"
]
] |
In the first test case, it's optimal to make $s$ equal to (())(). The cost of $s$ will be equal to $3 + 1 + 1 = 5$.
In the second test case, the only option is to make $s$ equal to () with cost $1$.
In the third test case, the only possible RBS is ()()()() with cost $1 + 1 + 1 + 1 = 4$.
In the fourth test case, it's optimal to make $s$ equal to (())(()) with cost $3 + 1 + 3 + 1 = 8$.
|
Title: Even Positions
time_limit_ms: 2000
memory_limit_mb: 256
Description: Monocarp had a regular bracket sequence $s$ of length $n$ ($n$ is even). He even came up with his own way to calculate its cost.
He knows that in a regular bracket sequence (RBS), each opening bracket is paired up with the corresponding closing bracket. So he decided to calculate the cost of RBS as the sum of distances between pairs of corresponding bracket pairs.
For example, let's look at RBS (())(). It has three pairs of brackets:
* (__)__: the distance between brackets at position $1$ and at $4$ is $4 - 1 = 3$; * _()___: the distance is $3 - 2 = 1$; * ____(): the distance is $6 - 5 = 1$.
So the cost of (())() is $3 + 1 + 1 = 5$.
Unfortunately, due to data corruption, Monocarp lost all characters on odd positions $s_1, s_3, \dots, s_{n-1}$. Only characters on even positions ($s_2, s_4, \dots, s_{n}$) remain. For example, (())() turned to _(_)_).
Monocarp wants to restore his RBS by placing brackets on the odd positions. But since the restored RBS may not be unique, he wants to choose one with minimum cost. It's too hard to do for Monocarp alone, so can you help him?
Reminder: A regular bracket sequence is a string consisting of only brackets, such that this sequence, when inserted 1-s and +-s, gives a valid mathematical expression. For example, (), (()) or (()())() are RBS, while ), ()( or ())(() are not.
Input: The first line contains a single integer $t$ ($1 \le t \le 5000$) β the number of test cases. Next $t$ cases follow.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$; $n$ is even) β the length of string $s$.
The second line of each test case contains a string $s$ of length $n$, where all characters on the odd positions are '_' and all characters on the even positions are either '(' or ')'.
Additional constraints:
* $s$ can be restored to at least one regular bracket sequence; * the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
Output: For each test case, print one integer β the minimum cost of the regular bracket sequence that can be obtained from $s$ by replacing '_'-s with brackets.
Examples:
input:
4
6
_(_)_)
2
_)
8
_)_)_)_)
8
_(_)_(_)
output:
5
1
4
8
Note: In the first test case, it's optimal to make $s$ equal to (())(). The cost of $s$ will be equal to $3 + 1 + 1 = 5$.
In the second test case, the only option is to make $s$ equal to () with cost $1$.
In the third test case, the only possible RBS is ()()()() with cost $1 + 1 + 1 + 1 = 4$.
In the fourth test case, it's optimal to make $s$ equal to (())(()) with cost $3 + 1 + 3 + 1 = 8$.
|
2002D2
|
https://codeforces.com/problemset/problem/2002/D2
|
DFS Checker (Hard Version)
| 2,300
|
[
"binary search",
"data structures",
"dfs and similar",
"graphs",
"hashing",
"trees"
] |
Div. 1 + 2
| 2,000
| 512
|
This is the hard version of the problem. In this version, you are given a generic tree and the constraints on $n$ and $q$ are higher. You can make hacks only if both versions of the problem are solved.
You are given a rooted tree 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$^\dagger$ of the given tree.
Please note that the swaps are persistent through queries.
$^\dagger$ 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$ ($2\le n\le 3\cdot 10^5$, $2\le q\le 10^5$) β the number of vertices in the tree and the number of queries.
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.
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 $3\cdot 10^5$, and the sum of all $q$ does not exceed $10^5$.
|
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).
|
[
[
"3\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\n5 4\n1 1 3 4\n2 3 4 5 1\n5 1\n4 5\n3 4\n2 3",
"YES\nYES\nNO\nYES\nNO\nNO\nYES\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 (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: This is the hard version of the problem. In this version, you are given a generic tree and the constraints on $n$ and $q$ are higher. You can make hacks only if both versions of the problem are solved.
You are given a rooted tree 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$^\dagger$ of the given tree.
Please note that the swaps are persistent through queries.
$^\dagger$ 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$ ($2\le n\le 3\cdot 10^5$, $2\le q\le 10^5$) β the number of vertices in the tree and the number of queries.
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.
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 $3\cdot 10^5$, and the sum of all $q$ does not exceed $10^5$.
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:
3
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
5 4
1 1 3 4
2 3 4 5 1
5 1
4 5
3 4
2 3
output:
YES
YES
NO
YES
NO
NO
YES
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.
|
2000G
|
https://codeforces.com/problemset/problem/2000/G
|
Call During the Journey
| 2,100
|
[
"binary search",
"brute force",
"graphs",
"greedy",
"shortest paths"
] |
Div. 3
| 4,000
| 256
|
You live in a city consisting of $n$ intersections and $m$ streets connecting some pairs of intersections. You can travel in either direction on each street. No two streets connect the same pair of intersections, and no street connects an intersection to itself. You can reach any intersection from any other, possibly passing through some other intersections.
Every minute, you can board a bus at intersection $u_i$ and travel for $l_{i1}$ minutes to intersection $v_i$. Conversely, you can travel from intersection $v_i$ to intersection $u_i$ in $l_{i1}$ minutes. You can only board and exit the bus at intersections. You can only board the bus at an intersection if you are currently there.
You can also walk along each street, which takes $l_{i2} > l_{i1}$ minutes.
You can make stops at intersections.
You live at intersection number $1$. Today you woke up at time $0$, and you have an important event scheduled at intersection number $n$, which you must reach no later than time $t_0$. You also have a phone call planned that will last from $t_1$ to $t_2$ minutes ($t_1 < t_2 < t_0$).
During the phone call, you cannot ride the bus, but you can walk along any streets, make stops, or stay at home. You can exit the bus at minute $t_1$ and board the bus again at minute $t_2$.
Since you want to get enough sleep, you became curious β how late can you leave home to have time to talk on the phone and still not be late for the event?
|
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The following are the descriptions of the test cases.
The first line of each test case contains two integers $n$, $m$ ($2 \le n \le 10^5, 1 \le m \le 10^5$) β the number of intersections and streets in the city.
The second line of each test case contains three integers $t_0$, $t_1$, $t_2$ ($1 \le t_1 < t_2 < t_0 \le 10^9$) β the start time of the event, the start time of the phone call, and its end time, respectively.
The next $m$ lines of each test case contain descriptions of the streets.
The $i$-th line contains four integers $u_i$, $v_i$, $l_{i1}$, $l_{i2}$ ($1 \le u_i, v_i \le n$, $u_i \neq v_i$, $1 \le l_{i1} < l_{i2} \le 10^9$) β the numbers of the intersections connected by the $i$-th street, as well as the travel time along the street by bus and on foot. It is guaranteed that no two streets connect the same pair of intersections and that it is possible to reach any intersection from any other.
It is guaranteed that the sum of the values of $n$ across all test cases does not exceed $10^5$. It is also guaranteed that the sum of the values of $m$ across all test cases does not exceed $10^5$.
|
For each test case, output a single integer β the latest time you can leave home to have time to talk on the phone and not be late for the event. If you cannot reach the event on time, output -1.
|
[
[
"7\n5 5\n100 20 80\n1 5 30 100\n1 2 20 50\n2 3 20 50\n3 4 20 50\n4 5 20 50\n2 1\n100 50 60\n1 2 55 110\n4 4\n100 40 60\n1 2 30 100\n2 4 30 100\n1 3 20 50\n3 4 20 50\n3 3\n100 80 90\n1 2 1 10\n2 3 10 50\n1 3 20 21\n3 2\n58 55 57\n2 1 1 3\n2 3 3 4\n2 1\n12 9 10\n2 1 6 10\n5 5\n8 5 6\n2 1 1 8\n2 3 4 8\n4 2 2 4\n5 3 3 4\n4 5 2 6",
"0\n-1\n60\n80\n53\n3\n2"
]
] |
Title: Call During the Journey
time_limit_ms: 4000
memory_limit_mb: 256
Description: You live in a city consisting of $n$ intersections and $m$ streets connecting some pairs of intersections. You can travel in either direction on each street. No two streets connect the same pair of intersections, and no street connects an intersection to itself. You can reach any intersection from any other, possibly passing through some other intersections.
Every minute, you can board a bus at intersection $u_i$ and travel for $l_{i1}$ minutes to intersection $v_i$. Conversely, you can travel from intersection $v_i$ to intersection $u_i$ in $l_{i1}$ minutes. You can only board and exit the bus at intersections. You can only board the bus at an intersection if you are currently there.
You can also walk along each street, which takes $l_{i2} > l_{i1}$ minutes.
You can make stops at intersections.
You live at intersection number $1$. Today you woke up at time $0$, and you have an important event scheduled at intersection number $n$, which you must reach no later than time $t_0$. You also have a phone call planned that will last from $t_1$ to $t_2$ minutes ($t_1 < t_2 < t_0$).
During the phone call, you cannot ride the bus, but you can walk along any streets, make stops, or stay at home. You can exit the bus at minute $t_1$ and board the bus again at minute $t_2$.
Since you want to get enough sleep, you became curious β how late can you leave home to have time to talk on the phone and still not be late for the event?
Input: The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The following are the descriptions of the test cases.
The first line of each test case contains two integers $n$, $m$ ($2 \le n \le 10^5, 1 \le m \le 10^5$) β the number of intersections and streets in the city.
The second line of each test case contains three integers $t_0$, $t_1$, $t_2$ ($1 \le t_1 < t_2 < t_0 \le 10^9$) β the start time of the event, the start time of the phone call, and its end time, respectively.
The next $m$ lines of each test case contain descriptions of the streets.
The $i$-th line contains four integers $u_i$, $v_i$, $l_{i1}$, $l_{i2}$ ($1 \le u_i, v_i \le n$, $u_i \neq v_i$, $1 \le l_{i1} < l_{i2} \le 10^9$) β the numbers of the intersections connected by the $i$-th street, as well as the travel time along the street by bus and on foot. It is guaranteed that no two streets connect the same pair of intersections and that it is possible to reach any intersection from any other.
It is guaranteed that the sum of the values of $n$ across all test cases does not exceed $10^5$. It is also guaranteed that the sum of the values of $m$ across all test cases does not exceed $10^5$.
Output: For each test case, output a single integer β the latest time you can leave home to have time to talk on the phone and not be late for the event. If you cannot reach the event on time, output -1.
Examples:
input:
7
5 5
100 20 80
1 5 30 100
1 2 20 50
2 3 20 50
3 4 20 50
4 5 20 50
2 1
100 50 60
1 2 55 110
4 4
100 40 60
1 2 30 100
2 4 30 100
1 3 20 50
3 4 20 50
3 3
100 80 90
1 2 1 10
2 3 10 50
1 3 20 21
3 2
58 55 57
2 1 1 3
2 3 3 4
2 1
12 9 10
2 1 6 10
5 5
8 5 6
2 1 1 8
2 3 4 8
4 2 2 4
5 3 3 4
4 5 2 6
output:
0
-1
60
80
53
3
2
Note:
|
|
1992D
|
https://codeforces.com/problemset/problem/1992/D
|
Test of Love
| 1,200
|
[
"dp",
"greedy",
"implementation"
] |
Div. 3
| 2,000
| 256
|
ErnKor is ready to do anything for Julen, even to swim through crocodile- infested swamps. We decided to test this love. ErnKor will have to swim across a river with a width of $1$ meter and a length of $n$ meters.
The river is very cold. Therefore, in total (that is, throughout the entire swim from $0$ to $n+1$) ErnKor can swim in the water for no more than $k$ meters. For the sake of humanity, we have added not only crocodiles to the river, but also logs on which he can jump. Our test is as follows:
Initially, ErnKor is on the left bank and needs to reach the right bank. They are located at the $0$ and $n+1$ meters respectively. The river can be represented as $n$ segments, each with a length of $1$ meter. Each segment contains either a log 'L', a crocodile 'C', or just water 'W'. ErnKor can move as follows:
* If he is on the surface (i.e., on the bank or on a log), he can jump forward for no more than $m$ ($1 \le m \le 10$) meters (he can jump on the bank, on a log, or in the water). * If he is in the water, he can only swim to the next river segment (or to the bank if he is at the $n$-th meter). * ErnKor cannot land in a segment with a crocodile in any way.
Determine if ErnKor can reach the right bank.
|
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 numbers $n, m, k$ ($0 \le k \le 2 \cdot 10^5$, $1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10$) β the length of the river, the distance ErnKor can jump, and the number of meters ErnKor can swim without freezing.
The second line of each test case contains a string $a$ of length $n$. $a_i$ denotes the object located at the $i$-th meter. ($a_i \in \\{$'W','C','L'$\\}$)
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output "YES" if ErnKor can pass the test, and output "NO" otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
|
[
[
"6\n6 2 0\nLWLLLW\n6 1 1\nLWLLLL\n6 1 1\nLWLLWL\n6 2 15\nLWLLCC\n6 10 0\nCCCCCC\n6 6 1\nWCCCCW",
"YES\nYES\nNO\nNO\nYES\nYES"
]
] |
Let's consider examples:
* First example: We jump from the shore to the first log ($0 \rightarrow 1$), from the first log to the second ($1 \rightarrow 3$), from the second to the fourth ($3 \rightarrow 5$), and from the last log to the shore ($5 \rightarrow 7$). So, we have $0 \rightarrow 1 \rightarrow 3 \rightarrow 5 \rightarrow 7$. Since we did not encounter a crocodile and swam no more than k meters, the answer is Β«YESΒ». * Second example: $0 \rightarrow 1$, we jump into the water from the first log ($1 \rightarrow 2$), swim a cell to the log ($2 \leadsto 3$), $3 \rightarrow 4 \rightarrow 5 \rightarrow 6 \rightarrow 7$. Since we did not encounter a crocodile and swam no more than k meters, the answer is Β«YESΒ». * In the third example, ErnKor needs to swim two cells 'W', but can only swim one. Therefore, the answer is Β«NOΒ». * Sixth example: We jump from the shore into the water ($0 \rightarrow 6$) and swim one cell in the water ($6 \leadsto 7$). Since we did not encounter a crocodile and swam no more than k meters, the answer is Β«YESΒ».
|
Title: Test of Love
time_limit_ms: 2000
memory_limit_mb: 256
Description: ErnKor is ready to do anything for Julen, even to swim through crocodile- infested swamps. We decided to test this love. ErnKor will have to swim across a river with a width of $1$ meter and a length of $n$ meters.
The river is very cold. Therefore, in total (that is, throughout the entire swim from $0$ to $n+1$) ErnKor can swim in the water for no more than $k$ meters. For the sake of humanity, we have added not only crocodiles to the river, but also logs on which he can jump. Our test is as follows:
Initially, ErnKor is on the left bank and needs to reach the right bank. They are located at the $0$ and $n+1$ meters respectively. The river can be represented as $n$ segments, each with a length of $1$ meter. Each segment contains either a log 'L', a crocodile 'C', or just water 'W'. ErnKor can move as follows:
* If he is on the surface (i.e., on the bank or on a log), he can jump forward for no more than $m$ ($1 \le m \le 10$) meters (he can jump on the bank, on a log, or in the water). * If he is in the water, he can only swim to the next river segment (or to the bank if he is at the $n$-th meter). * ErnKor cannot land in a segment with a crocodile in any way.
Determine if ErnKor can reach the right bank.
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 numbers $n, m, k$ ($0 \le k \le 2 \cdot 10^5$, $1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10$) β the length of the river, the distance ErnKor can jump, and the number of meters ErnKor can swim without freezing.
The second line of each test case contains a string $a$ of length $n$. $a_i$ denotes the object located at the $i$-th meter. ($a_i \in \\{$'W','C','L'$\\}$)
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 "YES" if ErnKor can pass the test, and output "NO" otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
Examples:
input:
6
6 2 0
LWLLLW
6 1 1
LWLLLL
6 1 1
LWLLWL
6 2 15
LWLLCC
6 10 0
CCCCCC
6 6 1
WCCCCW
output:
YES
YES
NO
NO
YES
YES
Note: Let's consider examples:
* First example: We jump from the shore to the first log ($0 \rightarrow 1$), from the first log to the second ($1 \rightarrow 3$), from the second to the fourth ($3 \rightarrow 5$), and from the last log to the shore ($5 \rightarrow 7$). So, we have $0 \rightarrow 1 \rightarrow 3 \rightarrow 5 \rightarrow 7$. Since we did not encounter a crocodile and swam no more than k meters, the answer is Β«YESΒ». * Second example: $0 \rightarrow 1$, we jump into the water from the first log ($1 \rightarrow 2$), swim a cell to the log ($2 \leadsto 3$), $3 \rightarrow 4 \rightarrow 5 \rightarrow 6 \rightarrow 7$. Since we did not encounter a crocodile and swam no more than k meters, the answer is Β«YESΒ». * In the third example, ErnKor needs to swim two cells 'W', but can only swim one. Therefore, the answer is Β«NOΒ». * Sixth example: We jump from the shore into the water ($0 \rightarrow 6$) and swim one cell in the water ($6 \leadsto 7$). Since we did not encounter a crocodile and swam no more than k meters, the answer is Β«YESΒ».
|
1985A
|
https://codeforces.com/problemset/problem/1985/A
|
Creating Words
| 800
|
[
"implementation",
"strings"
] |
Div. 4
| 1,000
| 256
|
Matthew is given two strings $a$ and $b$, both of length $3$. He thinks it's particularly funny to create two new words by swapping the first character of $a$ with the first character of $b$. He wants you to output $a$ and $b$ after the swap.
Note that the new words may not necessarily be different.
|
The first line contains $t$ ($1 \leq t \leq 100$) β the number of test cases.
The first and only line of each test case contains two space-separated strings, $a$ and $b$, both of length $3$. The strings only contain lowercase Latin letters.
|
For each test case, after the swap, output $a$ and $b$, separated by a space.
|
[
[
"6\nbit set\ncat dog\nhot dog\nuwu owo\ncat cat\nzzz zzz",
"sit bet\ndat cog\ndot hog\nowu uwo\ncat cat\nzzz zzz"
]
] |
Title: Creating Words
time_limit_ms: 1000
memory_limit_mb: 256
Description: Matthew is given two strings $a$ and $b$, both of length $3$. He thinks it's particularly funny to create two new words by swapping the first character of $a$ with the first character of $b$. He wants you to output $a$ and $b$ after the swap.
Note that the new words may not necessarily be different.
Input: The first line contains $t$ ($1 \leq t \leq 100$) β the number of test cases.
The first and only line of each test case contains two space-separated strings, $a$ and $b$, both of length $3$. The strings only contain lowercase Latin letters.
Output: For each test case, after the swap, output $a$ and $b$, separated by a space.
Examples:
input:
6
bit set
cat dog
hot dog
uwu owo
cat cat
zzz zzz
output:
sit bet
dat cog
dot hog
owu uwo
cat cat
zzz zzz
Note:
|
|
1991A
|
https://codeforces.com/problemset/problem/1991/A
|
Maximize the Last Element
| 800
|
[
"greedy",
"implementation"
] |
Div. 1 + 2
| 1,000
| 256
|
You are given an array $a$ of $n$ integers, where $n$ is odd.
In one operation, you will remove two adjacent elements from the array $a$, and then concatenate the remaining parts of the array. For example, given the array $[4,7,4,2,9]$, we can obtain the arrays $[4,2,9]$ and $[4,7,9]$ by the operations $[\underline{4,7}, 4,2,9] \to [4,2,9]$ and $[4,7,\underline{4,2},9] \to [4,7,9]$ respectively. However, we cannot obtain the array $[7,2,9]$ as it requires deleting non-adjacent elements $[\underline{4},7,\underline{4},2,9]$.
You will repeatedly perform this operation until exactly one element remains in $a$.
Find the maximum possible value of the remaining element in $a$.
|
Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 99$; $n$ is odd) β the length of 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 100$) β the elements of the array $a$.
Note that there is no bound on the sum of $n$ over all test cases.
|
For each test case, output a single integer β the maximum possible value of the remaining element in $a$.
|
[
[
"4\n1\n6\n3\n1 3 2\n5\n4 7 4 2 9\n7\n3 1 4 1 5 9 2",
"6\n2\n9\n5"
]
] |
In the first test case, the array $a$ is $[6]$. Since there is only one element, no operations are needed. The maximum possible value of the remaining element is $6$.
In the second test case, the array $a$ is $[1, 3, 2]$. We can remove the first two elements $[\underline{1, 3}, 2] \to [2]$, or remove the last two elements $[1, \underline{3, 2}] \to [1]$. Therefore, the maximum possible value of the remaining element is $2$.
In the third test case, the array $a$ is $[4, 7, 4, 2, 9]$. One way to maximize the remaining element is $[4, \underline{7, 4}, 2, 9] \to [\underline{4, 2}, 9] \to [9]$. Therefore, the maximum possible value of the remaining element is $9$.
In the fourth test case, the array $a$ is $[3, 1, 4, 1, 5, 9, 2]$. It can be shown that the maximum possible value of the remaining element is $5$.
|
Title: Maximize the Last Element
time_limit_ms: 1000
memory_limit_mb: 256
Description: You are given an array $a$ of $n$ integers, where $n$ is odd.
In one operation, you will remove two adjacent elements from the array $a$, and then concatenate the remaining parts of the array. For example, given the array $[4,7,4,2,9]$, we can obtain the arrays $[4,2,9]$ and $[4,7,9]$ by the operations $[\underline{4,7}, 4,2,9] \to [4,2,9]$ and $[4,7,\underline{4,2},9] \to [4,7,9]$ respectively. However, we cannot obtain the array $[7,2,9]$ as it requires deleting non-adjacent elements $[\underline{4},7,\underline{4},2,9]$.
You will repeatedly perform this operation until exactly one element remains in $a$.
Find the maximum possible value of the remaining element in $a$.
Input: Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 99$; $n$ is odd) β the length of 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 100$) β the elements of the array $a$.
Note that there is no bound on the sum of $n$ over all test cases.
Output: For each test case, output a single integer β the maximum possible value of the remaining element in $a$.
Examples:
input:
4
1
6
3
1 3 2
5
4 7 4 2 9
7
3 1 4 1 5 9 2
output:
6
2
9
5
Note: In the first test case, the array $a$ is $[6]$. Since there is only one element, no operations are needed. The maximum possible value of the remaining element is $6$.
In the second test case, the array $a$ is $[1, 3, 2]$. We can remove the first two elements $[\underline{1, 3}, 2] \to [2]$, or remove the last two elements $[1, \underline{3, 2}] \to [1]$. Therefore, the maximum possible value of the remaining element is $2$.
In the third test case, the array $a$ is $[4, 7, 4, 2, 9]$. One way to maximize the remaining element is $[4, \underline{7, 4}, 2, 9] \to [\underline{4, 2}, 9] \to [9]$. Therefore, the maximum possible value of the remaining element is $9$.
In the fourth test case, the array $a$ is $[3, 1, 4, 1, 5, 9, 2]$. It can be shown that the maximum possible value of the remaining element is $5$.
|
2035F
|
https://codeforces.com/problemset/problem/2035/F
|
Tree Operations
| 2,500
|
[
"binary search",
"brute force",
"dfs and similar",
"dp",
"trees"
] |
Div. 1 + 2
| 4,000
| 256
|
This really says a lot about our society.
One day, a turtle gives you a tree with $n$ nodes rooted at node $x$. Each node has an initial nonnegative value; the $i$-th node has starting value $a_i$.
You want to make the values of all nodes equal to $0$. To do so, you will perform a series of operations on the tree, where each operation will be performed on a certain node. Define an operation on node $u$ as choosing a single node in $u$'s subtree$^{\text{β}}$ and incrementing or decrementing its value by $1$. The order in which operations are performed on nodes is as follows:
* For $1 \le i \le n$, the $i$-th operation will be performed on node $i$. * For $i > n$, the $i$-th operation will be performed on the same node as operation $i - n$.
More formally, the $i$-th operation will be performed on the $(((i - 1) \bmod n) + 1)$-th node.$^{\text{β }}$
Note that you cannot skip over operations; that is, you cannot perform the $i$-th operation without first performing operations $1, 2, \ldots, i - 1$.
Find the minimum number of operations you must perform before you can make the values of all nodes equal to $0$, assuming you pick operations optimally. If it's impossible to make the values of all nodes equal to $0$ after finite operations, output $-1$.
$^{\text{β}}$The subtree of a node $u$ is the set of nodes for which $u$ lies on the shortest path from this node to the root, including $u$ itself.
$^{\text{β }}$Here, $a \bmod b$ denotes the remainder from dividing $a$ by $b$.
|
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 two integers $n$ and $x$ ($1 \le n \le 2000$, $1 \le x \le n$) β the number of nodes and the root of the tree.
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 starting value of each node.
Each of the next $n - 1$ lines of each test case contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) representing an undirected edge from $u$ to $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 $2000$.
|
For each test case, output a single integer denoting the minimum amount of operations needed to make all nodes $0$. If it's impossible to make all nodes $0$, output $-1$.
|
[
[
"5\n2 1\n1 2\n1 2\n3 2\n2 1 3\n2 1\n3 2\n4 1\n1 1 0 1\n1 2\n2 3\n1 4\n12 6\n14 4 5 6 12 9 5 11 6 2 1 12\n3 9\n10 6\n6 12\n4 3\n3 1\n5 11\n9 7\n5 6\n1 8\n2 8\n5 1\n1 1\n0",
"3\n6\n5\n145\n0"
]
] |
In the first test case, you can make the following valid sequence of operations:
* For operation $1$, decrease the value of node $1$. This is valid because $(((1 - 1) \bmod n) + 1) = 1$, and node $1$ is in the subtree of node $1$. * For operation $2$, decrease the value of node $2$. This is valid because $(((2 - 1) \bmod n) + 1) = 2$, and node $2$ is in the subtree of node $2$. * For operation $3$, decrease the value of node $2$. This is valid because $(((3 - 1) \bmod n) + 1) = 1$, and node $2$ is in the subtree of node $1$.
|
Title: Tree Operations
time_limit_ms: 4000
memory_limit_mb: 256
Description: This really says a lot about our society.
One day, a turtle gives you a tree with $n$ nodes rooted at node $x$. Each node has an initial nonnegative value; the $i$-th node has starting value $a_i$.
You want to make the values of all nodes equal to $0$. To do so, you will perform a series of operations on the tree, where each operation will be performed on a certain node. Define an operation on node $u$ as choosing a single node in $u$'s subtree$^{\text{β}}$ and incrementing or decrementing its value by $1$. The order in which operations are performed on nodes is as follows:
* For $1 \le i \le n$, the $i$-th operation will be performed on node $i$. * For $i > n$, the $i$-th operation will be performed on the same node as operation $i - n$.
More formally, the $i$-th operation will be performed on the $(((i - 1) \bmod n) + 1)$-th node.$^{\text{β }}$
Note that you cannot skip over operations; that is, you cannot perform the $i$-th operation without first performing operations $1, 2, \ldots, i - 1$.
Find the minimum number of operations you must perform before you can make the values of all nodes equal to $0$, assuming you pick operations optimally. If it's impossible to make the values of all nodes equal to $0$ after finite operations, output $-1$.
$^{\text{β}}$The subtree of a node $u$ is the set of nodes for which $u$ lies on the shortest path from this node to the root, including $u$ itself.
$^{\text{β }}$Here, $a \bmod b$ denotes the remainder from dividing $a$ by $b$.
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 two integers $n$ and $x$ ($1 \le n \le 2000$, $1 \le x \le n$) β the number of nodes and the root of the tree.
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 starting value of each node.
Each of the next $n - 1$ lines of each test case contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) representing an undirected edge from $u$ to $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 $2000$.
Output: For each test case, output a single integer denoting the minimum amount of operations needed to make all nodes $0$. If it's impossible to make all nodes $0$, output $-1$.
Examples:
input:
5
2 1
1 2
1 2
3 2
2 1 3
2 1
3 2
4 1
1 1 0 1
1 2
2 3
1 4
12 6
14 4 5 6 12 9 5 11 6 2 1 12
3 9
10 6
6 12
4 3
3 1
5 11
9 7
5 6
1 8
2 8
5 1
1 1
0
output:
3
6
5
145
0
Note: In the first test case, you can make the following valid sequence of operations:
* For operation $1$, decrease the value of node $1$. This is valid because $(((1 - 1) \bmod n) + 1) = 1$, and node $1$ is in the subtree of node $1$. * For operation $2$, decrease the value of node $2$. This is valid because $(((2 - 1) \bmod n) + 1) = 2$, and node $2$ is in the subtree of node $2$. * For operation $3$, decrease the value of node $2$. This is valid because $(((3 - 1) \bmod n) + 1) = 1$, and node $2$ is in the subtree of node $1$.
|
2004D
|
https://codeforces.com/problemset/problem/2004/D
|
Colored Portals
| 1,600
|
[
"binary search",
"brute force",
"data structures",
"graphs",
"greedy",
"implementation",
"shortest paths"
] |
Div. 2
| 2,000
| 256
|
There are $n$ cities located on a straight line. The cities are numbered from $1$ to $n$.
Portals are used to move between cities. There are $4$ colors of portals: blue, green, red, and yellow. Each city has portals of two different colors. You can move from city $i$ to city $j$ if they have portals of the same color (for example, you can move between a "blue-red" city and a "blue- green" city). This movement costs $|i-j|$ coins.
Your task is to answer $q$ independent queries: calculate the minimum cost to move from city $x$ to city $y$.
|
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains two integers $n$ and $q$ ($1 \le n, q \le 2 \cdot 10^5$) β the number of cities and the number of queries, respectively.
The second line contains $n$ strings of the following types: BG, BR, BY, GR, GY, or RY; the $i$-th of them describes the portals located in the $i$-th city; the symbol B indicates that there is a blue portal in the city, G β green, R β red, and Y β yellow.
The $j$-th of the next $q$ lines contains two integers $x_j$ and $y_j$ ($1 \le x_j, y_j \le n$) β the description of the $j$-th query.
Additional constraints on the input:
* the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$; * the sum of $q$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each query, print a single integer β the minimum cost to move from city $x$ to city $y$ (or $-1$ if it is impossible).
|
[
[
"2\n4 5\nBR BR GY GR\n1 2\n3 1\n4 4\n1 4\n4 2\n2 1\nBG RY\n1 2",
"1\n4\n0\n3\n2\n-1"
]
] |
Title: Colored Portals
time_limit_ms: 2000
memory_limit_mb: 256
Description: There are $n$ cities located on a straight line. The cities are numbered from $1$ to $n$.
Portals are used to move between cities. There are $4$ colors of portals: blue, green, red, and yellow. Each city has portals of two different colors. You can move from city $i$ to city $j$ if they have portals of the same color (for example, you can move between a "blue-red" city and a "blue- green" city). This movement costs $|i-j|$ coins.
Your task is to answer $q$ independent queries: calculate the minimum cost to move from city $x$ to city $y$.
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 $q$ ($1 \le n, q \le 2 \cdot 10^5$) β the number of cities and the number of queries, respectively.
The second line contains $n$ strings of the following types: BG, BR, BY, GR, GY, or RY; the $i$-th of them describes the portals located in the $i$-th city; the symbol B indicates that there is a blue portal in the city, G β green, R β red, and Y β yellow.
The $j$-th of the next $q$ lines contains two integers $x_j$ and $y_j$ ($1 \le x_j, y_j \le n$) β the description of the $j$-th query.
Additional constraints on the input:
* the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$; * the sum of $q$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each query, print a single integer β the minimum cost to move from city $x$ to city $y$ (or $-1$ if it is impossible).
Examples:
input:
2
4 5
BR BR GY GR
1 2
3 1
4 4
1 4
4 2
2 1
BG RY
1 2
output:
1
4
0
3
2
-1
Note:
|
|
2027C
|
https://codeforces.com/problemset/problem/2027/C
|
Add Zeros
| 1,500
|
[
"brute force",
"data structures",
"dfs and similar",
"dp",
"graphs",
"greedy"
] |
Div. 2
| 3,000
| 256
|
You're given an array $a$ initially containing $n$ integers. In one operation, you must do the following:
* Choose a position $i$ such that $1 < i \le |a|$ and $a_i = |a| + 1 - i$, where $|a|$ is the current size of the array. * Append $i - 1$ zeros onto the end of $a$.
After performing this operation as many times as you want, what is the maximum possible length of the array $a$?
|
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 $n$ ($1 \le n \le 3 \cdot 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 \le a_i \le 10^{12}$).
It is guaranteed that 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 possible length of $a$ after performing some sequence of operations.
|
[
[
"4\n5\n2 4 6 2 5\n5\n5 4 4 5 1\n4\n6 8 2 3\n1\n1",
"10\n11\n10\n1"
]
] |
In the first test case, we can first choose $i = 4$, since $a_4 = 5 + 1 - 4 = 2$. After this, the array becomes $[2, 4, 6, 2, 5, 0, 0, 0]$. We can then choose $i = 3$ since $a_3 = 8 + 1 - 3 = 6$. After this, the array becomes $[2, 4, 6, 2, 5, 0, 0, 0, 0, 0]$, which has a length of $10$. It can be shown that no sequence of operations will make the final array longer.
In the second test case, we can choose $i=2$, then $i=3$, then $i=4$. The final array will be $[5, 4, 4, 5, 1, 0, 0, 0, 0, 0, 0]$, with a length of $11$.
|
Title: Add Zeros
time_limit_ms: 3000
memory_limit_mb: 256
Description: You're given an array $a$ initially containing $n$ integers. In one operation, you must do the following:
* Choose a position $i$ such that $1 < i \le |a|$ and $a_i = |a| + 1 - i$, where $|a|$ is the current size of the array. * Append $i - 1$ zeros onto the end of $a$.
After performing this operation as many times as you want, what is the maximum possible length of the array $a$?
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 $n$ ($1 \le n \le 3 \cdot 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 \le a_i \le 10^{12}$).
It is guaranteed that 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 possible length of $a$ after performing some sequence of operations.
Examples:
input:
4
5
2 4 6 2 5
5
5 4 4 5 1
4
6 8 2 3
1
1
output:
10
11
10
1
Note: In the first test case, we can first choose $i = 4$, since $a_4 = 5 + 1 - 4 = 2$. After this, the array becomes $[2, 4, 6, 2, 5, 0, 0, 0]$. We can then choose $i = 3$ since $a_3 = 8 + 1 - 3 = 6$. After this, the array becomes $[2, 4, 6, 2, 5, 0, 0, 0, 0, 0]$, which has a length of $10$. It can be shown that no sequence of operations will make the final array longer.
In the second test case, we can choose $i=2$, then $i=3$, then $i=4$. The final array will be $[5, 4, 4, 5, 1, 0, 0, 0, 0, 0, 0]$, with a length of $11$.
|
2020E
|
https://codeforces.com/problemset/problem/2020/E
|
Expected Power
| 2,000
|
[
"bitmasks",
"dp",
"math",
"probabilities"
] |
Div. 2
| 4,000
| 256
|
You are given an array of $n$ integers $a_1,a_2,\ldots,a_n$. You are also given an array $p_1, p_2, \ldots, p_n$.
Let $S$ denote the random multiset (i. e., it may contain equal elements) constructed as follows:
* Initially, $S$ is empty. * For each $i$ from $1$ to $n$, insert $a_i$ into $S$ with probability $\frac{p_i}{10^4}$. Note that each element is inserted independently.
Denote $f(S)$ as the [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of all elements of $S$. Please calculate the expected value of $(f(S))^2$. Output the answer modulo $10^9 + 7$.
Formally, let $M = 10^9 + 7$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \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 1023$).
The third line of each test case contains $n$ integers $p_1,p_2,\ldots,p_n$ ($1 \le p_i \le 10^4$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output the expected value of $(f(S))^2$, modulo $10^9 + 7$.
|
[
[
"4\n2\n1 2\n5000 5000\n2\n1 1\n1000 2000\n6\n343 624 675 451 902 820\n6536 5326 7648 2165 9430 5428\n1\n1\n10000",
"500000007\n820000006\n280120536\n1"
]
] |
In the first test case, $a = [1, 2]$ and each element is inserted into $S$ with probability $\frac{1}{2}$, since $p_1 = p_2 = 5000$ and $\frac{p_i}{10^4} = \frac{1}{2}$. Thus, there are $4$ outcomes for $S$, each happening with the same probability of $\frac{1}{4}$:
* $S = \varnothing$. In this case, $f(S) = 0$, $(f(S))^2 = 0$. * $S = \\{1\\}$. In this case, $f(S) = 1$, $(f(S))^2 = 1$. * $S = \\{2\\}$. In this case, $f(S) = 2$, $(f(S))^2 = 4$. * $S = \\{1,2\\}$. In this case, $f(S) = 1 \oplus 2 = 3$, $(f(S))^2 = 9$.
Hence, the answer is $0 \cdot \frac{1}{4} + 1 \cdot \frac{1}{4} + 4\cdot \frac{1}{4} + 9 \cdot \frac{1}{4} = \frac{14}{4} = \frac{7}{2} \equiv 500\,000\,007 \pmod{10^9 + 7}$.
In the second test case, $a = [1, 1]$, $a_1$ is inserted into $S$ with probability $0.1$, while $a_2$ is inserted into $S$ with probability $0.2$. There are $3$ outcomes for $S$:
* $S = \varnothing$. In this case, $f(S) = 0$, $(f(S))^2 = 0$. This happens with probability $(1-0.1) \cdot (1-0.2) = 0.72$. * $S = \\{1\\}$. In this case, $f(S) = 1$, $(f(S))^2 = 1$. This happens with probability $(1-0.1) \cdot 0.2 + 0.1 \cdot (1-0.2) = 0.26$. * $S = \\{1, 1\\}$. In this case, $f(S) = 0$, $(f(S))^2 = 0$. This happens with probability $0.1 \cdot 0.2 = 0.02$.
Hence, the answer is $0 \cdot 0.72 + 1 \cdot 0.26 + 0 \cdot 0.02 = 0.26 = \frac{26}{100} \equiv 820\,000\,006 \pmod{10^9 + 7}$.
|
Title: Expected Power
time_limit_ms: 4000
memory_limit_mb: 256
Description: You are given an array of $n$ integers $a_1,a_2,\ldots,a_n$. You are also given an array $p_1, p_2, \ldots, p_n$.
Let $S$ denote the random multiset (i. e., it may contain equal elements) constructed as follows:
* Initially, $S$ is empty. * For each $i$ from $1$ to $n$, insert $a_i$ into $S$ with probability $\frac{p_i}{10^4}$. Note that each element is inserted independently.
Denote $f(S)$ as the [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of all elements of $S$. Please calculate the expected value of $(f(S))^2$. Output the answer modulo $10^9 + 7$.
Formally, let $M = 10^9 + 7$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$.
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 second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 1023$).
The third line of each test case contains $n$ integers $p_1,p_2,\ldots,p_n$ ($1 \le p_i \le 10^4$).
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 the expected value of $(f(S))^2$, modulo $10^9 + 7$.
Examples:
input:
4
2
1 2
5000 5000
2
1 1
1000 2000
6
343 624 675 451 902 820
6536 5326 7648 2165 9430 5428
1
1
10000
output:
500000007
820000006
280120536
1
Note: In the first test case, $a = [1, 2]$ and each element is inserted into $S$ with probability $\frac{1}{2}$, since $p_1 = p_2 = 5000$ and $\frac{p_i}{10^4} = \frac{1}{2}$. Thus, there are $4$ outcomes for $S$, each happening with the same probability of $\frac{1}{4}$:
* $S = \varnothing$. In this case, $f(S) = 0$, $(f(S))^2 = 0$. * $S = \\{1\\}$. In this case, $f(S) = 1$, $(f(S))^2 = 1$. * $S = \\{2\\}$. In this case, $f(S) = 2$, $(f(S))^2 = 4$. * $S = \\{1,2\\}$. In this case, $f(S) = 1 \oplus 2 = 3$, $(f(S))^2 = 9$.
Hence, the answer is $0 \cdot \frac{1}{4} + 1 \cdot \frac{1}{4} + 4\cdot \frac{1}{4} + 9 \cdot \frac{1}{4} = \frac{14}{4} = \frac{7}{2} \equiv 500\,000\,007 \pmod{10^9 + 7}$.
In the second test case, $a = [1, 1]$, $a_1$ is inserted into $S$ with probability $0.1$, while $a_2$ is inserted into $S$ with probability $0.2$. There are $3$ outcomes for $S$:
* $S = \varnothing$. In this case, $f(S) = 0$, $(f(S))^2 = 0$. This happens with probability $(1-0.1) \cdot (1-0.2) = 0.72$. * $S = \\{1\\}$. In this case, $f(S) = 1$, $(f(S))^2 = 1$. This happens with probability $(1-0.1) \cdot 0.2 + 0.1 \cdot (1-0.2) = 0.26$. * $S = \\{1, 1\\}$. In this case, $f(S) = 0$, $(f(S))^2 = 0$. This happens with probability $0.1 \cdot 0.2 = 0.02$.
Hence, the answer is $0 \cdot 0.72 + 1 \cdot 0.26 + 0 \cdot 0.02 = 0.26 = \frac{26}{100} \equiv 820\,000\,006 \pmod{10^9 + 7}$.
|
2030G1
|
https://codeforces.com/problemset/problem/2030/G1
|
The Destruction of the Universe (Easy Version)
| 2,900
|
[
"combinatorics",
"greedy",
"math"
] |
Div. 2
| 4,000
| 512
|
This is the easy version of the problem. In this version, $n \leq 5000$. You can only make hacks if both versions of the problem are solved.
Orangutans are powerful beingsβso powerful that they only need $1$ unit of time to destroy every vulnerable planet in the universe!
There are $n$ planets in the universe. Each planet has an interval of vulnerability $[l, r]$, during which it will be exposed to destruction by orangutans. Orangutans can also expand the interval of vulnerability of any planet by $1$ unit.
Specifically, suppose the expansion is performed on planet $p$ with interval of vulnerability $[l_p, r_p]$. Then, the resulting interval of vulnerability may be either $[l_p - 1, r_p]$ or $[l_p, r_p + 1]$.
Given a set of planets, orangutans can destroy all planets if the intervals of vulnerability of all planets in the set intersect at least one common point. Let the score of such a set denote the minimum number of expansions that must be performed.
Orangutans are interested in the sum of scores of all non-empty subsets of the planets in the universe. As the answer can be large, output it modulo $998\,244\,353$.
|
The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains an integer $n$ ($1 \leq n \leq 5000$) β the number of planets in the universe.
The following $n$ lines contain two integers $l_i$ and $r_i$ ($1 \leq l_i \leq r_i \leq n$) β the initial interval of vulnerability of the $i$-th planet.
It is guaranteed that the sum of $n$ does not exceed $5000$ over all test cases.
|
For each test case, output an integer β the sum of scores to destroy all non- empty subsets of the planets in the universe, modulo $998\,244\,353$.
|
[
[
"3\n3\n1 1\n2 3\n3 3\n4\n1 4\n2 3\n2 4\n1 1\n5\n1 2\n2 3\n3 4\n4 5\n1 5",
"5\n6\n24"
]
] |
In the first testcase, there are seven non-empty subsets of planets we must consider:
* For each of the subsets $\\{[1,1]\\}, \\{[2,3]\\}, \\{[3,3]\\}$, the score is $0$. * For the subset $\\{[2,3], [3,3]\\}$, the score is $0$, because the point $3$ is already contained in both planets' interval of vulnerability. * For the subset $\\{[1,1], [2,3]\\}$, the score is $1$. By using one operation on changing the interval of vulnerability of the second planet to be $[1,3]$, the two planets now both have the point $1$ in their interval. * For the subset $\\{[1,1], [3,3]\\}$, the score is $2$. By using two operations on changing the interval of vulnerability of the first planet to be $[1,3]$, the two planets now both have the point $3$ in their interval. * For the subset $\\{[1,1], [2,3], [3,3]\\}$, the score is $2$. By using one operation on changing the interval of vulnerability of the first planet to be $[1,2]$ and one operation on changing the interval of vulnerability of the third planet to $[2,3]$, all three planets will have the point $2$ in their interval.
The sum of scores of all non-empty subsets of the first testcase is $0 \cdot 4 + 1 \cdot 1 + 2\cdot2 = 5$.
|
Title: The Destruction of the Universe (Easy Version)
time_limit_ms: 4000
memory_limit_mb: 512
Description: This is the easy version of the problem. In this version, $n \leq 5000$. You can only make hacks if both versions of the problem are solved.
Orangutans are powerful beingsβso powerful that they only need $1$ unit of time to destroy every vulnerable planet in the universe!
There are $n$ planets in the universe. Each planet has an interval of vulnerability $[l, r]$, during which it will be exposed to destruction by orangutans. Orangutans can also expand the interval of vulnerability of any planet by $1$ unit.
Specifically, suppose the expansion is performed on planet $p$ with interval of vulnerability $[l_p, r_p]$. Then, the resulting interval of vulnerability may be either $[l_p - 1, r_p]$ or $[l_p, r_p + 1]$.
Given a set of planets, orangutans can destroy all planets if the intervals of vulnerability of all planets in the set intersect at least one common point. Let the score of such a set denote the minimum number of expansions that must be performed.
Orangutans are interested in the sum of scores of all non-empty subsets of the planets in the universe. As the answer can be large, output it modulo $998\,244\,353$.
Input: The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains an integer $n$ ($1 \leq n \leq 5000$) β the number of planets in the universe.
The following $n$ lines contain two integers $l_i$ and $r_i$ ($1 \leq l_i \leq r_i \leq n$) β the initial interval of vulnerability of the $i$-th planet.
It is guaranteed that the sum of $n$ does not exceed $5000$ over all test cases.
Output: For each test case, output an integer β the sum of scores to destroy all non- empty subsets of the planets in the universe, modulo $998\,244\,353$.
Examples:
input:
3
3
1 1
2 3
3 3
4
1 4
2 3
2 4
1 1
5
1 2
2 3
3 4
4 5
1 5
output:
5
6
24
Note: In the first testcase, there are seven non-empty subsets of planets we must consider:
* For each of the subsets $\\{[1,1]\\}, \\{[2,3]\\}, \\{[3,3]\\}$, the score is $0$. * For the subset $\\{[2,3], [3,3]\\}$, the score is $0$, because the point $3$ is already contained in both planets' interval of vulnerability. * For the subset $\\{[1,1], [2,3]\\}$, the score is $1$. By using one operation on changing the interval of vulnerability of the second planet to be $[1,3]$, the two planets now both have the point $1$ in their interval. * For the subset $\\{[1,1], [3,3]\\}$, the score is $2$. By using two operations on changing the interval of vulnerability of the first planet to be $[1,3]$, the two planets now both have the point $3$ in their interval. * For the subset $\\{[1,1], [2,3], [3,3]\\}$, the score is $2$. By using one operation on changing the interval of vulnerability of the first planet to be $[1,2]$ and one operation on changing the interval of vulnerability of the third planet to $[2,3]$, all three planets will have the point $2$ in their interval.
The sum of scores of all non-empty subsets of the first testcase is $0 \cdot 4 + 1 \cdot 1 + 2\cdot2 = 5$.
|
2008G
|
https://codeforces.com/problemset/problem/2008/G
|
Sakurako's Task
| 1,800
|
[
"binary search",
"greedy",
"math",
"number theory"
] |
Div. 3
| 2,000
| 256
|
Sakurako has prepared a task for you:
She gives you an array of $n$ integers and allows you to choose $i$ and $j$ such that $i \neq j$ and $a_i \ge a_j$, and then assign $a_i = a_i - a_j$ or $a_i = a_i + a_j$. You can perform this operation any number of times for any $i$ and $j$, as long as they satisfy the conditions.
Sakurako asks you what is the maximum possible value of $mex_k$$^{\text{β}}$ of the array after any number of operations.
$^{\text{β}}$$mex_k$ is the $k$-th non-negative integer that is absent in the array. For example, $mex_1(\\{1,2,3 \\})=0$, since $0$ is the first element that is not in the array, and $mex_2(\\{0,2,4 \\})=3$, since $3$ is the second element that is not in the array.
|
The first line contains a single integer $t$ ($1\le t\le 10^4$) β the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1\le n\le 2\cdot 10^5,1\le k\le 10^9$) β the number of elements in the array and the value $k$ for $mex_k$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots,a_n$ ($1\le a_i\le 10^9$) β the elements of the array.
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 maximum $mex_k$ that can be achieved through the operations.
|
[
[
"6\n1 3\n3\n2 10\n1 1\n3 1\n1 2 3\n3 2\n1 2 4\n4 5\n2 2 2 16\n4 5\n2 2 2 3",
"2\n11\n3\n4\n8\n8"
]
] |
Title: Sakurako's Task
time_limit_ms: 2000
memory_limit_mb: 256
Description: Sakurako has prepared a task for you:
She gives you an array of $n$ integers and allows you to choose $i$ and $j$ such that $i \neq j$ and $a_i \ge a_j$, and then assign $a_i = a_i - a_j$ or $a_i = a_i + a_j$. You can perform this operation any number of times for any $i$ and $j$, as long as they satisfy the conditions.
Sakurako asks you what is the maximum possible value of $mex_k$$^{\text{β}}$ of the array after any number of operations.
$^{\text{β}}$$mex_k$ is the $k$-th non-negative integer that is absent in the array. For example, $mex_1(\\{1,2,3 \\})=0$, since $0$ is the first element that is not in the array, and $mex_2(\\{0,2,4 \\})=3$, since $3$ is the second element that is not in the array.
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\le 2\cdot 10^5,1\le k\le 10^9$) β the number of elements in the array and the value $k$ for $mex_k$.
The second line of each test case contains $n$ integers $a_1, a_2, \dots,a_n$ ($1\le a_i\le 10^9$) β the elements of the array.
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 maximum $mex_k$ that can be achieved through the operations.
Examples:
input:
6
1 3
3
2 10
1 1
3 1
1 2 3
3 2
1 2 4
4 5
2 2 2 16
4 5
2 2 2 3
output:
2
11
3
4
8
8
Note:
|
|
1999C
|
https://codeforces.com/problemset/problem/1999/C
|
Showering
| 800
|
[
"greedy",
"implementation"
] |
Div. 4
| 2,000
| 256
|
As a computer science student, Alex faces a hard challenge β showering. He tries to shower daily, but despite his best efforts there are always challenges. He takes $s$ minutes to shower and a day only has $m$ minutes!
He already has $n$ tasks planned for the day. Task $i$ is represented as an interval $(l_i$, $r_i)$, which means that Alex is busy and can not take a shower in that time interval (at any point in time strictly between $l_i$ and $r_i$). No two tasks overlap.
Given all $n$ time intervals, will Alex be able to shower that day? In other words, will Alex have a free time interval of length at least $s$?

In the first test case, Alex can shower for the first $3$ minutes of the day and not miss any of the tasks.
|
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 three integers $n$, $s$, and $m$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq s, m \leq 10^9$) β the number of time intervals Alex already has planned, the amount of time Alex takes to take a shower, and the amount of minutes a day has.
Then $n$ lines follow, the $i$-th of which contains two integers $l_i$ and $r_i$ ($0 \leq l_i < r_i \leq m$) β the time interval of the $i$-th task. No two tasks overlap.
Additional constraint on the input: $l_i > r_{i-1}$ for every $i > 1$.
The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case output "YES" (without quotes) if Alex can take a shower for that given test case, and "NO" (also without quotes) otherwise.
You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", and "Yes" will be recognized as a positive response).
|
[
[
"4\n3 3 10\n3 5\n6 8\n9 10\n3 3 10\n1 2\n3 5\n6 7\n3 3 10\n1 2\n3 5\n6 8\n3 4 10\n1 2\n6 7\n8 9",
"YES\nYES\nNO\nYES"
]
] |
Title: Showering
time_limit_ms: 2000
memory_limit_mb: 256
Description: As a computer science student, Alex faces a hard challenge β showering. He tries to shower daily, but despite his best efforts there are always challenges. He takes $s$ minutes to shower and a day only has $m$ minutes!
He already has $n$ tasks planned for the day. Task $i$ is represented as an interval $(l_i$, $r_i)$, which means that Alex is busy and can not take a shower in that time interval (at any point in time strictly between $l_i$ and $r_i$). No two tasks overlap.
Given all $n$ time intervals, will Alex be able to shower that day? In other words, will Alex have a free time interval of length at least $s$?

In the first test case, Alex can shower for the first $3$ minutes of the day and not miss any of the tasks.
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 three integers $n$, $s$, and $m$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq s, m \leq 10^9$) β the number of time intervals Alex already has planned, the amount of time Alex takes to take a shower, and the amount of minutes a day has.
Then $n$ lines follow, the $i$-th of which contains two integers $l_i$ and $r_i$ ($0 \leq l_i < r_i \leq m$) β the time interval of the $i$-th task. No two tasks overlap.
Additional constraint on the input: $l_i > r_{i-1}$ for every $i > 1$.
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 Alex can take a shower for that given test case, and "NO" (also without quotes) otherwise.
You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", and "Yes" will be recognized as a positive response).
Examples:
input:
4
3 3 10
3 5
6 8
9 10
3 3 10
1 2
3 5
6 7
3 3 10
1 2
3 5
6 8
3 4 10
1 2
6 7
8 9
output:
YES
YES
NO
YES
Note:
|
|
1985H2
|
https://codeforces.com/problemset/problem/1985/H2
|
Maximize the Largest Component (Hard Version)
| 2,200
|
[
"data structures",
"dfs and similar",
"dp",
"dsu",
"implementation"
] |
Div. 4
| 2,000
| 512
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. The only difference between the two versions is the operation.
Alex has a grid with $n$ rows and $m$ columns consisting of '.' and '#' characters. A set of '#' cells forms a connected component if from any cell in this set, it is possible to reach any other cell in this set by only moving to another cell in the set that shares a common side. The size of a connected component is the number of cells in the set.
In one operation, Alex selects any row $r$ ($1 \le r \le n$) and any column $c$ ($1 \le c \le m$), then sets every cell in row $r$ and column $c$ to be '#'. Help Alex find the maximum possible size of the largest connected component of '#' cells that he can achieve after performing the operation at most once.
|
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$ and $m$ ($1 \le n \cdot m \le 10^6$) β the number of rows and columns of the grid.
The next $n$ lines each contain $m$ characters. Each character is either '.' or '#'.
It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$.
|
For each test case, output a single integer β the maximum possible size of a connected component of '#' cells that Alex can achieve.
|
[
[
"6\n1 1\n.\n4 2\n..\n#.\n#.\n.#\n3 5\n.#.#.\n..#..\n.#.#.\n5 5\n#...#\n....#\n#...#\n.....\n...##\n6 6\n.#..#.\n#..#..\n.#...#\n#.#.#.\n.#.##.\n###..#\n6 8\n..#....#\n.####.#.\n###.#..#\n.##.#.##\n.#.##.##\n#..##.#.",
"1\n7\n11\n16\n22\n36"
]
] |
In the fourth test case, it is optimal for Alex to set all cells in row $4$ and column $2$ to be '#'. Doing so will lead to the largest connected component of '#' having a size of $16$.
In the fifth test case, it is optimal for Alex to set all cells in row $2$ and column $4$ to be '#'. Doing so will lead to the largest connected component of '#' having a size of $22$.
|
Title: Maximize the Largest Component (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. The only difference between the two versions is the operation.
Alex has a grid with $n$ rows and $m$ columns consisting of '.' and '#' characters. A set of '#' cells forms a connected component if from any cell in this set, it is possible to reach any other cell in this set by only moving to another cell in the set that shares a common side. The size of a connected component is the number of cells in the set.
In one operation, Alex selects any row $r$ ($1 \le r \le n$) and any column $c$ ($1 \le c \le m$), then sets every cell in row $r$ and column $c$ to be '#'. Help Alex find the maximum possible size of the largest connected component of '#' cells that he can achieve after performing the operation at most once.
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$ and $m$ ($1 \le n \cdot m \le 10^6$) β the number of rows and columns of the grid.
The next $n$ lines each contain $m$ characters. Each character is either '.' or '#'.
It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$.
Output: For each test case, output a single integer β the maximum possible size of a connected component of '#' cells that Alex can achieve.
Examples:
input:
6
1 1
.
4 2
..
#.
#.
.#
3 5
.#.#.
..#..
.#.#.
5 5
#...#
....#
#...#
.....
...##
6 6
.#..#.
#..#..
.#...#
#.#.#.
.#.##.
###..#
6 8
..#....#
.####.#.
###.#..#
.##.#.##
.#.##.##
#..##.#.
output:
1
7
11
16
22
36
Note: In the fourth test case, it is optimal for Alex to set all cells in row $4$ and column $2$ to be '#'. Doing so will lead to the largest connected component of '#' having a size of $16$.
In the fifth test case, it is optimal for Alex to set all cells in row $2$ and column $4$ to be '#'. Doing so will lead to the largest connected component of '#' having a size of $22$.
|
2032A
|
https://codeforces.com/problemset/problem/2032/A
|
Circuit
| 800
|
[
"greedy",
"implementation",
"math",
"number theory"
] |
Div. 2
| 1,000
| 256
|
Alice has just crafted a circuit with $n$ lights and $2n$ switches. Each component (a light or a switch) has two states: on or off. The lights and switches are arranged in a way that:
* Each light is connected to exactly two switches. * Each switch is connected to exactly one light. It's unknown which light each switch is connected to. * When all switches are off, all lights are also off. * If a switch is toggled (from on to off, or vice versa), the state of the light connected to it will also toggle.
Alice brings the circuit, which shows only the states of the $2n$ switches, to her sister Iris and gives her a riddle: what is the minimum and maximum number of lights that can be turned on?
Knowing her little sister's antics too well, Iris takes no more than a second to give Alice a correct answer. Can you do the same?
|
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 50$) β the number of lights in the circuit.
The second line of each test case contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($0 \le a_i \le 1$) β the states of the switches in the circuit. $a_i = 0$ means the $i$-th switch is off, and $a_i = 1$ means the $i$-th switch is on.
|
For each test case, output two integers β the minimum and maximum number of lights, respectively, that can be turned on.
|
[
[
"5\n1\n0 0\n1\n0 1\n1\n1 1\n3\n0 0 1 0 1 0\n3\n0 1 1 1 0 0",
"0 0\n1 1\n0 0\n0 2\n1 3"
]
] |
In the first test case, there is only one light in the circuit, and no switch is on, so the light is certainly off.
In the second test case, there is only one light in the circuit, but one switch connected to it is on, so the light is on.
In the third test case, there is only one light in the circuit, and both switches are on, so the light is off as it was toggled twice.
In the fourth test case, to have no lights on, the switches can be arranged in this way:
* Switch $1$ and switch $4$ are connected to light $1$. Since both switches are off, light $1$ is also off. * Switch $2$ and switch $6$ are connected to light $2$. Since both switches are off, light $2$ is also off. * Switch $3$ and switch $5$ are connected to light $3$. Both switches are on, so light $3$ is toggled twice from its initial off state, and thus also stays off.
And to have $2$ lights on, the switches can be arranged in this way:
* Switch $1$ and switch $2$ are connected to light $1$. Since both switches are off, light $1$ is also off. * Switch $3$ and switch $4$ are connected to light $2$. Since switch $3$ is on and switch $4$ is off, light $2$ is toggled once from its initial off state, so it is on. * Switch $5$ and switch $6$ are connected to light $3$. Since switch $5$ is on and switch $6$ is off, light $3$ is toggled once from its initial off state, so it is on.
|
Title: Circuit
time_limit_ms: 1000
memory_limit_mb: 256
Description: Alice has just crafted a circuit with $n$ lights and $2n$ switches. Each component (a light or a switch) has two states: on or off. The lights and switches are arranged in a way that:
* Each light is connected to exactly two switches. * Each switch is connected to exactly one light. It's unknown which light each switch is connected to. * When all switches are off, all lights are also off. * If a switch is toggled (from on to off, or vice versa), the state of the light connected to it will also toggle.
Alice brings the circuit, which shows only the states of the $2n$ switches, to her sister Iris and gives her a riddle: what is the minimum and maximum number of lights that can be turned on?
Knowing her little sister's antics too well, Iris takes no more than a second to give Alice a correct answer. Can you do the same?
Input: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 50$) β the number of lights in the circuit.
The second line of each test case contains $2n$ integers $a_1, a_2, \ldots, a_{2n}$ ($0 \le a_i \le 1$) β the states of the switches in the circuit. $a_i = 0$ means the $i$-th switch is off, and $a_i = 1$ means the $i$-th switch is on.
Output: For each test case, output two integers β the minimum and maximum number of lights, respectively, that can be turned on.
Examples:
input:
5
1
0 0
1
0 1
1
1 1
3
0 0 1 0 1 0
3
0 1 1 1 0 0
output:
0 0
1 1
0 0
0 2
1 3
Note: In the first test case, there is only one light in the circuit, and no switch is on, so the light is certainly off.
In the second test case, there is only one light in the circuit, but one switch connected to it is on, so the light is on.
In the third test case, there is only one light in the circuit, and both switches are on, so the light is off as it was toggled twice.
In the fourth test case, to have no lights on, the switches can be arranged in this way:
* Switch $1$ and switch $4$ are connected to light $1$. Since both switches are off, light $1$ is also off. * Switch $2$ and switch $6$ are connected to light $2$. Since both switches are off, light $2$ is also off. * Switch $3$ and switch $5$ are connected to light $3$. Both switches are on, so light $3$ is toggled twice from its initial off state, and thus also stays off.
And to have $2$ lights on, the switches can be arranged in this way:
* Switch $1$ and switch $2$ are connected to light $1$. Since both switches are off, light $1$ is also off. * Switch $3$ and switch $4$ are connected to light $2$. Since switch $3$ is on and switch $4$ is off, light $2$ is toggled once from its initial off state, so it is on. * Switch $5$ and switch $6$ are connected to light $3$. Since switch $5$ is on and switch $6$ is off, light $3$ is toggled once from its initial off state, so it is on.
|
1989A
|
https://codeforces.com/problemset/problem/1989/A
|
Catch the Coin
| 800
|
[
"implementation"
] |
Div. 2
| 2,000
| 256
|
Monocarp visited a retro arcade club with arcade cabinets. There got curious about the "Catch the Coin" cabinet.
The game is pretty simple. The screen represents a coordinate grid such that:
* the X-axis is directed from left to right; * the Y-axis is directed from bottom to top; * the center of the screen has coordinates $(0, 0)$.
At the beginning of the game, the character is located in the center, and $n$ coins appear on the screen β the $i$-th coin is at coordinates $(x_i, y_i)$. The coordinates of all coins are different and not equal to $(0, 0)$.
In one second, Monocarp can move the character in one of eight directions. If the character is at coordinates $(x, y)$, then it can end up at any of the coordinates $(x, y + 1)$, $(x + 1, y + 1)$, $(x + 1, y)$, $(x + 1, y - 1)$, $(x, y - 1)$, $(x - 1, y - 1)$, $(x - 1, y)$, $(x - 1, y + 1)$.
If the character ends up at the coordinates with a coin, then Monocarp collects that coin.
After Monocarp makes a move, all coins fall down by $1$, that is, they move from $(x, y)$ to $(x, y - 1)$. You can assume that the game field is infinite in all directions.
Monocarp wants to collect at least one coin, but cannot decide which coin to go for. Help him determine, for each coin, whether he can collect it.
|
The first line contains a single integer $n$ ($1 \le n \le 500$) β the number of coins.
In the $i$-th of the next $n$ lines, two integers $x_i$ and $y_i$ ($-50 \le x_i, y_i \le 50$) are written β the coordinates of the $i$-th coin. The coordinates of all coins are different. No coin is located at $(0, 0)$.
|
For each coin, print "YES" if Monocarp can collect it. Otherwise, print "NO".
|
[
[
"5\n24 42\n-2 -1\n-1 -2\n0 -50\n15 0",
"YES\nYES\nNO\nNO\nYES"
]
] |
Pay attention to the second coin in the example. Monocarp can first move from $(0, 0)$ to $(-1, -1)$. Then the coin falls $1$ down and ends up at $(-2, -2)$. Finally, Monocarp moves to $(-2, -2)$ and collects the coin.
|
Title: Catch the Coin
time_limit_ms: 2000
memory_limit_mb: 256
Description: Monocarp visited a retro arcade club with arcade cabinets. There got curious about the "Catch the Coin" cabinet.
The game is pretty simple. The screen represents a coordinate grid such that:
* the X-axis is directed from left to right; * the Y-axis is directed from bottom to top; * the center of the screen has coordinates $(0, 0)$.
At the beginning of the game, the character is located in the center, and $n$ coins appear on the screen β the $i$-th coin is at coordinates $(x_i, y_i)$. The coordinates of all coins are different and not equal to $(0, 0)$.
In one second, Monocarp can move the character in one of eight directions. If the character is at coordinates $(x, y)$, then it can end up at any of the coordinates $(x, y + 1)$, $(x + 1, y + 1)$, $(x + 1, y)$, $(x + 1, y - 1)$, $(x, y - 1)$, $(x - 1, y - 1)$, $(x - 1, y)$, $(x - 1, y + 1)$.
If the character ends up at the coordinates with a coin, then Monocarp collects that coin.
After Monocarp makes a move, all coins fall down by $1$, that is, they move from $(x, y)$ to $(x, y - 1)$. You can assume that the game field is infinite in all directions.
Monocarp wants to collect at least one coin, but cannot decide which coin to go for. Help him determine, for each coin, whether he can collect it.
Input: The first line contains a single integer $n$ ($1 \le n \le 500$) β the number of coins.
In the $i$-th of the next $n$ lines, two integers $x_i$ and $y_i$ ($-50 \le x_i, y_i \le 50$) are written β the coordinates of the $i$-th coin. The coordinates of all coins are different. No coin is located at $(0, 0)$.
Output: For each coin, print "YES" if Monocarp can collect it. Otherwise, print "NO".
Examples:
input:
5
24 42
-2 -1
-1 -2
0 -50
15 0
output:
YES
YES
NO
NO
YES
Note: Pay attention to the second coin in the example. Monocarp can first move from $(0, 0)$ to $(-1, -1)$. Then the coin falls $1$ down and ends up at $(-2, -2)$. Finally, Monocarp moves to $(-2, -2)$ and collects the coin.
|
2008F
|
https://codeforces.com/problemset/problem/2008/F
|
Sakurako's Box
| 1,400
|
[
"combinatorics",
"math",
"number theory"
] |
Div. 3
| 2,000
| 256
|
Sakurako has a box with $n$ balls. Each ball has it's value. She wants to bet with her friend that if the friend randomly picks two balls from the box (it could be two distinct balls, but they may have the same value), the product of their values will be the same as the number that Sakurako guessed.
Since Sakurako has a PhD in probability, she knows that the best number to pick is [the expected value](http://tiny.cc/matozh_en), but she forgot how to calculate it. Help Sakurako and find the expected value of the product of two elements from the array.
It can be shown that the expected value has the form $\frac{P}{Q}$, where $P$ and $Q$ are non-negative integers, and $Q \ne 0$. Report the value of $P \cdot Q^{-1}(\bmod 10^9+7)$.
|
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 number of elements in the array.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0\le a_i\le 10^9$) β the elements of the array.
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 value of $P \cdot Q^{-1}(\bmod 10^9+7)$.
|
[
[
"3\n3\n3 2 3\n4\n2 2 2 4\n5\n1 2 3 4 5",
"7\n6\n500000012"
]
] |
For the first test, Sakurako's friend can pick these pairs of balls: $(a_1,a_2)$, $(a_1,a_3)$, $(a_2,a_3)$. Their products equal to $3\cdot 2=6$ , $3\cdot 3=9$ , $3\cdot 2=6$ respectively, so the expected value is $\frac{6+9+6}{3}=7$.
For the second test, Sakurako's friend can pick these pairs of balls: $(a_1,a_2)$, $(a_1,a_3)$, $(a_1,a_4)$, $(a_2,a_3)$, $(a_2,a_4)$, $(a_3,a_4)$. Their products equal to $2\cdot 2=4$ , $2\cdot 2=4$ , $2\cdot 4=8$, $2\cdot 2=4$, $2\cdot 4=8$, $2\cdot 4=8$ respectively, so the expected value is $\frac{4+4+8+4+8+8}{6}=\frac{36}{6}=6$.
|
Title: Sakurako's Box
time_limit_ms: 2000
memory_limit_mb: 256
Description: Sakurako has a box with $n$ balls. Each ball has it's value. She wants to bet with her friend that if the friend randomly picks two balls from the box (it could be two distinct balls, but they may have the same value), the product of their values will be the same as the number that Sakurako guessed.
Since Sakurako has a PhD in probability, she knows that the best number to pick is [the expected value](http://tiny.cc/matozh_en), but she forgot how to calculate it. Help Sakurako and find the expected value of the product of two elements from the array.
It can be shown that the expected value has the form $\frac{P}{Q}$, where $P$ and $Q$ are non-negative integers, and $Q \ne 0$. Report the value of $P \cdot Q^{-1}(\bmod 10^9+7)$.
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 number of elements in the array.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0\le a_i\le 10^9$) β the elements of the array.
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 value of $P \cdot Q^{-1}(\bmod 10^9+7)$.
Examples:
input:
3
3
3 2 3
4
2 2 2 4
5
1 2 3 4 5
output:
7
6
500000012
Note: For the first test, Sakurako's friend can pick these pairs of balls: $(a_1,a_2)$, $(a_1,a_3)$, $(a_2,a_3)$. Their products equal to $3\cdot 2=6$ , $3\cdot 3=9$ , $3\cdot 2=6$ respectively, so the expected value is $\frac{6+9+6}{3}=7$.
For the second test, Sakurako's friend can pick these pairs of balls: $(a_1,a_2)$, $(a_1,a_3)$, $(a_1,a_4)$, $(a_2,a_3)$, $(a_2,a_4)$, $(a_3,a_4)$. Their products equal to $2\cdot 2=4$ , $2\cdot 2=4$ , $2\cdot 4=8$, $2\cdot 2=4$, $2\cdot 4=8$, $2\cdot 4=8$ respectively, so the expected value is $\frac{4+4+8+4+8+8}{6}=\frac{36}{6}=6$.
|
2000A
|
https://codeforces.com/problemset/problem/2000/A
|
Primary Task
| 800
|
[
"implementation",
"math",
"strings"
] |
Div. 3
| 1,000
| 256
|
Dmitry wrote down $t$ integers on the board, and that is good. He is sure that he lost an important integer $n$ among them, and that is bad.
The integer $n$ had the form $\text{10^x}$ ($x \ge 2$), where the symbol '$\text{^}$' denotes exponentiation.. Something went wrong, and Dmitry missed the symbol '$\text{^}$' when writing the important integer. For example, instead of the integer $10^5$, he would have written $105$, and instead of $10^{19}$, he would have written $1019$.
Dmitry wants to understand which of the integers on the board could have been the important integer and which could not.
|
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of integers on the board.
The next $t$ lines each contain an integer $a$ ($1 \le a \le 10000$) β the next integer from the board.
|
For each integer on the board, output "YES" if it could have been the important integer and "NO" otherwise.
You may 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.
|
[
[
"7\n100\n1010\n101\n105\n2033\n1019\n1002",
"NO\nYES\nNO\nYES\nNO\nYES\nNO"
]
] |
Title: Primary Task
time_limit_ms: 1000
memory_limit_mb: 256
Description: Dmitry wrote down $t$ integers on the board, and that is good. He is sure that he lost an important integer $n$ among them, and that is bad.
The integer $n$ had the form $\text{10^x}$ ($x \ge 2$), where the symbol '$\text{^}$' denotes exponentiation.. Something went wrong, and Dmitry missed the symbol '$\text{^}$' when writing the important integer. For example, instead of the integer $10^5$, he would have written $105$, and instead of $10^{19}$, he would have written $1019$.
Dmitry wants to understand which of the integers on the board could have been the important integer and which could not.
Input: The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β the number of integers on the board.
The next $t$ lines each contain an integer $a$ ($1 \le a \le 10000$) β the next integer from the board.
Output: For each integer on the board, output "YES" if it could have been the important integer and "NO" otherwise.
You may 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:
7
100
1010
101
105
2033
1019
1002
output:
NO
YES
NO
YES
NO
YES
NO
Note:
|
|
1992A
|
https://codeforces.com/problemset/problem/1992/A
|
Only Pluses
| 800
|
[
"brute force",
"constructive algorithms",
"greedy",
"math",
"sortings"
] |
Div. 3
| 1,000
| 256
|
Kmes has written three integers $a$, $b$ and $c$ in order to remember that he has to give Noobish_Monk $a \times b \times c$ bananas.
Noobish_Monk has found these integers and decided to do the following at most $5$ times:
* pick one of these integers; * increase it by $1$.
For example, if $a = 2$, $b = 3$ and $c = 4$, then one can increase $a$ three times by one and increase $b$ two times. After that $a = 5$, $b = 5$, $c = 4$. Then the total number of bananas will be $5 \times 5 \times 4 = 100$.
What is the maximum value of $a \times b \times c$ Noobish_Monk can achieve with these operations?
|
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases. The description of the test cases follows.
The first and only line of each test case contains three integers $a$, $b$ and $c$ ($1 \le a, b, c \le 10$) β Kmes's integers.
|
For each test case, output a single integer β the maximum amount of bananas Noobish_Monk can get.
|
[
[
"2\n2 3 4\n10 1 10",
"100\n600"
]
] |
Title: Only Pluses
time_limit_ms: 1000
memory_limit_mb: 256
Description: Kmes has written three integers $a$, $b$ and $c$ in order to remember that he has to give Noobish_Monk $a \times b \times c$ bananas.
Noobish_Monk has found these integers and decided to do the following at most $5$ times:
* pick one of these integers; * increase it by $1$.
For example, if $a = 2$, $b = 3$ and $c = 4$, then one can increase $a$ three times by one and increase $b$ two times. After that $a = 5$, $b = 5$, $c = 4$. Then the total number of bananas will be $5 \times 5 \times 4 = 100$.
What is the maximum value of $a \times b \times c$ Noobish_Monk can achieve with these operations?
Input: Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases. The description of the test cases follows.
The first and only line of each test case contains three integers $a$, $b$ and $c$ ($1 \le a, b, c \le 10$) β Kmes's integers.
Output: For each test case, output a single integer β the maximum amount of bananas Noobish_Monk can get.
Examples:
input:
2
2 3 4
10 1 10
output:
100
600
Note:
|
|
1995C
|
https://codeforces.com/problemset/problem/1995/C
|
Squaring
| 1,800
|
[
"brute force",
"constructive algorithms",
"greedy",
"implementation",
"math",
"number theory"
] |
Div. 2
| 2,000
| 256
|
ikrpprpp found an array $a$ consisting of integers. He likes justice, so he wants to make $a$ fair β that is, make it non-decreasing. To do that, he can perform an act of justice on an index $1 \le i \le n$ of the array, which will replace $a_i$ with $a_i ^ 2$ (the element at position $i$ with its square). For example, if $a = [2,4,3,3,5,3]$ and ikrpprpp chooses to perform an act of justice on $i = 4$, $a$ becomes $[2,4,3,9,5,3]$.
What is the minimum number of acts of justice needed to make the array non- decreasing?
|
First line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases. It is followed by the description of test cases.
For each test case, the first line contains an integer $n$ β size of the array $a$. The second line contains $n$ ($1 \le n \le 2 \cdot 10 ^5$) integers $a_1, a_2,\ldots, a_n$ ($1 \le a_i \le 10 ^ 6$).
The sum of $n$ over all test cases does not exceed $2 \cdot {10}^5$.
|
For each testcase, print an integer β minimum number of acts of justice required to make the array $a$ non-decreasing. If it is impossible to do that, print $-1$.
|
[
[
"7\n3\n1 2 3\n2\n3 2\n3\n3 1 5\n4\n1 1 2 3\n3\n4 3 2\n9\n16 2 4 2 256 2 4 2 8\n11\n10010 10009 10008 10007 10006 10005 10004 10003 10002 10001 10000",
"0\n1\n-1\n0\n3\n15\n55"
]
] |
In the first test case, there's no need to perform acts of justice. The array is fair on its own!
In the third test case, it can be proven that the array cannot become non- decreasing.
In the fifth test case, ikrpprppp can perform an act of justice on index 3, then an act of justice on index 2, and finally yet another act of justice on index 3. After that, $a$ will become $[4, 9, 16]$.
|
Title: Squaring
time_limit_ms: 2000
memory_limit_mb: 256
Description: ikrpprpp found an array $a$ consisting of integers. He likes justice, so he wants to make $a$ fair β that is, make it non-decreasing. To do that, he can perform an act of justice on an index $1 \le i \le n$ of the array, which will replace $a_i$ with $a_i ^ 2$ (the element at position $i$ with its square). For example, if $a = [2,4,3,3,5,3]$ and ikrpprpp chooses to perform an act of justice on $i = 4$, $a$ becomes $[2,4,3,9,5,3]$.
What is the minimum number of acts of justice needed to make the array non- decreasing?
Input: First line contains an integer $t$ ($1 \le t \le 1000$) β the number of test cases. It is followed by the description of test cases.
For each test case, the first line contains an integer $n$ β size of the array $a$. The second line contains $n$ ($1 \le n \le 2 \cdot 10 ^5$) integers $a_1, a_2,\ldots, a_n$ ($1 \le a_i \le 10 ^ 6$).
The sum of $n$ over all test cases does not exceed $2 \cdot {10}^5$.
Output: For each testcase, print an integer β minimum number of acts of justice required to make the array $a$ non-decreasing. If it is impossible to do that, print $-1$.
Examples:
input:
7
3
1 2 3
2
3 2
3
3 1 5
4
1 1 2 3
3
4 3 2
9
16 2 4 2 256 2 4 2 8
11
10010 10009 10008 10007 10006 10005 10004 10003 10002 10001 10000
output:
0
1
-1
0
3
15
55
Note: In the first test case, there's no need to perform acts of justice. The array is fair on its own!
In the third test case, it can be proven that the array cannot become non- decreasing.
In the fifth test case, ikrpprppp can perform an act of justice on index 3, then an act of justice on index 2, and finally yet another act of justice on index 3. After that, $a$ will become $[4, 9, 16]$.
|
2002C
|
https://codeforces.com/problemset/problem/2002/C
|
Black Circles
| 1,200
|
[
"brute force",
"geometry",
"greedy",
"math"
] |
Div. 1 + 2
| 2,000
| 256
|
There are $n$ circles on a two-dimensional plane. The $i$-th circle is centered at $(x_i,y_i)$. Initially, all circles have a radius of $0$.
The circles' radii increase at a rate of $1$ unit per second.
You are currently at $(x_s,y_s)$; your goal is to reach $(x_t,y_t)$ without touching the circumference of any circle (including the moment you reach $(x_t,y_t)$). You can move in any direction you want. However, your speed is limited to $1$ unit per second.
Please determine whether this is possible.
|
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\le10^5$) β the number of circles.
The next $n$ lines each contain two integers $x_i$, $y_i$ ($1\le x_i,y_i\le10^9$) β the center of each circle.
The final line contains four integers $x_s$, $y_s$, $x_t$, $y_t$ ($1\le x_s,y_s,x_t,y_t\le10^9$) β the coordinates of the starting point and the goal, respectively.
It is guaranteed that these $n+2$ points are distinct.
It is guaranteed that the sum of $n$ over all testcases does not exceed $10^5$.
|
For each test case, output $\texttt{YES}$ if it is possible to reach the goal without touching the circle boundaries, 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).
|
[
[
"7\n3\n2 5\n2 14\n10 13\n4 9 9 7\n3\n10 11\n6 9\n12 12\n14 13 4 8\n1\n5 7\n12 6 11 13\n2\n1000000000 2\n2 1000000000\n1 1 2 2\n1\n999999998 1000000000\n999999999 999999999 1 1\n1\n1000000000 1\n1 1000000000 1 1\n10\n989237121 2397081\n206669655 527238537\n522705783 380636165\n532545346 320061691\n207818728 199485303\n884520552 315781807\n992311437 802563521\n205138355 324818663\n223575704 395073023\n281560523 236279118\n216941610 572010615 323956540 794523071",
"YES\nNO\nYES\nYES\nYES\nNO\nYES"
]
] |
In the first test case, a feasible way of movement is as follows.

|
Title: Black Circles
time_limit_ms: 2000
memory_limit_mb: 256
Description: There are $n$ circles on a two-dimensional plane. The $i$-th circle is centered at $(x_i,y_i)$. Initially, all circles have a radius of $0$.
The circles' radii increase at a rate of $1$ unit per second.
You are currently at $(x_s,y_s)$; your goal is to reach $(x_t,y_t)$ without touching the circumference of any circle (including the moment you reach $(x_t,y_t)$). You can move in any direction you want. However, your speed is limited to $1$ unit per second.
Please determine whether this is possible.
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\le10^5$) β the number of circles.
The next $n$ lines each contain two integers $x_i$, $y_i$ ($1\le x_i,y_i\le10^9$) β the center of each circle.
The final line contains four integers $x_s$, $y_s$, $x_t$, $y_t$ ($1\le x_s,y_s,x_t,y_t\le10^9$) β the coordinates of the starting point and the goal, respectively.
It is guaranteed that these $n+2$ points are distinct.
It is guaranteed that the sum of $n$ over all testcases does not exceed $10^5$.
Output: For each test case, output $\texttt{YES}$ if it is possible to reach the goal without touching the circle boundaries, 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:
7
3
2 5
2 14
10 13
4 9 9 7
3
10 11
6 9
12 12
14 13 4 8
1
5 7
12 6 11 13
2
1000000000 2
2 1000000000
1 1 2 2
1
999999998 1000000000
999999999 999999999 1 1
1
1000000000 1
1 1000000000 1 1
10
989237121 2397081
206669655 527238537
522705783 380636165
532545346 320061691
207818728 199485303
884520552 315781807
992311437 802563521
205138355 324818663
223575704 395073023
281560523 236279118
216941610 572010615 323956540 794523071
output:
YES
NO
YES
YES
YES
NO
YES
Note: In the first test case, a feasible way of movement is as follows.

|
2030A
|
https://codeforces.com/problemset/problem/2030/A
|
A Gift From Orangutan
| 800
|
[
"constructive algorithms",
"greedy",
"math",
"sortings"
] |
Div. 2
| 1,000
| 256
|
While exploring the jungle, you have bumped into a rare orangutan with a bow tie! You shake hands with the orangutan and offer him some food and water. In return...
The orangutan has gifted you an array $a$ of length $n$. Using $a$, you will construct two arrays $b$ and $c$, both containing $n$ elements, in the following manner:
* $b_i = \min(a_1, a_2, \ldots, a_i)$ for each $1 \leq i \leq n$. * $c_i = \max(a_1, a_2, \ldots, a_i)$ for each $1 \leq i \leq n$.
Define the score of $a$ as $\sum_{i=1}^n c_i - b_i$ (i.e. the sum of $c_i - b_i$ over all $1 \leq i \leq n$). Before you calculate the score, you can shuffle the elements of $a$ however you want.
Find the maximum score that you can get if you shuffle the elements of $a$ optimally.
|
The first line contains $t$ ($1 \leq t \leq 100$) β the number of test cases.
The first line of each test case contains an integer $n$ ($1 \leq n \leq 1000$) β the number of elements in $a$.
The following line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 1000$) β the elements of the array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$.
|
For each test case, output the maximum score that you can get.
|
[
[
"3\n1\n69\n3\n7 6 5\n5\n1 1 1 2 2",
"0\n4\n4"
]
] |
In the first test case, there is no other way to rearrange $a$. So, $b = [69]$ and $c = [69]$. The only possible score is $69 - 69 = 0$.
In the second test case, you can rearrange $a$ as $[7, 5, 6]$. Here, $b = [7, 5, 5]$ and $c = [7, 7, 7]$. The score in this case is $(7 - 7) + (7 - 5) + (7 - 5) = 4$. It can be shown this is the maximum possible score.
|
Title: A Gift From Orangutan
time_limit_ms: 1000
memory_limit_mb: 256
Description: While exploring the jungle, you have bumped into a rare orangutan with a bow tie! You shake hands with the orangutan and offer him some food and water. In return...
The orangutan has gifted you an array $a$ of length $n$. Using $a$, you will construct two arrays $b$ and $c$, both containing $n$ elements, in the following manner:
* $b_i = \min(a_1, a_2, \ldots, a_i)$ for each $1 \leq i \leq n$. * $c_i = \max(a_1, a_2, \ldots, a_i)$ for each $1 \leq i \leq n$.
Define the score of $a$ as $\sum_{i=1}^n c_i - b_i$ (i.e. the sum of $c_i - b_i$ over all $1 \leq i \leq n$). Before you calculate the score, you can shuffle the elements of $a$ however you want.
Find the maximum score that you can get if you shuffle the elements of $a$ optimally.
Input: The first line contains $t$ ($1 \leq t \leq 100$) β the number of test cases.
The first line of each test case contains an integer $n$ ($1 \leq n \leq 1000$) β the number of elements in $a$.
The following line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 1000$) β the elements of the array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$.
Output: For each test case, output the maximum score that you can get.
Examples:
input:
3
1
69
3
7 6 5
5
1 1 1 2 2
output:
0
4
4
Note: In the first test case, there is no other way to rearrange $a$. So, $b = [69]$ and $c = [69]$. The only possible score is $69 - 69 = 0$.
In the second test case, you can rearrange $a$ as $[7, 5, 6]$. Here, $b = [7, 5, 5]$ and $c = [7, 7, 7]$. The score in this case is $(7 - 7) + (7 - 5) + (7 - 5) = 4$. It can be shown this is the maximum possible score.
|
1996B
|
https://codeforces.com/problemset/problem/1996/B
|
Scale
| 800
|
[
"greedy",
"implementation"
] |
Div. 3
| 2,000
| 256
|
Tina has a square grid with $n$ rows and $n$ columns. Each cell in the grid is either $0$ or $1$.
Tina wants to reduce the grid by a factor of $k$ ($k$ is a divisor of $n$). To do this, Tina splits the grid into $k \times k$ nonoverlapping blocks of cells such that every cell belongs to exactly one block.
Tina then replaces each block of cells with a single cell equal to the value of the cells in the block. It is guaranteed that every cell in the same block has the same value.
For example, the following demonstration shows a grid being reduced by factor of $3$.
Original grid $0$| $0$| $0$| $1$| $1$| $1$ ---|---|---|---|---|--- $0$| $0$| $0$| $1$| $1$| $1$ $0$| $0$| $0$| $1$| $1$| $1$ $1$| $1$| $1$| $0$| $0$| $0$ $1$| $1$| $1$| $0$| $0$| $0$ $1$| $1$| $1$| $0$| $0$| $0$ Reduced grid $0$| $1$ ---|--- $1$| $0$ Help Tina reduce the grid by a factor of $k$.
|
The first line contains $t$ ($1 \leq t \leq 100$) β the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \leq n \leq 1000$, $1 \le k \le n$, $k$ is a divisor of $n$) β the number of rows and columns of the grid, and the factor that Tina wants to reduce the grid by.
Each of the following $n$ lines contain $n$ characters describing the cells of the grid. Each character is either $0$ or $1$. It is guaranteed every $k$ by $k$ block has the same value.
It is guaranteed the sum of $n$ over all test cases does not exceed $1000$.
|
For each test case, output the grid reduced by a factor of $k$ on a new line.
|
[
[
"4\n4 4\n0000\n0000\n0000\n0000\n6 3\n000111\n000111\n000111\n111000\n111000\n111000\n6 2\n001100\n001100\n111111\n111111\n110000\n110000\n8 1\n11111111\n11111111\n11111111\n11111111\n11111111\n11111111\n11111111\n11111111",
"0\n01\n10\n010\n111\n100\n11111111\n11111111\n11111111\n11111111\n11111111\n11111111\n11111111\n11111111"
]
] |
Title: Scale
time_limit_ms: 2000
memory_limit_mb: 256
Description: Tina has a square grid with $n$ rows and $n$ columns. Each cell in the grid is either $0$ or $1$.
Tina wants to reduce the grid by a factor of $k$ ($k$ is a divisor of $n$). To do this, Tina splits the grid into $k \times k$ nonoverlapping blocks of cells such that every cell belongs to exactly one block.
Tina then replaces each block of cells with a single cell equal to the value of the cells in the block. It is guaranteed that every cell in the same block has the same value.
For example, the following demonstration shows a grid being reduced by factor of $3$.
Original grid $0$| $0$| $0$| $1$| $1$| $1$ ---|---|---|---|---|--- $0$| $0$| $0$| $1$| $1$| $1$ $0$| $0$| $0$| $1$| $1$| $1$ $1$| $1$| $1$| $0$| $0$| $0$ $1$| $1$| $1$| $0$| $0$| $0$ $1$| $1$| $1$| $0$| $0$| $0$ Reduced grid $0$| $1$ ---|--- $1$| $0$ Help Tina reduce the grid by a factor of $k$.
Input: The first line contains $t$ ($1 \leq t \leq 100$) β the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \leq n \leq 1000$, $1 \le k \le n$, $k$ is a divisor of $n$) β the number of rows and columns of the grid, and the factor that Tina wants to reduce the grid by.
Each of the following $n$ lines contain $n$ characters describing the cells of the grid. Each character is either $0$ or $1$. It is guaranteed every $k$ by $k$ block has the same value.
It is guaranteed the sum of $n$ over all test cases does not exceed $1000$.
Output: For each test case, output the grid reduced by a factor of $k$ on a new line.
Examples:
input:
4
4 4
0000
0000
0000
0000
6 3
000111
000111
000111
111000
111000
111000
6 2
001100
001100
111111
111111
110000
110000
8 1
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
output:
0
01
10
010
111
100
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
Note:
|
|
2006D
|
https://codeforces.com/problemset/problem/2006/D
|
Iris and Adjacent Products
| 2,600
|
[
"data structures",
"greedy",
"implementation",
"math"
] |
Div. 1
| 3,000
| 256
|
Iris has just learned multiplication in her Maths lessons. However, since her brain is unable to withstand too complex calculations, she could not multiply two integers with the product greater than $k$ together. Otherwise, her brain may explode!
Her teacher sets a difficult task every day as her daily summer holiday homework. Now she is given an array $a$ consisting of $n$ elements, and she needs to calculate the product of each two adjacent elements (that is, $a_1 \cdot a_2$, $a_2 \cdot a_3$, and so on). Iris wants her brain to work safely, and in order to do that, she would like to modify the array $a$ in such a way that $a_i \cdot a_{i + 1} \leq k$ holds for every $1 \leq i < n$. There are two types of operations she can perform:
1. She can rearrange the elements of the array $a$ in an arbitrary way. 2. She can select an arbitrary element of the array $a$ and change its value to an arbitrary integer from $1$ to $k$.
Iris wants to minimize the number of operations of type $2$ that she uses.
However, that's completely not the end of the summer holiday! Summer holiday lasts for $q$ days, and on the $i$-th day, Iris is asked to solve the Math homework for the subarray $b_{l_i}, b_{l_i + 1}, \ldots, b_{r_i}$. Help Iris and tell her the minimum number of type $2$ operations she needs to perform for each day. Note that the operations are independent for each day, i.e. the array $b$ is not changed.
|
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 three integers $n$, $q$ and $k$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$, $1 \leq k \leq 10^6$) β the length of array $b$, the number of days, and the upper bound for the multiplication calculation.
The second line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq k$) β the elements of the array $b$.
Then $q$ lines follow, the $i$-th of them contains two integers $l_i$ and $r_i$ ($1 \leq l_i < r_i \leq n$) β the boundaries of the subarray on the $i$-th day.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$, and the sum of $q$ over all test cases does not exceed $10^5$.
|
For each test, output a single line containing $q$ integers β the minimum number of operations of type $2$ needed for each day.
|
[
[
"5\n3 1 1\n1 1 1\n1 3\n3 2 10\n1 10 9\n1 3\n2 3\n5 4 2\n2 2 2 2 2\n1 2\n2 4\n2 5\n1 5\n6 5 10\n3 2 5 10 10 1\n1 4\n3 6\n1 6\n2 5\n5 6\n10 10 10\n10 9 8 7 6 5 4 3 2 1\n1 10\n1 9\n1 8\n1 7\n2 10\n3 10\n4 10\n5 10\n3 9\n6 8",
"0 \n0 1 \n1 1 2 2 \n1 1 1 1 0 \n3 3 4 3 2 2 1 1 2 1"
]
] |
In the first test case, as Iris can always multiply $1$ and $1$ together, no operations are needed, so the answer is $0$.
In the second test case, the first day's homework is $[1, 10, 9]$. Iris can rearrange its elements to get $[9, 1, 10]$, so no operations of type $2$ are needed. The second day's homework is $[10, 9]$, and she can change one element to get the array $[1, 9]$, so one operation of type $2$ is needed.
|
Title: Iris and Adjacent Products
time_limit_ms: 3000
memory_limit_mb: 256
Description: Iris has just learned multiplication in her Maths lessons. However, since her brain is unable to withstand too complex calculations, she could not multiply two integers with the product greater than $k$ together. Otherwise, her brain may explode!
Her teacher sets a difficult task every day as her daily summer holiday homework. Now she is given an array $a$ consisting of $n$ elements, and she needs to calculate the product of each two adjacent elements (that is, $a_1 \cdot a_2$, $a_2 \cdot a_3$, and so on). Iris wants her brain to work safely, and in order to do that, she would like to modify the array $a$ in such a way that $a_i \cdot a_{i + 1} \leq k$ holds for every $1 \leq i < n$. There are two types of operations she can perform:
1. She can rearrange the elements of the array $a$ in an arbitrary way. 2. She can select an arbitrary element of the array $a$ and change its value to an arbitrary integer from $1$ to $k$.
Iris wants to minimize the number of operations of type $2$ that she uses.
However, that's completely not the end of the summer holiday! Summer holiday lasts for $q$ days, and on the $i$-th day, Iris is asked to solve the Math homework for the subarray $b_{l_i}, b_{l_i + 1}, \ldots, b_{r_i}$. Help Iris and tell her the minimum number of type $2$ operations she needs to perform for each day. Note that the operations are independent for each day, i.e. the array $b$ is not changed.
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 three integers $n$, $q$ and $k$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$, $1 \leq k \leq 10^6$) β the length of array $b$, the number of days, and the upper bound for the multiplication calculation.
The second line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq k$) β the elements of the array $b$.
Then $q$ lines follow, the $i$-th of them contains two integers $l_i$ and $r_i$ ($1 \leq l_i < r_i \leq n$) β the boundaries of the subarray on the $i$-th day.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$, and the sum of $q$ over all test cases does not exceed $10^5$.
Output: For each test, output a single line containing $q$ integers β the minimum number of operations of type $2$ needed for each day.
Examples:
input:
5
3 1 1
1 1 1
1 3
3 2 10
1 10 9
1 3
2 3
5 4 2
2 2 2 2 2
1 2
2 4
2 5
1 5
6 5 10
3 2 5 10 10 1
1 4
3 6
1 6
2 5
5 6
10 10 10
10 9 8 7 6 5 4 3 2 1
1 10
1 9
1 8
1 7
2 10
3 10
4 10
5 10
3 9
6 8
output:
0
0 1
1 1 2 2
1 1 1 1 0
3 3 4 3 2 2 1 1 2 1
Note: In the first test case, as Iris can always multiply $1$ and $1$ together, no operations are needed, so the answer is $0$.
In the second test case, the first day's homework is $[1, 10, 9]$. Iris can rearrange its elements to get $[9, 1, 10]$, so no operations of type $2$ are needed. The second day's homework is $[10, 9]$, and she can change one element to get the array $[1, 9]$, so one operation of type $2$ is needed.
|
2033B
|
https://codeforces.com/problemset/problem/2033/B
|
Sakurako and Water
| 900
|
[
"brute force",
"constructive algorithms",
"greedy"
] |
Div. 3
| 2,000
| 256
|
During her journey with Kosuke, Sakurako and Kosuke found a valley that can be represented as a matrix of size $n \times n$, where at the intersection of the $i$-th row and the $j$-th column is a mountain with a height of $a_{i,j}$. If $a_{i,j} < 0$, then there is a lake there.
Kosuke is very afraid of water, so Sakurako needs to help him:
* With her magic, she can select a square area of mountains and increase the height of each mountain on the main diagonal of that area by exactly one.
More formally, she can choose a submatrix with the upper left corner located at $(i, j)$ and the lower right corner at $(p, q)$, such that $p-i=q-j$. She can then add one to each element at the intersection of the $(i + k)$-th row and the $(j + k)$-th column, for all $k$ such that $0 \le k \le p-i$.
Determine the minimum number of times Sakurako must use her magic so that there are no lakes.
|
The first line contains a single integer $t$ ($1 \le t \le 200$) β the number of test cases.
Each test case is described as follows:
* The first line of each test case consists of a single number $n$ ($1 \le n \le 500$). * Each of the following $n$ lines consists of $n$ integers separated by spaces, which correspond to the heights of the mountains in the valley $a$ ($-10^5 \le a_{i,j} \le 10^5$).
It is guaranteed that the sum of $n$ across all test cases does not exceed $1000$.
|
For each test case, output the minimum number of times Sakurako will have to use her magic so that all lakes disappear.
|
[
[
"4\n1\n1\n2\n-1 2\n3 0\n3\n1 2 3\n-2 1 -1\n0 0 -1\n5\n1 1 -1 -1 3\n-3 1 4 4 -4\n-1 -1 3 0 -5\n4 5 3 -3 -1\n3 1 -3 -1 5",
"0\n1\n4\n19"
]
] |
Title: Sakurako and Water
time_limit_ms: 2000
memory_limit_mb: 256
Description: During her journey with Kosuke, Sakurako and Kosuke found a valley that can be represented as a matrix of size $n \times n$, where at the intersection of the $i$-th row and the $j$-th column is a mountain with a height of $a_{i,j}$. If $a_{i,j} < 0$, then there is a lake there.
Kosuke is very afraid of water, so Sakurako needs to help him:
* With her magic, she can select a square area of mountains and increase the height of each mountain on the main diagonal of that area by exactly one.
More formally, she can choose a submatrix with the upper left corner located at $(i, j)$ and the lower right corner at $(p, q)$, such that $p-i=q-j$. She can then add one to each element at the intersection of the $(i + k)$-th row and the $(j + k)$-th column, for all $k$ such that $0 \le k \le p-i$.
Determine the minimum number of times Sakurako must use her magic so that there are no lakes.
Input: The first line contains a single integer $t$ ($1 \le t \le 200$) β the number of test cases.
Each test case is described as follows:
* The first line of each test case consists of a single number $n$ ($1 \le n \le 500$). * Each of the following $n$ lines consists of $n$ integers separated by spaces, which correspond to the heights of the mountains in the valley $a$ ($-10^5 \le a_{i,j} \le 10^5$).
It is guaranteed that the sum of $n$ across all test cases does not exceed $1000$.
Output: For each test case, output the minimum number of times Sakurako will have to use her magic so that all lakes disappear.
Examples:
input:
4
1
1
2
-1 2
3 0
3
1 2 3
-2 1 -1
0 0 -1
5
1 1 -1 -1 3
-3 1 4 4 -4
-1 -1 3 0 -5
4 5 3 -3 -1
3 1 -3 -1 5
output:
0
1
4
19
Note:
|
|
2036B
|
https://codeforces.com/problemset/problem/2036/B
|
Startup
| 800
|
[
"greedy",
"sortings"
] |
Div. 3
| 2,000
| 256
|
Arseniy came up with another business plan β to sell soda from a vending machine! For this, he purchased a machine with $n$ shelves, as well as $k$ bottles, where the $i$-th bottle is characterized by the brand index $b_i$ and the cost $c_i$.
You can place any number of bottles on each shelf, but all bottles on the same shelf must be of the same brand.
Arseniy knows that all the bottles he puts on the shelves of the machine will be sold. Therefore, he asked you to calculate the maximum amount he can earn.
|
The first line contains one 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 2 \cdot 10^5$), where $n$ is the number of shelves in the machine, and $k$ is the number of bottles available to Arseniy.
The next $k$ lines contain two integers $b_i$ and $c_i$ ($1 \le b_i \le k, 1 \le c_i \le 1000$) β the brand and cost of the $i$-th bottle.
It is also guaranteed that the sum of $n$ across all test cases does not exceed $2 \cdot 10^5$ and that the sum of $k$ across all test cases also does not exceed $2 \cdot 10^5$.
|
For each test case, output one integer β the maximum amount that Arseniy can earn.
|
[
[
"4\n3 3\n2 6\n2 7\n1 15\n1 3\n2 6\n2 7\n1 15\n6 2\n1 7\n2 5\n190000 1\n1 1000",
"28\n15\n12\n1000"
]
] |
In the first test case, Arseniy has $3$ shelves in the vending machine. He can place, for example, two bottles of the brand $2$ on the first shelf and a bottle of the brand $1$ on the second shelf. Then the total cost of the bottles would be $6 + 7 + 15 = 28$.
In the second test case, he has only one shelf. It is not difficult to show that the optimal option is to place a bottle of the brand $1$ on it. Then the total cost will be $15$.
In the third test case, he has as many as $6$ shelves, so he can place all available bottles with a total cost of $7 + 5 = 12$.
|
Title: Startup
time_limit_ms: 2000
memory_limit_mb: 256
Description: Arseniy came up with another business plan β to sell soda from a vending machine! For this, he purchased a machine with $n$ shelves, as well as $k$ bottles, where the $i$-th bottle is characterized by the brand index $b_i$ and the cost $c_i$.
You can place any number of bottles on each shelf, but all bottles on the same shelf must be of the same brand.
Arseniy knows that all the bottles he puts on the shelves of the machine will be sold. Therefore, he asked you to calculate the maximum amount he can earn.
Input: The first line contains one 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 2 \cdot 10^5$), where $n$ is the number of shelves in the machine, and $k$ is the number of bottles available to Arseniy.
The next $k$ lines contain two integers $b_i$ and $c_i$ ($1 \le b_i \le k, 1 \le c_i \le 1000$) β the brand and cost of the $i$-th bottle.
It is also guaranteed that the sum of $n$ across all test cases does not exceed $2 \cdot 10^5$ and that the sum of $k$ across all test cases also does not exceed $2 \cdot 10^5$.
Output: For each test case, output one integer β the maximum amount that Arseniy can earn.
Examples:
input:
4
3 3
2 6
2 7
1 15
1 3
2 6
2 7
1 15
6 2
1 7
2 5
190000 1
1 1000
output:
28
15
12
1000
Note: In the first test case, Arseniy has $3$ shelves in the vending machine. He can place, for example, two bottles of the brand $2$ on the first shelf and a bottle of the brand $1$ on the second shelf. Then the total cost of the bottles would be $6 + 7 + 15 = 28$.
In the second test case, he has only one shelf. It is not difficult to show that the optimal option is to place a bottle of the brand $1$ on it. Then the total cost will be $15$.
In the third test case, he has as many as $6$ shelves, so he can place all available bottles with a total cost of $7 + 5 = 12$.
|
1998E1
|
https://codeforces.com/problemset/problem/1998/E1
|
Eliminating Balls With Merging (Easy Version)
| 2,200
|
[
"binary search",
"brute force",
"data structures",
"divide and conquer",
"greedy"
] |
Div. 2
| 4,000
| 512
|
Drink water.
β Sun Tzu, The Art of Becoming a Healthy Programmer
This is the easy version of the problem. The only difference is that $x=n$ in this version. You must solve both versions to be able to hack.
You are given two integers $n$ and $x$ ($x=n$). 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 = n$) β 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 5\n1 2 3 2 1\n7 7\n4 5 1 2 1 4 5\n11 11\n1 2 3 1 1 9 3 2 4 1 3",
"3\n4\n4"
]
] |
In the first test case, you are required to calculate $f(5)$. It can be shown that after $4$ operations, $S$ can contain $2$, $3$, or $4$. The following shows the operations required to make $S = \\{4\\}$.
* Initially, $S = \\{1, 2, 3, 4, 5\\}$ and $a = [1, 2, 3, 2, 1]$. * Choose $l = 1$. Naturally, $r = 2$. Since $a_1 < a_2$, we set $a_2 = 1 + 2$ and remove $1$ from $S$. Now, $S = \\{2, 3, 4, 5\\}$ and $a = [1, 3, 3, 2, 1]$. * Choose $l = 4$. Naturally, $r = 5$. Since $a_4 > a_5$, we set $a_4 = 2 + 1$ and remove $5$ from $S$. Now, $S = \\{2, 3, 4\\}$ and $a = [1, 3, 3, 3, 1]$. * Choose $l = 3$. Naturally, $r = 4$. Since $a_3 = a_4$, we have a choice whether to remove $3$ or $4$. Since we want to preserve $4$, let's remove $3$. So, set $a_4 = 3 + 3$ and remove $3$ from $S$. Now, $S = \\{2, 4\\}$ and $a = [1, 3, 3, 6, 1]$. * Choose $l = 2$. Naturally, $r = 4$. Since $a_2 < a_4$, we set $a_4 = 3 + 6$ and remove $2$ from $S$. Finally, $S = \\{4\\}$ and $a = [1, 3, 3, 9, 1]$.
In the second test case, you are required to calculate $f(7)$. It can be shown that after $6$ operations, $S$ can contain $2$, $4$, $6$, or $7$.
|
Title: Eliminating Balls With Merging (Easy Version)
time_limit_ms: 4000
memory_limit_mb: 512
Description: Drink water.
β Sun Tzu, The Art of Becoming a Healthy Programmer
This is the easy version of the problem. The only difference is that $x=n$ in this version. You must solve both versions to be able to hack.
You are given two integers $n$ and $x$ ($x=n$). 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 = n$) β 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 5
1 2 3 2 1
7 7
4 5 1 2 1 4 5
11 11
1 2 3 1 1 9 3 2 4 1 3
output:
3
4
4
Note: In the first test case, you are required to calculate $f(5)$. It can be shown that after $4$ operations, $S$ can contain $2$, $3$, or $4$. The following shows the operations required to make $S = \\{4\\}$.
* Initially, $S = \\{1, 2, 3, 4, 5\\}$ and $a = [1, 2, 3, 2, 1]$. * Choose $l = 1$. Naturally, $r = 2$. Since $a_1 < a_2$, we set $a_2 = 1 + 2$ and remove $1$ from $S$. Now, $S = \\{2, 3, 4, 5\\}$ and $a = [1, 3, 3, 2, 1]$. * Choose $l = 4$. Naturally, $r = 5$. Since $a_4 > a_5$, we set $a_4 = 2 + 1$ and remove $5$ from $S$. Now, $S = \\{2, 3, 4\\}$ and $a = [1, 3, 3, 3, 1]$. * Choose $l = 3$. Naturally, $r = 4$. Since $a_3 = a_4$, we have a choice whether to remove $3$ or $4$. Since we want to preserve $4$, let's remove $3$. So, set $a_4 = 3 + 3$ and remove $3$ from $S$. Now, $S = \\{2, 4\\}$ and $a = [1, 3, 3, 6, 1]$. * Choose $l = 2$. Naturally, $r = 4$. Since $a_2 < a_4$, we set $a_4 = 3 + 6$ and remove $2$ from $S$. Finally, $S = \\{4\\}$ and $a = [1, 3, 3, 9, 1]$.
In the second test case, you are required to calculate $f(7)$. It can be shown that after $6$ operations, $S$ can contain $2$, $4$, $6$, or $7$.
|
1983E
|
https://codeforces.com/problemset/problem/1983/E
|
I Love Balls
| 2,300
|
[
"combinatorics",
"math",
"probabilities"
] |
Div. 2
| 2,000
| 256
|
Alice and Bob are playing a game. There are $n$ balls, out of which $k$ are special. Each ball has a value associated with it.
The players play turn by turn. In each turn, the player randomly picks a ball and adds the value of the ball to their score, which is $0$ at the beginning of the game. The selected ball is removed from the game. If the ball was special, the same player takes the next turn if at least one ball is remaining. If the ball picked was not special, the next player plays their turn.
They play this game until no balls are remaining in the game. Alice plays first.
Find the expected score that both the players have at the end of the game modulo $10^9+7$.
Formally, let $M = 10^9+7$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$.
|
There are multiple test cases. The first line of the input contains an integer $t$, the number of test cases ($1 \le t \le 2 \cdot 10^5$).
Each test case description is on a new line. The first line of the test case contains two integers $n$ and $k$ in the respective order separated by a space ($1 \le k \le n \le 4 \cdot 10^5$).
The second line of the test case contains $n$ integers: $v_1, v_2, \ldots, v_n$, the value for each ball separated by spaces. The first $k$ balls are special ($1 \le v_i \le 10^7$).
The sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.
|
Output two integers per test case in a new line, the expected score of Alice and the expected score of Bob modulo $10^9+7$.
|
[
[
"1\n5 2\n10 20 5 15 25",
"45 30"
],
[
"5\n1 1\n732507\n2 2\n5817860 5398510\n5 1\n2122894 4951549 2750585 7821535 3214167\n8 4\n1405323 5069867 6883092 6972029 328406 2478975 7628890 9973340\n4 2\n9662050 3566134 3996473 9872255",
"732507 0\n11216370 0\n810642660 210218077\n722402997 318336932\n349086489 678010430"
],
[
"5\n3 3\n1095611 8219204 7773462\n2 1\n8176490 2774103\n3 1\n9178636 5138057 3367761\n12 9\n7597698 6843019 2298534 1522386 4969588 1340345 3967362 9152890 6689668 9986080 4745473 7407325\n10 5\n6986368 2397882 5804127 6980694 3740836 3215836 5195724 3179261 4136769 4544231",
"17088277 0\n6862348 4088245\n677038671 340645790\n36949997 29570371\n725118051 321063684"
]
] |
In the first test case, Alice's expected score is $45$, and Bob's is $30$ at the end of the game.
|
Title: I Love Balls
time_limit_ms: 2000
memory_limit_mb: 256
Description: Alice and Bob are playing a game. There are $n$ balls, out of which $k$ are special. Each ball has a value associated with it.
The players play turn by turn. In each turn, the player randomly picks a ball and adds the value of the ball to their score, which is $0$ at the beginning of the game. The selected ball is removed from the game. If the ball was special, the same player takes the next turn if at least one ball is remaining. If the ball picked was not special, the next player plays their turn.
They play this game until no balls are remaining in the game. Alice plays first.
Find the expected score that both the players have at the end of the game modulo $10^9+7$.
Formally, let $M = 10^9+7$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$.
Input: There are multiple test cases. The first line of the input contains an integer $t$, the number of test cases ($1 \le t \le 2 \cdot 10^5$).
Each test case description is on a new line. The first line of the test case contains two integers $n$ and $k$ in the respective order separated by a space ($1 \le k \le n \le 4 \cdot 10^5$).
The second line of the test case contains $n$ integers: $v_1, v_2, \ldots, v_n$, the value for each ball separated by spaces. The first $k$ balls are special ($1 \le v_i \le 10^7$).
The sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.
Output: Output two integers per test case in a new line, the expected score of Alice and the expected score of Bob modulo $10^9+7$.
Examples:
input:
1
5 2
10 20 5 15 25
output:
45 30
input:
5
1 1
732507
2 2
5817860 5398510
5 1
2122894 4951549 2750585 7821535 3214167
8 4
1405323 5069867 6883092 6972029 328406 2478975 7628890 9973340
4 2
9662050 3566134 3996473 9872255
output:
732507 0
11216370 0
810642660 210218077
722402997 318336932
349086489 678010430
input:
5
3 3
1095611 8219204 7773462
2 1
8176490 2774103
3 1
9178636 5138057 3367761
12 9
7597698 6843019 2298534 1522386 4969588 1340345 3967362 9152890 6689668 9986080 4745473 7407325
10 5
6986368 2397882 5804127 6980694 3740836 3215836 5195724 3179261 4136769 4544231
output:
17088277 0
6862348 4088245
677038671 340645790
36949997 29570371
725118051 321063684
Note: In the first test case, Alice's expected score is $45$, and Bob's is $30$ at the end of the game.
|
1978D
|
https://codeforces.com/problemset/problem/1978/D
|
Elections
| 1,600
|
[
"data structures",
"greedy",
"implementation",
"math"
] |
Div. 2
| 2,000
| 256
|
Elections are taking place in Berland. There are $n$ candidates participating in the elections, numbered from $1$ to $n$. The $i$-th candidate has $a_i$ fans who will vote for him. Additionally, there are $c$ people who are undecided about their favorite candidate, let's call them undecided. Undecided people will vote for the candidate with the lowest number.
The candidate who receives the maximum number of votes wins the elections, and if multiple candidates receive the same maximum number of votes, the candidate with the lowest number among them wins.
You found these elections too boring and predictable, so you decided to exclude some candidates from them. If you do not allow candidate number $i$ to participate in the elections, all $a_i$ of his fans will become undecided, and will vote for the candidate with the lowest number.
You are curious to find, for each $i$ from $1$ to $n$, the minimum number of candidates that need to be excluded from the elections for candidate number $i$ to win the elections.
|
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 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 $c$ ($1 \le n \le 2 \cdot 10^5$, $0 \le c \le 10^9$) β the number of candidates in the elections and the number of undecided people.
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 number of fans for each candidate.
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$ integers, the $i$-th of which should be equal to the minimum number of candidates that need to be excluded from the elections for candidate number $i$ to win.
|
[
[
"5\n3 1\n2 0 3\n2 3\n0 10\n5 3\n5 4 3 2 1\n4 5\n3 10 7 1\n6 0\n2 2 2 3 3 3",
"0 1 2\n1 0\n0 1 2 3 4\n1 0 2 3\n1 1 2 0 4 5"
]
] |
In the first test case:
* If all candidates are allowed, candidate number $1$ will receive $3$ votes ($1$ undecided person will vote for him), candidate number $2$ will receive $0$ votes, and candidate number $3$ will receive $3$ votes. Therefore, candidate number $1$ wins (he received the same number of votes as candidate $3$, but his number is lower), so the answer for him is $0$. * If candidate number $1$ is not allowed, his $2$ fans will become undecided. Then candidate number $2$ will receive $3$ votes ($3$ undecided people will vote for him) and candidate number $3$ will receive $3$ votes. Therefore, candidate number $2$ wins (he received the same number of votes as candidate $3$, but his number is lower), so the answer for him is $1$. * If candidates with numbers $1$ and $2$ are not allowed, candidate number $3$ wins, so the answer for him is $2$.
In the second test case, candidate number $1$ will win if candidate number $2$ is not allowed to participate.
|
Title: Elections
time_limit_ms: 2000
memory_limit_mb: 256
Description: Elections are taking place in Berland. There are $n$ candidates participating in the elections, numbered from $1$ to $n$. The $i$-th candidate has $a_i$ fans who will vote for him. Additionally, there are $c$ people who are undecided about their favorite candidate, let's call them undecided. Undecided people will vote for the candidate with the lowest number.
The candidate who receives the maximum number of votes wins the elections, and if multiple candidates receive the same maximum number of votes, the candidate with the lowest number among them wins.
You found these elections too boring and predictable, so you decided to exclude some candidates from them. If you do not allow candidate number $i$ to participate in the elections, all $a_i$ of his fans will become undecided, and will vote for the candidate with the lowest number.
You are curious to find, for each $i$ from $1$ to $n$, the minimum number of candidates that need to be excluded from the elections for candidate number $i$ to win the elections.
Input: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 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 $c$ ($1 \le n \le 2 \cdot 10^5$, $0 \le c \le 10^9$) β the number of candidates in the elections and the number of undecided people.
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 number of fans for each candidate.
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$ integers, the $i$-th of which should be equal to the minimum number of candidates that need to be excluded from the elections for candidate number $i$ to win.
Examples:
input:
5
3 1
2 0 3
2 3
0 10
5 3
5 4 3 2 1
4 5
3 10 7 1
6 0
2 2 2 3 3 3
output:
0 1 2
1 0
0 1 2 3 4
1 0 2 3
1 1 2 0 4 5
Note: In the first test case:
* If all candidates are allowed, candidate number $1$ will receive $3$ votes ($1$ undecided person will vote for him), candidate number $2$ will receive $0$ votes, and candidate number $3$ will receive $3$ votes. Therefore, candidate number $1$ wins (he received the same number of votes as candidate $3$, but his number is lower), so the answer for him is $0$. * If candidate number $1$ is not allowed, his $2$ fans will become undecided. Then candidate number $2$ will receive $3$ votes ($3$ undecided people will vote for him) and candidate number $3$ will receive $3$ votes. Therefore, candidate number $2$ wins (he received the same number of votes as candidate $3$, but his number is lower), so the answer for him is $1$. * If candidates with numbers $1$ and $2$ are not allowed, candidate number $3$ wins, so the answer for him is $2$.
In the second test case, candidate number $1$ will win if candidate number $2$ is not allowed to participate.
|
1975D
|
https://codeforces.com/problemset/problem/1975/D
|
Paint the Tree
| 1,700
|
[
"brute force",
"dfs and similar",
"dp",
"greedy",
"shortest paths",
"trees"
] |
Div. 1 + 2
| 2,000
| 256
|
378QAQ has a tree with $n$ vertices. Initially, all vertices are white.
There are two chess pieces called $P_A$ and $P_B$ on the tree. $P_A$ and $P_B$ are initially located on vertices $a$ and $b$ respectively. In one step, 378QAQ will do the following in order:
1. Move $P_A$ to a neighboring vertex. If the target vertex is white, this vertex will be painted red. 2. Move $P_B$ to a neighboring vertex. If the target vertex is colored in red, this vertex will be painted blue.
Initially, the vertex $a$ is painted red. If $a=b$, the vertex $a$ is painted blue instead. Note that both the chess pieces must be moved in each step. Two pieces can be on the same vertex at any given time.
378QAQ wants to know the minimum number of steps to paint all vertices blue.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\leq t\leq 10^4$). The description of the test cases follows.
The first line of each test case contains one integer $n$ ($1\leq n\leq 2\cdot 10^5$).
The second line of each test case contains two integers $a$ and $b$ ($1\leq a,b\leq n$).
Then $n - 1$ lines follow, each line contains two integers $x_i$ and $y_i$ ($1 \le x_i,y_i \le n$), indicating an edge between vertices $x_i$ and $y_i$. It is guaranteed that these edges form a tree.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.
|
For each test case, output the minimum number of steps to paint all vertices blue.
|
[
[
"3\n2\n1 2\n1 2\n5\n1 2\n1 2\n1 3\n1 4\n1 5\n8\n5 4\n7 1\n1 5\n1 8\n8 3\n7 2\n8 6\n3 4",
"2\n8\n13"
]
] |
In the first test case, 378QAQ can paint all vertices blue in the following order:
* Initially, $P_A$ is located on the vertex $1$, and $P_B$ is located on the vertex $2$. The vertex $1$ is painted red and the vertex $2$ is white. * 378QAQ moves $P_A$ to the vertex $2$ and paints it red. Then 378QAQ moves $P_B$ to the vertex $1$ and paints it blue. * 378QAQ moves $P_A$ to the vertex $1$. Then 378QAQ moves $P_B$ to the vertex $2$ and paints it blue.
|
Title: Paint the Tree
time_limit_ms: 2000
memory_limit_mb: 256
Description: 378QAQ has a tree with $n$ vertices. Initially, all vertices are white.
There are two chess pieces called $P_A$ and $P_B$ on the tree. $P_A$ and $P_B$ are initially located on vertices $a$ and $b$ respectively. In one step, 378QAQ will do the following in order:
1. Move $P_A$ to a neighboring vertex. If the target vertex is white, this vertex will be painted red. 2. Move $P_B$ to a neighboring vertex. If the target vertex is colored in red, this vertex will be painted blue.
Initially, the vertex $a$ is painted red. If $a=b$, the vertex $a$ is painted blue instead. Note that both the chess pieces must be moved in each step. Two pieces can be on the same vertex at any given time.
378QAQ wants to know the minimum number of steps to paint all vertices blue.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\leq t\leq 10^4$). The description of the test cases follows.
The first line of each test case contains one integer $n$ ($1\leq n\leq 2\cdot 10^5$).
The second line of each test case contains two integers $a$ and $b$ ($1\leq a,b\leq n$).
Then $n - 1$ lines follow, each line contains two integers $x_i$ and $y_i$ ($1 \le x_i,y_i \le n$), indicating an edge between vertices $x_i$ and $y_i$. It is guaranteed that these edges form a tree.
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 the minimum number of steps to paint all vertices blue.
Examples:
input:
3
2
1 2
1 2
5
1 2
1 2
1 3
1 4
1 5
8
5 4
7 1
1 5
1 8
8 3
7 2
8 6
3 4
output:
2
8
13
Note: In the first test case, 378QAQ can paint all vertices blue in the following order:
* Initially, $P_A$ is located on the vertex $1$, and $P_B$ is located on the vertex $2$. The vertex $1$ is painted red and the vertex $2$ is white. * 378QAQ moves $P_A$ to the vertex $2$ and paints it red. Then 378QAQ moves $P_B$ to the vertex $1$ and paints it blue. * 378QAQ moves $P_A$ to the vertex $1$. Then 378QAQ moves $P_B$ to the vertex $2$ and paints it blue.
|
1989C
|
https://codeforces.com/problemset/problem/1989/C
|
Two Movies
| 1,400
|
[
"greedy",
"math"
] |
Div. 2
| 2,000
| 256
|
A movie company has released $2$ movies. These $2$ movies were watched by $n$ people. For each person, we know their attitude towards the first movie (liked it, neutral, or disliked it) and towards the second movie.
If a person is asked to leave a review for the movie, then:
* if that person liked the movie, they will leave a positive review, and the movie's rating will increase by $1$; * if that person disliked the movie, they will leave a negative review, and the movie's rating will decrease by $1$; * otherwise, they will leave a neutral review, and the movie's rating will not change.
Every person will review exactly one movie β and for every person, you can choose which movie they will review.
The company's rating is the minimum of the ratings of the two movies. Your task is to calculate the maximum possible rating of the company.
|
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 second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-1 \le a_i \le 1$), where $a_i$ is equal to $-1$ if the first movie was disliked by the $i$-th viewer; equal to $1$ if the first movie was liked; and $0$ if the attitude is neutral.
The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($-1 \le b_i \le 1$), where $b_i$ is equal to $-1$ if the second movie was disliked by the $i$-th viewer; equal to $1$ if the second movie was liked; and $0$ if the attitude is neutral.
Additional constraint on the input: the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, print a single integer β the maximum possible rating of the company, if for each person, choose which movie to leave a review on.
|
[
[
"4\n2\n-1 1\n-1 -1\n1\n-1\n-1\n5\n0 -1 1 0 1\n-1 1 0 0 1\n4\n-1 -1 -1 1\n-1 1 1 1",
"0\n-1\n1\n1"
]
] |
Title: Two Movies
time_limit_ms: 2000
memory_limit_mb: 256
Description: A movie company has released $2$ movies. These $2$ movies were watched by $n$ people. For each person, we know their attitude towards the first movie (liked it, neutral, or disliked it) and towards the second movie.
If a person is asked to leave a review for the movie, then:
* if that person liked the movie, they will leave a positive review, and the movie's rating will increase by $1$; * if that person disliked the movie, they will leave a negative review, and the movie's rating will decrease by $1$; * otherwise, they will leave a neutral review, and the movie's rating will not change.
Every person will review exactly one movie β and for every person, you can choose which movie they will review.
The company's rating is the minimum of the ratings of the two movies. Your task is to calculate the maximum possible rating of the company.
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 second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-1 \le a_i \le 1$), where $a_i$ is equal to $-1$ if the first movie was disliked by the $i$-th viewer; equal to $1$ if the first movie was liked; and $0$ if the attitude is neutral.
The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($-1 \le b_i \le 1$), where $b_i$ is equal to $-1$ if the second movie was disliked by the $i$-th viewer; equal to $1$ if the second movie was liked; and $0$ if the attitude is neutral.
Additional constraint on the input: the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, print a single integer β the maximum possible rating of the company, if for each person, choose which movie to leave a review on.
Examples:
input:
4
2
-1 1
-1 -1
1
-1
-1
5
0 -1 1 0 1
-1 1 0 0 1
4
-1 -1 -1 1
-1 1 1 1
output:
0
-1
1
1
Note:
|
|
2029F
|
https://codeforces.com/problemset/problem/2029/F
|
Palindrome Everywhere
| 2,500
|
[
"constructive algorithms",
"greedy"
] |
Div. 1 + 2
| 2,000
| 512
|
You are given a cycle with $n$ vertices numbered from $0$ to $n-1$. For each $0\le i\le n-1$, there is an undirected edge between vertex $i$ and vertex $((i+1)\bmod n)$ with the color $c_i$ ($c_i=\texttt{R}$ or $\texttt{B}$).
Determine whether the following condition holds for every pair of vertices $(i,j)$ ($0\le i<j\le n-1$):
* There exists a palindrome route between vertex $i$ and vertex $j$. Note that the route may not be simple. Formally, there must exist a sequence $p=[p_0,p_1,p_2,\ldots,p_m]$ such that: * $p_0=i$, $p_m=j$; * For each $0\leq x\le m-1$, either $p_{x+1}=(p_x+1)\bmod n$ or $p_{x+1}=(p_{x}-1)\bmod n$; * For each $0\le x\le y\le m-1$ satisfying $x+y=m-1$, the edge between $p_x$ and $p_{x+1}$ has the same color as the edge between $p_y$ and $p_{y+1}$.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer $n$ ($3\leq n\leq10^6$) β the number of vertices in the cycle.
The second line contains a string $c$ of length $n$ ($c_i=\texttt{R}$ or $\texttt{B}$) β the color of each edge.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
|
For each test case, print "YES" (without quotes) if there is a palindrome route between any pair of nodes, and "NO" (without quotes) otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
|
[
[
"7\n5\nRRRRR\n5\nRRRRB\n5\nRBBRB\n6\nRBRBRB\n6\nRRBBRB\n5\nRBRBR\n12\nRRBRRBRRBRRB",
"YES\nYES\nYES\nNO\nNO\nYES\nNO"
]
] |
In the first test case, it is easy to show that there is a palindrome route between any two vertices.
In the second test case, for any two vertices, there exists a palindrome route with only red edges.
In the third test case, the cycle is as follows: $0\color{red}{\overset{\texttt{R}}{\longleftrightarrow}}1\color{blue}{\overset{\texttt{B}}{\longleftrightarrow}}2\color{blue}{\overset{\texttt{B}}{\longleftrightarrow}}3\color{red}{\overset{\texttt{R}}{\longleftrightarrow}}4\color{blue}{\overset{\texttt{B}}{\longleftrightarrow}}0$. Take $(i,j)=(0,3)$ as an example, then $0\color{red}{\overset{\texttt{R}}{\longrightarrow}}1\color{blue}{\overset{\texttt{B}}{\longrightarrow}}2\color{blue}{\overset{\texttt{B}}{\longrightarrow}}3\color{red}{\overset{\texttt{R}}{\longrightarrow}}4\color{blue}{\overset{\texttt{B}}{\longrightarrow}}0\color{blue}{\overset{\texttt{B}}{\longrightarrow}}4\color{red}{\overset{\texttt{R}}{\longrightarrow}}3$ is a palindrome route. Thus, the condition holds for $(i,j)=(0,3)$.
In the fourth test case, when $(i,j)=(0,2)$, there does not exist a palindrome route.
|
Title: Palindrome Everywhere
time_limit_ms: 2000
memory_limit_mb: 512
Description: You are given a cycle with $n$ vertices numbered from $0$ to $n-1$. For each $0\le i\le n-1$, there is an undirected edge between vertex $i$ and vertex $((i+1)\bmod n)$ with the color $c_i$ ($c_i=\texttt{R}$ or $\texttt{B}$).
Determine whether the following condition holds for every pair of vertices $(i,j)$ ($0\le i<j\le n-1$):
* There exists a palindrome route between vertex $i$ and vertex $j$. Note that the route may not be simple. Formally, there must exist a sequence $p=[p_0,p_1,p_2,\ldots,p_m]$ such that: * $p_0=i$, $p_m=j$; * For each $0\leq x\le m-1$, either $p_{x+1}=(p_x+1)\bmod n$ or $p_{x+1}=(p_{x}-1)\bmod n$; * For each $0\le x\le y\le m-1$ satisfying $x+y=m-1$, the edge between $p_x$ and $p_{x+1}$ has the same color as the edge between $p_y$ and $p_{y+1}$.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer $n$ ($3\leq n\leq10^6$) β the number of vertices in the cycle.
The second line contains a string $c$ of length $n$ ($c_i=\texttt{R}$ or $\texttt{B}$) β the color of each edge.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
Output: For each test case, print "YES" (without quotes) if there is a palindrome route between any pair of nodes, and "NO" (without quotes) otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
Examples:
input:
7
5
RRRRR
5
RRRRB
5
RBBRB
6
RBRBRB
6
RRBBRB
5
RBRBR
12
RRBRRBRRBRRB
output:
YES
YES
YES
NO
NO
YES
NO
Note: In the first test case, it is easy to show that there is a palindrome route between any two vertices.
In the second test case, for any two vertices, there exists a palindrome route with only red edges.
In the third test case, the cycle is as follows: $0\color{red}{\overset{\texttt{R}}{\longleftrightarrow}}1\color{blue}{\overset{\texttt{B}}{\longleftrightarrow}}2\color{blue}{\overset{\texttt{B}}{\longleftrightarrow}}3\color{red}{\overset{\texttt{R}}{\longleftrightarrow}}4\color{blue}{\overset{\texttt{B}}{\longleftrightarrow}}0$. Take $(i,j)=(0,3)$ as an example, then $0\color{red}{\overset{\texttt{R}}{\longrightarrow}}1\color{blue}{\overset{\texttt{B}}{\longrightarrow}}2\color{blue}{\overset{\texttt{B}}{\longrightarrow}}3\color{red}{\overset{\texttt{R}}{\longrightarrow}}4\color{blue}{\overset{\texttt{B}}{\longrightarrow}}0\color{blue}{\overset{\texttt{B}}{\longrightarrow}}4\color{red}{\overset{\texttt{R}}{\longrightarrow}}3$ is a palindrome route. Thus, the condition holds for $(i,j)=(0,3)$.
In the fourth test case, when $(i,j)=(0,2)$, there does not exist a palindrome route.
|
2024A
|
https://codeforces.com/problemset/problem/2024/A
|
Profitable Interest Rate
| 800
|
[
"greedy",
"math"
] |
Div. 2
| 1,000
| 256
|
Alice has $a$ coins. She can open a bank deposit called "Profitable", but the minimum amount required to open this deposit is $b$ coins.
There is also a deposit called "Unprofitable", which can be opened with any amount of coins. Alice noticed that if she opens the "Unprofitable" deposit with $x$ coins, the minimum amount required to open the "Profitable" deposit decreases by $2x$ coins. However, these coins cannot later be deposited into the "Profitable" deposit.
Help Alice determine the maximum number of coins she can deposit into the "Profitable" deposit if she first deposits some amount of coins (possibly $0$) into the "Unprofitable" deposit. If Alice can never open the "Profitable" deposit, output $0$.
|
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.
A single line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$) β the number of coins Alice has and the initial minimum amount required to open the "Profitable" deposit.
|
For each test case, output a single integer β the maximum number of coins that Alice can deposit into the "Profitable" deposit. If Alice can never open the "Profitable" deposit, output $0$.
|
[
[
"5\n10 5\n7 9\n5 100\n1 1\n1 2",
"10\n5\n0\n1\n0"
]
] |
In the first test case, $a \ge b$, so Alice can immediately open the "Profitable" deposit with all $10$ coins.
In the second test case, Alice can open the "Unprofitable" deposit with $2$ coins. Then she will have $5$ coins left, but the minimum amount required to open the "Profitable" deposit will decrease by $4$ coins, making it equal to $5$ coins. Thus, Alice will be able to open the "Profitable" deposit with $5$ coins.
In the third test case, Alice will not be able to open the "Profitable" deposit.
|
Title: Profitable Interest Rate
time_limit_ms: 1000
memory_limit_mb: 256
Description: Alice has $a$ coins. She can open a bank deposit called "Profitable", but the minimum amount required to open this deposit is $b$ coins.
There is also a deposit called "Unprofitable", which can be opened with any amount of coins. Alice noticed that if she opens the "Unprofitable" deposit with $x$ coins, the minimum amount required to open the "Profitable" deposit decreases by $2x$ coins. However, these coins cannot later be deposited into the "Profitable" deposit.
Help Alice determine the maximum number of coins she can deposit into the "Profitable" deposit if she first deposits some amount of coins (possibly $0$) into the "Unprofitable" deposit. If Alice can never open the "Profitable" deposit, output $0$.
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.
A single line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$) β the number of coins Alice has and the initial minimum amount required to open the "Profitable" deposit.
Output: For each test case, output a single integer β the maximum number of coins that Alice can deposit into the "Profitable" deposit. If Alice can never open the "Profitable" deposit, output $0$.
Examples:
input:
5
10 5
7 9
5 100
1 1
1 2
output:
10
5
0
1
0
Note: In the first test case, $a \ge b$, so Alice can immediately open the "Profitable" deposit with all $10$ coins.
In the second test case, Alice can open the "Unprofitable" deposit with $2$ coins. Then she will have $5$ coins left, but the minimum amount required to open the "Profitable" deposit will decrease by $4$ coins, making it equal to $5$ coins. Thus, Alice will be able to open the "Profitable" deposit with $5$ coins.
In the third test case, Alice will not be able to open the "Profitable" deposit.
|
1990A
|
https://codeforces.com/problemset/problem/1990/A
|
Submission Bait
| 900
|
[
"brute force",
"games",
"greedy",
"sortings"
] |
Div. 2
| 1,000
| 256
|
Alice and Bob are playing a game in an array $a$ of size $n$.
They take turns to do operations, with Alice starting first. The player who can not operate will lose. At first, a variable $mx$ is set to $0$.
In one operation, a player can do:
* Choose an index $i$ ($1 \le i \le n$) such that $a_{i} \geq mx$ and set $mx$ to $a_{i}$. Then, set $a_{i}$ to $0$.
Determine whether Alice has a winning strategy.
|
The first line contains an integer $t$ ($1 \leq t \leq 10^3$) β the number of test cases.
For each test case:
* The first line contains an integer $n$ ($2 \leq n \leq 50$) β the size of the array. * The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) β the elements of the array.
|
For each test case, if Alice has a winning strategy, output "YES". 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.
|
[
[
"5\n2\n2 1\n2\n1 1\n3\n3 3 3\n4\n3 3 4 4\n4\n1 2 2 2",
"YES\nNO\nYES\nNO\nYES"
]
] |
In the first test case, Alice can choose $i=1$ since $a_1=2 \ge mx=0$.
After Alice's operation, $a=[0,1]$ and $mx=2$. Bob can not do any operation. Alice wins.
In the second test case, Alice doesn't have a winning strategy.
For example, if Alice chooses $i=1$, after Alice's operation: $a=[0,1]$ and $mx=1$. Then, Bob can choose $i=2$ since $a_2=1 \ge mx=1$. After Bob's operation: $a=[0,0]$ and $mx=1$. Alice can not do any operation. Bob wins.
|
Title: Submission Bait
time_limit_ms: 1000
memory_limit_mb: 256
Description: Alice and Bob are playing a game in an array $a$ of size $n$.
They take turns to do operations, with Alice starting first. The player who can not operate will lose. At first, a variable $mx$ is set to $0$.
In one operation, a player can do:
* Choose an index $i$ ($1 \le i \le n$) such that $a_{i} \geq mx$ and set $mx$ to $a_{i}$. Then, set $a_{i}$ to $0$.
Determine whether Alice has a winning strategy.
Input: The first line contains an integer $t$ ($1 \leq t \leq 10^3$) β the number of test cases.
For each test case:
* The first line contains an integer $n$ ($2 \leq n \leq 50$) β the size of the array. * The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) β the elements of the array.
Output: For each test case, if Alice has a winning strategy, output "YES". 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:
5
2
2 1
2
1 1
3
3 3 3
4
3 3 4 4
4
1 2 2 2
output:
YES
NO
YES
NO
YES
Note: In the first test case, Alice can choose $i=1$ since $a_1=2 \ge mx=0$.
After Alice's operation, $a=[0,1]$ and $mx=2$. Bob can not do any operation. Alice wins.
In the second test case, Alice doesn't have a winning strategy.
For example, if Alice chooses $i=1$, after Alice's operation: $a=[0,1]$ and $mx=1$. Then, Bob can choose $i=2$ since $a_2=1 \ge mx=1$. After Bob's operation: $a=[0,0]$ and $mx=1$. Alice can not do any operation. Bob wins.
|
1973A
|
https://codeforces.com/problemset/problem/1973/A
|
Chess For Three
| 900
|
[
"brute force",
"dp",
"implementation",
"math"
] |
Div. 2
| 1,000
| 256
|
Three friends gathered to play a few games of chess together.
In every game, two of them play against each other. The winner gets $2$ points while the loser gets $0$, and in case of a draw, both players get $1$ point each. Note that the same pair of players could have played any non-negative number of times (possibly zero). It is also possible that no games were played at all.
You've been told that their scores after all the games were played were $p_1$, $p_2$ and $p_3$. Additionally, it is guaranteed that $p_1 \leq p_2 \leq p_3$ holds.
Find the maximum number of draws that could have happened and print it. If there isn't any way to obtain $p_1$, $p_2$ and $p_3$ as a result of a non-negative number of games between the three players, print $-1$ instead.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.
The first line of each test case contains three integers $p_1$, $p_2$ and $p_3$ ($0 \leq p_1 \leq p_2 \leq p_3 \leq 30$) β the scores of the three players, sorted non-decreasingly.
|
For each testcase, print one number β the maximum possible number of draws that could've happened, or $-1$ if the scores aren't consistent with any valid set of games and results.
|
[
[
"7\n0 0 0\n0 1 1\n1 1 1\n1 1 2\n3 3 3\n3 4 5\n1 1 10",
"0\n1\n-1\n2\n-1\n6\n2"
]
] |
In the first example, no games were played at all, so no draws could occur either.
For the second example, exactly one game occurred between the second and the third player and it ended in draw, so the answer is $1$.
It's easy to see that there's no set of games achieving the scores in third example, so the answer for it is $-1$.
|
Title: Chess For Three
time_limit_ms: 1000
memory_limit_mb: 256
Description: Three friends gathered to play a few games of chess together.
In every game, two of them play against each other. The winner gets $2$ points while the loser gets $0$, and in case of a draw, both players get $1$ point each. Note that the same pair of players could have played any non-negative number of times (possibly zero). It is also possible that no games were played at all.
You've been told that their scores after all the games were played were $p_1$, $p_2$ and $p_3$. Additionally, it is guaranteed that $p_1 \leq p_2 \leq p_3$ holds.
Find the maximum number of draws that could have happened and print it. If there isn't any way to obtain $p_1$, $p_2$ and $p_3$ as a result of a non-negative number of games between the three players, print $-1$ instead.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.
The first line of each test case contains three integers $p_1$, $p_2$ and $p_3$ ($0 \leq p_1 \leq p_2 \leq p_3 \leq 30$) β the scores of the three players, sorted non-decreasingly.
Output: For each testcase, print one number β the maximum possible number of draws that could've happened, or $-1$ if the scores aren't consistent with any valid set of games and results.
Examples:
input:
7
0 0 0
0 1 1
1 1 1
1 1 2
3 3 3
3 4 5
1 1 10
output:
0
1
-1
2
-1
6
2
Note: In the first example, no games were played at all, so no draws could occur either.
For the second example, exactly one game occurred between the second and the third player and it ended in draw, so the answer is $1$.
It's easy to see that there's no set of games achieving the scores in third example, so the answer for it is $-1$.
|
1972A
|
https://codeforces.com/problemset/problem/1972/A
|
Contest Proposal
| 800
|
[
"brute force",
"greedy",
"two pointers"
] |
Div. 2
| 1,000
| 256
|
A contest contains $n$ problems and the difficulty of the $i$-th problem is expected to be at most $b_i$. There are already $n$ problem proposals and the difficulty of the $i$-th problem is $a_i$. Initially, both $a_1, a_2, \ldots, a_n$ and $b_1, b_2, \ldots, b_n$ are sorted in non-decreasing order.
Some of the problems may be more difficult than expected, so the writers must propose more problems. When a new problem with difficulty $w$ is proposed, the most difficult problem will be deleted from the contest, and the problems will be sorted in a way that the difficulties are non-decreasing.
In other words, in each operation, you choose an integer $w$, insert it into the array $a$, sort array $a$ in non-decreasing order, and remove the last element from it.
Find the minimum number of new problems to make $a_i\le b_i$ for all $i$.
|
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 only one positive integer $n$ ($1 \leq n \leq 100$), representing the number of problems.
The second line of each test case contains an array $a$ of length $n$ ($1\le a_1\le a_2\le\cdots\le a_n\le 10^9$).
The third line of each test case contains an array $b$ of length $n$ ($1\le b_1\le b_2\le\cdots\le b_n\le 10^9$).
|
For each test case, print an integer as your answer in a new line.
|
[
[
"2\n6\n1000 1400 2000 2000 2200 2700\n800 1200 1500 1800 2200 3000\n6\n4 5 6 7 8 9\n1 2 3 4 5 6",
"2\n3"
]
] |
In the first test case:
* Propose a problem with difficulty $w=800$ and $a$ becomes $[800,1000,1400,2000,2000,2200]$. * Propose a problem with difficulty $w=1800$ and $a$ becomes $[800,1000,1400,1800,2000,2000]$.
It can be proved that it's impossible to reach the goal by proposing fewer new problems.
In the second test case:
* Propose a problem with difficulty $w=1$ and $a$ becomes $[1,4,5,6,7,8]$. * Propose a problem with difficulty $w=2$ and $a$ becomes $[1,2,4,5,6,7]$. * Propose a problem with difficulty $w=3$ and $a$ becomes $[1,2,3,4,5,6]$.
It can be proved that it's impossible to reach the goal by proposing fewer new problems.
|
Title: Contest Proposal
time_limit_ms: 1000
memory_limit_mb: 256
Description: A contest contains $n$ problems and the difficulty of the $i$-th problem is expected to be at most $b_i$. There are already $n$ problem proposals and the difficulty of the $i$-th problem is $a_i$. Initially, both $a_1, a_2, \ldots, a_n$ and $b_1, b_2, \ldots, b_n$ are sorted in non-decreasing order.
Some of the problems may be more difficult than expected, so the writers must propose more problems. When a new problem with difficulty $w$ is proposed, the most difficult problem will be deleted from the contest, and the problems will be sorted in a way that the difficulties are non-decreasing.
In other words, in each operation, you choose an integer $w$, insert it into the array $a$, sort array $a$ in non-decreasing order, and remove the last element from it.
Find the minimum number of new problems to make $a_i\le b_i$ for all $i$.
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 only one positive integer $n$ ($1 \leq n \leq 100$), representing the number of problems.
The second line of each test case contains an array $a$ of length $n$ ($1\le a_1\le a_2\le\cdots\le a_n\le 10^9$).
The third line of each test case contains an array $b$ of length $n$ ($1\le b_1\le b_2\le\cdots\le b_n\le 10^9$).
Output: For each test case, print an integer as your answer in a new line.
Examples:
input:
2
6
1000 1400 2000 2000 2200 2700
800 1200 1500 1800 2200 3000
6
4 5 6 7 8 9
1 2 3 4 5 6
output:
2
3
Note: In the first test case:
* Propose a problem with difficulty $w=800$ and $a$ becomes $[800,1000,1400,2000,2000,2200]$. * Propose a problem with difficulty $w=1800$ and $a$ becomes $[800,1000,1400,1800,2000,2000]$.
It can be proved that it's impossible to reach the goal by proposing fewer new problems.
In the second test case:
* Propose a problem with difficulty $w=1$ and $a$ becomes $[1,4,5,6,7,8]$. * Propose a problem with difficulty $w=2$ and $a$ becomes $[1,2,4,5,6,7]$. * Propose a problem with difficulty $w=3$ and $a$ becomes $[1,2,3,4,5,6]$.
It can be proved that it's impossible to reach the goal by proposing fewer new problems.
|
2007B
|
https://codeforces.com/problemset/problem/2007/B
|
Index and Maximum Value
| 900
|
[
"data structures",
"greedy"
] |
Div. 2
| 1,000
| 256
|
After receiving yet another integer array $a_1, a_2, \ldots, a_n$ at her birthday party, Index decides to perform some operations on it.
Formally, there are $m$ operations that she is going to perform in order. Each of them belongs to one of the two types:
* $\texttt{+ l r}$. Given two integers $l$ and $r$, for all $1 \leq i \leq n$ such that $l \leq a_i \leq r$, set $a_i := a_i + 1$. * $\texttt{- l r}$. Given two integers $l$ and $r$, for all $1 \leq i \leq n$ such that $l \leq a_i \leq r$, set $a_i := a_i - 1$.
For example, if the initial array $a = [7, 1, 3, 4, 3]$, after performing the operation $\texttt{+} \space 2 \space 4$, the array $a = [7, 1, 4, 5, 4]$. Then, after performing the operation $\texttt{-} \space 1 \space 10$, the array $a = [6, 0, 3, 4, 3]$.
Index is curious about the maximum value in the array $a$. Please help her find it after each of the $m$ operations.
|
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($1 \leq n \leq 10^5$, $1 \leq m \leq 10^5$) β the length of the array and the number of operations.
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 initial array $a$.
Then $m$ lines follow, each line corresponds to the operation, in the following format: $\texttt{c l r}$ ($c \in \\{\texttt +, \texttt -\\}$, $l$ and $r$ are integers, $1 \leq l \leq r \leq 10^9$) β the description of the operation.
Note that the elements $a_i$ may not satisfy $1\le a_i\le 10^9$ after some operations, as it is shown in the example.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$, and the sum of $m$ over all test cases does not exceed $10^5$.
|
For each test case, output one single line containing $m$ integers, with the $i$-th of them describing the maximum value of the array after the $i$-th operation.
|
[
[
"5\n5 5\n1 2 3 2 1\n+ 1 3\n- 2 3\n+ 1 2\n+ 2 4\n- 6 8\n5 5\n1 3 3 4 5\n+ 1 4\n+ 2 3\n- 4 5\n- 3 3\n- 2 6\n5 5\n1 1 1 1 1\n+ 2 3\n- 4 5\n+ 1 6\n- 2 5\n+ 1 8\n1 1\n1\n- 1 1\n1 1\n1000000000\n+ 1000000000 1000000000",
"4 4 4 5 5\n5 5 4 4 3\n1 1 2 1 2\n0\n1000000001"
]
] |
In the first test case, the process of the operations is listed below:
* After the first operation, the array becomes equal $[2,3,4,3,2]$. The maximum value is $4$. * After the second operation, the array becomes equal $[1,2,4,2,1]$. The maximum value is $4$. * After the third operation, the array becomes equal $[2,3,4,3,2]$. The maximum value is $4$. * After the fourth operation, the array becomes equal $[3,4,5,4,3]$. The maximum value is $5$. * After the fifth operation, the array becomes equal $[3,4,5,4,3]$. The maximum value is $5$.
In the second test case, the process of the operations is listed below:
* After the first operation, the array becomes equal $[2,4,4,5,5]$. The maximum value is $5$. * After the second operation, the array becomes equal $[3,4,4,5,5]$. The maximum value is $5$. * After the third operation, the array becomes equal $[3,3,3,4,4]$. The maximum value is $4$. * After the fourth operation, the array becomes equal $[2,2,2,4,4]$. The maximum value is $4$. * After the fifth operation, the array becomes equal $[1,1,1,3,3]$. The maximum value is $3$.
|
Title: Index and Maximum Value
time_limit_ms: 1000
memory_limit_mb: 256
Description: After receiving yet another integer array $a_1, a_2, \ldots, a_n$ at her birthday party, Index decides to perform some operations on it.
Formally, there are $m$ operations that she is going to perform in order. Each of them belongs to one of the two types:
* $\texttt{+ l r}$. Given two integers $l$ and $r$, for all $1 \leq i \leq n$ such that $l \leq a_i \leq r$, set $a_i := a_i + 1$. * $\texttt{- l r}$. Given two integers $l$ and $r$, for all $1 \leq i \leq n$ such that $l \leq a_i \leq r$, set $a_i := a_i - 1$.
For example, if the initial array $a = [7, 1, 3, 4, 3]$, after performing the operation $\texttt{+} \space 2 \space 4$, the array $a = [7, 1, 4, 5, 4]$. Then, after performing the operation $\texttt{-} \space 1 \space 10$, the array $a = [6, 0, 3, 4, 3]$.
Index is curious about the maximum value in the array $a$. Please help her find it after each of the $m$ operations.
Input: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($1 \leq n \leq 10^5$, $1 \leq m \leq 10^5$) β the length of the array and the number of operations.
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 initial array $a$.
Then $m$ lines follow, each line corresponds to the operation, in the following format: $\texttt{c l r}$ ($c \in \\{\texttt +, \texttt -\\}$, $l$ and $r$ are integers, $1 \leq l \leq r \leq 10^9$) β the description of the operation.
Note that the elements $a_i$ may not satisfy $1\le a_i\le 10^9$ after some operations, as it is shown in the example.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$, and the sum of $m$ over all test cases does not exceed $10^5$.
Output: For each test case, output one single line containing $m$ integers, with the $i$-th of them describing the maximum value of the array after the $i$-th operation.
Examples:
input:
5
5 5
1 2 3 2 1
+ 1 3
- 2 3
+ 1 2
+ 2 4
- 6 8
5 5
1 3 3 4 5
+ 1 4
+ 2 3
- 4 5
- 3 3
- 2 6
5 5
1 1 1 1 1
+ 2 3
- 4 5
+ 1 6
- 2 5
+ 1 8
1 1
1
- 1 1
1 1
1000000000
+ 1000000000 1000000000
output:
4 4 4 5 5
5 5 4 4 3
1 1 2 1 2
0
1000000001
Note: In the first test case, the process of the operations is listed below:
* After the first operation, the array becomes equal $[2,3,4,3,2]$. The maximum value is $4$. * After the second operation, the array becomes equal $[1,2,4,2,1]$. The maximum value is $4$. * After the third operation, the array becomes equal $[2,3,4,3,2]$. The maximum value is $4$. * After the fourth operation, the array becomes equal $[3,4,5,4,3]$. The maximum value is $5$. * After the fifth operation, the array becomes equal $[3,4,5,4,3]$. The maximum value is $5$.
In the second test case, the process of the operations is listed below:
* After the first operation, the array becomes equal $[2,4,4,5,5]$. The maximum value is $5$. * After the second operation, the array becomes equal $[3,4,4,5,5]$. The maximum value is $5$. * After the third operation, the array becomes equal $[3,3,3,4,4]$. The maximum value is $4$. * After the fourth operation, the array becomes equal $[2,2,2,4,4]$. The maximum value is $4$. * After the fifth operation, the array becomes equal $[1,1,1,3,3]$. The maximum value is $3$.
|
2013F2
|
https://codeforces.com/problemset/problem/2013/F2
|
Game in Tree (Hard Version)
| 3,500
|
[
"binary search",
"data structures",
"trees"
] |
Div. 2
| 4,000
| 256
|
This is the hard version of the problem. In this version, it is not guaranteed that $u = v$. You can make hacks only if both versions of the problem are solved.
Alice and Bob are playing a fun game on a tree. This game is played on a tree with $n$ vertices, numbered from $1$ to $n$. Recall that a tree with $n$ vertices is an undirected connected graph with $n - 1$ edges.
Alice and Bob take turns, with Alice going first. Each player starts at some vertex.
On their turn, a player must move from the current vertex to a neighboring vertex that has not yet been visited by anyone. The first player who cannot make a move loses.
You are given two vertices $u$ and $v$. Represent the simple path from vertex $u$ to $v$ as an array $p_1, p_2, p_3, \ldots, p_m$, where $p_1 = u$, $p_m = v$, and there is an edge between $p_i$ and $p_{i + 1}$ for all $i$ ($1 \le i < m$).
You need to determine the winner of the game if Alice starts at vertex $1$ and Bob starts at vertex $p_j$ for each $j$ (where $1 \le j \le m$).
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of vertices in the tree.
Each of the following $n - 1$ lines contains two integers $a$ and $b$ ($1 \le a, b \le n$), denoting an undirected edge between vertices $a$ and $b$. It is guaranteed that these edges form a tree.
The last line of each test case contains two integers $u$ and $v$ ($2 \le u, v \le n$).
It is guaranteed that the path from $u$ to $v$ does not pass through vertex $1$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output $m$ lines.
In the $i$-th line, print the winner of the game if Alice starts at vertex $1$ and Bob starts at vertex $p_i$. Print "Alice" (without quotes) if Alice wins, or "Bob" (without quotes) otherwise.
|
[
[
"3\n3\n1 2\n2 3\n2 3\n6\n1 2\n1 3\n2 4\n2 5\n1 6\n4 5\n4\n1 2\n1 3\n2 4\n2 4",
"Bob\nAlice\nAlice\nBob\nAlice\nBob\nAlice"
]
] |
 Tree from the first example.
In the first test case, the path will be ($2,3$). If Bob starts at vertex $2$, Alice will not be able to move anywhere on her first turn and will lose.
However, if Bob starts at vertex $3$, Alice will move to vertex $2$, and Bob will have no remaining vertices to visit and will lose.
|
Title: Game in Tree (Hard Version)
time_limit_ms: 4000
memory_limit_mb: 256
Description: This is the hard version of the problem. In this version, it is not guaranteed that $u = v$. You can make hacks only if both versions of the problem are solved.
Alice and Bob are playing a fun game on a tree. This game is played on a tree with $n$ vertices, numbered from $1$ to $n$. Recall that a tree with $n$ vertices is an undirected connected graph with $n - 1$ edges.
Alice and Bob take turns, with Alice going first. Each player starts at some vertex.
On their turn, a player must move from the current vertex to a neighboring vertex that has not yet been visited by anyone. The first player who cannot make a move loses.
You are given two vertices $u$ and $v$. Represent the simple path from vertex $u$ to $v$ as an array $p_1, p_2, p_3, \ldots, p_m$, where $p_1 = u$, $p_m = v$, and there is an edge between $p_i$ and $p_{i + 1}$ for all $i$ ($1 \le i < m$).
You need to determine the winner of the game if Alice starts at vertex $1$ and Bob starts at vertex $p_j$ for each $j$ (where $1 \le j \le m$).
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 \cdot 10^5$) β the number of vertices in the tree.
Each of the following $n - 1$ lines contains two integers $a$ and $b$ ($1 \le a, b \le n$), denoting an undirected edge between vertices $a$ and $b$. It is guaranteed that these edges form a tree.
The last line of each test case contains two integers $u$ and $v$ ($2 \le u, v \le n$).
It is guaranteed that the path from $u$ to $v$ does not pass through vertex $1$.
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 $m$ lines.
In the $i$-th line, print the winner of the game if Alice starts at vertex $1$ and Bob starts at vertex $p_i$. Print "Alice" (without quotes) if Alice wins, or "Bob" (without quotes) otherwise.
Examples:
input:
3
3
1 2
2 3
2 3
6
1 2
1 3
2 4
2 5
1 6
4 5
4
1 2
1 3
2 4
2 4
output:
Bob
Alice
Alice
Bob
Alice
Bob
Alice
Note:  Tree from the first example.
In the first test case, the path will be ($2,3$). If Bob starts at vertex $2$, Alice will not be able to move anywhere on her first turn and will lose.
However, if Bob starts at vertex $3$, Alice will move to vertex $2$, and Bob will have no remaining vertices to visit and will lose.
|
2004B
|
https://codeforces.com/problemset/problem/2004/B
|
Game with Doors
| 1,000
|
[
"brute force",
"greedy"
] |
Div. 2
| 2,000
| 256
|
There are $100$ rooms arranged in a row and $99$ doors between them; the $i$-th door connects rooms $i$ and $i+1$. Each door can be either locked or unlocked. Initially, all doors are unlocked.
We say that room $x$ is reachable from room $y$ if all doors between them are unlocked.
You know that:
* Alice is in some room from the segment $[l, r]$; * Bob is in some room from the segment $[L, R]$; * Alice and Bob are in different rooms.
However, you don't know the exact rooms they are in.
You don't want Alice and Bob to be able to reach each other, so you are going to lock some doors to prevent that. What's the smallest number of doors you have to lock so that Alice and Bob cannot meet, regardless of their starting positions inside the given segments?
|
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 $l$ and $r$ ($1 \le l < r \le 100$) β the bounds of the segment of rooms where Alice is located.
The second line of each test case contains two integers $L$ and $R$ ($1 \le L < R \le 100$) β the bounds of the segment of rooms where Bob is located.
|
For each test case, print a single integer β the smallest number of doors you have to lock so that Alice and Bob cannot meet, regardless of their starting positions inside the given segments.
|
[
[
"4\n1 2\n3 4\n2 5\n2 5\n3 7\n6 7\n4 5\n2 8",
"1\n3\n2\n3"
]
] |
In the first test case, it is sufficient to lock the door between rooms $2$ and $3$.
In the second test case, the following doors have to be locked: $(2,3)$, $(3,4)$, $(4,5)$.
In the third test case, the following doors have to be locked: $(5, 6)$ and $(6,7)$.
|
Title: Game with Doors
time_limit_ms: 2000
memory_limit_mb: 256
Description: There are $100$ rooms arranged in a row and $99$ doors between them; the $i$-th door connects rooms $i$ and $i+1$. Each door can be either locked or unlocked. Initially, all doors are unlocked.
We say that room $x$ is reachable from room $y$ if all doors between them are unlocked.
You know that:
* Alice is in some room from the segment $[l, r]$; * Bob is in some room from the segment $[L, R]$; * Alice and Bob are in different rooms.
However, you don't know the exact rooms they are in.
You don't want Alice and Bob to be able to reach each other, so you are going to lock some doors to prevent that. What's the smallest number of doors you have to lock so that Alice and Bob cannot meet, regardless of their starting positions inside the given segments?
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 $l$ and $r$ ($1 \le l < r \le 100$) β the bounds of the segment of rooms where Alice is located.
The second line of each test case contains two integers $L$ and $R$ ($1 \le L < R \le 100$) β the bounds of the segment of rooms where Bob is located.
Output: For each test case, print a single integer β the smallest number of doors you have to lock so that Alice and Bob cannot meet, regardless of their starting positions inside the given segments.
Examples:
input:
4
1 2
3 4
2 5
2 5
3 7
6 7
4 5
2 8
output:
1
3
2
3
Note: In the first test case, it is sufficient to lock the door between rooms $2$ and $3$.
In the second test case, the following doors have to be locked: $(2,3)$, $(3,4)$, $(4,5)$.
In the third test case, the following doors have to be locked: $(5, 6)$ and $(6,7)$.
|
1972F
|
https://codeforces.com/problemset/problem/1972/F
|
Long Way to be Non-decreasing
| 2,800
|
[
"binary search",
"dfs and similar",
"graphs",
"implementation"
] |
Div. 2
| 4,000
| 512
|
Little R is a magician who likes non-decreasing arrays. She has an array of length $n$, initially as $a_1, \ldots, a_n$, in which each element is an integer between $[1, m]$. She wants it to be non-decreasing, i.e., $a_1 \leq a_2 \leq \ldots \leq a_n$.
To do this, she can perform several magic tricks. Little R has a fixed array $b_1\ldots b_m$ of length $m$. Formally, let's define a trick as a procedure that does the following things in order:
* Choose a set $S \subseteq \\{1, 2, \ldots, n\\}$. * For each $u \in S$, assign $a_u$ with $b_{a_u}$.
Little R wonders how many tricks are needed at least to make the initial array non-decreasing. If it is not possible with any amount of tricks, print $-1$ instead.
|
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$ and $m$ ($1\leq n \leq 10^6$, $1 \leq m \leq 10^6$) β the length of the initial array and the range of the elements in the array.
The second line of each test case contains $n$ integers $a_1, \ldots, a_n$ ($1 \leq a_i \leq m$) β the initial array.
The third line of each test case contains $m$ integers $b_1, \ldots, b_m$ ($1 \leq b_i \leq m$) β the fixed magic array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ and the sum of $m$ over all test cases does not exceed $10^6$.
|
For each test case, output a single integer: the minimum number of tricks needed, or $-1$ if it is impossible to make $a_1, \ldots, a_n$ non- decreasing.
|
[
[
"3\n5 8\n1 6 3 7 1\n2 3 5 8 7 1 5 6\n3 3\n1 3 2\n2 1 3\n10 10\n2 8 5 4 8 4 1 5 10 10\n6 7 2 6 3 4 1 1 3 5",
"3\n-1\n3"
]
] |
In the first case, the initial array $a_1, \ldots, a_n$ is $[1, 6, 3, 7, 1]$. You can choose $S$ as follows:
* first trick: $S = [2, 4, 5]$, $a = [1, 1, 3, 5, 2]$; * second trick: $S = [5]$, $a = [1, 1, 3, 5, 3]$; * third trick: $S = [5]$, $a = [1, 1, 3, 5, 5]$.
So it is possible to make $a_1, \ldots, a_n$ non-decreasing using $3$ tricks. It can be shown that this is the minimum possible amount of tricks.
In the second case, it is impossible to make $a_1, \ldots, a_n$ non- decreasing.
|
Title: Long Way to be Non-decreasing
time_limit_ms: 4000
memory_limit_mb: 512
Description: Little R is a magician who likes non-decreasing arrays. She has an array of length $n$, initially as $a_1, \ldots, a_n$, in which each element is an integer between $[1, m]$. She wants it to be non-decreasing, i.e., $a_1 \leq a_2 \leq \ldots \leq a_n$.
To do this, she can perform several magic tricks. Little R has a fixed array $b_1\ldots b_m$ of length $m$. Formally, let's define a trick as a procedure that does the following things in order:
* Choose a set $S \subseteq \\{1, 2, \ldots, n\\}$. * For each $u \in S$, assign $a_u$ with $b_{a_u}$.
Little R wonders how many tricks are needed at least to make the initial array non-decreasing. If it is not possible with any amount of tricks, print $-1$ instead.
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$ and $m$ ($1\leq n \leq 10^6$, $1 \leq m \leq 10^6$) β the length of the initial array and the range of the elements in the array.
The second line of each test case contains $n$ integers $a_1, \ldots, a_n$ ($1 \leq a_i \leq m$) β the initial array.
The third line of each test case contains $m$ integers $b_1, \ldots, b_m$ ($1 \leq b_i \leq m$) β the fixed magic array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ and the sum of $m$ over all test cases does not exceed $10^6$.
Output: For each test case, output a single integer: the minimum number of tricks needed, or $-1$ if it is impossible to make $a_1, \ldots, a_n$ non- decreasing.
Examples:
input:
3
5 8
1 6 3 7 1
2 3 5 8 7 1 5 6
3 3
1 3 2
2 1 3
10 10
2 8 5 4 8 4 1 5 10 10
6 7 2 6 3 4 1 1 3 5
output:
3
-1
3
Note: In the first case, the initial array $a_1, \ldots, a_n$ is $[1, 6, 3, 7, 1]$. You can choose $S$ as follows:
* first trick: $S = [2, 4, 5]$, $a = [1, 1, 3, 5, 2]$; * second trick: $S = [5]$, $a = [1, 1, 3, 5, 3]$; * third trick: $S = [5]$, $a = [1, 1, 3, 5, 5]$.
So it is possible to make $a_1, \ldots, a_n$ non-decreasing using $3$ tricks. It can be shown that this is the minimum possible amount of tricks.
In the second case, it is impossible to make $a_1, \ldots, a_n$ non- decreasing.
|
2035E
|
https://codeforces.com/problemset/problem/2035/E
|
Monster
| 2,300
|
[
"binary search",
"brute force",
"constructive algorithms",
"greedy",
"implementation",
"math",
"ternary search"
] |
Div. 1 + 2
| 2,000
| 256
|
Man, this Genshin boss is so hard. Good thing they have a top-up of $6$ coins for only $ \$4.99$. I should be careful and spend no more than I need to, lest my mom catches me...
You are fighting a monster with $z$ health using a weapon with $d$ damage. Initially, $d=0$. You can perform the following operations.
* Increase $d$ β the damage of your weapon by $1$, costing $x$ coins. * Attack the monster, dealing $d$ damage and costing $y$ coins.
You cannot perform the first operation for more than $k$ times in a row.
Find the minimum number of coins needed to defeat the monster by dealing at least $z$ damage.
|
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 4 integers $x$, $y$, $z$, and $k$ ($1\leq x, y, z, k\leq 10^8$) β the first operation's cost, the second operation's cost, the monster's health, and the limitation on the first operation.
|
For each test case, output the minimum number of coins needed to defeat the monster.
|
[
[
"4\n2 3 5 5\n10 20 40 5\n1 60 100 10\n60 1 100 10",
"12\n190\n280\n160"
]
] |
In the first test case, $x = 2$, $y = 3$, $z = 5$, and $k = 5$. Here's a strategy that achieves the lowest possible cost of $12$ coins:
* Increase damage by $1$, costing $2$ coins. * Increase damage by $1$, costing $2$ coins. * Increase damage by $1$, costing $2$ coins. * Attack the monster, dealing $3$ damage, costing $3$ coins. * Attack the monster, dealing $3$ damage, costing $3$ coins.
You deal a total of $3 + 3 = 6$ damage, defeating the monster who has $5$ health. The total number of coins you use is $2 + 2 + 2 + 3 + 3 = 12$ coins.
In the second test case, $x = 10$, $y = 20$, $z = 40$, and $k = 5$. Here's a strategy that achieves the lowest possible cost of $190$ coins:
* Increase damage by $5$, costing $5\cdot x$ = $50$ coins. * Attack the monster once, dealing $5$ damage, costing $20$ coins. * Increase damage by $2$, costing $2\cdot x$ = $20$ coins. * Attack the monster $5$ times, dealing $5\cdot 7 = 35$ damage, costing $5\cdot y$ = $100$ coins.
You deal a total of $5 + 35 = 40$ damage, defeating the monster who has exactly $40$ health. The total number of coins you use is $50 + 20 + 20 + 100 = 190$ coins.
|
Title: Monster
time_limit_ms: 2000
memory_limit_mb: 256
Description: Man, this Genshin boss is so hard. Good thing they have a top-up of $6$ coins for only $ \$4.99$. I should be careful and spend no more than I need to, lest my mom catches me...
You are fighting a monster with $z$ health using a weapon with $d$ damage. Initially, $d=0$. You can perform the following operations.
* Increase $d$ β the damage of your weapon by $1$, costing $x$ coins. * Attack the monster, dealing $d$ damage and costing $y$ coins.
You cannot perform the first operation for more than $k$ times in a row.
Find the minimum number of coins needed to defeat the monster by dealing at least $z$ damage.
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 4 integers $x$, $y$, $z$, and $k$ ($1\leq x, y, z, k\leq 10^8$) β the first operation's cost, the second operation's cost, the monster's health, and the limitation on the first operation.
Output: For each test case, output the minimum number of coins needed to defeat the monster.
Examples:
input:
4
2 3 5 5
10 20 40 5
1 60 100 10
60 1 100 10
output:
12
190
280
160
Note: In the first test case, $x = 2$, $y = 3$, $z = 5$, and $k = 5$. Here's a strategy that achieves the lowest possible cost of $12$ coins:
* Increase damage by $1$, costing $2$ coins. * Increase damage by $1$, costing $2$ coins. * Increase damage by $1$, costing $2$ coins. * Attack the monster, dealing $3$ damage, costing $3$ coins. * Attack the monster, dealing $3$ damage, costing $3$ coins.
You deal a total of $3 + 3 = 6$ damage, defeating the monster who has $5$ health. The total number of coins you use is $2 + 2 + 2 + 3 + 3 = 12$ coins.
In the second test case, $x = 10$, $y = 20$, $z = 40$, and $k = 5$. Here's a strategy that achieves the lowest possible cost of $190$ coins:
* Increase damage by $5$, costing $5\cdot x$ = $50$ coins. * Attack the monster once, dealing $5$ damage, costing $20$ coins. * Increase damage by $2$, costing $2\cdot x$ = $20$ coins. * Attack the monster $5$ times, dealing $5\cdot 7 = 35$ damage, costing $5\cdot y$ = $100$ coins.
You deal a total of $5 + 35 = 40$ damage, defeating the monster who has exactly $40$ health. The total number of coins you use is $50 + 20 + 20 + 100 = 190$ coins.
|
1982E
|
https://codeforces.com/problemset/problem/1982/E
|
Number of k-good subarrays
| 2,300
|
[
"bitmasks",
"brute force",
"combinatorics",
"divide and conquer",
"dp",
"math",
"meet-in-the-middle"
] |
Div. 2
| 2,000
| 256
|
Let $bit(x)$ denote the number of ones in the binary representation of a non-negative integer $x$.
A subarray of an array is called $k$-good if it consists only of numbers with no more than $k$ ones in their binary representation, i.e., a subarray $(l, r)$ of array $a$ is good if for any $i$ such that $l \le i \le r$ condition $bit(a_{i}) \le k$ is satisfied.
You are given an array $a$ of length $n$, consisting of consecutive non-negative integers starting from $0$, i.e., $a_{i} = i$ for $0 \le i \le n - 1$ (in $0$-based indexing). You need to count the number of $k$-good subarrays in this array.
As the answer can be very large, output it modulo $10^{9} + 7$.
|
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. The following lines describe the test cases.
The single line of each test case contains two integers $n$, $k$ ($1 \le n \le 10^{18}, 1 \le k \le 60$).
|
For each test case, output a single integer β the number of $k$-good subarrays modulo $10^{9} + 7$.
|
[
[
"10\n6 1\n16 2\n1 1\n3 1\n31 3\n14 1\n1337 5\n100000 20\n795569939321040850 56\n576460752303423268 59",
"7\n35\n1\n6\n155\n8\n7323\n49965\n741136395\n66679884"
]
] |
For the first test case $a = [0, 1, 2, 3, 4, 5]$, $k = 1$.
To find the answer, let's write all the numbers in binary representation:
$$a = [\color{green}{000}, \color{green}{001}, \color{green}{010}, \color{red}{011}, \color{green}{100}, \color{red}{101}]$$
From this, it can be seen that the numbers $3$ and $5$ have $2 \ge (k = 1)$ ones in their binary representation, so the answer should include all subarrays that do not contain either $3$ or $5$, which are the subarrays (in $0$-based indexing): ($0$, $0$), ($0$, $1$), ($0$, $2$), ($1$, $1$), ($1$, $2$), ($2$, $2$), ($4$, $4$).
|
Title: Number of k-good subarrays
time_limit_ms: 2000
memory_limit_mb: 256
Description: Let $bit(x)$ denote the number of ones in the binary representation of a non-negative integer $x$.
A subarray of an array is called $k$-good if it consists only of numbers with no more than $k$ ones in their binary representation, i.e., a subarray $(l, r)$ of array $a$ is good if for any $i$ such that $l \le i \le r$ condition $bit(a_{i}) \le k$ is satisfied.
You are given an array $a$ of length $n$, consisting of consecutive non-negative integers starting from $0$, i.e., $a_{i} = i$ for $0 \le i \le n - 1$ (in $0$-based indexing). You need to count the number of $k$-good subarrays in this array.
As the answer can be very large, output it modulo $10^{9} + 7$.
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. The following lines describe the test cases.
The single line of each test case contains two integers $n$, $k$ ($1 \le n \le 10^{18}, 1 \le k \le 60$).
Output: For each test case, output a single integer β the number of $k$-good subarrays modulo $10^{9} + 7$.
Examples:
input:
10
6 1
16 2
1 1
3 1
31 3
14 1
1337 5
100000 20
795569939321040850 56
576460752303423268 59
output:
7
35
1
6
155
8
7323
49965
741136395
66679884
Note: For the first test case $a = [0, 1, 2, 3, 4, 5]$, $k = 1$.
To find the answer, let's write all the numbers in binary representation:
$$a = [\color{green}{000}, \color{green}{001}, \color{green}{010}, \color{red}{011}, \color{green}{100}, \color{red}{101}]$$
From this, it can be seen that the numbers $3$ and $5$ have $2 \ge (k = 1)$ ones in their binary representation, so the answer should include all subarrays that do not contain either $3$ or $5$, which are the subarrays (in $0$-based indexing): ($0$, $0$), ($0$, $1$), ($0$, $2$), ($1$, $1$), ($1$, $2$), ($2$, $2$), ($4$, $4$).
|
1972D2
|
https://codeforces.com/problemset/problem/1972/D2
|
Reverse Card (Hard Version)
| 2,200
|
[
"brute force",
"math",
"number theory"
] |
Div. 2
| 2,000
| 256
|
The two versions are different problems. You may want to read both versions. You can make hacks only if both versions are solved.
You are given two positive integers $n$, $m$.
Calculate the number of ordered pairs $(a, b)$ satisfying the following conditions:
* $1\le a\le n$, $1\le b\le m$; * $b \cdot \gcd(a,b)$ is a multiple of $a+b$.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le 10^4$). The description of the test cases follows.
The first line of each test case contains two integers $n$, $m$ ($1\le n,m\le 2 \cdot 10^6$).
It is guaranteed that neither the sum of $n$ nor the sum of $m$ over all test cases exceeds $2 \cdot 10^6$.
|
For each test case, print a single integer: the number of valid pairs.
|
[
[
"6\n1 1\n2 3\n3 5\n10 8\n100 1233\n1000000 1145141",
"0\n1\n1\n6\n423\n5933961"
]
] |
In the first test case, no pair satisfies the conditions.
In the fourth test case, $(2,2),(3,6),(4,4),(6,3),(6,6),(8,8)$ satisfy the conditions.
|
Title: Reverse Card (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: The two versions are different problems. You may want to read both versions. You can make hacks only if both versions are solved.
You are given two positive integers $n$, $m$.
Calculate the number of ordered pairs $(a, b)$ satisfying the following conditions:
* $1\le a\le n$, $1\le b\le m$; * $b \cdot \gcd(a,b)$ is a multiple of $a+b$.
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$ ($1\le n,m\le 2 \cdot 10^6$).
It is guaranteed that neither the sum of $n$ nor the sum of $m$ over all test cases exceeds $2 \cdot 10^6$.
Output: For each test case, print a single integer: the number of valid pairs.
Examples:
input:
6
1 1
2 3
3 5
10 8
100 1233
1000000 1145141
output:
0
1
1
6
423
5933961
Note: In the first test case, no pair satisfies the conditions.
In the fourth test case, $(2,2),(3,6),(4,4),(6,3),(6,6),(8,8)$ satisfy the conditions.
|
1981A
|
https://codeforces.com/problemset/problem/1981/A
|
Turtle and Piggy Are Playing a Game
| 800
|
[
"brute force",
"greedy",
"math"
] |
Div. 2
| 1,000
| 256
|
Turtle and Piggy are playing a number game.
First, Turtle will choose an integer $x$, such that $l \le x \le r$, where $l, r$ are given. It's also guaranteed that $2l \le r$.
Then, Piggy will keep doing the following operation until $x$ becomes $1$:
* Choose an integer $p$ such that $p \ge 2$ and $p \mid x$ (i.e. $x$ is a multiple of $p$). * Set $x$ to $\frac{x}{p}$, and the score will increase by $1$.
The score is initially $0$. Both Turtle and Piggy want to maximize the score. Please help them to calculate the maximum score.
|
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 $l, r$ ($1 \le l \le r \le 10^9, 2l \le r$) β The range where Turtle can choose the integer from.
|
For each test case, output a single integer β the maximum score.
|
[
[
"5\n2 4\n3 6\n2 15\n6 22\n114514 1919810",
"2\n2\n3\n4\n20"
]
] |
In the first test case, Turtle can choose an integer $x$, such that $2 \le x \le 4$. He can choose $x = 4$. Then Piggy can choose $p = 2$ for $2$ times. After that, $x$ will become $1$, and the score will be $2$, which is maximized.
In the second test case, Turtle can choose an integer $3 \le x \le 6$. He can choose $x = 6$. Then Piggy can choose $p = 2$, then choose $p = 3$. After that, $x$ will become $1$, and the score will be $2$, which is maximum.
In the third test case, Turtle can choose $x = 12$.
In the fourth test case, Turtle can choose $x = 16$.
|
Title: Turtle and Piggy Are Playing a Game
time_limit_ms: 1000
memory_limit_mb: 256
Description: Turtle and Piggy are playing a number game.
First, Turtle will choose an integer $x$, such that $l \le x \le r$, where $l, r$ are given. It's also guaranteed that $2l \le r$.
Then, Piggy will keep doing the following operation until $x$ becomes $1$:
* Choose an integer $p$ such that $p \ge 2$ and $p \mid x$ (i.e. $x$ is a multiple of $p$). * Set $x$ to $\frac{x}{p}$, and the score will increase by $1$.
The score is initially $0$. Both Turtle and Piggy want to maximize the score. Please help them to calculate the maximum score.
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 $l, r$ ($1 \le l \le r \le 10^9, 2l \le r$) β The range where Turtle can choose the integer from.
Output: For each test case, output a single integer β the maximum score.
Examples:
input:
5
2 4
3 6
2 15
6 22
114514 1919810
output:
2
2
3
4
20
Note: In the first test case, Turtle can choose an integer $x$, such that $2 \le x \le 4$. He can choose $x = 4$. Then Piggy can choose $p = 2$ for $2$ times. After that, $x$ will become $1$, and the score will be $2$, which is maximized.
In the second test case, Turtle can choose an integer $3 \le x \le 6$. He can choose $x = 6$. Then Piggy can choose $p = 2$, then choose $p = 3$. After that, $x$ will become $1$, and the score will be $2$, which is maximum.
In the third test case, Turtle can choose $x = 12$.
In the fourth test case, Turtle can choose $x = 16$.
|
1996D
|
https://codeforces.com/problemset/problem/1996/D
|
Fun
| 1,500
|
[
"binary search",
"brute force",
"combinatorics",
"math",
"number theory"
] |
Div. 3
| 2,000
| 256
|
Counting is Fun!
β satyam343
Given two integers $n$ and $x$, find the number of triplets ($a,b,c$) of positive integers such that $ab + ac + bc \le n$ and $a + b + c \le x$.
Note that order matters (e.g. ($1, 1, 2$) and ($1, 2, 1$) are treated as different) and $a$, $b$, $c$ must be strictly greater than $0$.
|
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
Each test case contains two integers $n$ and $x$ ($1 \leq n,x \leq 10^6$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ and that the sum of $x$ over all test cases does not exceed $10^6$.
|
Output a single integer β the number of triplets ($a,b,c$) of positive integers such that $ab + ac + bc \le n$ and $a + b + c \le x$.
|
[
[
"4\n7 4\n10 5\n7 1000\n900000 400000",
"4\n10\n7\n1768016938"
]
] |
In the first test case, the triplets are ($1, 1, 1$), ($1, 1, 2$), ($1, 2, 1$), and ($2, 1, 1$).
In the second test case, the triplets are ($1, 1, 1$), ($1, 1, 2$), ($1, 1, 3$), ($1, 2, 1$), ($1, 2, 2$), ($1, 3, 1$), ($2, 1, 1$), ($2, 1, 2$), ($2, 2, 1$), and ($3, 1, 1$).
|
Title: Fun
time_limit_ms: 2000
memory_limit_mb: 256
Description: Counting is Fun!
β satyam343
Given two integers $n$ and $x$, find the number of triplets ($a,b,c$) of positive integers such that $ab + ac + bc \le n$ and $a + b + c \le x$.
Note that order matters (e.g. ($1, 1, 2$) and ($1, 2, 1$) are treated as different) and $a$, $b$, $c$ must be strictly greater than $0$.
Input: The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
Each test case contains two integers $n$ and $x$ ($1 \leq n,x \leq 10^6$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ and that the sum of $x$ over all test cases does not exceed $10^6$.
Output: Output a single integer β the number of triplets ($a,b,c$) of positive integers such that $ab + ac + bc \le n$ and $a + b + c \le x$.
Examples:
input:
4
7 4
10 5
7 1000
900000 400000
output:
4
10
7
1768016938
Note: In the first test case, the triplets are ($1, 1, 1$), ($1, 1, 2$), ($1, 2, 1$), and ($2, 1, 1$).
In the second test case, the triplets are ($1, 1, 1$), ($1, 1, 2$), ($1, 1, 3$), ($1, 2, 1$), ($1, 2, 2$), ($1, 3, 1$), ($2, 1, 1$), ($2, 1, 2$), ($2, 2, 1$), and ($3, 1, 1$).
|
2013A
|
https://codeforces.com/problemset/problem/2013/A
|
Zhan's Blender
| 800
|
[
"constructive algorithms",
"math"
] |
Div. 2
| 1,000
| 256
|
Today, a club fair was held at "NSPhM". In order to advertise his pastry club, Zhan decided to demonstrate the power of his blender.
To demonstrate the power of his blender, Zhan has $n$ fruits.
The blender can mix up to $x$ fruits per second.
In each second, Zhan can put up to $y$ fruits into the blender. After that, the blender will blend $\min(x, c)$ fruits, where $c$ is the number of fruits inside the blender. After blending, blended fruits are removed from the blender.
Help Zhan determine the minimum amount of time required for Zhan to blend all fruits.
|
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 one integer $n$ ($0 \le n \le 10^9$) β the number of fruits Zhan has.
The second line of each test case contains two integers $x$ and $y$ ($1 \le x, y \le 10^9$) β the number of fruits the blender can blend per second and the number of fruits Zhan can put into the blender per second.
|
For each testcase, output a single integer β the minimum number of seconds to blend all fruits.
|
[
[
"5\n5\n3 4\n3\n1 2\n6\n4 3\n100\n4 3\n9\n3 3",
"2\n3\n2\n34\n3"
]
] |
In the first example, you can first put $2$ fruits in the blender. After that, the blender will mix these $2$ fruits, and in the end, there will be $0$ fruits left in the blender. Then you can put $3$ fruits into the blender, after which the blender will mix these $3$ fruits.
In the second example, you can put $1$ fruit into the blender $3$ times.
In the third example, you can first put $3$ fruits into the blender, then add another $3$ fruits.
|
Title: Zhan's Blender
time_limit_ms: 1000
memory_limit_mb: 256
Description: Today, a club fair was held at "NSPhM". In order to advertise his pastry club, Zhan decided to demonstrate the power of his blender.
To demonstrate the power of his blender, Zhan has $n$ fruits.
The blender can mix up to $x$ fruits per second.
In each second, Zhan can put up to $y$ fruits into the blender. After that, the blender will blend $\min(x, c)$ fruits, where $c$ is the number of fruits inside the blender. After blending, blended fruits are removed from the blender.
Help Zhan determine the minimum amount of time required for Zhan to blend all fruits.
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 one integer $n$ ($0 \le n \le 10^9$) β the number of fruits Zhan has.
The second line of each test case contains two integers $x$ and $y$ ($1 \le x, y \le 10^9$) β the number of fruits the blender can blend per second and the number of fruits Zhan can put into the blender per second.
Output: For each testcase, output a single integer β the minimum number of seconds to blend all fruits.
Examples:
input:
5
5
3 4
3
1 2
6
4 3
100
4 3
9
3 3
output:
2
3
2
34
3
Note: In the first example, you can first put $2$ fruits in the blender. After that, the blender will mix these $2$ fruits, and in the end, there will be $0$ fruits left in the blender. Then you can put $3$ fruits into the blender, after which the blender will mix these $3$ fruits.
In the second example, you can put $1$ fruit into the blender $3$ times.
In the third example, you can first put $3$ fruits into the blender, then add another $3$ fruits.
|
1996C
|
https://codeforces.com/problemset/problem/1996/C
|
Sort
| 1,200
|
[
"dp",
"greedy",
"sortings",
"strings"
] |
Div. 3
| 5,000
| 256
|
You are given two strings $a$ and $b$ of length $n$. Then, you are (forced against your will) to answer $q$ queries.
For each query, you are given a range bounded by $l$ and $r$. In one operation, you can choose an integer $i$ ($l \leq i \leq r$) and set $a_i = x$ where $x$ is any character you desire. Output the minimum number of operations you must perform such that $\texttt{sorted(a[l..r])} = \texttt{sorted(b[l..r])}$. The operations you perform on one query does not affect other queries.
For an arbitrary string $c$, $\texttt{sorted(c[l..r])}$ denotes the substring consisting of characters $c_l, c_{l+1}, ... , c_r$ sorted in lexicographical order.
|
The first line contains $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains two integers $n$ and $q$ ($1 \leq n, q \leq 2 \cdot 10^5$) β the length of both strings and the number of queries.
The following line contains $a$ of length $n$. It is guaranteed $a$ only contains lowercase latin letters.
The following line contains $b$ of length $n$. It is guaranteed $b$ only contains lowercase latin letters.
The following $q$ lines contain two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) β the range of the query.
It is guaranteed the sum of $n$ and $q$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each query, output an integer, the minimum number of operations you need to perform in a new line.
|
[
[
"3\n5 3\nabcde\nedcba\n1 5\n1 4\n3 3\n4 2\nzzde\nazbe\n1 3\n1 4\n6 3\nuwuwuw\nwuwuwu\n2 4\n1 3\n1 6",
"0\n1\n0\n2\n2\n1\n1\n0"
]
] |
For the first query, $\texttt{sorted(a[1..5])} =$ abcde and $\texttt{sorted(b[1..5])} =$ abcde, so no operations are necessary.
For the second query, you need to set $a_1 = $ e. Then, $\texttt{sorted(a[1..4])} = \texttt{sorted(b[1..4])} = $ bcde.
|
Title: Sort
time_limit_ms: 5000
memory_limit_mb: 256
Description: You are given two strings $a$ and $b$ of length $n$. Then, you are (forced against your will) to answer $q$ queries.
For each query, you are given a range bounded by $l$ and $r$. In one operation, you can choose an integer $i$ ($l \leq i \leq r$) and set $a_i = x$ where $x$ is any character you desire. Output the minimum number of operations you must perform such that $\texttt{sorted(a[l..r])} = \texttt{sorted(b[l..r])}$. The operations you perform on one query does not affect other queries.
For an arbitrary string $c$, $\texttt{sorted(c[l..r])}$ denotes the substring consisting of characters $c_l, c_{l+1}, ... , c_r$ sorted in lexicographical order.
Input: The first line contains $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains two integers $n$ and $q$ ($1 \leq n, q \leq 2 \cdot 10^5$) β the length of both strings and the number of queries.
The following line contains $a$ of length $n$. It is guaranteed $a$ only contains lowercase latin letters.
The following line contains $b$ of length $n$. It is guaranteed $b$ only contains lowercase latin letters.
The following $q$ lines contain two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) β the range of the query.
It is guaranteed the sum of $n$ and $q$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each query, output an integer, the minimum number of operations you need to perform in a new line.
Examples:
input:
3
5 3
abcde
edcba
1 5
1 4
3 3
4 2
zzde
azbe
1 3
1 4
6 3
uwuwuw
wuwuwu
2 4
1 3
1 6
output:
0
1
0
2
2
1
1
0
Note: For the first query, $\texttt{sorted(a[1..5])} =$ abcde and $\texttt{sorted(b[1..5])} =$ abcde, so no operations are necessary.
For the second query, you need to set $a_1 = $ e. Then, $\texttt{sorted(a[1..4])} = \texttt{sorted(b[1..4])} = $ bcde.
|
1984D
|
https://codeforces.com/problemset/problem/1984/D
|
''a'' String Problem
| 2,000
|
[
"brute force",
"hashing",
"implementation",
"math",
"string suffix structures",
"strings"
] |
Div. 1 + 2
| 2,000
| 256
|
You are given a string $s$ consisting of lowercase Latin characters. Count the number of nonempty strings $t \neq$ "$\texttt{a}$" such that it is possible to partition$^{\dagger}$ $s$ into some substrings satisfying the following conditions:
* each substring either equals $t$ or "$\texttt{a}$", and * at least one substring equals $t$.
$^{\dagger}$ A partition of a string $s$ is an ordered sequence of some $k$ strings $t_1, t_2, \ldots, t_k$ (called substrings) such that $t_1 + t_2 + \ldots + t_k = s$, where $+$ represents the concatenation operation.
|
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The only line of each test case contains a string $s$ consisting of lowercase Latin characters ($2 \leq |s| \leq 2 \cdot 10^5$).
The sum of $|s|$ over all test cases does not exceed $3 \cdot 10^5$.
|
For each test case, output a single integer β the number of nonempty strings $t \neq$ "$\texttt{a}$" that satisfy all constraints.
|
[
[
"8\naaaaa\nbaba\ncabacb\naaabaaa\nbitset\nab\nabbaaaabbb\nyearnineteeneightyfour",
"4\n4\n1\n16\n1\n2\n3\n1"
]
] |
In the first test case, $t$ can be "$\texttt{aa}$", "$\texttt{aaa}$", "$\texttt{aaaa}$", or the full string.
In the second test case, $t$ can be "$\texttt{b}$", "$\texttt{bab}$", "$\texttt{ba}$", or the full string.
In the third test case, the only such $t$ is the full string.
|
Title: ''a'' String Problem
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given a string $s$ consisting of lowercase Latin characters. Count the number of nonempty strings $t \neq$ "$\texttt{a}$" such that it is possible to partition$^{\dagger}$ $s$ into some substrings satisfying the following conditions:
* each substring either equals $t$ or "$\texttt{a}$", and * at least one substring equals $t$.
$^{\dagger}$ A partition of a string $s$ is an ordered sequence of some $k$ strings $t_1, t_2, \ldots, t_k$ (called substrings) such that $t_1 + t_2 + \ldots + t_k = s$, where $+$ represents the concatenation operation.
Input: The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The only line of each test case contains a string $s$ consisting of lowercase Latin characters ($2 \leq |s| \leq 2 \cdot 10^5$).
The sum of $|s|$ over all test cases does not exceed $3 \cdot 10^5$.
Output: For each test case, output a single integer β the number of nonempty strings $t \neq$ "$\texttt{a}$" that satisfy all constraints.
Examples:
input:
8
aaaaa
baba
cabacb
aaabaaa
bitset
ab
abbaaaabbb
yearnineteeneightyfour
output:
4
4
1
16
1
2
3
1
Note: In the first test case, $t$ can be "$\texttt{aa}$", "$\texttt{aaa}$", "$\texttt{aaaa}$", or the full string.
In the second test case, $t$ can be "$\texttt{b}$", "$\texttt{bab}$", "$\texttt{ba}$", or the full string.
In the third test case, the only such $t$ is the full string.
|
1988D
|
https://codeforces.com/problemset/problem/1988/D
|
The Omnipotent Monster Killer
| 2,000
|
[
"brute force",
"dfs and similar",
"dp",
"trees"
] |
Div. 2
| 3,000
| 512
|
You, the monster killer, want to kill a group of monsters. The monsters are on a tree with $n$ vertices. On vertex with number $i$ ($1\le i\le n$), there is a monster with $a_i$ attack points. You want to battle with monsters for $10^{100}$ rounds.
In each round, the following happens in order:
1. All living monsters attack you. Your health decreases by the sum of attack points of all living monsters. 2. You select some (possibly all or none) monsters and kill them. After being killed, the monster will not be able to do any attacks in the future.
There is a restriction: in one round, you cannot kill two monsters that are directly connected by an edge.
If you choose what monsters to attack optimally, what is the smallest health decrement you can have after all rounds?
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows.
The first line of each test case contains an integer $n$ ($1\le n\le 3\cdot 10^5$).
The second line of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\le a_i\le 10^{12}$).
The following $n-1$ lines each contain two integers $x,y$ ($1\le x,y\le n$), denoting an edge on the tree connecting vertex $x$ and $y$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot 10^5$.
|
For each test case, print one integer: the minimum possible health decrement.
|
[
[
"3\n1\n1000000000000\n5\n47 15 32 29 23\n1 2\n1 3\n2 4\n2 5\n7\n8 10 2 3 5 7 4\n1 2\n1 4\n3 2\n5 3\n6 2\n7 5",
"1000000000000\n193\n57"
]
] |
In the first test case, an optimal sequence of operations would be:
* In the first round: first, receive the attack from the monster on vertex $1$, so your health decreases by $10^{12}$. Then kill the monster on vertex $1$. * In the second round to the $10^{100}$-th round: all monsters have been killed, so nothing happens.
The total health decrement is $10^{12}$.
In the second test case, an optimal sequence of operations would be:
* In the first round: first, receive the attack from the monster on vertex $1,2,3,4,5$, so your health decreases by $47+15+32+29+23=146$. Then kill the monsters on vertex $1,4,5$. * In the second round: first, receive the attack from the monster on vertex $2,3$, so your health decreases by $15+32=47$. Then kill the monsters on vertex $2,3$. * In the third round to the $10^{100}$-th round: all monsters have been killed, so nothing happens.
The total health decrement is $193$.
In the third test case, an optimal sequence of operations would be:
* In the first round: first, receive the attack from the monster on vertex $1,2,3,4,5,6,7$, so your health decreases by $8+10+2+3+5+7+4=39$. Then kill the monsters on vertex $1,3,6,7$. * In the second round: first, receive the attack from the monster on vertex $2,4,5$, so your health decreases by $10+3+5=18$. Then kill the monsters on vertex $2,4,5$. * In the third round to the $10^{100}$-th round: all monsters have been killed, so nothing happens.
The total health decrement is $57$.
|
Title: The Omnipotent Monster Killer
time_limit_ms: 3000
memory_limit_mb: 512
Description: You, the monster killer, want to kill a group of monsters. The monsters are on a tree with $n$ vertices. On vertex with number $i$ ($1\le i\le n$), there is a monster with $a_i$ attack points. You want to battle with monsters for $10^{100}$ rounds.
In each round, the following happens in order:
1. All living monsters attack you. Your health decreases by the sum of attack points of all living monsters. 2. You select some (possibly all or none) monsters and kill them. After being killed, the monster will not be able to do any attacks in the future.
There is a restriction: in one round, you cannot kill two monsters that are directly connected by an edge.
If you choose what monsters to attack optimally, what is the smallest health decrement you can have after all rounds?
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 the test cases follows.
The first line of each test case contains an integer $n$ ($1\le n\le 3\cdot 10^5$).
The second line of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\le a_i\le 10^{12}$).
The following $n-1$ lines each contain two integers $x,y$ ($1\le x,y\le n$), denoting an edge on the tree connecting vertex $x$ and $y$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot 10^5$.
Output: For each test case, print one integer: the minimum possible health decrement.
Examples:
input:
3
1
1000000000000
5
47 15 32 29 23
1 2
1 3
2 4
2 5
7
8 10 2 3 5 7 4
1 2
1 4
3 2
5 3
6 2
7 5
output:
1000000000000
193
57
Note: In the first test case, an optimal sequence of operations would be:
* In the first round: first, receive the attack from the monster on vertex $1$, so your health decreases by $10^{12}$. Then kill the monster on vertex $1$. * In the second round to the $10^{100}$-th round: all monsters have been killed, so nothing happens.
The total health decrement is $10^{12}$.
In the second test case, an optimal sequence of operations would be:
* In the first round: first, receive the attack from the monster on vertex $1,2,3,4,5$, so your health decreases by $47+15+32+29+23=146$. Then kill the monsters on vertex $1,4,5$. * In the second round: first, receive the attack from the monster on vertex $2,3$, so your health decreases by $15+32=47$. Then kill the monsters on vertex $2,3$. * In the third round to the $10^{100}$-th round: all monsters have been killed, so nothing happens.
The total health decrement is $193$.
In the third test case, an optimal sequence of operations would be:
* In the first round: first, receive the attack from the monster on vertex $1,2,3,4,5,6,7$, so your health decreases by $8+10+2+3+5+7+4=39$. Then kill the monsters on vertex $1,3,6,7$. * In the second round: first, receive the attack from the monster on vertex $2,4,5$, so your health decreases by $10+3+5=18$. Then kill the monsters on vertex $2,4,5$. * In the third round to the $10^{100}$-th round: all monsters have been killed, so nothing happens.
The total health decrement is $57$.
|
1989D
|
https://codeforces.com/problemset/problem/1989/D
|
Smithing Skill
| 1,900
|
[
"brute force",
"data structures",
"dp",
"greedy",
"math",
"sortings",
"two pointers"
] |
Div. 2
| 3,000
| 256
|
You are playing a famous computer game (that just works) where you have various skills you can level up. Today, you focused on the "Smithing" skill. Your tactic is obvious: forging weapons from ingots and then melting them back to return the materials partially. For simplicity, every time you create an item, you get $1$ experience point, and every time you melt an item, you also get $1$ experience point.
There are $n$ classes of weapons you can forge and $m$ types of metal ingots.
You can create one weapon of the $i$-th class, spending $a_i$ ingots of metal of the same type. Melting a weapon of the $i$-th class (which you crafted earlier) returns you $b_i$ ingots of the type of metal it was made of.
You have $c_j$ metal ingots of the $j$-th type, and you know that you can craft a weapon of any class from any metal type. Each combination of a weapon class and a metal type can be used any number of times.
What is the maximum total amount of experience you can earn by crafting and melting weapons?
|
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 10^6$) β the number of weapon classes and metal types.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the number of ingots you need to forge one weapon of the $i$-th class.
The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($0 \le b_i < a_i$), where $b_i$ is the number of ingots you return by melting one weapon of the $i$-th class you forged earlier.
The fourth line contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le 10^9$) β the number of ingots you have of the corresponding metal type.
|
Print one integer β the maximum total experience points you can gain by repeatedly forging and melting weapons.
|
[
[
"5 3\n9 6 7 5 5\n8 4 5 1 2\n10 4 7",
"12"
],
[
"3 4\n10 20 20\n0 0 0\n9 10 19 20",
"8"
],
[
"1 5\n3\n1\n1000000000 1000000000 1000000000 1000000000 1000000000",
"4999999990"
]
] |
In the first example, you can do the following:
1. craft one weapon of the $1$-st class from the $1$-st type of metal, spending $9$ ingots; 2. melt that weapon, returning $8$ ingots of the $1$-st metal type; 3. again, craft and melt one weapon of the $1$-st class from the $1$-st metal type; 4. craft and melt one weapon of the $3$-rd class from the $1$-st metal type; 5. craft and melt one weapon of the $3$-rd class from the $3$-rd metal type; 6. craft and melt one weapon of the $4$-th class from the $1$-st metal type; 7. craft and melt one weapon of the $5$-th class from the $3$-rd metal type;
In the end you'll have $c = [2, 4, 2]$ ingots left. In total, you've crafted $6$ weapons and melted $6$ weapons, gaining $12$ experience points in total.
|
Title: Smithing Skill
time_limit_ms: 3000
memory_limit_mb: 256
Description: You are playing a famous computer game (that just works) where you have various skills you can level up. Today, you focused on the "Smithing" skill. Your tactic is obvious: forging weapons from ingots and then melting them back to return the materials partially. For simplicity, every time you create an item, you get $1$ experience point, and every time you melt an item, you also get $1$ experience point.
There are $n$ classes of weapons you can forge and $m$ types of metal ingots.
You can create one weapon of the $i$-th class, spending $a_i$ ingots of metal of the same type. Melting a weapon of the $i$-th class (which you crafted earlier) returns you $b_i$ ingots of the type of metal it was made of.
You have $c_j$ metal ingots of the $j$-th type, and you know that you can craft a weapon of any class from any metal type. Each combination of a weapon class and a metal type can be used any number of times.
What is the maximum total amount of experience you can earn by crafting and melting weapons?
Input: The first line contains two integers $n$ and $m$ ($1 \le n, m \le 10^6$) β the number of weapon classes and metal types.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the number of ingots you need to forge one weapon of the $i$-th class.
The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($0 \le b_i < a_i$), where $b_i$ is the number of ingots you return by melting one weapon of the $i$-th class you forged earlier.
The fourth line contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le 10^9$) β the number of ingots you have of the corresponding metal type.
Output: Print one integer β the maximum total experience points you can gain by repeatedly forging and melting weapons.
Examples:
input:
5 3
9 6 7 5 5
8 4 5 1 2
10 4 7
output:
12
input:
3 4
10 20 20
0 0 0
9 10 19 20
output:
8
input:
1 5
3
1
1000000000 1000000000 1000000000 1000000000 1000000000
output:
4999999990
Note: In the first example, you can do the following:
1. craft one weapon of the $1$-st class from the $1$-st type of metal, spending $9$ ingots; 2. melt that weapon, returning $8$ ingots of the $1$-st metal type; 3. again, craft and melt one weapon of the $1$-st class from the $1$-st metal type; 4. craft and melt one weapon of the $3$-rd class from the $1$-st metal type; 5. craft and melt one weapon of the $3$-rd class from the $3$-rd metal type; 6. craft and melt one weapon of the $4$-th class from the $1$-st metal type; 7. craft and melt one weapon of the $5$-th class from the $3$-rd metal type;
In the end you'll have $c = [2, 4, 2]$ ingots left. In total, you've crafted $6$ weapons and melted $6$ weapons, gaining $12$ experience points in total.
|
2035A
|
https://codeforces.com/problemset/problem/2035/A
|
Sliding
| 800
|
[
"implementation",
"math"
] |
Div. 1 + 2
| 1,000
| 256
|
Red was ejected. They were not the imposter.
There are $n$ rows of $m$ people. Let the position in the $r$-th row and the $c$-th column be denoted by $(r, c)$. Number each person starting from $1$ in row-major order, i.e., the person numbered $(r-1)\cdot m+c$ is initially at $(r,c)$.
The person at $(r, c)$ decides to leave. To fill the gap, let the person who left be numbered $i$. Each person numbered $j>i$ will move to the position where the person numbered $j-1$ is initially at. The following diagram illustrates the case where $n=2$, $m=3$, $r=1$, and $c=2$.

Calculate the sum of the Manhattan distances of each person's movement. If a person was initially at $(r_0, c_0)$ and then moved to $(r_1, c_1)$, the Manhattan distance is $|r_0-r_1|+|c_0-c_1|$.
|
The first line contains a single integer $t$ ($1\le t\le 10^4$) β the number of test cases.
The only line of each testcase contains $4$ integers $n$, $m$, $r$, and $c$ ($1\le r\le n\le 10^6$, $1 \le c \le m \le 10^6$), where $n$ is the number of rows, $m$ is the number of columns, and $(r,c)$ is the position where the person who left is initially at.
|
For each test case, output a single integer denoting the sum of the Manhattan distances.
|
[
[
"4\n2 3 1 2\n2 2 2 1\n1 1 1 1\n1000000 1000000 1 1",
"6\n1\n0\n1999998000000"
]
] |
For the first test case, the person numbered $2$ leaves, and the distances of the movements of the person numbered $3$, $4$, $5$, and $6$ are $1$, $3$, $1$, and $1$, respectively. So the answer is $1+3+1+1=6$.
For the second test case, the person numbered $3$ leaves, and the person numbered $4$ moves. The answer is $1$.
|
Title: Sliding
time_limit_ms: 1000
memory_limit_mb: 256
Description: Red was ejected. They were not the imposter.
There are $n$ rows of $m$ people. Let the position in the $r$-th row and the $c$-th column be denoted by $(r, c)$. Number each person starting from $1$ in row-major order, i.e., the person numbered $(r-1)\cdot m+c$ is initially at $(r,c)$.
The person at $(r, c)$ decides to leave. To fill the gap, let the person who left be numbered $i$. Each person numbered $j>i$ will move to the position where the person numbered $j-1$ is initially at. The following diagram illustrates the case where $n=2$, $m=3$, $r=1$, and $c=2$.

Calculate the sum of the Manhattan distances of each person's movement. If a person was initially at $(r_0, c_0)$ and then moved to $(r_1, c_1)$, the Manhattan distance is $|r_0-r_1|+|c_0-c_1|$.
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 testcase contains $4$ integers $n$, $m$, $r$, and $c$ ($1\le r\le n\le 10^6$, $1 \le c \le m \le 10^6$), where $n$ is the number of rows, $m$ is the number of columns, and $(r,c)$ is the position where the person who left is initially at.
Output: For each test case, output a single integer denoting the sum of the Manhattan distances.
Examples:
input:
4
2 3 1 2
2 2 2 1
1 1 1 1
1000000 1000000 1 1
output:
6
1
0
1999998000000
Note: For the first test case, the person numbered $2$ leaves, and the distances of the movements of the person numbered $3$, $4$, $5$, and $6$ are $1$, $3$, $1$, and $1$, respectively. So the answer is $1+3+1+1=6$.
For the second test case, the person numbered $3$ leaves, and the person numbered $4$ moves. The answer is $1$.
|
1993E
|
https://codeforces.com/problemset/problem/1993/E
|
Xor-Grid Problem
| 2,700
|
[
"bitmasks",
"constructive algorithms",
"dp",
"implementation"
] |
Div. 2
| 5,000
| 256
|
Given a matrix $a$ of size $n \times m$, each cell of which contains a non-negative integer. The integer lying at the intersection of the $i$-th row and the $j$-th column of the matrix is called $a_{i,j}$.
Let's define $f(i)$ and $g(j)$ as the [XOR](https://en.wikipedia.org/wiki/Exclusive_or) of all integers in the $i$-th row and the $j$-th column, respectively. In one operation, you can either:
* Select any row $i$, then assign $a_{i,j} := g(j)$ for each $1 \le j \le m$; or * Select any column $j$, then assign $a_{i,j} := f(i)$ for each $1 \le i \le n$.
 An example of applying an operation on column $2$ of the matrix.
In this example, as we apply an operation on column $2$, all elements in this column are changed:
* $a_{1,2} := f(1) = a_{1,1} \oplus a_{1,2} \oplus a_{1,3} \oplus a_{1,4} = 1 \oplus 1 \oplus 1 \oplus 1 = 0$ * $a_{2,2} := f(2) = a_{2,1} \oplus a_{2,2} \oplus a_{2,3} \oplus a_{2,4} = 2 \oplus 3 \oplus 5 \oplus 7 = 3$ * $a_{3,2} := f(3) = a_{3,1} \oplus a_{3,2} \oplus a_{3,3} \oplus a_{3,4} = 2 \oplus 0 \oplus 3 \oplus 0 = 1$ * $a_{4,2} := f(4) = a_{4,1} \oplus a_{4,2} \oplus a_{4,3} \oplus a_{4,4} = 10 \oplus 11 \oplus 12 \oplus 16 = 29$
You can apply the operations any number of times. Then, we calculate the $\textit{beauty}$ of the final matrix by summing the absolute differences between all pairs of its adjacent cells.
More formally, $\textit{beauty}(a) = \sum|a_{x,y} - a_{r,c}|$ for all cells $(x, y)$ and $(r, c)$ if they are adjacent. Two cells are considered adjacent if they share a side.
Find the minimum $\textit{beauty}$ among all obtainable matrices.
|
The first line contains a single integer $t$ ($1 \le t \le 250$) β the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 15$) β the number of rows and columns of $a$, respectively.
The next $n$ lines, each containing $m$ integers $a_{i,1}, a_{i,2}, \ldots, a_{i,m}$ ($0 \le a_{i,j} < 2^{20}$) β description of the matrix $a$.
It is guaranteed that the sum of $(n^2 + m^2)$ over all test cases does not exceed $500$.
|
For each test case, print a single integer $b$ β the smallest possible $\textit{beauty}$ of the matrix.
|
[
[
"4\n1 2\n1 3\n2 3\n0 1 0\n5 4 4\n2 3\n0 2 4\n4 5 1\n3 3\n1 2 3\n4 5 6\n7 8 9",
"1\n3\n13\n24"
]
] |
Let's denote $r(i)$ as the first type operation applied on the $i$-th row, and $c(j)$ as the second type operation applied on the $j$-th column.
In the first test case, you can apply an operation $c(1)$, which assigns $a_{1,1} := 1 \oplus 3 = 2$. Then, we'll receive this matrix:
2| 3 ---|--- In the second test case, you can apply an operation $r(1)$, which assigns:
* $a_{1,1} := g(1) = 0 \oplus 5 = 5$ * $a_{1,2} := g(2) = 1 \oplus 4 = 5$ * $a_{1,3} := g(3) = 0 \oplus 4 = 4$
The resulting matrix after performing the operation is:
5| 5| 4 ---|---|--- 5| 4| 4 In the third test case, the best way to achieve minimum $\textit{beauty}$ is applying three operations: $c(3)$, $r(2)$, and $c(2)$. The resulting matrix is:
0| 4| 6 ---|---|--- 4| 5| 6
|
Title: Xor-Grid Problem
time_limit_ms: 5000
memory_limit_mb: 256
Description: Given a matrix $a$ of size $n \times m$, each cell of which contains a non-negative integer. The integer lying at the intersection of the $i$-th row and the $j$-th column of the matrix is called $a_{i,j}$.
Let's define $f(i)$ and $g(j)$ as the [XOR](https://en.wikipedia.org/wiki/Exclusive_or) of all integers in the $i$-th row and the $j$-th column, respectively. In one operation, you can either:
* Select any row $i$, then assign $a_{i,j} := g(j)$ for each $1 \le j \le m$; or * Select any column $j$, then assign $a_{i,j} := f(i)$ for each $1 \le i \le n$.
 An example of applying an operation on column $2$ of the matrix.
In this example, as we apply an operation on column $2$, all elements in this column are changed:
* $a_{1,2} := f(1) = a_{1,1} \oplus a_{1,2} \oplus a_{1,3} \oplus a_{1,4} = 1 \oplus 1 \oplus 1 \oplus 1 = 0$ * $a_{2,2} := f(2) = a_{2,1} \oplus a_{2,2} \oplus a_{2,3} \oplus a_{2,4} = 2 \oplus 3 \oplus 5 \oplus 7 = 3$ * $a_{3,2} := f(3) = a_{3,1} \oplus a_{3,2} \oplus a_{3,3} \oplus a_{3,4} = 2 \oplus 0 \oplus 3 \oplus 0 = 1$ * $a_{4,2} := f(4) = a_{4,1} \oplus a_{4,2} \oplus a_{4,3} \oplus a_{4,4} = 10 \oplus 11 \oplus 12 \oplus 16 = 29$
You can apply the operations any number of times. Then, we calculate the $\textit{beauty}$ of the final matrix by summing the absolute differences between all pairs of its adjacent cells.
More formally, $\textit{beauty}(a) = \sum|a_{x,y} - a_{r,c}|$ for all cells $(x, y)$ and $(r, c)$ if they are adjacent. Two cells are considered adjacent if they share a side.
Find the minimum $\textit{beauty}$ among all obtainable matrices.
Input: The first line contains a single integer $t$ ($1 \le t \le 250$) β the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 15$) β the number of rows and columns of $a$, respectively.
The next $n$ lines, each containing $m$ integers $a_{i,1}, a_{i,2}, \ldots, a_{i,m}$ ($0 \le a_{i,j} < 2^{20}$) β description of the matrix $a$.
It is guaranteed that the sum of $(n^2 + m^2)$ over all test cases does not exceed $500$.
Output: For each test case, print a single integer $b$ β the smallest possible $\textit{beauty}$ of the matrix.
Examples:
input:
4
1 2
1 3
2 3
0 1 0
5 4 4
2 3
0 2 4
4 5 1
3 3
1 2 3
4 5 6
7 8 9
output:
1
3
13
24
Note: Let's denote $r(i)$ as the first type operation applied on the $i$-th row, and $c(j)$ as the second type operation applied on the $j$-th column.
In the first test case, you can apply an operation $c(1)$, which assigns $a_{1,1} := 1 \oplus 3 = 2$. Then, we'll receive this matrix:
2| 3 ---|--- In the second test case, you can apply an operation $r(1)$, which assigns:
* $a_{1,1} := g(1) = 0 \oplus 5 = 5$ * $a_{1,2} := g(2) = 1 \oplus 4 = 5$ * $a_{1,3} := g(3) = 0 \oplus 4 = 4$
The resulting matrix after performing the operation is:
5| 5| 4 ---|---|--- 5| 4| 4 In the third test case, the best way to achieve minimum $\textit{beauty}$ is applying three operations: $c(3)$, $r(2)$, and $c(2)$. The resulting matrix is:
0| 4| 6 ---|---|--- 4| 5| 6
|
1984F
|
https://codeforces.com/problemset/problem/1984/F
|
Reconstruction
| 2,500
|
[
"brute force",
"dp",
"math"
] |
Div. 1 + 2
| 2,000
| 256
|
There is a hidden array $a_1, a_2, \ldots, a_n$ of length $n$ whose elements are integers between $-m$ and $m$, inclusive.
You are given an array $b_1, b_2, \ldots, b_n$ of length $n$ and a string $s$ of length $n$ consisting of the characters $\texttt{P}$, $\texttt{S}$, and $\texttt{?}$.
For each $i$ from $1$ to $n$ inclusive, we must have:
* If $s_i = \texttt{P}$, $b_i$ is the sum of $a_1$ through $a_i$. * If $s_i = \texttt{S}$, $b_i$ is the sum of $a_i$ through $a_n$.
Output the number of ways to replace all $\texttt{?}$ in $s$ with either $\texttt{P}$ or $\texttt{S}$ such that there exists an array $a_1, a_2, \ldots, a_n$ with elements not exceeding $m$ by absolute value satisfying the constraints given by the array $b_1, b_2, \ldots, b_n$ and the string $s$.
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^3$) β the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 2 \cdot 10^3$, $2 \leq m \leq 10^{9}$) β the length of the hidden array $a_1, a_2, \ldots, a_n$ and the maximum absolute value of an element $a_i$.
The second line of each test case contains a string $s$ of length $n$ consisting of characters $\texttt{P}$, $\texttt{S}$, and $\texttt{?}$.
The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($|b_i| \leq m \cdot n$).
The sum of $n$ over all test cases does not exceed $5 \cdot 10^3$.
|
For each test case, output a single integer β the number of ways to replace all $\texttt{?}$ in $s$ with either $\texttt{P}$ or $\texttt{S}$ that result in the existence of a valid array $a_1, a_2, \ldots, a_n$, modulo $998\,244\,353$.
|
[
[
"6\n4 10\nPSPP\n1 9 8 10\n4 1000000000\n????\n1 1 1 4000000000\n8 1000000000\n?P?SSP?P\n-857095623 -1424391899 -851974476 673437144 471253851 -543483033 364945701 -178537332\n4 7\nPPSS\n4 2 1 3\n9 20\n?????????\n1 2 3 4 5 6 7 8 9\n3 1000000000\nP??\n-145463248 -974068460 -1287458396",
"1\n0\n2\n1\n14\n1"
]
] |
In the first test case, we can see that the following array satisfies all constraints, thus the answer is $1$:
1. $\texttt{P}$ β ${[\color{red}{\textbf{1}},3,4,2]}$: sum of $1$. 2. $\texttt{S}$ β ${[1,\color{red}{\textbf{3},4,2}]}$: sum of $9$. 3. $\texttt{P}$ β ${[\color{red}{1,3,\textbf{4}},2]}$: sum of $8$. 4. $\texttt{P}$ β ${[\color{red}{1,3,4,\textbf{2}}]}$: sum of $10$.
In the second test case, it can be shown that no array $a$ with all $|a_i| \leq m = 10^9$ satisfies all constraints.
|
Title: Reconstruction
time_limit_ms: 2000
memory_limit_mb: 256
Description: There is a hidden array $a_1, a_2, \ldots, a_n$ of length $n$ whose elements are integers between $-m$ and $m$, inclusive.
You are given an array $b_1, b_2, \ldots, b_n$ of length $n$ and a string $s$ of length $n$ consisting of the characters $\texttt{P}$, $\texttt{S}$, and $\texttt{?}$.
For each $i$ from $1$ to $n$ inclusive, we must have:
* If $s_i = \texttt{P}$, $b_i$ is the sum of $a_1$ through $a_i$. * If $s_i = \texttt{S}$, $b_i$ is the sum of $a_i$ through $a_n$.
Output the number of ways to replace all $\texttt{?}$ in $s$ with either $\texttt{P}$ or $\texttt{S}$ such that there exists an array $a_1, a_2, \ldots, a_n$ with elements not exceeding $m$ by absolute value satisfying the constraints given by the array $b_1, b_2, \ldots, b_n$ and the string $s$.
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^3$) β the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 2 \cdot 10^3$, $2 \leq m \leq 10^{9}$) β the length of the hidden array $a_1, a_2, \ldots, a_n$ and the maximum absolute value of an element $a_i$.
The second line of each test case contains a string $s$ of length $n$ consisting of characters $\texttt{P}$, $\texttt{S}$, and $\texttt{?}$.
The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($|b_i| \leq m \cdot n$).
The sum of $n$ over all test cases does not exceed $5 \cdot 10^3$.
Output: For each test case, output a single integer β the number of ways to replace all $\texttt{?}$ in $s$ with either $\texttt{P}$ or $\texttt{S}$ that result in the existence of a valid array $a_1, a_2, \ldots, a_n$, modulo $998\,244\,353$.
Examples:
input:
6
4 10
PSPP
1 9 8 10
4 1000000000
????
1 1 1 4000000000
8 1000000000
?P?SSP?P
-857095623 -1424391899 -851974476 673437144 471253851 -543483033 364945701 -178537332
4 7
PPSS
4 2 1 3
9 20
?????????
1 2 3 4 5 6 7 8 9
3 1000000000
P??
-145463248 -974068460 -1287458396
output:
1
0
2
1
14
1
Note: In the first test case, we can see that the following array satisfies all constraints, thus the answer is $1$:
1. $\texttt{P}$ β ${[\color{red}{\textbf{1}},3,4,2]}$: sum of $1$. 2. $\texttt{S}$ β ${[1,\color{red}{\textbf{3},4,2}]}$: sum of $9$. 3. $\texttt{P}$ β ${[\color{red}{1,3,\textbf{4}},2]}$: sum of $8$. 4. $\texttt{P}$ β ${[\color{red}{1,3,4,\textbf{2}}]}$: sum of $10$.
In the second test case, it can be shown that no array $a$ with all $|a_i| \leq m = 10^9$ satisfies all constraints.
|
2003B
|
https://codeforces.com/problemset/problem/2003/B
|
Turtle and Piggy Are Playing a Game 2
| 800
|
[
"games",
"greedy",
"sortings"
] |
Div. 2
| 1,000
| 256
|
Turtle and Piggy are playing a game on a sequence. They are given a sequence $a_1, a_2, \ldots, a_n$, and Turtle goes first. Turtle and Piggy alternate in turns (so, Turtle does the first turn, Piggy does the second, Turtle does the third, etc.).
The game goes as follows:
* Let the current length of the sequence be $m$. If $m = 1$, the game ends. * If the game does not end and it's Turtle's turn, then Turtle must choose an integer $i$ such that $1 \le i \le m - 1$, set $a_i$ to $\max(a_i, a_{i + 1})$, and remove $a_{i + 1}$. * If the game does not end and it's Piggy's turn, then Piggy must choose an integer $i$ such that $1 \le i \le m - 1$, set $a_i$ to $\min(a_i, a_{i + 1})$, and remove $a_{i + 1}$.
Turtle wants to maximize the value of $a_1$ in the end, while Piggy wants to minimize the value of $a_1$ in the end. Find the value of $a_1$ in the end if both players play optimally.
You can refer to notes for further clarification.
|
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 10^5$) β the length of the sequence.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$) β the elements of the sequence $a$.
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 value of $a_1$ in the end if both players play optimally.
|
[
[
"5\n2\n1 2\n3\n1 1 2\n3\n1 2 3\n5\n3 1 2 2 3\n10\n10 2 5 2 7 9 2 5 10 7",
"2\n1\n2\n2\n7"
]
] |
In the first test case, initially $a = [1, 2]$. Turtle can only choose $i = 1$. Then he will set $a_1$ to $\max(a_1, a_2) = 2$ and remove $a_2$. The sequence $a$ becomes $[2]$. Then the length of the sequence becomes $1$, and the game will end. The value of $a_1$ is $2$, so you should output $2$.
In the second test case, one of the possible game processes is as follows:
* Initially $a = [1, 1, 2]$. * Turtle can choose $i = 2$. Then he will set $a_2$ to $\max(a_2, a_3) = 2$ and remove $a_3$. The sequence $a$ will become $[1, 2]$. * Piggy can choose $i = 1$. Then he will set $a_1$ to $\min(a_1, a_2) = 1$ and remove $a_2$. The sequence $a$ will become $[1]$. * The length of the sequence becomes $1$, so the game will end. The value of $a_1$ will be $1$ in the end.
In the fourth test case, one of the possible game processes is as follows:
* Initially $a = [3, 1, 2, 2, 3]$. * Turtle can choose $i = 4$. Then he will set $a_4$ to $\max(a_4, a_5) = 3$ and remove $a_5$. The sequence $a$ will become $[3, 1, 2, 3]$. * Piggy can choose $i = 3$. Then he will set $a_3$ to $\min(a_3, a_4) = 2$ and remove $a_4$. The sequence $a$ will become $[3, 1, 2]$. * Turtle can choose $i = 2$. Then he will set $a_2$ to $\max(a_2, a_3) = 2$ and remove $a_3$. The sequence $a$ will become $[3, 2]$. * Piggy can choose $i = 1$. Then he will set $a_1$ to $\min(a_1, a_2) = 2$ and remove $a_2$. The sequence $a$ will become $[2]$. * The length of the sequence becomes $1$, so the game will end. The value of $a_1$ will be $2$ in the end.
|
Title: Turtle and Piggy Are Playing a Game 2
time_limit_ms: 1000
memory_limit_mb: 256
Description: Turtle and Piggy are playing a game on a sequence. They are given a sequence $a_1, a_2, \ldots, a_n$, and Turtle goes first. Turtle and Piggy alternate in turns (so, Turtle does the first turn, Piggy does the second, Turtle does the third, etc.).
The game goes as follows:
* Let the current length of the sequence be $m$. If $m = 1$, the game ends. * If the game does not end and it's Turtle's turn, then Turtle must choose an integer $i$ such that $1 \le i \le m - 1$, set $a_i$ to $\max(a_i, a_{i + 1})$, and remove $a_{i + 1}$. * If the game does not end and it's Piggy's turn, then Piggy must choose an integer $i$ such that $1 \le i \le m - 1$, set $a_i$ to $\min(a_i, a_{i + 1})$, and remove $a_{i + 1}$.
Turtle wants to maximize the value of $a_1$ in the end, while Piggy wants to minimize the value of $a_1$ in the end. Find the value of $a_1$ in the end if both players play optimally.
You can refer to notes for further clarification.
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 10^5$) β the length of the sequence.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$) β the elements of the sequence $a$.
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 value of $a_1$ in the end if both players play optimally.
Examples:
input:
5
2
1 2
3
1 1 2
3
1 2 3
5
3 1 2 2 3
10
10 2 5 2 7 9 2 5 10 7
output:
2
1
2
2
7
Note: In the first test case, initially $a = [1, 2]$. Turtle can only choose $i = 1$. Then he will set $a_1$ to $\max(a_1, a_2) = 2$ and remove $a_2$. The sequence $a$ becomes $[2]$. Then the length of the sequence becomes $1$, and the game will end. The value of $a_1$ is $2$, so you should output $2$.
In the second test case, one of the possible game processes is as follows:
* Initially $a = [1, 1, 2]$. * Turtle can choose $i = 2$. Then he will set $a_2$ to $\max(a_2, a_3) = 2$ and remove $a_3$. The sequence $a$ will become $[1, 2]$. * Piggy can choose $i = 1$. Then he will set $a_1$ to $\min(a_1, a_2) = 1$ and remove $a_2$. The sequence $a$ will become $[1]$. * The length of the sequence becomes $1$, so the game will end. The value of $a_1$ will be $1$ in the end.
In the fourth test case, one of the possible game processes is as follows:
* Initially $a = [3, 1, 2, 2, 3]$. * Turtle can choose $i = 4$. Then he will set $a_4$ to $\max(a_4, a_5) = 3$ and remove $a_5$. The sequence $a$ will become $[3, 1, 2, 3]$. * Piggy can choose $i = 3$. Then he will set $a_3$ to $\min(a_3, a_4) = 2$ and remove $a_4$. The sequence $a$ will become $[3, 1, 2]$. * Turtle can choose $i = 2$. Then he will set $a_2$ to $\max(a_2, a_3) = 2$ and remove $a_3$. The sequence $a$ will become $[3, 2]$. * Piggy can choose $i = 1$. Then he will set $a_1$ to $\min(a_1, a_2) = 2$ and remove $a_2$. The sequence $a$ will become $[2]$. * The length of the sequence becomes $1$, so the game will end. The value of $a_1$ will be $2$ in the end.
|
1996F
|
https://codeforces.com/problemset/problem/1996/F
|
Bomb
| 1,900
|
[
"binary search",
"greedy",
"math"
] |
Div. 3
| 2,000
| 256
|
Sparkle gives you two arrays $a$ and $b$ of length $n$. Initially, your score is $0$. In one operation, you can choose an integer $i$ and add $a_i$ to your score. Then, you must set $a_i$ = $\max(0, a_i - b_i)$.
You only have time to perform $k$ operations before Sparkle sets off a nuclear bomb! What is the maximum score you can acquire after $k$ operations?
|
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 $k$ ($1 \leq n \leq 2 \cdot 10^5, 1 \leq k \leq 10^9$) β the length of the arrays and the number of operations you can perform.
The following line contains $n$ integers $a_1, a_2, ... a_n$ ($1 \leq a_i \leq 10^9$).
The following line contains $n$ integers $b_1, b_2, ... b_n$ ($1 \leq b_i \leq 10^9$).
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output an integer, the maximum score you can acquire after $k$ operations.
|
[
[
"5\n3 4\n5 6 7\n2 3 4\n5 9\n32 52 68 64 14\n18 14 53 24 8\n5 1000\n1 2 3 4 5\n5 4 3 2 1\n1 1000000\n1000000\n1\n10 6\n3 3 5 10 6 8 6 8 7 7\n6 1 7 4 1 1 8 9 3 1",
"21\n349\n27\n500000500000\n47"
]
] |
Title: Bomb
time_limit_ms: 2000
memory_limit_mb: 256
Description: Sparkle gives you two arrays $a$ and $b$ of length $n$. Initially, your score is $0$. In one operation, you can choose an integer $i$ and add $a_i$ to your score. Then, you must set $a_i$ = $\max(0, a_i - b_i)$.
You only have time to perform $k$ operations before Sparkle sets off a nuclear bomb! What is the maximum score you can acquire after $k$ operations?
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 $k$ ($1 \leq n \leq 2 \cdot 10^5, 1 \leq k \leq 10^9$) β the length of the arrays and the number of operations you can perform.
The following line contains $n$ integers $a_1, a_2, ... a_n$ ($1 \leq a_i \leq 10^9$).
The following line contains $n$ integers $b_1, b_2, ... b_n$ ($1 \leq b_i \leq 10^9$).
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output an integer, the maximum score you can acquire after $k$ operations.
Examples:
input:
5
3 4
5 6 7
2 3 4
5 9
32 52 68 64 14
18 14 53 24 8
5 1000
1 2 3 4 5
5 4 3 2 1
1 1000000
1000000
1
10 6
3 3 5 10 6 8 6 8 7 7
6 1 7 4 1 1 8 9 3 1
output:
21
349
27
500000500000
47
Note:
|
|
2025C
|
https://codeforces.com/problemset/problem/2025/C
|
New Game
| 1,300
|
[
"binary search",
"brute force",
"greedy",
"implementation",
"sortings",
"two pointers"
] |
Div. 2
| 2,000
| 512
|
There's a new game Monocarp wants to play. The game uses a deck of $n$ cards, where the $i$-th card has exactly one integer $a_i$ written on it.
At the beginning of the game, on the first turn, Monocarp can take any card from the deck. During each subsequent turn, Monocarp can take exactly one card that has either the same number as on the card taken on the previous turn or a number that is one greater than the number on the card taken on the previous turn.
In other words, if on the previous turn Monocarp took a card with the number $x$, then on the current turn he can take either a card with the number $x$ or a card with the number $x + 1$. Monocarp can take any card which meets that condition, regardless of its position in the deck.
After Monocarp takes a card on the current turn, it is removed from the deck.
According to the rules of the game, the number of distinct numbers written on the cards that Monocarp has taken must not exceed $k$.
If, after a turn, Monocarp cannot take a card without violating the described rules, the game ends.
Your task is to determine the maximum number of cards that Monocarp can take from the deck during the game, given that on the first turn he can take any card from the deck.
|
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 k \le n \le 200\,000$) β the number of cards in the deck and the maximum number of distinct numbers that can be written on the cards that Monocarp takes.
The second line contains a sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{9}$), where $a_i$ is the number written on the $i$-th card.
Additional constraint of the input: the sum of $n$ over all test cases doesn't exceed $200\,000$.
|
For each test case, print the maximum number of cards that Monocarp can take from the deck during the game, given that on the first turn he can take any card from the deck.
|
[
[
"4\n10 2\n5 2 4 3 4 3 4 5 3 2\n5 1\n10 11 10 11 10\n9 3\n4 5 4 4 6 5 4 4 6\n3 2\n1 3 1",
"6\n3\n9\n2"
]
] |
In the first example, Monocarp needs to take any of the cards with the number $3$. On the next two turns, he needs to take the two remaining cards with the number $3$. On the next three turns, he needs to take three cards with the number $4$. After that, Monocarp will not be able to take any more cards from the deck, and he will have $6$ cards.
|
Title: New Game
time_limit_ms: 2000
memory_limit_mb: 512
Description: There's a new game Monocarp wants to play. The game uses a deck of $n$ cards, where the $i$-th card has exactly one integer $a_i$ written on it.
At the beginning of the game, on the first turn, Monocarp can take any card from the deck. During each subsequent turn, Monocarp can take exactly one card that has either the same number as on the card taken on the previous turn or a number that is one greater than the number on the card taken on the previous turn.
In other words, if on the previous turn Monocarp took a card with the number $x$, then on the current turn he can take either a card with the number $x$ or a card with the number $x + 1$. Monocarp can take any card which meets that condition, regardless of its position in the deck.
After Monocarp takes a card on the current turn, it is removed from the deck.
According to the rules of the game, the number of distinct numbers written on the cards that Monocarp has taken must not exceed $k$.
If, after a turn, Monocarp cannot take a card without violating the described rules, the game ends.
Your task is to determine the maximum number of cards that Monocarp can take from the deck during the game, given that on the first turn he can take any card from the deck.
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 k \le n \le 200\,000$) β the number of cards in the deck and the maximum number of distinct numbers that can be written on the cards that Monocarp takes.
The second line contains a sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{9}$), where $a_i$ is the number written on the $i$-th card.
Additional constraint of the input: the sum of $n$ over all test cases doesn't exceed $200\,000$.
Output: For each test case, print the maximum number of cards that Monocarp can take from the deck during the game, given that on the first turn he can take any card from the deck.
Examples:
input:
4
10 2
5 2 4 3 4 3 4 5 3 2
5 1
10 11 10 11 10
9 3
4 5 4 4 6 5 4 4 6
3 2
1 3 1
output:
6
3
9
2
Note: In the first example, Monocarp needs to take any of the cards with the number $3$. On the next two turns, he needs to take the two remaining cards with the number $3$. On the next three turns, he needs to take three cards with the number $4$. After that, Monocarp will not be able to take any more cards from the deck, and he will have $6$ cards.
|
2029H
|
https://codeforces.com/problemset/problem/2029/H
|
Message Spread
| 3,500
|
[
"bitmasks",
"combinatorics",
"dp"
] |
Div. 1 + 2
| 12,000
| 1,024
|
Given is an undirected graph with $n$ vertices and $m$ edges. Each edge connects two vertices $(u, v)$ and has a probability of $\frac{p}{q}$ of appearing each day.
Initially, vertex $1$ has a message. At the end of the day, a vertex has a message if and only if itself or at least one of the vertices adjacent to it had the message the day before. Note that each day, each edge chooses its appearance independently.
Calculate the expected number of days before all the vertices have the message, modulo $998\,244\,353$.
|
The first line contains two integers $n$ and $m$ ($1\leq n\leq 21$, $n-1\leq m\leq\frac{n(n-1)}{2}$).
Then $m$ lines follow, each containing four integers $u$, $v$, $p$, and $q$ ($1\leq u\neq v\leq n$, $1\leq p<q<998\,244\,353$, $\gcd(p,q)=1$) β there is an undirected edge between $u$ and $v$, and it has a probability of appearance of $\frac{p}{q}$ each day.
It is guaranteed that there are no self-loops or multiple-edges in the graph and that the graph is connected if all of the edges appear.
Additional constraint in the input: Let $g_{i,j}$ be the probability of appearance of the edge between $i$ and $j$ ($g_{i,j}=0$ if there is no edge between $i$ and $j$). It is guaranteed that for any $S\subseteq\\{1,2,\ldots,n\\}$ ($|S|\ge 1$), $$ \prod_{i\in S}\left(\prod_{j\in\\{1,2,\ldots,n\\}\setminus S}(1-g_{i,j})\right)\not\equiv1\pmod{998\,244\,353}. $$
|
Print a single integer in the only line of the output β the expected number of days, modulo $998\,244\,353$.
Formally, let $M = 998\,244\,353$. It can be shown that the exact answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$.
|
[
[
"2 1\n1 2 1 10",
"10"
],
[
"3 3\n1 2 1 2\n1 3 1 2\n2 3 1 2",
"887328316"
],
[
"1 0",
"0"
],
[
"5 8\n1 2 1 11\n1 3 2 11\n1 4 3 11\n1 5 4 11\n2 4 5 11\n2 5 6 11\n3 4 7 11\n4 5 8 11",
"469993557"
],
[
"21 22\n1 2 3 4\n2 3 4 5\n3 4 5 6\n5 6 7 8\n6 7 8 9\n7 8 9 10\n8 9 2 3\n9 10 3 4\n10 11 4 5\n11 12 5 6\n12 13 6 7\n13 14 7 8\n14 15 8 9\n15 16 9 10\n16 17 2 3\n17 18 3 4\n18 19 4 5\n19 20 5 6\n20 21 6 7\n1 10 100 1001\n15 4 147 220\n4 11 1 998244352",
"299529765"
]
] |
In the first test, the answer is equal to the expected number of days before the only edge in the graph first appears, and that is $\frac{1}{0.1}=10$.
In the second test, the answer is equal to $\frac{20}{9}$ before it is taken modulo $998\,244\,353$.
In the third test, the only vertex already has the message, so the answer is $0$.
|
Title: Message Spread
time_limit_ms: 12000
memory_limit_mb: 1024
Description: Given is an undirected graph with $n$ vertices and $m$ edges. Each edge connects two vertices $(u, v)$ and has a probability of $\frac{p}{q}$ of appearing each day.
Initially, vertex $1$ has a message. At the end of the day, a vertex has a message if and only if itself or at least one of the vertices adjacent to it had the message the day before. Note that each day, each edge chooses its appearance independently.
Calculate the expected number of days before all the vertices have the message, modulo $998\,244\,353$.
Input: The first line contains two integers $n$ and $m$ ($1\leq n\leq 21$, $n-1\leq m\leq\frac{n(n-1)}{2}$).
Then $m$ lines follow, each containing four integers $u$, $v$, $p$, and $q$ ($1\leq u\neq v\leq n$, $1\leq p<q<998\,244\,353$, $\gcd(p,q)=1$) β there is an undirected edge between $u$ and $v$, and it has a probability of appearance of $\frac{p}{q}$ each day.
It is guaranteed that there are no self-loops or multiple-edges in the graph and that the graph is connected if all of the edges appear.
Additional constraint in the input: Let $g_{i,j}$ be the probability of appearance of the edge between $i$ and $j$ ($g_{i,j}=0$ if there is no edge between $i$ and $j$). It is guaranteed that for any $S\subseteq\\{1,2,\ldots,n\\}$ ($|S|\ge 1$), $$ \prod_{i\in S}\left(\prod_{j\in\\{1,2,\ldots,n\\}\setminus S}(1-g_{i,j})\right)\not\equiv1\pmod{998\,244\,353}. $$
Output: Print a single integer in the only line of the output β the expected number of days, modulo $998\,244\,353$.
Formally, let $M = 998\,244\,353$. It can be shown that the exact answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$.
Examples:
input:
2 1
1 2 1 10
output:
10
input:
3 3
1 2 1 2
1 3 1 2
2 3 1 2
output:
887328316
input:
1 0
output:
0
input:
5 8
1 2 1 11
1 3 2 11
1 4 3 11
1 5 4 11
2 4 5 11
2 5 6 11
3 4 7 11
4 5 8 11
output:
469993557
input:
21 22
1 2 3 4
2 3 4 5
3 4 5 6
5 6 7 8
6 7 8 9
7 8 9 10
8 9 2 3
9 10 3 4
10 11 4 5
11 12 5 6
12 13 6 7
13 14 7 8
14 15 8 9
15 16 9 10
16 17 2 3
17 18 3 4
18 19 4 5
19 20 5 6
20 21 6 7
1 10 100 1001
15 4 147 220
4 11 1 998244352
output:
299529765
Note: In the first test, the answer is equal to the expected number of days before the only edge in the graph first appears, and that is $\frac{1}{0.1}=10$.
In the second test, the answer is equal to $\frac{20}{9}$ before it is taken modulo $998\,244\,353$.
In the third test, the only vertex already has the message, so the answer is $0$.
|
1974G
|
https://codeforces.com/problemset/problem/1974/G
|
Money Buys Less Happiness Now
| 2,000
|
[
"data structures",
"greedy",
"sortings"
] |
Div. 3
| 2,000
| 256
|
You can never buy enough happiness, so here we go again! In this version, you can only buy $h_i = 1$ unit of happiness each month, but the number of months is hugely increased. We are in the realm of quantum happiness and time dilation.
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 one unit of happiness. You cannot buy more than one unit each month.
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 reachable units of happiness.
|
The first line of the input contains $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The first line of each test case contains two integers, $m$ and $x$ ($1 \le m \le 2 \cdot 10^5$, $1 \le x \le 10^3$) β the total number of months and the monthly salary.
The second line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \leq c_i \leq 10^3$) β the cost of one unit of happiness for each month.
It is guaranteed that sum of $m$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output one integer β the maximal amount of happiness Charlie can get.
|
[
[
"6\n3 3\n2 2 2\n6 5\n2 2 8 2 6 8\n6 4\n4 10 3 8 6 10\n2 1\n1 1\n4 1\n4 1 3 1\n4 2\n1 3 4 3",
"2\n4\n3\n1\n2\n1"
]
] |
Title: Money Buys Less Happiness Now
time_limit_ms: 2000
memory_limit_mb: 256
Description: You can never buy enough happiness, so here we go again! In this version, you can only buy $h_i = 1$ unit of happiness each month, but the number of months is hugely increased. We are in the realm of quantum happiness and time dilation.
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 one unit of happiness. You cannot buy more than one unit each month.
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 reachable units of happiness.
Input: The first line of the input contains $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The first line of each test case contains two integers, $m$ and $x$ ($1 \le m \le 2 \cdot 10^5$, $1 \le x \le 10^3$) β the total number of months and the monthly salary.
The second line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \leq c_i \leq 10^3$) β the cost of one unit of happiness for each month.
It is guaranteed that sum of $m$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output one integer β the maximal amount of happiness Charlie can get.
Examples:
input:
6
3 3
2 2 2
6 5
2 2 8 2 6 8
6 4
4 10 3 8 6 10
2 1
1 1
4 1
4 1 3 1
4 2
1 3 4 3
output:
2
4
3
1
2
1
Note:
|
|
2000D
|
https://codeforces.com/problemset/problem/2000/D
|
Right Left Wrong
| 1,200
|
[
"greedy",
"implementation",
"two pointers"
] |
Div. 3
| 2,000
| 256
|
Vlad found a strip of $n$ cells, numbered from left to right from $1$ to $n$. In the $i$-th cell, there is a positive integer $a_i$ and a letter $s_i$, where all $s_i$ are either 'L' or 'R'.
Vlad invites you to try to score the maximum possible points by performing any (possibly zero) number of operations.
In one operation, you can choose two indices $l$ and $r$ ($1 \le l < r \le n$) such that $s_l$ = 'L' and $s_r$ = 'R' and do the following:
* add $a_l + a_{l + 1} + \dots + a_{r - 1} + a_r$ points to the current score; * replace $s_i$ with '.' for all $l \le i \le r$, meaning you can no longer choose these indices.
For example, consider the following strip:
$3$| $5$| $1$| $4$| $3$| $2$ ---|---|---|---|---|--- L| R| L| L| L| R You can first choose $l = 1$, $r = 2$ and add $3 + 5 = 8$ to your score.
$3$| $5$| $1$| $4$| $3$| $2$ ---|---|---|---|---|--- .| .| L| L| L| R Then choose $l = 3$, $r = 6$ and add $1 + 4 + 3 + 2 = 10$ to your score.
$3$| $5$| $1$| $4$| $3$| $2$ ---|---|---|---|---|--- .| .| .| .| .| . As a result, it is impossible to perform another operation, and the final score is $18$.
What is the maximum score that can be achieved?
|
The first line contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the length of the strip.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^5$) β the numbers written on the strip.
The third line of each test case contains a string $s$ of $n$ characters 'L' and 'R'.
It is guaranteed that the sum of the values of $n$ across all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output one integer β the maximum possible number of points that can be scored.
|
[
[
"4\n6\n3 5 1 4 3 2\nLRLLLR\n2\n2 8\nLR\n2\n3 9\nRL\n5\n1 2 3 4 5\nLRLRR",
"18\n10\n0\n22"
]
] |
Title: Right Left Wrong
time_limit_ms: 2000
memory_limit_mb: 256
Description: Vlad found a strip of $n$ cells, numbered from left to right from $1$ to $n$. In the $i$-th cell, there is a positive integer $a_i$ and a letter $s_i$, where all $s_i$ are either 'L' or 'R'.
Vlad invites you to try to score the maximum possible points by performing any (possibly zero) number of operations.
In one operation, you can choose two indices $l$ and $r$ ($1 \le l < r \le n$) such that $s_l$ = 'L' and $s_r$ = 'R' and do the following:
* add $a_l + a_{l + 1} + \dots + a_{r - 1} + a_r$ points to the current score; * replace $s_i$ with '.' for all $l \le i \le r$, meaning you can no longer choose these indices.
For example, consider the following strip:
$3$| $5$| $1$| $4$| $3$| $2$ ---|---|---|---|---|--- L| R| L| L| L| R You can first choose $l = 1$, $r = 2$ and add $3 + 5 = 8$ to your score.
$3$| $5$| $1$| $4$| $3$| $2$ ---|---|---|---|---|--- .| .| L| L| L| R Then choose $l = 3$, $r = 6$ and add $1 + 4 + 3 + 2 = 10$ to your score.
$3$| $5$| $1$| $4$| $3$| $2$ ---|---|---|---|---|--- .| .| .| .| .| . As a result, it is impossible to perform another operation, and the final score is $18$.
What is the maximum score that can be achieved?
Input: The first line contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the length of the strip.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^5$) β the numbers written on the strip.
The third line of each test case contains a string $s$ of $n$ characters 'L' and 'R'.
It is guaranteed that the sum of the values of $n$ across all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output one integer β the maximum possible number of points that can be scored.
Examples:
input:
4
6
3 5 1 4 3 2
LRLLLR
2
2 8
LR
2
3 9
RL
5
1 2 3 4 5
LRLRR
output:
18
10
0
22
Note:
|
|
1982C
|
https://codeforces.com/problemset/problem/1982/C
|
Boring Day
| 1,200
|
[
"binary search",
"data structures",
"dp",
"greedy",
"two pointers"
] |
Div. 2
| 2,000
| 256
|
On another boring day, Egor got bored and decided to do something. But since he has no friends, he came up with a game to play.
Egor has a deck of $n$ cards, the $i$-th card from the top has a number $a_i$ written on it. Egor wants to play a certain number of rounds until the cards run out. In each round, he takes a non-zero number of cards from the top of the deck and finishes the round. If the sum of the numbers on the cards collected during the round is between $l$ and $r$, inclusive, the round is won; otherwise, it is lost.
Egor knows by heart the order of the cards. Help Egor determine the maximum number of rounds he can win in such a game. Note that Egor is not required to win rounds consecutively.
|
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. This is followed by a description of the test cases.
The first line of each test case contains three integers $n$, $l$, and $r$ ($1 \le n \le 10^{5}$, $1 \le l \le r \le 10^9$).
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 numbers on the cards from top to bottom.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^{5}$.
|
For each test case, output a single number β the maximum number of rounds Egor can win.
|
[
[
"8\n5 3 10\n2 1 11 3 7\n10 1 5\n17 8 12 11 7 11 21 13 10 8\n3 4 5\n3 4 2\n8 12 25\n10 7 5 13 8 9 12 7\n2 3 3\n5 2\n9 7 9\n2 10 5 1 3 7 6 2 3\n1 8 10\n9\n5 5 6\n1 4 2 6 4",
"3\n0\n1\n4\n0\n3\n1\n2"
]
] |
In the first test case, Egor can win $3$ rounds:
* In the first round, take the top $2$ cards with values $2$ and $1$ and win, as their sum is $3$. After this, the deck will look like this: $[11, 3, 7]$. * In the second round, take the top card and lose, as its value $11$ is greater than $r = 10$. After this, the deck will look like this: $[3, 7]$. * In the third round, take the top card with value $3$ and win. After this, the deck will look like this: $[7]$. * After this, in the fourth round, Egor only has to take the last card in the deck with value $7$ and win again.
In the second test case, Egor cannot win any rounds, no matter how hard he tries.
In the third test case, you can take one card in each round, then the first and third rounds will be losing, and the second round will be winning.
In the fourth test case, you can take two cards in each round and always win.
|
Title: Boring Day
time_limit_ms: 2000
memory_limit_mb: 256
Description: On another boring day, Egor got bored and decided to do something. But since he has no friends, he came up with a game to play.
Egor has a deck of $n$ cards, the $i$-th card from the top has a number $a_i$ written on it. Egor wants to play a certain number of rounds until the cards run out. In each round, he takes a non-zero number of cards from the top of the deck and finishes the round. If the sum of the numbers on the cards collected during the round is between $l$ and $r$, inclusive, the round is won; otherwise, it is lost.
Egor knows by heart the order of the cards. Help Egor determine the maximum number of rounds he can win in such a game. Note that Egor is not required to win rounds consecutively.
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. This is followed by a description of the test cases.
The first line of each test case contains three integers $n$, $l$, and $r$ ($1 \le n \le 10^{5}$, $1 \le l \le r \le 10^9$).
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 numbers on the cards from top to bottom.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^{5}$.
Output: For each test case, output a single number β the maximum number of rounds Egor can win.
Examples:
input:
8
5 3 10
2 1 11 3 7
10 1 5
17 8 12 11 7 11 21 13 10 8
3 4 5
3 4 2
8 12 25
10 7 5 13 8 9 12 7
2 3 3
5 2
9 7 9
2 10 5 1 3 7 6 2 3
1 8 10
9
5 5 6
1 4 2 6 4
output:
3
0
1
4
0
3
1
2
Note: In the first test case, Egor can win $3$ rounds:
* In the first round, take the top $2$ cards with values $2$ and $1$ and win, as their sum is $3$. After this, the deck will look like this: $[11, 3, 7]$. * In the second round, take the top card and lose, as its value $11$ is greater than $r = 10$. After this, the deck will look like this: $[3, 7]$. * In the third round, take the top card with value $3$ and win. After this, the deck will look like this: $[7]$. * After this, in the fourth round, Egor only has to take the last card in the deck with value $7$ and win again.
In the second test case, Egor cannot win any rounds, no matter how hard he tries.
In the third test case, you can take one card in each round, then the first and third rounds will be losing, and the second round will be winning.
In the fourth test case, you can take two cards in each round and always win.
|
1995B1
|
https://codeforces.com/problemset/problem/1995/B1
|
Bouquet (Easy Version)
| 1,100
|
[
"binary search",
"brute force",
"greedy",
"sortings",
"two pointers"
] |
Div. 2
| 1,500
| 512
|
This is the easy version of the problem. The only difference is that in this version, the flowers are specified by enumeration.
A girl is preparing for her birthday and wants to buy the most beautiful bouquet. There are a total of $n$ flowers in the store, each of which is characterized by the number of petals, and a flower with $k$ petals costs $k$ coins. The girl has decided that the difference in the number of petals between any two flowers she will use in her bouquet should not exceed one. At the same time, the girl wants to assemble a bouquet with the maximum possible number of petals. Unfortunately, she only has $m$ coins, and she cannot spend more. What is the maximum total number of petals she can assemble in the bouquet?
|
Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 10\,000$) β the number of test cases. This is followed by descriptions of the test cases.
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^{18}$) β the number of flowers in the store and the number of coins the girl possesses, respectively. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the number of petals of the $i$-th flower in the store.
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 number of petals in the bouquet that the girl can assemble while meeting all the conditions listed above.
|
[
[
"5\n5 10\n1 1 2 2 3\n8 20\n4 2 7 5 6 1 1 1\n8 100000\n239 30 610 122 24 40 8 2\n11 13\n2 4 11 1 1 2 3 5 4 3 2\n8 1033\n206 206 206 207 207 207 207 1000",
"7\n13\n610\n13\n1033"
]
] |
In the first test case, you can assemble a bouquet with $(1, 1, 2, 2), (2, 2, 3), (1, 1), (2, 2)$. The maximum over all valid bouquets not greater than $10$ is $7$ for $(2, 2, 3)$. In the third test case, you can assemble a bouquet with only one flower of any type, so the answer is $610$. In the fourth test case, you can assemble a bouquet with $(4, 4, 5)$, which gives you $13$ petals, and it is the maximum amount of petals that the girl can buy.
|
Title: Bouquet (Easy Version)
time_limit_ms: 1500
memory_limit_mb: 512
Description: This is the easy version of the problem. The only difference is that in this version, the flowers are specified by enumeration.
A girl is preparing for her birthday and wants to buy the most beautiful bouquet. There are a total of $n$ flowers in the store, each of which is characterized by the number of petals, and a flower with $k$ petals costs $k$ coins. The girl has decided that the difference in the number of petals between any two flowers she will use in her bouquet should not exceed one. At the same time, the girl wants to assemble a bouquet with the maximum possible number of petals. Unfortunately, she only has $m$ coins, and she cannot spend more. What is the maximum total number of petals she can assemble in the bouquet?
Input: Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 10\,000$) β the number of test cases. This is followed by descriptions of the test cases.
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^{18}$) β the number of flowers in the store and the number of coins the girl possesses, respectively. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the number of petals of the $i$-th flower in the store.
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 number of petals in the bouquet that the girl can assemble while meeting all the conditions listed above.
Examples:
input:
5
5 10
1 1 2 2 3
8 20
4 2 7 5 6 1 1 1
8 100000
239 30 610 122 24 40 8 2
11 13
2 4 11 1 1 2 3 5 4 3 2
8 1033
206 206 206 207 207 207 207 1000
output:
7
13
610
13
1033
Note: In the first test case, you can assemble a bouquet with $(1, 1, 2, 2), (2, 2, 3), (1, 1), (2, 2)$. The maximum over all valid bouquets not greater than $10$ is $7$ for $(2, 2, 3)$. In the third test case, you can assemble a bouquet with only one flower of any type, so the answer is $610$. In the fourth test case, you can assemble a bouquet with $(4, 4, 5)$, which gives you $13$ petals, and it is the maximum amount of petals that the girl can buy.
|
2030C
|
https://codeforces.com/problemset/problem/2030/C
|
A TRUE Battle
| 1,100
|
[
"brute force",
"games",
"greedy"
] |
Div. 2
| 2,000
| 256
|
Alice and Bob are playing a game. There is a list of $n$ booleans, each of which is either true or false, given as a binary string $^{\text{β}}$ of length $n$ (where $\texttt{1}$ represents true, and $\texttt{0}$ represents false). Initially, there are no operators between the booleans.
Alice and Bob will take alternate turns placing and or or between the booleans, with Alice going first. Thus, the game will consist of $n-1$ turns since there are $n$ booleans. Alice aims for the final statement to evaluate to true, while Bob aims for it to evaluate to false. Given the list of boolean values, determine whether Alice will win if both players play optimally.
To evaluate the final expression, repeatedly perform the following steps until the statement consists of a single true or false:
* If the statement contains an and operator, choose any one and replace the subexpression surrounding it with its evaluation. * Otherwise, the statement contains an or operator. Choose any one and replace the subexpression surrounding the or with its evaluation.
For example, the expression true or false and false is evaluated as true or (false and false) $=$ true or false $=$ true. It can be shown that the result of any compound statement is unique.
$^{\text{β}}$A binary string is a string that only consists of characters $\texttt{0}$ and $\texttt{1}$
|
The first line contains $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The first line of each test case contains an integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β the length of the string.
The second line contains a binary string of length $n$, consisting of characters $\texttt{0}$ and $\texttt{1}$ β the list of boolean values.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each testcase, output "YES" (without quotes) if Alice wins, and "NO" (without quotes) otherwise.
You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response).
|
[
[
"5\n2\n11\n3\n010\n12\n101111111100\n10\n0111111011\n8\n01000010",
"YES\nNO\nYES\nYES\nNO"
]
] |
In the first testcase, Alice can place and between the two booleans. The game ends as there are no other places to place operators, and Alice wins because true and true is true.
In the second testcase, Alice can place or between the middle true and the left false. Bob can place and between the middle true and the right false. The statement false or true and false is false.
Note that these examples may not be the best strategies for either Alice or Bob.
|
Title: A TRUE Battle
time_limit_ms: 2000
memory_limit_mb: 256
Description: Alice and Bob are playing a game. There is a list of $n$ booleans, each of which is either true or false, given as a binary string $^{\text{β}}$ of length $n$ (where $\texttt{1}$ represents true, and $\texttt{0}$ represents false). Initially, there are no operators between the booleans.
Alice and Bob will take alternate turns placing and or or between the booleans, with Alice going first. Thus, the game will consist of $n-1$ turns since there are $n$ booleans. Alice aims for the final statement to evaluate to true, while Bob aims for it to evaluate to false. Given the list of boolean values, determine whether Alice will win if both players play optimally.
To evaluate the final expression, repeatedly perform the following steps until the statement consists of a single true or false:
* If the statement contains an and operator, choose any one and replace the subexpression surrounding it with its evaluation. * Otherwise, the statement contains an or operator. Choose any one and replace the subexpression surrounding the or with its evaluation.
For example, the expression true or false and false is evaluated as true or (false and false) $=$ true or false $=$ true. It can be shown that the result of any compound statement is unique.
$^{\text{β}}$A binary string is a string that only consists of characters $\texttt{0}$ and $\texttt{1}$
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 an integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β the length of the string.
The second line contains a binary string of length $n$, consisting of characters $\texttt{0}$ and $\texttt{1}$ β the list of boolean values.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each testcase, output "YES" (without quotes) if Alice wins, and "NO" (without quotes) otherwise.
You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response).
Examples:
input:
5
2
11
3
010
12
101111111100
10
0111111011
8
01000010
output:
YES
NO
YES
YES
NO
Note: In the first testcase, Alice can place and between the two booleans. The game ends as there are no other places to place operators, and Alice wins because true and true is true.
In the second testcase, Alice can place or between the middle true and the left false. Bob can place and between the middle true and the right false. The statement false or true and false is false.
Note that these examples may not be the best strategies for either Alice or Bob.
|
1978E
|
https://codeforces.com/problemset/problem/1978/E
|
Computing Machine
| 2,000
|
[
"brute force",
"data structures",
"dp",
"greedy",
"implementation"
] |
Div. 2
| 2,000
| 256
|
Sasha has two binary strings $s$ and $t$ of the same length $n$, consisting of the characters 0 and 1.
There is also a computing machine that can perform two types of operations on binary strings $a$ and $b$ of the same length $k$:
1. If $a_{i} = a_{i + 2} =$ 0, then you can assign $b_{i + 1} :=$ 1 ($1 \le i \le k - 2$). 2. If $b_{i} = b_{i + 2} =$ 1, then you can assign $a_{i + 1} :=$ 1 ($1 \le i \le k - 2$).
Sasha became interested in the following: if we consider the string $a=s_ls_{l+1}\ldots s_r$ and the string $b=t_lt_{l+1}\ldots t_r$, what is the maximum number of 1 characters in the string $a$ that can be obtained using the computing machine. Since Sasha is very curious but lazy, it is up to you to answer this question for several pairs $(l_i, r_i)$ that interest him.
|
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$ ($1 \le n \le 2 \cdot 10^5$) β the length of the strings $s$ and $t$.
The second line of each test case contains a binary string $s$ of length $n$, consisting of the characters 0 and 1.
The third line of each test case contains a binary string $t$ of length $n$, consisting of the characters 0 and 1.
The fourth line of each test case contains a single integer $q$ ($1 \le q \le 2 \cdot 10^5$) β the number of queries.
The $i$-th of the following lines contains two integers $l_{i}$ and $r_{i}$ ($1 \le l_{i} \le r_{i} \le n$) β the boundaries of the $i$-th pair of substrings that interest Sasha.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ and the sum of $q$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output $q$ integers β the answers to all queries.
|
[
[
"3\n4\n1111\n0000\n2\n1 2\n2 4\n4\n1010\n1101\n2\n1 3\n1 4\n6\n010101\n011010\n5\n2 3\n1 6\n2 5\n4 4\n3 6",
"2\n3\n2\n3\n1\n4\n3\n1\n2"
]
] |
In the first test case:
* In the first query, $a =$ 11, so the maximum number of 1 characters is $2$. * In the second query, $a =$ 111, so the maximum number of 1 characters is $3$.
In the second test case:
* In the first query, $a =$ 101 and $b =$ 110. No operations can be performed, so the maximum number of 1 characters is $2$. * In the second query, $a =$ 1010 and $b =$ 1101. Since $a_2 = a_4 =$ 0, we can assign $b_3 :=$ 1. Now $b_1 = b_3 =$ 1, so we can assign $a_2 :=$ 1. The string $a$ becomes 1110, so the maximum number of 1 characters is $3$.
|
Title: Computing Machine
time_limit_ms: 2000
memory_limit_mb: 256
Description: Sasha has two binary strings $s$ and $t$ of the same length $n$, consisting of the characters 0 and 1.
There is also a computing machine that can perform two types of operations on binary strings $a$ and $b$ of the same length $k$:
1. If $a_{i} = a_{i + 2} =$ 0, then you can assign $b_{i + 1} :=$ 1 ($1 \le i \le k - 2$). 2. If $b_{i} = b_{i + 2} =$ 1, then you can assign $a_{i + 1} :=$ 1 ($1 \le i \le k - 2$).
Sasha became interested in the following: if we consider the string $a=s_ls_{l+1}\ldots s_r$ and the string $b=t_lt_{l+1}\ldots t_r$, what is the maximum number of 1 characters in the string $a$ that can be obtained using the computing machine. Since Sasha is very curious but lazy, it is up to you to answer this question for several pairs $(l_i, r_i)$ that interest him.
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$ ($1 \le n \le 2 \cdot 10^5$) β the length of the strings $s$ and $t$.
The second line of each test case contains a binary string $s$ of length $n$, consisting of the characters 0 and 1.
The third line of each test case contains a binary string $t$ of length $n$, consisting of the characters 0 and 1.
The fourth line of each test case contains a single integer $q$ ($1 \le q \le 2 \cdot 10^5$) β the number of queries.
The $i$-th of the following lines contains two integers $l_{i}$ and $r_{i}$ ($1 \le l_{i} \le r_{i} \le n$) β the boundaries of the $i$-th pair of substrings that interest Sasha.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ and the sum of $q$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output $q$ integers β the answers to all queries.
Examples:
input:
3
4
1111
0000
2
1 2
2 4
4
1010
1101
2
1 3
1 4
6
010101
011010
5
2 3
1 6
2 5
4 4
3 6
output:
2
3
2
3
1
4
3
1
2
Note: In the first test case:
* In the first query, $a =$ 11, so the maximum number of 1 characters is $2$. * In the second query, $a =$ 111, so the maximum number of 1 characters is $3$.
In the second test case:
* In the first query, $a =$ 101 and $b =$ 110. No operations can be performed, so the maximum number of 1 characters is $2$. * In the second query, $a =$ 1010 and $b =$ 1101. Since $a_2 = a_4 =$ 0, we can assign $b_3 :=$ 1. Now $b_1 = b_3 =$ 1, so we can assign $a_2 :=$ 1. The string $a$ becomes 1110, so the maximum number of 1 characters is $3$.
|
1971F
|
https://codeforces.com/problemset/problem/1971/F
|
Circle Perimeter
| 1,600
|
[
"binary search",
"brute force",
"dfs and similar",
"geometry",
"implementation",
"math"
] |
Div. 4
| 1,000
| 256
|
Given an integer $r$, find the number of lattice points that have a Euclidean distance from $(0, 0)$ greater than or equal to $r$ but strictly less than $r+1$.
A lattice point is a point with integer coordinates. The Euclidean distance from $(0, 0)$ to the point $(x,y)$ is $\sqrt{x^2 + y^2}$.
|
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The only line of each test case contains a single integer $r$ ($1 \leq r \leq 10^5$).
The sum of $r$ over all test cases does not exceed $10^5$.
|
For each test case, output a single integer β the number of lattice points that have an Euclidean distance $d$ from $(0, 0)$ such that $r \leq d < r+1$.
|
[
[
"6\n1\n2\n3\n4\n5\n1984",
"8\n16\n20\n24\n40\n12504"
]
] |
The points for the first three test cases are shown below.

|
Title: Circle Perimeter
time_limit_ms: 1000
memory_limit_mb: 256
Description: Given an integer $r$, find the number of lattice points that have a Euclidean distance from $(0, 0)$ greater than or equal to $r$ but strictly less than $r+1$.
A lattice point is a point with integer coordinates. The Euclidean distance from $(0, 0)$ to the point $(x,y)$ is $\sqrt{x^2 + y^2}$.
Input: The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The only line of each test case contains a single integer $r$ ($1 \leq r \leq 10^5$).
The sum of $r$ over all test cases does not exceed $10^5$.
Output: For each test case, output a single integer β the number of lattice points that have an Euclidean distance $d$ from $(0, 0)$ such that $r \leq d < r+1$.
Examples:
input:
6
1
2
3
4
5
1984
output:
8
16
20
24
40
12504
Note: The points for the first three test cases are shown below.

|
2028A
|
https://codeforces.com/problemset/problem/2028/A
|
Alice's Adventures in ''Chess''
| 800
|
[
"brute force",
"implementation",
"math",
"strings"
] |
Div. 2
| 1,000
| 256
|
Alice is trying to meet up with the Red Queen in the countryside! Right now, Alice is at position $(0, 0)$, and the Red Queen is at position $(a, b)$. Alice can only move in the four cardinal directions (north, east, south, west).
More formally, if Alice is at the point $(x, y)$, she will do one of the following:
* go north (represented by N), moving to $(x, y+1)$; * go east (represented by E), moving to $(x+1, y)$; * go south (represented by S), moving to $(x, y-1)$; or * go west (represented by W), moving to $(x-1, y)$.
Alice's movements are predetermined. She has a string $s$ representing a sequence of moves that she performs from left to right. Once she reaches the end of the sequence, she repeats the same pattern of moves forever.
Can you help Alice figure out if she will ever meet the Red Queen?
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $a$, $b$ ($1 \le n$, $a$, $b \le 10$) β the length of the string and the initial coordinates of the Red Queen.
The second line contains a string $s$ of length $n$ consisting only of the characters N, E, S, or W.
|
For each test case, output a single string "YES" or "NO" (without the quotes) denoting whether Alice will eventually meet the Red Queen.
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.
|
[
[
"6\n2 2 2\nNE\n3 2 2\nNNE\n6 2 1\nNNEESW\n6 10 10\nNNEESW\n3 4 2\nNEE\n4 5 5\nNEWS",
"YES\nNO\nYES\nYES\nYES\nNO"
]
] |
In the first test case, Alice follows the path $(0,0) \xrightarrow[\texttt{N}]{} (0,1) \xrightarrow[\texttt{E}]{} (1,1) \xrightarrow[\texttt{N}]{} (1,2) \xrightarrow[\texttt{E}]{} (2,2)$.
In the second test case, Alice can never reach the Red Queen.
|
Title: Alice's Adventures in ''Chess''
time_limit_ms: 1000
memory_limit_mb: 256
Description: Alice is trying to meet up with the Red Queen in the countryside! Right now, Alice is at position $(0, 0)$, and the Red Queen is at position $(a, b)$. Alice can only move in the four cardinal directions (north, east, south, west).
More formally, if Alice is at the point $(x, y)$, she will do one of the following:
* go north (represented by N), moving to $(x, y+1)$; * go east (represented by E), moving to $(x+1, y)$; * go south (represented by S), moving to $(x, y-1)$; or * go west (represented by W), moving to $(x-1, y)$.
Alice's movements are predetermined. She has a string $s$ representing a sequence of moves that she performs from left to right. Once she reaches the end of the sequence, she repeats the same pattern of moves forever.
Can you help Alice figure out if she will ever meet the Red Queen?
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $a$, $b$ ($1 \le n$, $a$, $b \le 10$) β the length of the string and the initial coordinates of the Red Queen.
The second line contains a string $s$ of length $n$ consisting only of the characters N, E, S, or W.
Output: For each test case, output a single string "YES" or "NO" (without the quotes) denoting whether Alice will eventually meet the Red Queen.
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:
6
2 2 2
NE
3 2 2
NNE
6 2 1
NNEESW
6 10 10
NNEESW
3 4 2
NEE
4 5 5
NEWS
output:
YES
NO
YES
YES
YES
NO
Note: In the first test case, Alice follows the path $(0,0) \xrightarrow[\texttt{N}]{} (0,1) \xrightarrow[\texttt{E}]{} (1,1) \xrightarrow[\texttt{N}]{} (1,2) \xrightarrow[\texttt{E}]{} (2,2)$.
In the second test case, Alice can never reach the Red Queen.
|
2026B
|
https://codeforces.com/problemset/problem/2026/B
|
Black Cells
| 1,300
|
[
"binary search",
"brute force",
"constructive algorithms",
"greedy"
] |
Div. 2
| 2,000
| 256
|
You are given a strip divided into cells, numbered from left to right from $0$ to $10^{18}$. Initially, all cells are white.
You can perform the following operation: choose two white cells $i$ and $j$, such that $i \ne j$ and $|i - j| \le k$, and paint them black.
A list $a$ is given. All cells from this list must be painted black. Additionally, at most one cell that is not in this list can also be painted black. Your task is to determine the minimum value of $k$ for which this is possible.
|
The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2000$).
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 < a_i < 10^{18}$; $a_i < a_{i + 1}$).
Additional constraint on the input: the sum of $n$ across all test cases does not exceed $2000$.
|
For each test case, print a single integer β the minimum value of $k$ for which it is possible to paint all the given cells black.
|
[
[
"4\n2\n1 2\n1\n7\n3\n2 4 9\n5\n1 5 8 10 13",
"1\n1\n2\n3"
]
] |
In the first example, with $k=1$, it is possible to paint the cells $(1, 2)$.
In the second example, with $k=1$, it is possible to paint the cells $(7, 8)$.
In the third example, with $k=2$, it is possible to paint the cells $(2, 4)$ and $(8, 9)$.
In the fourth example, with $k=3$, it is possible to paint the cells $(0, 1)$, $(5, 8)$ and $(10, 13)$.
|
Title: Black Cells
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given a strip divided into cells, numbered from left to right from $0$ to $10^{18}$. Initially, all cells are white.
You can perform the following operation: choose two white cells $i$ and $j$, such that $i \ne j$ and $|i - j| \le k$, and paint them black.
A list $a$ is given. All cells from this list must be painted black. Additionally, at most one cell that is not in this list can also be painted black. Your task is to determine the minimum value of $k$ for which this is possible.
Input: The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2000$).
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 < a_i < 10^{18}$; $a_i < a_{i + 1}$).
Additional constraint on the input: the sum of $n$ across all test cases does not exceed $2000$.
Output: For each test case, print a single integer β the minimum value of $k$ for which it is possible to paint all the given cells black.
Examples:
input:
4
2
1 2
1
7
3
2 4 9
5
1 5 8 10 13
output:
1
1
2
3
Note: In the first example, with $k=1$, it is possible to paint the cells $(1, 2)$.
In the second example, with $k=1$, it is possible to paint the cells $(7, 8)$.
In the third example, with $k=2$, it is possible to paint the cells $(2, 4)$ and $(8, 9)$.
In the fourth example, with $k=3$, it is possible to paint the cells $(0, 1)$, $(5, 8)$ and $(10, 13)$.
|
2029B
|
https://codeforces.com/problemset/problem/2029/B
|
Replacement
| 1,100
|
[
"games",
"greedy",
"strings"
] |
Div. 1 + 2
| 1,000
| 256
|
You have a binary string$^{\text{β}}$ $s$ of length $n$, and Iris gives you another binary string $r$ of length $n-1$.
Iris is going to play a game with you. During the game, you will perform $n-1$ operations on $s$. In the $i$-th operation ($1 \le i \le n-1$):
* First, you choose an index $k$ such that $1\le k\le |s| - 1$ and $s_{k} \neq s_{k+1}$. If it is impossible to choose such an index, you lose; * Then, you replace $s_ks_{k+1}$ with $r_i$. Note that this decreases the length of $s$ by $1$.
If all the $n-1$ operations are performed successfully, you win.
Determine whether it is possible for you to win this game.
$^{\text{β}}$A binary string is a string where each character is either $\mathtt{0}$ or $\mathtt{1}$.
|
Each test contains multiple test cases. The first line of the input contains a single integer $t$ ($1\le t\le 10^4$) β the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 10^5$) β the length of $s$.
The second line contains the binary string $s$ of length $n$ ($s_i=\mathtt{0}$ or $\mathtt{1}$).
The third line contains the binary string $r$ of length $n-1$ ($r_i=\mathtt{0}$ or $\mathtt{1}$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
|
For each test case, print "YES" (without quotes) if you can win the game, and "NO" (without quotes) otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
|
[
[
"6\n2\n11\n0\n2\n01\n1\n4\n1101\n001\n6\n111110\n10000\n6\n010010\n11010\n8\n10010010\n0010010",
"NO\nYES\nYES\nNO\nYES\nNO"
]
] |
In the first test case, you cannot perform the first operation. Thus, you lose the game.
In the second test case, you can choose $k=1$ in the only operation, and after that, $s$ becomes equal to $\mathtt{1}$. Thus, you win the game.
In the third test case, you can perform the following operations: $\mathtt{1}\underline{\mathtt{10}}\mathtt{1}\xrightarrow{r_1=\mathtt{0}} \mathtt{1}\underline{\mathtt{01}} \xrightarrow{r_2=\mathtt{0}} \underline{\mathtt{10}} \xrightarrow{r_3=\mathtt{1}} \mathtt{1}$.
|
Title: Replacement
time_limit_ms: 1000
memory_limit_mb: 256
Description: You have a binary string$^{\text{β}}$ $s$ of length $n$, and Iris gives you another binary string $r$ of length $n-1$.
Iris is going to play a game with you. During the game, you will perform $n-1$ operations on $s$. In the $i$-th operation ($1 \le i \le n-1$):
* First, you choose an index $k$ such that $1\le k\le |s| - 1$ and $s_{k} \neq s_{k+1}$. If it is impossible to choose such an index, you lose; * Then, you replace $s_ks_{k+1}$ with $r_i$. Note that this decreases the length of $s$ by $1$.
If all the $n-1$ operations are performed successfully, you win.
Determine whether it is possible for you to win this game.
$^{\text{β}}$A binary string is a string where each character is either $\mathtt{0}$ or $\mathtt{1}$.
Input: Each test contains multiple test cases. The first line of the input contains a single integer $t$ ($1\le t\le 10^4$) β the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 10^5$) β the length of $s$.
The second line contains the binary string $s$ of length $n$ ($s_i=\mathtt{0}$ or $\mathtt{1}$).
The third line contains the binary string $r$ of length $n-1$ ($r_i=\mathtt{0}$ or $\mathtt{1}$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
Output: For each test case, print "YES" (without quotes) if you can win the game, and "NO" (without quotes) otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
Examples:
input:
6
2
11
0
2
01
1
4
1101
001
6
111110
10000
6
010010
11010
8
10010010
0010010
output:
NO
YES
YES
NO
YES
NO
Note: In the first test case, you cannot perform the first operation. Thus, you lose the game.
In the second test case, you can choose $k=1$ in the only operation, and after that, $s$ becomes equal to $\mathtt{1}$. Thus, you win the game.
In the third test case, you can perform the following operations: $\mathtt{1}\underline{\mathtt{10}}\mathtt{1}\xrightarrow{r_1=\mathtt{0}} \mathtt{1}\underline{\mathtt{01}} \xrightarrow{r_2=\mathtt{0}} \underline{\mathtt{10}} \xrightarrow{r_3=\mathtt{1}} \mathtt{1}$.
|
2003D1
|
https://codeforces.com/problemset/problem/2003/D1
|
Turtle and a MEX Problem (Easy Version)
| 1,500
|
[
"greedy",
"math"
] |
Div. 2
| 2,000
| 256
|
The two versions are different problems. In this version of the problem, you can choose the same integer twice or more. You can make hacks only if both versions are solved.
One day, Turtle was playing with $n$ sequences. Let the length of the $i$-th sequence be $l_i$. Then the $i$-th sequence was $a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i}$.
Piggy gave Turtle a problem to solve when Turtle was playing. The statement of the problem was:
* There was a non-negative integer $x$ at first. Turtle would perform an arbitrary number (possibly zero) of operations on the integer. * In each operation, Turtle could choose an integer $i$ such that $1 \le i \le n$, and set $x$ to $\text{mex}^{\dagger}(x, a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i})$. * Turtle was asked to find the answer, which was the maximum value of $x$ after performing an arbitrary number of operations.
Turtle solved the above problem without difficulty. He defined $f(k)$ as the answer to the above problem when the initial value of $x$ was $k$.
Then Piggy gave Turtle a non-negative integer $m$ and asked Turtle to find the value of $\sum\limits_{i = 0}^m f(i)$ (i.e., the value of $f(0) + f(1) + \ldots + f(m)$). Unfortunately, he couldn't solve this problem. Please help him!
$^{\dagger}\text{mex}(c_1, c_2, \ldots, c_k)$ is defined as the smallest non-negative integer $x$ which does not occur in the sequence $c$. For example, $\text{mex}(2, 2, 0, 3)$ is $1$, $\text{mex}(1, 2)$ is $0$.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains two integers $n, m$ ($1 \le n \le 2 \cdot 10^5, 0 \le m \le 10^9$).
Each of the following $n$ lines contains several integers. The first integer $l_i$ ($1 \le l_i \le 2 \cdot 10^5$) represents the length of the $i$-th sequence, and the following $l_i$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i}$ ($0 \le a_{i, j} \le 10^9$) represent the elements of the $i$-th sequence.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$, and the sum of $\sum l_i$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output a single integer β the value of $\sum\limits_{i = 0}^m f(i)$.
|
[
[
"6\n3 4\n2 0 2\n3 2 3 3\n4 7 0 1 5\n3 4\n5 0 2 0 4 11\n1 1\n5 1 3 0 3 3\n2 50\n2 1 2\n2 1 2\n1 1\n7 1 2 4 1 4 9 5\n4 114514\n2 2 2\n5 7 3 6 0 3\n3 0 1 1\n5 0 9 2 1 5\n5 1919810\n1 2\n2 324003 0\n3 1416324 2 1460728\n4 1312631 2 0 1415195\n5 1223554 192248 2 1492515 725556",
"16\n20\n1281\n6\n6556785365\n1842836177961"
]
] |
In the first test case, when $x$ is initially $2$, Turtle can choose $i = 3$ and set $x$ to $\text{mex}(x, a_{3, 1}, a_{3, 2}, a_{3, 3}, a_{3, 4}) = \text{mex}(2, 7, 0, 1, 5) = 3$. It can be proved that Turtle can't make the value of $x$ greater than $3$, so $f(2) = 3$.
It can be seen that $f(0) = 3$, $f(1) = 3$, $f(2) = 3$, $f(3) = 3$, and $f(4) = 4$. So $f(0) + f(1) + f(2) + f(3) + f(4) = 3 + 3 + 3 + 3 + 4 = 16$.
In the second test case, when $x$ is initially $1$, Turtle can choose $i = 3$ and set $x$ to $\text{mex}(x, a_{3, 1}, a_{3, 2}, a_{3, 3}, a_{3, 4}, a_{3, 5}) = \text{mex}(1, 1, 3, 0, 3, 3) = 2$, and choose $i = 3$ and set $x$ to $\text{mex}(x, a_{3, 1}, a_{3, 2}, a_{3, 3}, a_{3, 4}, a_{3, 5}) = \text{mex}(2, 1, 3, 0, 3, 3) = 4$. It can be proved that Turtle can't make the value of $x$ greater than $4$, so $f(1) = 4$.
It can be seen that $f(0) = 4$, $f(1) = 4$, $f(2) = 4$, $f(3) = 4$, and $f(4) = 4$. So $f(0) + f(1) + f(2) + f(3) + f(4) = 4 + 4 + 4 + 4 + 4 = 20$.
In the fourth test case, it can be seen that $f(0) = 3$ and $f(1) = 3$. So $f(0) + f(1) = 3 + 3 = 6$.
|
Title: Turtle and a MEX Problem (Easy Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: The two versions are different problems. In this version of the problem, you can choose the same integer twice or more. You can make hacks only if both versions are solved.
One day, Turtle was playing with $n$ sequences. Let the length of the $i$-th sequence be $l_i$. Then the $i$-th sequence was $a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i}$.
Piggy gave Turtle a problem to solve when Turtle was playing. The statement of the problem was:
* There was a non-negative integer $x$ at first. Turtle would perform an arbitrary number (possibly zero) of operations on the integer. * In each operation, Turtle could choose an integer $i$ such that $1 \le i \le n$, and set $x$ to $\text{mex}^{\dagger}(x, a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i})$. * Turtle was asked to find the answer, which was the maximum value of $x$ after performing an arbitrary number of operations.
Turtle solved the above problem without difficulty. He defined $f(k)$ as the answer to the above problem when the initial value of $x$ was $k$.
Then Piggy gave Turtle a non-negative integer $m$ and asked Turtle to find the value of $\sum\limits_{i = 0}^m f(i)$ (i.e., the value of $f(0) + f(1) + \ldots + f(m)$). Unfortunately, he couldn't solve this problem. Please help him!
$^{\dagger}\text{mex}(c_1, c_2, \ldots, c_k)$ is defined as the smallest non-negative integer $x$ which does not occur in the sequence $c$. For example, $\text{mex}(2, 2, 0, 3)$ is $1$, $\text{mex}(1, 2)$ is $0$.
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$ ($1 \le n \le 2 \cdot 10^5, 0 \le m \le 10^9$).
Each of the following $n$ lines contains several integers. The first integer $l_i$ ($1 \le l_i \le 2 \cdot 10^5$) represents the length of the $i$-th sequence, and the following $l_i$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i}$ ($0 \le a_{i, j} \le 10^9$) represent the elements of the $i$-th sequence.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$, and the sum of $\sum l_i$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output a single integer β the value of $\sum\limits_{i = 0}^m f(i)$.
Examples:
input:
6
3 4
2 0 2
3 2 3 3
4 7 0 1 5
3 4
5 0 2 0 4 11
1 1
5 1 3 0 3 3
2 50
2 1 2
2 1 2
1 1
7 1 2 4 1 4 9 5
4 114514
2 2 2
5 7 3 6 0 3
3 0 1 1
5 0 9 2 1 5
5 1919810
1 2
2 324003 0
3 1416324 2 1460728
4 1312631 2 0 1415195
5 1223554 192248 2 1492515 725556
output:
16
20
1281
6
6556785365
1842836177961
Note: In the first test case, when $x$ is initially $2$, Turtle can choose $i = 3$ and set $x$ to $\text{mex}(x, a_{3, 1}, a_{3, 2}, a_{3, 3}, a_{3, 4}) = \text{mex}(2, 7, 0, 1, 5) = 3$. It can be proved that Turtle can't make the value of $x$ greater than $3$, so $f(2) = 3$.
It can be seen that $f(0) = 3$, $f(1) = 3$, $f(2) = 3$, $f(3) = 3$, and $f(4) = 4$. So $f(0) + f(1) + f(2) + f(3) + f(4) = 3 + 3 + 3 + 3 + 4 = 16$.
In the second test case, when $x$ is initially $1$, Turtle can choose $i = 3$ and set $x$ to $\text{mex}(x, a_{3, 1}, a_{3, 2}, a_{3, 3}, a_{3, 4}, a_{3, 5}) = \text{mex}(1, 1, 3, 0, 3, 3) = 2$, and choose $i = 3$ and set $x$ to $\text{mex}(x, a_{3, 1}, a_{3, 2}, a_{3, 3}, a_{3, 4}, a_{3, 5}) = \text{mex}(2, 1, 3, 0, 3, 3) = 4$. It can be proved that Turtle can't make the value of $x$ greater than $4$, so $f(1) = 4$.
It can be seen that $f(0) = 4$, $f(1) = 4$, $f(2) = 4$, $f(3) = 4$, and $f(4) = 4$. So $f(0) + f(1) + f(2) + f(3) + f(4) = 4 + 4 + 4 + 4 + 4 = 20$.
In the fourth test case, it can be seen that $f(0) = 3$ and $f(1) = 3$. So $f(0) + f(1) = 3 + 3 = 6$.
|
1987B
|
https://codeforces.com/problemset/problem/1987/B
|
K-Sort
| 1,000
|
[
"greedy"
] |
Div. 1 + 2
| 1,000
| 256
|
You are given an array of integers $a$ of length $n$.
You can apply the following operation any number of times (maybe, zero):
* First, choose an integer $k$ such that $1 \le k \le n$ and pay $k + 1$ coins. * Then, choose exactly $k$ indices such that $1 \le i_1 < i_2 < \ldots < i_k \le n$. * Then, for each $x$ from $1$ to $k$, increase $a_{i_x}$ by $1$.
Find the minimum number of coins needed to make $a$ non-decreasing. That is, $a_1 \le a_2 \le \ldots \le a_n$.
|
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) β the length of 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 $10^5$.
|
For each test case, output a single integer β the minimum number of coins needed to make $a$ non-decreasing.
|
[
[
"5\n3\n1 7 9\n5\n2 1 4 7 6\n4\n1 3 2 4\n1\n179\n9\n344 12 37 60 311 613 365 328 675",
"0\n3\n2\n0\n1821"
]
] |
In the first test case, $a$ is already sorted, so you don't have to spend any coins.
In the second test case, the optimal sequence of operations is:
* Choose $k = 2$ and the indices $2$ and $5$: $[ 2, \color{red}{1}, 4, 7, \color{red}{6} ] \rightarrow [2, 2, 4, 7, 7]$. This costs $3$ coins.
It can be proven that it is not possible to make $a$ non-decreasing by spending less than $3$ coins.
|
Title: K-Sort
time_limit_ms: 1000
memory_limit_mb: 256
Description: You are given an array of integers $a$ of length $n$.
You can apply the following operation any number of times (maybe, zero):
* First, choose an integer $k$ such that $1 \le k \le n$ and pay $k + 1$ coins. * Then, choose exactly $k$ indices such that $1 \le i_1 < i_2 < \ldots < i_k \le n$. * Then, for each $x$ from $1$ to $k$, increase $a_{i_x}$ by $1$.
Find the minimum number of coins needed to make $a$ non-decreasing. That is, $a_1 \le a_2 \le \ldots \le a_n$.
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 a single integer $n$ ($1 \le n \le 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 \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 $10^5$.
Output: For each test case, output a single integer β the minimum number of coins needed to make $a$ non-decreasing.
Examples:
input:
5
3
1 7 9
5
2 1 4 7 6
4
1 3 2 4
1
179
9
344 12 37 60 311 613 365 328 675
output:
0
3
2
0
1821
Note: In the first test case, $a$ is already sorted, so you don't have to spend any coins.
In the second test case, the optimal sequence of operations is:
* Choose $k = 2$ and the indices $2$ and $5$: $[ 2, \color{red}{1}, 4, 7, \color{red}{6} ] \rightarrow [2, 2, 4, 7, 7]$. This costs $3$ coins.
It can be proven that it is not possible to make $a$ non-decreasing by spending less than $3$ coins.
|
2035G1
|
https://codeforces.com/problemset/problem/2035/G1
|
Go Learn! (Easy Version)
| 3,300
|
[
"dp",
"trees"
] |
Div. 1 + 2
| 5,000
| 256
|
The differences between the easy and hard versions are the constraints on $n$ and the sum of $n$. In this version, $n \leq 3000$ and the sum of $n$ does not exceed $10^4$. You can only make hacks if both versions are solved.
Well, well, well, let's see how Bessie is managing her finances. She seems to be in the trenches! Fortunately, she is applying for a job at Moogle to resolve this issue. Moogle interviews require intensive knowledge of obscure algorithms and complex data structures, but Bessie received a tip-off from an LGM on exactly what she has to go learn.
Bessie wrote the following code to binary search for a certain element $k$ in a possibly unsorted array $[a_1, a_2,\ldots,a_n]$ with $n$ elements.
let l = 1 let h = n while l < h: let m = floor((l + h) / 2) if a[m] < k: l = m + 1 else: h = m return l
Bessie submitted her code to Farmer John's problem with $m$ ($1 \leq m \leq n$) tests. The $i$-th test is of the form $(x_i, k_i)$ ($1 \leq x, k \leq n$). It is guaranteed all the $x_i$ are distinct and all the $k_i$ are distinct.
Test $i$ is correct if the following hold:
1. The $x_i$-th element in the array is $k_i$. 2. If Bessie calls the binary search as shown in the above code for $k_i$, it will return $x_i$.
It might not be possible for all $m$ tests to be correct on the same array, so Farmer John will remove some of them so Bessie can AC. Let $r$ be the minimum of tests removed so that there exists an array $[a_1, a_2,\ldots,a_n]$ with $1 \leq a_i \leq n$ so that all remaining tests are correct.
In addition to finding $r$, Farmer John wants you to count the number of arrays $[a_1, a_2,\ldots,a_n]$ with $1 \leq a_i \leq n$ such that there exists a way to remove exactly $r$ tests so that all the remaining tests are correct. Since this number may be very large, please find it modulo $998\,244\,353$.
|
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 $m$ ($1 \leq m \leq n \leq 3000$) denoting the number of the array and the number of tests.
The following $m$ lines each contain two integers, describing the tests. The $i$-th line contains two integers $x_i$ and $k_i$ ($1 \leq x_i, k_i \leq n$) denoting the index and value of the test. It is guaranteed all $x_i$ are distinct and all $k_i$ are distinct.
It is guaranteed the sum of $n$ across all test cases does not exceed $10^4$.
|
For each test case, output two integers, $r$ β the minimum of tests removed so that there exists an array so that all remaining tests are correct, and the number of arrays such that it is possible to remove $r$ tests to make all remaining tests correct modulo $998\,244\,353$.
|
[
[
"2\n5 4\n1 1\n2 2\n4 3\n5 4\n5 4\n5 4\n2 5\n1 2\n3 3",
"0 1\n1 3"
],
[
"2\n6 6\n1 3\n2 5\n3 1\n4 2\n5 4\n6 6\n30 8\n19 22\n6 12\n12 1\n28 27\n3 4\n14 25\n29 14\n11 15",
"3 78\n3 839271911"
]
] |
Consider the first example.
In the first test case, the array $[1,2,2,3,4]$ satisfies all $m$ tests, so the minimum number of tests Bessie has to remove is $0$. Note that this is also the only array that satisfies all $m$ tests.
In the second test case, the minimum number of tests Bessie has to remove is $1$. The only test Bessie can remove is $(2,5)$. If Bessie removes test $(2,5)$, then the arrays satisfying the remaining $m-1$ tests are $[2,2,3,1,4]$, $[2,2,3,2,4]$, $[2,2,3,3,4]$.
|
Title: Go Learn! (Easy Version)
time_limit_ms: 5000
memory_limit_mb: 256
Description: The differences between the easy and hard versions are the constraints on $n$ and the sum of $n$. In this version, $n \leq 3000$ and the sum of $n$ does not exceed $10^4$. You can only make hacks if both versions are solved.
Well, well, well, let's see how Bessie is managing her finances. She seems to be in the trenches! Fortunately, she is applying for a job at Moogle to resolve this issue. Moogle interviews require intensive knowledge of obscure algorithms and complex data structures, but Bessie received a tip-off from an LGM on exactly what she has to go learn.
Bessie wrote the following code to binary search for a certain element $k$ in a possibly unsorted array $[a_1, a_2,\ldots,a_n]$ with $n$ elements.
let l = 1 let h = n while l < h: let m = floor((l + h) / 2) if a[m] < k: l = m + 1 else: h = m return l
Bessie submitted her code to Farmer John's problem with $m$ ($1 \leq m \leq n$) tests. The $i$-th test is of the form $(x_i, k_i)$ ($1 \leq x, k \leq n$). It is guaranteed all the $x_i$ are distinct and all the $k_i$ are distinct.
Test $i$ is correct if the following hold:
1. The $x_i$-th element in the array is $k_i$. 2. If Bessie calls the binary search as shown in the above code for $k_i$, it will return $x_i$.
It might not be possible for all $m$ tests to be correct on the same array, so Farmer John will remove some of them so Bessie can AC. Let $r$ be the minimum of tests removed so that there exists an array $[a_1, a_2,\ldots,a_n]$ with $1 \leq a_i \leq n$ so that all remaining tests are correct.
In addition to finding $r$, Farmer John wants you to count the number of arrays $[a_1, a_2,\ldots,a_n]$ with $1 \leq a_i \leq n$ such that there exists a way to remove exactly $r$ tests so that all the remaining tests are correct. Since this number may be very large, please find it modulo $998\,244\,353$.
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 $m$ ($1 \leq m \leq n \leq 3000$) denoting the number of the array and the number of tests.
The following $m$ lines each contain two integers, describing the tests. The $i$-th line contains two integers $x_i$ and $k_i$ ($1 \leq x_i, k_i \leq n$) denoting the index and value of the test. It is guaranteed all $x_i$ are distinct and all $k_i$ are distinct.
It is guaranteed the sum of $n$ across all test cases does not exceed $10^4$.
Output: For each test case, output two integers, $r$ β the minimum of tests removed so that there exists an array so that all remaining tests are correct, and the number of arrays such that it is possible to remove $r$ tests to make all remaining tests correct modulo $998\,244\,353$.
Examples:
input:
2
5 4
1 1
2 2
4 3
5 4
5 4
5 4
2 5
1 2
3 3
output:
0 1
1 3
input:
2
6 6
1 3
2 5
3 1
4 2
5 4
6 6
30 8
19 22
6 12
12 1
28 27
3 4
14 25
29 14
11 15
output:
3 78
3 839271911
Note: Consider the first example.
In the first test case, the array $[1,2,2,3,4]$ satisfies all $m$ tests, so the minimum number of tests Bessie has to remove is $0$. Note that this is also the only array that satisfies all $m$ tests.
In the second test case, the minimum number of tests Bessie has to remove is $1$. The only test Bessie can remove is $(2,5)$. If Bessie removes test $(2,5)$, then the arrays satisfying the remaining $m-1$ tests are $[2,2,3,1,4]$, $[2,2,3,2,4]$, $[2,2,3,3,4]$.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.