実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 700 点
問題文
( と ) のみからなる文字列 S があります。
太郎君と次郎君はこの文字列を使って以下のゲームをします。
太郎君が先手、次郎君が後手となり、以下の 3 種類の操作のうちいずれか 1 つを選んで行うことを交互に繰り返します。
- S の先頭の文字を削除する。
- S の末尾の文字を削除する。
- 終了宣言をする。
S の長さがちょうど K になったとき、あるいはいずれかのプレイヤーが終了宣言をした時点で、ゲームが終了します。ゲームの終了時点で S が正しい括弧列であるならば次郎君の勝利、そうでないならば太郎君の勝利となります。
ゲーム開始前の S が与えられるので、両者が最適に行動した場合の勝者を求めてください。
T 個のテストケースが与えられるので、それぞれについて解いてください。
正しい括弧列とは
正しい括弧列とは、() である部分文字列を削除することを 0 回以上繰り返して空文字列にできる文字列を指します。
制約
- 1 \le T \le 10^5
- 1 \le K < |S| \le 10^6
- S は
(と)のみからなる文字列 - T, K は整数
- すべてのテストケースにわたる |S| の総和は 10^6 以下
入力
入力は以下の形式で標準入力から与えられる。
T case_1 case_2 \vdots case_T
各ケースは以下の形式で与えられる。
S K
出力
各テストケースについて、太郎君が勝つならば First、次郎君が勝つならば Second を出力せよ。
入力例 1
4 (()()) 2 (())() 2 (() 2 (()()())((()()(())())) 12
出力例 1
Second First First First
1 つ目のテストケースについて、ありうるゲームの進行の例を示します。
-
ゲーム開始前の S は
(()())です。 -
太郎君が S の先頭の文字を削除し
()())となります。 -
次に次郎君が S の末尾の文字を削除し
()()となります。 -
次に太郎君が S の末尾の文字を削除し
()(となります。 -
次に次郎君が S の末尾の文字を削除し
()となり、S の長さが K=2 となったのでゲームが終了します。 -
最終的な S は正しい括弧列なので次郎君の勝利となります。
3 つ目のテストケースでは例えば太郎君が最初のターンで終了宣言をすることで太郎君の勝利となります。
Score : 700 points
Problem Statement
There is a string S consisting of ( and ).
Taro and Jiro will play the following game using this string.
Taro goes first and Jiro goes second; they alternately choose and perform one of the following three types of operations:
- Delete the first character of S.
- Delete the last character of S.
- Declare termination.
The game ends when the length of S becomes exactly K, or when either player declares termination. At the end of the game, if S is a correct bracket sequence, Jiro wins; otherwise, Taro wins.
Given S before the game starts, determine the winner when both players act optimally.
You are given T test cases. Solve each of them.
What is a correct bracket sequence?
A correct bracket sequence is a string that can be reduced to an empty string by repeating the operation of deleting a substring() zero or more times.
Constraints
- 1 \le T \le 10^5
- 1 \le K < |S| \le 10^6
- S is a string consisting of
(and). - T and K are integers.
- The sum of |S| over all test cases is at most 10^6.
Input
The input is given from Standard Input in the following format:
T case_1 case_2 \vdots case_T
Each case is given in the following format:
S K
Output
For each test case, print First if Taro wins, and Second if Jiro wins.
Sample Input 1
4 (()()) 2 (())() 2 (() 2 (()()())((()()(())())) 12
Sample Output 1
Second First First First
For the first test case, here is an example of how the game might proceed:
-
Before the game starts, S is
(()()). -
Taro deletes the first character of S, resulting in
()()). -
Next, Jiro deletes the last character of S, resulting in
()(). -
Next, Taro deletes the last character of S, resulting in
()(. -
Next, Jiro deletes the last character of S, resulting in
(), and the length of S becomes K=2, so the game ends. -
The final S is a correct bracket sequence, so Jiro wins.
In the third test case, for example, Taro can win by declaring termination on the first turn.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 700 点
問題文
英小文字からなる文字列 s に対し、s の部分文字列であって、偶数長の回文であるものの個数を f(s) で表します。より厳密には、f(s) は以下の条件を全て満たす整数 l, r の組の個数です。
- 1 \leq l \leq r \leq |s|
- r - l + 1 は偶数
- s_l s_{l+1} \cdots s_{r} は回文
英小文字からなる文字列 S が与えられるので、S の文字を並べ替えて得られる文字列 S' のうち、f(S') が最小となるものを 1 つ出力してください。
T 個のテストケースが与えられるので、それぞれについて解いてください。
制約
- 1 \le T \le 10^5
- 2 \le |S| \le 2 \times 10^5
- S は英小文字からなる
- 全てのテストケースにおける |S| の総和は 2 \times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
S
出力
T 行出力せよ。
i 行目には i 番目のテストケースについて、S の文字を並べ替えて得られる文字列 S' のうち、f(S') が最小となるものを 1 つ出力せよ。そのような S' が複数ある場合は、どれを出力しても正答となる。
入力例 1
3 see xxxxxy abracadabra
出力例 1
ese xxxyxx abracadabra
1 番目のテストケースについて、S の文字を並べ替えて得られる文字列 S' として see, ese, ees が考えられますが、このうち f(S') が最小となるものは ese のみなので、これを出力します。
2 番目のテストケースについて、例えば xxyxxx と出力しても正答となります。
Score : 700 points
Problem Statement
For a string s consisting of lowercase English letters, let f(s) denote the number of substrings of s that are palindromes of even length. More precisely, f(s) is the number of pairs of integers l, r that satisfy all of the following conditions:
- 1 \leq l \leq r \leq |s|.
- r - l + 1 is even.
- s_l s_{l+1} \cdots s_{r} is a palindrome.
Given a string S consisting of lowercase English letters, output one string S' obtained by rearranging the characters of S such that f(S') is minimized.
You are given T test cases. Solve each of them.
Constraints
- 1 \le T \le 10^5
- 2 \le |S| \le 2 \times 10^5
- S consists of lowercase English letters.
- The sum of |S| over all test cases is at most 2 \times 10^5.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
S
Output
Output T lines.
The i-th line should contain one string S' obtained by rearranging the characters of S in the i-th test case such that f(S') is minimized. If there are multiple such S', any of them will be considered correct.
Sample Input 1
3 see xxxxxy abracadabra
Sample Output 1
ese xxxyxx abracadabra
For the first test case, the strings S' obtained by rearranging the characters of S are see, ese, and ees. Among these, only ese minimizes f(S'), so output it.
For the second test case, for example, outputting xxyxxx will also be considered correct.
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 900 点
問題文
正の整数 N, X および長さ N の整数列 S = (S_1, S_2, \dots, S_N), P = (P_1, P_2, \dots, P_N) が与えられます。 ここで、P の各要素は 1 または -1 です。
クエリが Q 個与えられます。各クエリでは、1 \leq l \leq r \leq N を満たす整数 l, r が与えられ、あなたは以下のようなゲームを行います。
- はじめ、あなたのスコアは 0 であり、整数 a, b が a = 1, b = 1 で初期化されている。
- i = l, l+1, \dots, r の順に、以下の操作を行う。
- i が偶数のときは a の値、i が奇数のときは b の値を、1 以上 X 以下の好きな整数に置き換える。
- その後、ab \geq S_i ならあなたのスコアに P_i を加算する。そうでないとき、スコアは変動しない。P_i は負の値も取り得ることに注意せよ。
各クエリに対して、ゲームが終了した時点でのあなたのスコアとしてあり得る最大値を求めてください。
制約
- 1 \le N, X, Q \le 10^5
- 1 \le S_i \le 10^5
- P_i = 1 または P_i = -1
- 各クエリに対して 1 \le l \le r \le N
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N X Q
S_1 S_2 \ldots S_N
P_1 P_2 \ldots P_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
各クエリは以下の形式で与えられる。
l r
出力
Q 行出力せよ。
i 行目には、i 番目のクエリにおいて、ゲームが終了した時点でのあなたのスコアとしてあり得る最大値を出力せよ。
入力例 1
9 4 4 3 11 10 12 5 7 3 9 16 1 1 -1 1 -1 1 -1 1 1 5 9 1 4 5 5 2 8
出力例 1
2 3 0 1
1 番目のクエリにおいて、以下の手順によってゲームが終了した時点でのスコアを 2 にすることができます。
| i | 操作 | a | b | スコア |
|---|---|---|---|---|
| - | 1 | 1 | 0 | |
| 5 | b の値を 4 に置き換える。ab < S_5 \, (= 5) のためスコアは変動しない。 | 1 | 4 | 0 |
| 6 | a の値を 2 に置き換える。ab \geq S_6 \, (= 7) のためスコアに P_6 \, (= 1) を加算する。 | 2 | 4 | 1 |
| 7 | b の値を 3 に置き換える。ab \geq S_7 \, (= 3) のためスコアに P_7 \, (= -1) を加算する。 | 2 | 3 | 0 |
| 8 | a の値を 4 に置き換える。ab \geq S_8 \, (= 9) のためスコアに P_8 \, (= 1) を加算する。 | 4 | 3 | 1 |
| 9 | b の値を 4 に置き換える。ab \geq S_9 \, (= 16) のためスコアに P_9 \, (= 1) を加算する。 | 4 | 4 | 2 |
ゲームが終了した時点でのスコアを 3 以上にすることはできないので、2 を出力します。
Score : 900 points
Problem Statement
You are given positive integers N, X and integer sequences of length N: S = (S_1, S_2, \dots, S_N), P = (P_1, P_2, \dots, P_N). Here, each element of P is 1 or -1.
You are given Q queries. In each query, you are given integers l, r satisfying 1 \leq l \leq r \leq N, and you play the following game:
- Initially, your score is 0, and integers a, b are initialized as a = 1, b = 1.
- For i = l, l+1, \dots, r in this order, perform the following operation:
- If i is even, replace the value of a with any integer between 1 and X (inclusive); if i is odd, replace the value of b with any integer between 1 and X (inclusive).
- After that, if ab \geq S_i, add P_i to your score. Otherwise, the score does not change. Note that P_i can be negative.
For each query, find the maximum possible value of your score when the game ends.
Constraints
- 1 \le N, X, Q \le 10^5
- 1 \le S_i \le 10^5
- P_i = 1 or P_i = -1.
- For each query, 1 \le l \le r \le N.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X Q
S_1 S_2 \ldots S_N
P_1 P_2 \ldots P_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
Each query is given in the following format:
l r
Output
Output Q lines.
The i-th line should contain the maximum possible value of your score when the game ends in the i-th query.
Sample Input 1
9 4 4 3 11 10 12 5 7 3 9 16 1 1 -1 1 -1 1 -1 1 1 5 9 1 4 5 5 2 8
Sample Output 1
2 3 0 1
In the first query, you can make the score 2 when the game ends by the following procedure:
| i | Operation | a | b | Score |
|---|---|---|---|---|
| - | 1 | 1 | 0 | |
| 5 | Replace the value of b with 4. Since ab < S_5 \, (= 5), the score does not change. | 1 | 4 | 0 |
| 6 | Replace the value of a with 2. Since ab \geq S_6 \, (= 7), add P_6 \, (= 1) to the score. | 2 | 4 | 1 |
| 7 | Replace the value of b with 3. Since ab \geq S_7 \, (= 3), add P_7 \, (= -1) to the score. | 2 | 3 | 0 |
| 8 | Replace the value of a with 4. Since ab \geq S_8 \, (= 9), add P_8 \, (= 1) to the score. | 4 | 3 | 1 |
| 9 | Replace the value of b with 4. Since ab \geq S_9 \, (= 16), add P_9 \, (= 1) to the score. | 4 | 4 | 2 |
It is impossible to make the score 3 or more when the game ends, so output 2.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 900 点
問題文
長さ N の数列 A = (A_1, A_2, \ldots, A_N) が与えられます。 各要素は 1 以上 N 以下の整数もしくは -1 です。
すぬけ君はこの数列 A を用いて新しい数列を作ることにしました。
まず、この数列 A に登場する -1 を全て 1 以上 N 以下の整数に書き換えます。
次に、以下の式に従って長さ N の数列 B = (B_1, B_2, \ldots, B_N) を作ります。
- B_i=A_{A_i}
B としてありうるもののうち辞書順で最小のものを求めてください。
T 個のテストケースが与えられるので、それぞれについて解いてください。
数列の辞書順とは?
数列 C = (C_1,C_2,\ldots,C_N) が数列 D = (D_1,D_2,\ldots,D_N) より辞書順で小さいとは、ある整数 1 \leq i \leq N が存在して、下記の 2 つがともに成り立つことを言います。
- (C_1,C_2,\ldots,C_{i-1}) = (D_1,D_2,\ldots,D_{i-1})
- C_i が D_i より(数として)小さい。
制約
- 1 \le T \le 10^5
- 1 \le N \le 5 \times 10^5
- 1 \le A_i \le N または A_i = -1
- すべてのテストケースにわたる N の総和は 5\times 10^5 以下
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
T case_1 case_2 \vdots case_T
各ケースは以下の形式で与えられる。
N A_1 A_2 \dots A_N
出力
各テストケースに対する、B としてありうるもののうち辞書順で最小のものを改行区切りで出力せよ。
入力例 1
3 4 4 -1 -1 3 5 5 4 5 3 1 7 -1 6 5 -1 7 -1 6
出力例 1
3 1 4 1 1 3 1 5 5 1 1 7 1 6 1 1
1 番目のテストケースにおいて、すぬけ君が書き換える前の数列 A は (4,-1,-1,3) です。
すぬけ君が A=(4, 3, 1, 3) と書き換えたとします。
このとき、B=(A_4, A_3, A_1, A_3)=(3, 1, 4, 1) となります。
これよりも辞書順で小さい数列 B を得ることはできません。
Score : 900 points
Problem Statement
You are given a sequence A = (A_1, A_2, \ldots, A_N) of length N. Each element is either an integer between 1 and N (inclusive) or -1.
Snuke has decided to create a new sequence using this sequence A.
First, replace all -1s in this sequence A with integers between 1 and N (inclusive).
Next, create a sequence B = (B_1, B_2, \ldots, B_N) of length N according to the following formula:
- B_i=A_{A_i}
Find the lexicographically smallest possible sequence B.
You are given T test cases. Solve each of them.
What is the lexicographical order of sequences?
A sequence C = (C_1,C_2,\ldots,C_N) is lexicographically smaller than a sequence D = (D_1,D_2,\ldots,D_N) if and only if there exists an integer 1 \leq i \leq N such that both of the following hold:
- (C_1,C_2,\ldots,C_{i-1}) = (D_1,D_2,\ldots,D_{i-1})
- C_i is (numerically) smaller than D_i.
Constraints
- 1 \le T \le 10^5
- 1 \le N \le 5 \times 10^5
- 1 \le A_i \le N or A_i = -1.
- The sum of N over all test cases is at most 5\times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T case_1 case_2 \vdots case_T
Each case is given in the following format:
N A_1 A_2 \dots A_N
Output
For each test case, output the lexicographically smallest possible sequence B, separated by newlines.
Sample Input 1
3 4 4 -1 -1 3 5 5 4 5 3 1 7 -1 6 5 -1 7 -1 6
Sample Output 1
3 1 4 1 1 3 1 5 5 1 1 7 1 6 1 1
In the first test case, the sequence A before Snuke's replacement is (4,-1,-1,3).
Suppose he replaces it to get A=(4, 3, 1, 3).
Then, B=(A_4, A_3, A_1, A_3)=(3, 1, 4, 1).
It is impossible to obtain a lexicographically smaller sequence B than this.
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 1100 点
問題文
長さ N の A, B, C からなる文字列 S のうち、以下の値が K になるものの個数を 998244353 で割った余りを求めてください。
- S の(連続とは限らない)部分列のうち、(連続とは限らない)部分列として
ABCを含まないものの長さの最大値
T 個のテストケースが与えられるので、それぞれについて解いてください。
制約
- 1 \le T \le 2 \times 10^5
- 1 \le N \le 10^6
- \max(0,N-100) \le K \le N
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各ケースは以下の形式で与えられる。
N\ K
出力
T 行出力せよ。i(1 \le i \le T) 行目には、i 番目のテストケースの答えを出力せよ。
入力例 1
4 4 3 8 6 123 100 123456 123400
出力例 1
9 462 741573397 255048895
1 番目のテストケースについて、例えば ACBC は条件を満たします。ACBC 自身は部分列に ABC を含み、ACC は部分列に ABC を含まないため、部分列に ABC を含まないような部分列の長さの最大値は 3 となります。
他にも AABC, ABCA などが条件を満たします。
Score : 1100 points
Problem Statement
Find the number, modulo 998244353, of strings S of length N consisting of A, B, C such that the following value equals K:
- The maximum length of a (not necessarily contiguous) subsequence of S that does not contain
ABCas a (not necessarily contiguous) subsequence
You are given T test cases. Solve each of them.
Constraints
- 1 \le T \le 2 \times 10^5
- 1 \le N \le 10^6
- \max(0,N-100) \le K \le N
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each case is given in the following format:
N\ K
Output
Output T lines. The i-th line (1 \le i \le T) should contain the answer to the i-th test case.
Sample Input 1
4 4 3 8 6 123 100 123456 123400
Sample Output 1
9 462 741573397 255048895
For the first test case, for example, ACBC satisfies the condition. ACBC itself contains ABC as a subsequence, and ACC does not contain ABC as a subsequence, so the maximum length of a subsequence that does not contain ABC as a subsequence is 3.
Other examples that satisfy the condition include AABC and ABCA.