版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
title: "题解 - Codeforces Round #829 (Div. 2)" date: 2022-10-24 01:11:30 categories:
You work in the quality control department of technical support for a large company. Your job is to make sure all client issues have been resolved.
Today you need to check a copy of a dialog between a client and a technical support manager. According to the rules of work, each message of the client must be followed by one or several messages, which are the answer of a support manager. However, sometimes clients ask questions so quickly that some of the manager's answers to old questions appear after the client has asked some new questions.
Due to the privacy policy, the full text of messages is not available to you, only the order of messages is visible, as well as the type of each message: a customer question or a response from the technical support manager. It is guaranteed that the dialog begins with the question of the client.
You have to determine, if this dialog may correspond to the rules of work described above, or the rules are certainly breached.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). Description of the test cases follows.
The first line of each test case contains one integer $n$ ($1 \le n \le 100$) — the total number of messages in the dialog.
The second line of each test case consists of $n$ characters "Q
" and "A
", describing types of messages in the dialog in chronological order. Character "Q
" denotes the message with client question, and character "A
" — the message with technical support manager answer. It is guaranteed that the first character in the line equals to "Q
".
For each test case print "Yes
" (without quotes) if dialog may correspond to the rules of work, or "No
" (without quotes) otherwise.
5
4
QQAA
4
QQAQ
3
QAA
1
Q
14
QAQQAQAAQQQAAA
Yes
No
Yes
No
Yes
In the first test case the two questions from the client are followed with two specialist's answers. So this dialog may correspond to the rules of work.
In the second test case one of the first two questions was not answered.
In the third test case the technical support manager sent two messaged as the answer to the only message of the client.
没啥好说的, 扫一遍就行
For his birthday, Kevin received the set of pairwise distinct numbers $1, 2, 3, \ldots, n$ as a gift.
He is going to arrange these numbers in a way such that the minimum absolute difference between two consecutive numbers be maximum possible. More formally, if he arranges numbers in order $p_1, p_2, \ldots, p_n$, he wants to maximize the value
$$ \min \limits{i=1}^{n - 1} \lvert p{i + 1} - p_i \rvert, $$
where $|x|$ denotes the absolute value of $x$.
Help Kevin to do that.
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows.
The only line of each test case contains an integer $n$ ($2 \le n \leq 1\,000$) — the size of the set.
For each test case print a single line containing $n$ distinct integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$) describing the arrangement that maximizes the minimum absolute difference of consecutive elements.
Formally, you have to print a permutation $p$ which maximizes the value $\min \limits{i=1}^{n - 1} \lvert p{i + 1} - p_i \rvert$.
If there are multiple optimal solutions, print any of them.
2
4
3
2 4 1 3
1 2 3
In the first test case the minimum absolute difference of consecutive elements equals $\min {\lvert 4 - 2 \rvert, \lvert 1 - 4 \rvert, \lvert 3 - 1 \rvert } = \min {2, 3, 2} = 2$. It's easy to prove that this answer is optimal.
In the second test case each permutation of numbers $1, 2, 3$ is an optimal answer. The minimum absolute difference of consecutive elements equals to $1$.
显然差值最大为 $\lfloor\frac{p}{2}\rfloor$, 构造方式也不难想
This is the easy version of the problem. The difference is that in this version the array can not contain zeros. You can make hacks only if both versions of the problem are solved.
You are given an array $[a_1, a_2, \ldots a_n]$ consisting of integers $-1$ and $1$. You have to build a partition of this array into the set of segments $[l_1, r_1], [l_2, r_2], \ldots, [l_k, r_k]$ with the following property:
Note that each $s_i$ does not have to be equal to zero, this property is about sum of $s_i$ over all segments of partition.
The set of segments $[l_1, r_1], [l_2, r_2], \ldots, [l_k, r_k]$ is called a partition of the array $a$ of length $n$ if $1 = l_1 \le r_1, l_2 \le r_2, \ldots, l_k \le r_k = n$ and $ri + 1 = l{i+1}$ for all $i = 1, 2, \ldots k-1$. In other words, each element of the array must belong to exactly one segment.
You have to build a partition of the given array with properties described above or determine that such partition does not exist.
Note that it is not required to minimize the number of segments in the partition.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10\,000$). Description of the test cases follows.
The first line of each test case contains an integer $n$ ($1 \le n \le 200\,000$) — the length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i$ is $-1$ or $1$) — the elements of the given array.
It's guaranteed that the sum of $n$ over all test cases does not exceed $200\,000$.
For each test case, if required partition does not exist, print $-1$. Otherwise, print an integer $k$ — the number of segments in the partition.
Then in the $i$-th of the following $k$ lines print two integers $l_i$ and $r_i$ — description of the $i$-th segment. The following conditions should be satisfied:
If there are multiple correct partitions of the array, print any of them.
4
4
1 1 1 1
6
-1 1 1 1 1 1
3
1 -1 1
1
1
1
1 4
2
1 3
4 6
-1
-1
In the first test case we can build a partition of one segment of length $4$. The sum of this segment will be equal to $1 - 1 + 1 - 1 = 0$.
In the second test case we can build a partition of two segments of length $3$. The sum of the first segment will be equal to $-1 -1 + 1 = -1$, and the sum of the second segment: $1 - 1 + 1 = 1$. So, the total sum will be equal to $-1 + 1 = 0$.
In the third and in the fourth test cases it can be proved that there are no required partition.
直接看 C2 吧, C1 和 C2 的代码完全一致
This is the hard version of the problem. The difference is that in this version the array contains zeros. You can make hacks only if both versions of the problem are solved.
You are given an array $[a_1, a_2, \ldots a_n]$ consisting of integers $-1$, $0$ and $1$. You have to build a partition of this array into the set of segments $[l_1, r_1], [l_2, r_2], \ldots, [l_k, r_k]$ with the following property:
Note that each $s_i$ does not have to be equal to zero, this property is about sum of $s_i$ over all segments of partition.
The set of segments $[l_1, r_1], [l_2, r_2], \ldots, [l_k, r_k]$ is called a partition of the array $a$ of length $n$ if $1 = l_1 \le r_1, l_2 \le r_2, \ldots, l_k \le r_k = n$ and $ri + 1 = l{i+1}$ for all $i = 1, 2, \ldots k-1$. In other words, each element of the array must belong to exactly one segment.
You have to build a partition of the given array with properties described above or determine that such partition does not exist.
Note that it is not required to minimize the number of segments in the partition.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10\,000$). Description of the test cases follows.
The first line of each test case contains an integer $n$ ($1 \le n \le 200\,000$) — the length of array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i$ is $-1$, $0$, or $1$) — the elements of the given array.
It's guaranteed that the sum of $n$ over all test cases does not exceed $200\,000$.
For each test case print an integer $k$ — the number of segments in the partition. If required partition does not exist, print $-1$.
If partition exists, in the $i$-th of the following $k$ lines print two integers $l_i$ and $r_i$ — description of the $i$-th segment. The following conditions should be satisfied:
If there are multiple correct partitions of the array, print any of them.
5
4
0 0 0 0
7
-1 1 0 1 0 1 0
5
0 -1 1 0 1
3
1 0 1
1
1
4
1 1
2 2
3 3
4 4
4
1 1
2 2
3 5
6 7
-1
2
1 1
2 3
-1
In the first test case we can build a partition of $4$ segments — each of them will contain only one element of the array equals to $0$. So the sum will be equal to $0 + 0 + 0 + 0 = 0$.
In the second test case we can build a partition of $4$ segments. The alternating sum of the first segment will be equal to $-1$, the alternating sum of the second segment will be equal to $1$, of the third segment — $0 - 1 + 0 = -1$, of the fourth segment — $1 - 0 = 1$. The sum will be equal to $-1 + 1 -1 + 1 = 0$.
In the third test case it can be proved that the required partition does not exist.
题目相当于将 $a_i$ 中某些项取相反数使得总和为 $0$, 且不能修改连续的数
首先若 $\sum a_i$ 为奇数则显然必定无解
若 $\sum a_i<0$, 我们不妨先全局取反, 不难发现这样不会影响结果
接下来我们从 $a_2$ 开始找 $1$ 然后取反, 直到 $\sum a_i=0$
正确性显然
$O(n)$
You are given an integer $x$ and an array of integers $a_1, a_2, \ldots, a_n$. You have to determine if the number $a_1! + a_2! + \ldots + a_n!$ is divisible by $x!$.
Here $k!$ is a factorial of $k$ — the product of all positive integers less than or equal to $k$. For example, $3! = 1 \cdot 2 \cdot 3 = 6$, and $5! = 1 \cdot 2 \cdot 3 \cdot 4 \cdot 5 = 120$.
The first line contains two integers $n$ and $x$ ($1 \le n \le 500\,000$, $1 \le x \le 500\,000$).
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le x$) — elements of given array.
In the only line print "Yes
" (without quotes) if $a_1! + a_2! + \ldots + a_n!$ is divisible by $x!$, and "No
" (without quotes) otherwise.
6 4
3 2 2 2 3 3
Yes
8 3
3 2 2 2 2 2 1 1
Yes
7 8
7 7 7 7 7 7 7
No
10 5
4 3 2 1 4 3 2 4 3 4
No
2 500000
499999 499999
No
In the first example $3! + 2! + 2! + 2! + 3! + 3! = 6 + 2 + 2 + 2 + 6 + 6 = 24$. Number $24$ is divisible by $4! = 24$.
In the second example $3! + 2! + 2! + 2! + 2! + 2! + 1! + 1! = 18$, is divisible by $3! = 6$.
In the third example $7! + 7! + 7! + 7! + 7! + 7! + 7! = 7 \cdot 7!$. It is easy to prove that this number is not divisible by $8!$.
注意到
$$ \sum_{i=0}^{k-1}a_i i!<k!,~a_i\leq i $$
所以 $a_i$ 将视作 "阶乘进制数", 然后按 $(i+1)*i!=(i+1)!$ 进位, 最后看 $\bmod k!$ 后有无余项即可
$O(k)$
You are given a binary array $a$ (all elements of the array are $0$ or $1$) of length $n$. You wish to sort this array, but unfortunately, your algorithms teacher forgot to teach you sorting algorithms. You perform the following operations until $a$ is sorted:
What is the expected number of such operations you will perform before the array becomes sorted?
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{998\,244\,353}$. ### Output the integer equal to $p \cdot q^{-1} \bmod 998\,244\,353$. In other words, output such an integer $x$ that $0 \le x < 998\,244\,353$ and $x \cdot q \equiv p \pmod{998\,244\,353}$.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows.
The first line of each test case contains an integer $n$ ($1 \le n \le 200\,000$) — the number of elements in the binary array.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i \in {0, 1}$) — elements of the array.
It's guaranteed that sum of $n$ over all test cases does not exceed $200\,000$.
For each test case print one integer — the value $p \cdot q^{-1} \bmod 998\,244\,353$.
3
3
0 1 0
5
0 0 1 1 1
6
1 1 1 0 0 1
3
0
249561107
Consider the first test case. If the pair of indices $(2, 3)$ will be chosen, these elements will be swapped and array will become sorted. Otherwise, if one of pairs $(1, 2)$ or $(1, 3)$ will be selected, nothing will happen. So, the probability that the array will become sorted after one operation is $\frac{1}{3}$, the probability that the array will become sorted after two operations is $\frac{2}{3} \cdot \frac{1}{3}$, the probability that the array will become sorted after three operations is $\frac{2}{3} \cdot \frac{2}{3} \cdot \frac{1}{3}$ and so on. The expected number of operations is $\sum \limits_{i=1}^{\infty} \left(\frac{2}{3} \right)^{i - 1} \cdot \frac{1}{3} \cdot i = 3$.
In the second test case the array is already sorted so the expected number of operations is zero.
In the third test case the expected number of operations equals to $\frac{75}{4}$ so the answer is $75 \cdot 4^{-1} \equiv 249\,561\,107 \pmod {998\,244\,353}$.
设
则
答案即为 $f(t)$
$O(n)$
Andrew loves the sea. That's why, at the height of the summer season, he decided to go to the beach, taking a sunbed with him to sunbathe.
The beach is a rectangular field with $n$ rows and $m$ columns. Some cells of the beach are free, some have roads, stones, shops and other non-movable objects. Some of two adjacent along the side cells can have sunbeds located either horizontally or vertically.
Andrew hopes to put his sunbed somewhere, but that's a bad luck, there may no longer be free places for him! That's why Andrew asked you to help him to find a free place for his sunbed. Andrew's sunbed also should be places on two adjacent cells.
If there are no two adjacent free cells, then in order to free some place for a sunbed, you will have to disturb other tourists. You can do the following actions:
Come to some sunbed and, after causing $p$ units of discomfort to its owner, lift the sunbed by one of its sides and rotate it by $90$ degrees. One half of the sunbed must remain in the same cell and another half of the sunbed must move to the free cell. At the same time, anything could be on the way of a sunbed during the rotation.
| | | | | :------------: | :---------------: | :------------: |
<center>Rotation of the sunbed by 90 degrees around cell (1, 2).</center>Come to some sunbed and, after causing $q$ units of discomfort to its owner, shift the sunbed along its long side by one cell. One half of the sunbed must move to the place of another, and another — to the free cell.
| | | | | :------------: | :---------------: | :------------: |
<center>Shift of the sunbed by one cell to the right.</center>In any moment each sunbed occupies two adjacent free cells. You cannot move more than one sunbed at a time.
Help Andrew to free a space for his sunbed, causing the minimum possible number of units of discomfort to other tourists, or detect that it is impossible.
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 300\,000$, $1 \le n \cdot m \le 300\,000$) — the number of rows and columns in rectangle.
The second line contains two integers $p$ and $q$ ($1 \le p, q \le 10^9$) — the number of units of discomfort caused by rotation and shift of a sunbed, respectively.
Each of the following $n$ lines contains $m$ characters, describing cells of the rectangle. Each lines consists of characters "L
", "R
", "D
", "U
", ".
" and "#
", denoting the type of the cell. Characters "L
", "R
", "D
" and "U
" denote a half of a sunbed placed in the cell — left, right, bottom and top half, respectively. Character ".
" denotes a free cell and character "#
" — a cell, occupied by some non-movable object.
Print one integer — the minimum possible number of units of discomfort, caused to other tourists, to free a space for a sunbed. If it is impossible to free a space for a sunbed, print $-1$.
2 5
5 2
.LR##
##LR.
4
2 3
4 5
LR.
#.#
-1
4 3
10 10
.LR
###
UU#
DD.
-1
3 6
10 7
.U##.#
#DLR##
.##LR.
24
In the first example we can shift upper sunbed to the left and lower sunbed — to the right. Andrew will be able to put his sunbed vertically in the middle of the beach. We well cause $2 + 2 = 4$ units of discomfort. It is easy to prove that it is an optimal answer.
In the second example it is impossible to free a space for Andrew's sunbed. All possible states of the beach after any rotates and shifts are illustrated in the problem statement.
显然若有解, 每个沙滩椅最多只需移动一次
为方便理解, 将沙滩按国际象棋棋盘的方式染色. 我们考虑将移动沙滩椅转为移动空位, 显然每个空位都只能移动到同色且 Manhattan 距离为 $2$ 的位置上
那我们可以将每个位置视作点, 空位的移动方式视作边跑一下最短路即可
$O(nm\log (nm))$