Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
長さ 2N で、0 と 1 をそれぞれ N 個含む文字列をよい文字列と呼びます。
よい文字列 S, T に対して、S の隣り合う 2 文字を入れ替える操作を 0 回以上行って T に一致させるために必要な操作回数の最小値を \operatorname{dist}(S, T) とします。
よい文字列 A, B, C が与えられます。すべてのよい文字列 X のうち、
\[ \operatorname{dist}(A, X)+\operatorname{dist}(B, X)+\operatorname{dist}(C, X) \]
の値を最小にするものを 1 つ求め、その最小値とともに出力してください。
制約
- 1 \le N \le 2 \times 10^5
- A, B, C はそれぞれ長さ 2N のよい文字列
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N A B C
出力
求める最小値を K とし、それを達成するよい文字列を X とする。 以下の形式で K と X を出力せよ。
K X
条件を満たす出力が複数存在する場合、どれを出力してもよい。
入力例 1
2 1100 1010 0011
出力例 1
4 1010
よい文字列 X として 1010 を選ぶと、\operatorname{dist}(A, X)、
\operatorname{dist}(B, X)、\operatorname{dist}(C, X) はそれぞれ 1, 0, 3 となり、その和は 4 です。
距離の和を 4 より小さくすることはできません。
入力例 2
3 101010 101010 101010
出力例 2
0 101010
入力された 3 つの文字列がすべて等しいため、X に同じ文字列を選ぶと距離の和は 0 です。
Score : 400 points
Problem Statement
A string of length 2N that contains N occurrences of each of 0 and 1 is called a good string.
For good strings S and T, let \operatorname{dist}(S, T) denote the minimum number of swaps required to make S equal to T by swapping two adjacent characters of S zero or more times.
You are given good strings A, B, and C. Among all good strings X, find one that minimizes the value of
\[ \operatorname{dist}(A, X)+\operatorname{dist}(B, X)+\operatorname{dist}(C, X), \]
and output it together with the minimum value.
Constraints
- 1 \le N \le 2 \times 10^5
- Each of A, B, and C is a good string of length 2N.
- N is an integer.
Input
The input is given from Standard Input in the following format:
N A B C
Output
Let K be the minimum value to be found, and let X be a good string that achieves it. Output K and X in the following format:
K X
If multiple outputs satisfy the conditions, you may output any of them.
Sample Input 1
2 1100 1010 0011
Sample Output 1
4 1010
If we choose 1010 as the good string X, then \operatorname{dist}(A, X), \operatorname{dist}(B, X), and \operatorname{dist}(C, X) are 1, 0, 3, respectively, and their sum is 4. The sum of the distances cannot be made smaller than 4.
Sample Input 2
3 101010 101010 101010
Sample Output 2
0 101010
Since the three input strings are all equal, choosing the same string as X makes the sum of the distances 0.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
A の要素を並べ替えて得られる数列 B=(B_1,B_2,\ldots,B_N) であって、すべての i=1,2,\ldots,N について次の条件を満たすものが存在するか判定し、存在する場合は そのような数列を 1 つ構成してください。
- B_i は、1\le j\lt i かつ B_j\lt B_i を満たす整数 j の個数に等しい。
制約
- 1\le N\le 5\times 10^5
- 0\le A_i\lt N
- 入力される数値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
条件を満たす数列 B が存在しない場合、No と出力せよ。
存在する場合、以下の形式で Yes と数列 B を出力せよ。
Yes B_1 B_2 \ldots B_N
条件を満たす数列が複数存在する場合は、どれを出力してもよい。
入力例 1
4 3 0 1 0
出力例 1
Yes 0 1 0 3
出力された数列は A を並べ替えたものです。 i=1,2,3,4 に対し、B_i の左側にある B_i より小さい要素の個数は、それぞれ 0,1,0,3 個で、B_i と等しいです。
入力例 2
3 0 2 2
出力例 2
No
値 2 より小さい要素は 1 個しかないため、どのように並べ替えても条件を満たせません。
入力例 3
7 4 1 6 0 4 3 1
出力例 3
Yes 0 1 1 3 4 4 6
出力された数列は A を並べ替えたものです。 i=1,2,\ldots,7 に対し、B_i の左側にある B_i より小さい要素の個数は、それぞれ 0,1,1,3,4,4,6 個で、B_i と等しいです。
Score : 500 points
Problem Statement
You are given a length-N sequence of non-negative integers A=(A_1,A_2,\ldots,A_N).
Determine whether there exists a sequence B=(B_1,B_2,\ldots,B_N) that is a permutation of A that satisfies the following condition for every i=1,2,\ldots,N, and construct one such sequence if it exists.
- B_i is equal to the number of integers j satisfying 1 \le j < i and B_j < B_i.
Constraints
- 1 \le N \le 5 \times 10^5
- 0 \le A_i < N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
If there is no sequence B satisfying the condition, output No.
If one exists, output Yes and a sequence B in the following format:
Yes B_1 B_2 \ldots B_N
If multiple sequences satisfy the condition, you may output any of them.
Sample Input 1
4 3 0 1 0
Sample Output 1
Yes 0 1 0 3
The output sequence is a permutation of A. For i=1,2,3,4, the number of elements to the left of B_i that are smaller than B_i is 0,1,0,3, respectively, which equals B_i.
Sample Input 2
3 0 2 2
Sample Output 2
No
Since there is only one element smaller than the value 2, no permutation satisfies the condition.
Sample Input 3
7 4 1 6 0 4 3 1
Sample Output 3
Yes 0 1 1 3 4 4 6
The output sequence is a permutation of A. For i=1,2,\ldots,7, the number of elements to the left of B_i that are smaller than B_i is 0,1,1,3,4,4,6, respectively, which equals B_i.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
英小文字からなる長さ N の文字列 S が与えられます。S の i 文字目を S_i とします。
N 個の島が円環状に並んでおり、時計回りに島 1、島 2、\ldots、島 N の順で番号が付けられています。特に、島 N の時計回りの隣の島は島 1 です。島 i には文字 S_i が書かれています。はじめ、それぞれの島には人が 1 人ずついます。
あなたは次の操作を 0 回以上行うことができます。
- S に含まれる文字 c を 1 つ選ぶ。すべての人はそれぞれ現在いる島を出発し、時計回りに島を 1 個ずつ移動する。出発後に文字 c が書かれた島に初めて到着した時点で止まる。現在いる島に文字 c が書かれている場合でも出発する。
操作をすべて終えた後に人が 1 人以上いる島の個数としてあり得る最小値を K とします。
K と、操作後に人が 1 人以上いる島の個数を K にする操作列を求めてください。
なお、この問題の制約の下で、操作後に人が 1 人以上いる島の個数を K にする長さ 10^6 以下の操作列が必ず存在することが証明できます。
制約
- 1 \le N \le 1000
- S は英小文字からなる長さ N の文字列
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
操作回数を表す整数を L、操作列を表す文字列を X として、以下の形式で出力せよ。
K L X
L=0 の場合、3 行目は空行として出力せよ。
L は 0 \le L \le 10^6 を満たさなければならない。
X は S に含まれる文字からなる長さ L の文字列でなければならず、X の i 文字目は i 回目の操作で選ぶ文字を表す。
操作列 X に従って操作した後、人が 1 人以上いる島の個数は K でなければならない。
条件を満たす出力が複数存在する場合、どれを出力してもよい。
入力例 1
4 abca
出力例 1
1 1 b
文字 b を選ぶ操作を行うと、すべての人が文字 b が書かれた島に集まります。人がいる島は 1 個になり、これが最小です。
入力例 2
4 aabb
出力例 2
1 2 ab
はじめに a を、次に b を選ぶことで、すべての人が同じ島に集まります。人がいる島は 1 個になり、これが最小です。
入力例 3
4 aaaa
出力例 3
4 0
どの操作を行っても、それぞれの人は時計回りに次の島へ移動します。したがって、人がいる島の個数は常に 4 であり、K=4 です。この出力例では操作を 1 回も行いません。
Score : 700 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters. Let S_i denote the i-th character of S.
There are N islands arranged in a circle, numbered island 1, island 2, \ldots, island N in clockwise order. In particular, the island clockwise-adjacent to island N is island 1. The character S_i is written on island i. Initially, there is one person on each island.
You can perform the following operation zero or more times.
- Choose one character c that appears in S. Every person departs from the island they are currently on and moves one island at a time in the clockwise direction. Each person stops as soon as they first arrive, after departing, at an island on which the character c is written. Even if the island a person is currently on has the character c written on it, that person still departs.
Let K be the minimum possible number of islands that have one or more people on them after all operations are finished.
Find K, and a sequence of operations that makes the number of islands with one or more people equal to K after the operations.
Under the constraints of this problem, it can be proved that there always exists a sequence of operations of length at most 10^6 that makes the number of islands with one or more people equal to K.
Constraints
- 1 \le N \le 1000
- S is a string of length N consisting of lowercase English letters.
- N is an integer.
Input
The input is given from Standard Input in the following format:
N S
Output
Let L be an integer representing the number of operations, and let X be a string representing the sequence of operations. Output in the following format:
K L X
If L=0, output the third line as an empty line.
L must satisfy 0 \le L \le 10^6.
X must be a string of length L consisting of characters that appear in S, and the i-th character of X represents the character chosen in the i-th operation.
After performing the operations according to the sequence X, the number of islands with one or more people must be K.
If multiple outputs satisfy the conditions, you may output any of them.
Sample Input 1
4 abca
Sample Output 1
1 1 b
If we perform the operation choosing the character b, everyone gathers on the island on which the character b is written. The number of islands with people becomes 1, and this is the minimum.
Sample Input 2
4 aabb
Sample Output 2
1 2 ab
By choosing a first and then b, everyone gathers on the same island. The number of islands with people becomes 1, and this is the minimum.
Sample Input 3
4 aaaa
Sample Output 3
4 0
No matter which operation is performed, each person moves to the next island in the clockwise direction. Therefore, the number of islands with people is always 4, so K=4. In this sample output, no operation is performed.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
0 と 1 からなる長さ M の文字列 S_1,S_2,\ldots,S_N が与えられます。
はじめ、黒板には S_1,S_2,\ldots,S_N が書かれています。
あなたは次の操作を 0 回以上行うことができます。
- 黒板に書かれている文字列から、同じ文字列を複数回選んでもよいものとして 3 つ選び、それぞれ A,B,C とする。次の条件を満たす長さ M の文字列 D を新たに黒板に書く。
- i=1,2,\ldots,M のそれぞれについて、A,B,C,D の i 文字目をそれぞれ A_i,B_i,C_i,D_i としたとき、D_i は A_i,B_i,C_i のうち少なくとも 2 つと等しい。
Q 個の文字列 T_1,T_2,\ldots,T_Q が与えられます。i=1,2,\ldots,Q のそれぞれについて、はじめの状態から操作を行うことで、T_i が黒板に書かれている状態にできるか判定してください。
制約
- 1 \leq N \leq 500
- 1 \leq M \leq 500
- 1 \leq Q \leq 500
- S_i は
0と1からなる長さ M の文字列 (1 \leq i \leq N) - T_i は
0と1からなる長さ M の文字列 (1 \leq i \leq Q) - 入力される数値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M Q S_1 S_2 \vdots S_N T_1 T_2 \vdots T_Q
出力
Q 行出力せよ。i 行目には、T_i が黒板に書かれている状態にできるなら Yes、できないなら No と出力せよ。
入力例 1
3 3 2 000 011 101 000 001
出力例 1
Yes Yes
T_1 ははじめから黒板に書かれています。
T_2 は 001 です。000、011、101 の 3 つを選ぶ操作を行うと、001 を黒板に新たに書くことができます。
入力例 2
2 1 2 0 0 0 1
出力例 2
Yes No
はじめに黒板に書かれている文字列はどちらも 0 であり、操作によって新たに書かれる文字列も 0 です。よって、1 が黒板に書かれている状態にできません。
Score : 700 points
Problem Statement
You are given length-M strings S_1,S_2,\ldots,S_N consisting of 0 and 1.
Initially, S_1,S_2,\ldots,S_N are written on the blackboard.
You can perform the following operation zero or more times.
- Choose three strings from those written on the blackboard, allowing the same string to be chosen multiple times, and call them A,B,C. Newly write on the blackboard a string D of length M satisfying the following condition:
- For each i=1,2,\ldots,M, letting A_i,B_i,C_i,D_i denote the i-th characters of A,B,C,D, respectively, D_i is equal to at least two of A_i,B_i,C_i.
You are given Q strings T_1,T_2,\ldots,T_Q. For each i=1,2,\ldots,Q, determine whether it is possible to make T_i be written on the blackboard by performing operations starting from the initial state.
Constraints
- 1 \leq N \leq 500
- 1 \leq M \leq 500
- 1 \leq Q \leq 500
- S_i is a string of length M consisting of
0and1. (1 \leq i \leq N) - T_i is a string of length M consisting of
0and1. (1 \leq i \leq Q) - All input values are integers.
Input
The input is given from Standard Input in the following format:
N M Q S_1 S_2 \vdots S_N T_1 T_2 \vdots T_Q
Output
Output Q lines. The i-th line should contain Yes if T_i can be made to be written on the blackboard, and No otherwise.
Sample Input 1
3 3 2 000 011 101 000 001
Sample Output 1
Yes Yes
T_1 is written on the blackboard from the beginning.
T_2 is 001. If we perform the operation choosing the three strings 000, 011, 101, we can newly write 001 on the blackboard.
Sample Input 2
2 1 2 0 0 0 1
Sample Output 2
Yes No
The strings initially written on the blackboard are both 0, and the string newly written by the operation is also 0. Therefore, it is impossible to make 1 be written on the blackboard.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
0 と 1 からなる長さ N の数列 B=(B_1,B_2,\ldots,B_N) があります。 はじめ、B_1=1 であり、それ以外の要素は 0 です。
M 個のスイッチがあり、スイッチ i (1 \le i \le M) には整数 A_i が書かれています。 スイッチ i を押すと、操作を行う直前の状態を用いて、1 \le j \le N を満たすすべての整数 j について B_j を同時に次のように変更します。
\[ B_j \leftarrow \begin{cases} B_j \oplus B_{j-A_i} & (A_i \lt j),\\ B_j & (j \le A_i) \end{cases} \]
各スイッチは 0 回または 1 回押すことができ、スイッチを押す順番は自由です。
最終的な列 B としてあり得るものの個数を 998244353 で割った余りを求めてください。
ビット単位 \mathrm{XOR} 演算とは
非負整数 A, B のビット単位 \mathrm{XOR}、A \oplus B は、 以下のように定義されます。
- A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、 A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、 そうでなければ 0 である。
例えば、3 \oplus 5 = 6 となります (二進表記すると: 011 \oplus 101 = 110)。 一般に k 個の非負整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{XOR} は (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) と定義され、 これは p_1, p_2, p_3, \dots, p_k の順番によらないことが証明できます。
制約
- 2 \le N \le 2 \times 10^5
- 1 \le M \le 2 \times 10^5
- 1 \le A_i \lt N (1 \le i \le M)
- 入力される数値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_M
出力
最終的な列 B としてあり得るものの個数を 998244353 で割った余りを出力せよ。
入力例 1
4 2 2 3
出力例 1
4
最終的な列としてあり得るものは、次の 4 個です。
- (1,0,0,0)
- (1,0,1,0)
- (1,0,0,1)
- (1,0,1,1)
入力例 2
2 1 1
出力例 2
2
最終的な列としてあり得るものは、(1,0) と (1,1) の 2 個です。
入力例 3
96 30 56 6 46 18 9 38 20 25 18 44 46 71 44 65 42 20 38 25 9 95 18 65 71 9 95 65 9 42 65 6
出力例 3
384912
Score : 700 points
Problem Statement
There is a length-N sequence B=(B_1,B_2,\ldots,B_N) consisting of 0 and 1. Initially, B_1=1, and all other elements are 0.
There are M switches, and the integer A_i is written on switch i (1 \le i \le M). When switch i is pressed, using the state immediately before the operation, B_j is simultaneously changed as follows for every integer j satisfying 1 \le j \le N:
\[ B_j \leftarrow \begin{cases} B_j \oplus B_{j-A_i} & (A_i \lt j),\\ B_j & (j \le A_i) \end{cases} \]
Each switch can be pressed zero times or once, and the switches may be pressed in any order.
Find the number, modulo 998244353, of possible final sequences B.
What is the bitwise \mathrm{XOR} operation?
The bitwise \mathrm{XOR} of non-negative integers A and B, denoted A \oplus B, is defined as follows:
- The digit in the 2^k's place (k \geq 0) of A \oplus B, written in binary, is 1 if exactly one of the digits in the 2^k's place of A and B, written in binary, is 1, and 0 otherwise.
For example, 3 \oplus 5 = 6 (in binary: 011 \oplus 101 = 110). In general, the bitwise \mathrm{XOR} of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k), and it can be proved that this does not depend on the order of p_1, p_2, p_3, \dots, p_k.
Constraints
- 2 \le N \le 2 \times 10^5
- 1 \le M \le 2 \times 10^5
- 1 \le A_i \lt N (1 \le i \le M)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_M
Output
Output the number, modulo 998244353, of possible final sequences B.
Sample Input 1
4 2 2 3
Sample Output 1
4
There are four possible final sequences:
- (1,0,0,0)
- (1,0,1,0)
- (1,0,0,1)
- (1,0,1,1)
Sample Input 2
2 1 1
Sample Output 2
2
There are two possible final sequences: (1,0) and (1,1).
Sample Input 3
96 30 56 6 46 18 9 38 20 25 18 44 46 71 44 65 42 20 38 25 9 95 18 65 71 9 95 65 9 42 65 6
Sample Output 3
384912
Time Limit: 5 sec / Memory Limit: 1024 MiB
配点 : 800 点
問題文
長さ N の整数列 A=(0,0,\ldots,0) があります。 次の操作を行える限り、繰り返し行います。
- 1\le i\lt j\le |A| かつ A_i=A_j を満たす整数の組 (i,j) を選ぶ。
- A_i,A_j を列から取り除く。
- 取り除く直前に A_i と A_j の間にあったすべての要素に 1 を加える。
ここで |A| は、その時点での列 A の長さを表します。
操作を行えなくなったときの列としてあり得るものの個数を 998244353 で割った余りを求めてください。 最終的な列が同じならば、操作の過程が異なっても区別しません。 長さ 0 の列も 1 つの列として数えます。
制約
- 1 \le N \le 2 \times 10^5
- 入力される数値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
1
出力例 1
1
操作を行えないため、最終的な列は (0) の 1 個です。
入力例 2
5
出力例 2
3
最終的な列としてあり得るものは、(0)、(1)、(2) の 3 個です。
入力例 3
7
出力例 3
8
操作の選び方によって、8 個の異なる列を得られます。
入力例 4
200000
出力例 4
159211719
答えを 998244353 で割った余りを求めることに注意してください。
Score : 800 points
Problem Statement
There is an integer sequence A=(0,0,\ldots,0) of length N. The following operation is repeated as long as it can be performed.
- Choose a pair of integers (i,j) satisfying 1\le i\lt j\le |A| and A_i=A_j.
- Remove A_i and A_j from the sequence.
- Add 1 to every element that was between A_i and A_j immediately before the removal.
Here, |A| denotes the length of the sequence A at that point.
Find the number, modulo 998244353, of possible sequences when no more operations can be performed. If the final sequences are the same, they are not distinguished even if the process of operations differs. A sequence of length 0 is also counted as one sequence.
Constraints
- 1 \le N \le 2 \times 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
Output
Output the answer.
Sample Input 1
1
Sample Output 1
1
No operation can be performed, so there is one possible final sequence: (0).
Sample Input 2
5
Sample Output 2
3
There are three possible final sequences: (0), (1), (2).
Sample Input 3
7
Sample Output 3
8
Depending on the choices made during the operations, eight different sequences can be obtained.
Sample Input 4
200000
Sample Output 4
159211719
Be sure to find the count modulo 998244353.