Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は英小文字からなる文字列 T を青木君に向けて送りました。その結果、青木君は英小文字からなる文字列 T' を受信しました。
T' は T から一部が変更されてしまっている可能性があり、具体的には、下記の 4 つのうちのちょうど 1 つが成り立つことがわかっています。
- T' は、T と等しい。
- T' は、T のいずれか 1 つの位置(先頭と末尾も含む)に英小文字を 1 つ挿入して得られる文字列である。
- T' は、T からある 1 文字を削除して得られる文字列である。
- T' は、T のある 1 文字を別の英小文字に変更して得られる文字列である。
青木君が受信した文字列 T' と、英小文字からなる N 個の文字列 S_1, S_2, \ldots, S_N が入力として与えられるので、 S_1, S_2, \ldots, S_N のうち、高橋君が送った文字列 T と等しい可能性があるものをすべて求めてください。
制約
- N は整数
- 1 \leq N \leq 5 \times 10^5
- S_i と T' は英小文字からなる長さ 1 以上 5 \times 10^5 以下の文字列
- S_1, S_2, \ldots, S_N の長さの総和は 5 \times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
N T' S_1 S_2 \vdots S_N
出力
S_1, S_2, \ldots, S_N のうち T と等しい可能性があるものすべての添字を昇順に並べた列を (i_1, i_2, \ldots, i_K) とする。 この列の長さ K および列自体を、下記の形式にしたがって出力せよ。
K i_1 i_2 \ldots i_K
入力例 1
5 ababc ababc babc abacbc abdbc abbac
出力例 1
4 1 2 3 4
S_1, S_2, \ldots, S_5 のうち、T と等しい可能性があるものは S_1, S_2, S_3, S_4 の 4 つであることが下記の通りわかります。
- S_1 は T と等しい可能性があります。なぜなら、T' =
ababcは S_1 =ababcと等しいからです。 - S_2 は T と等しい可能性があります。なぜなら、T' =
ababcは S_2 =babcの先頭に文字aを挿入して得られる文字列だからです。 - S_3 は T と等しい可能性があります。なぜなら、T' =
ababcは S_3 =abacbcから 4 文字目のcを削除して得られる文字列だからです。 - S_4 は T と等しい可能性があります。なぜなら、T' =
ababcは S_4 =abdbcの 3 文字目のdをbに変更して得られる文字列だからです。 - S_5 は T と等しい可能性はありません。なぜなら、S_5 =
abbacを T としたとき、T' =ababcは問題文中の 4 つの条件をいずれも満たさないからです。
入力例 2
1 aoki takahashi
出力例 2
0
入力例 3
9 atcoder atoder atcode athqcoder atcoder tacoder jttcoder atoder atceoder atcoer
出力例 3
6 1 2 4 7 8 9
Score : 300 points
Problem Statement
Takahashi sent a string T consisting of lowercase English letters to Aoki. As a result, Aoki received a string T' consisting of lowercase English letters.
T' may have been altered from T. Specifically, exactly one of the following four conditions is known to hold.
- T' is equal to T.
- T' is a string obtained by inserting one lowercase English letter at one position (possibly the beginning and end) in T.
- T' is a string obtained by deleting one character from T.
- T' is a string obtained by changing one character in T to another lowercase English letter.
You are given the string T' received by Aoki and N strings S_1, S_2, \ldots, S_N consisting of lowercase English letters. Find all the strings among S_1, S_2, \ldots, S_N that could equal the string T sent by Takahashi.
Constraints
- N is an integer.
- 1 \leq N \leq 5 \times 10^5
- S_i and T' are strings of length between 1 and 5 \times 10^5, inclusive, consisting of lowercase English letters.
- The total length of S_1, S_2, \ldots, S_N is at most 5 \times 10^5.
Input
The input is given from Standard Input in the following format:
N T' S_1 S_2 \vdots S_N
Output
Let (i_1, i_2, \ldots, i_K) be the sequence of indices of all the strings among S_1, S_2, \ldots, S_N that could be equal to T, in ascending order. Print the length K of this sequence, and the sequence itself, in the following format:
K i_1 i_2 \ldots i_K
Sample Input 1
5 ababc ababc babc abacbc abdbc abbac
Sample Output 1
4 1 2 3 4
Among S_1, S_2, \ldots, S_5, the strings that could be equal to T are S_1, S_2, S_3, S_4, as explained below.
- S_1 could be equal to T, because T' =
ababcis equal to S_1 =ababc. - S_2 could be equal to T, because T' =
ababcis obtained by inserting the letteraat the beginning of S_2 =babc. - S_3 could be equal to T, because T' =
ababcis obtained by deleting the fourth charactercfrom S_3 =abacbc. - S_4 could be equal to T, because T' =
ababcis obtained by changing the third characterdin S_4 =abdbctob. - S_5 could not be equal to T, because if we take S_5 =
abbacas T, then T' =ababcdoes not satisfy any of the four conditions in the problem statement.
Sample Input 2
1 aoki takahashi
Sample Output 2
0
Sample Input 3
9 atcoder atoder atcode athqcoder atcoder tacoder jttcoder atoder atceoder atcoer
Sample Output 3
6 1 2 4 7 8 9
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
子供と大人があわせて N 人います。i 番目の人の体重は W_i です。
それぞれの人が子供か大人かは、0 と 1 からなる長さ N の文字列 S によって表され、
S の i 文字目が 0 であるとき i 番目の人が子供であることを、1 であるとき i 番目の人が大人であることをさします。
ロボットである高橋君に対して実数 X を設定すると、
高橋君はそれぞれの人に対して、体重が X 未満なら子供、X 以上なら大人と判定します。
実数 X に対してf(X) を、高橋君に X を設定したときに N 人のうち子供か大人かを正しく判定できる人数で定めます。
X が実数全体を動くとき、f(X) の最大値を求めてください。
制約
- 1\leq N\leq 2\times 10^5
- S は
0と1からなる長さ N の文字列 - 1\leq W_i\leq 10^9
- N,W_i は整数
入力
入力は以下の形式で標準入力から与えられる。
N S W_1 W_2 \ldots W_N
出力
f(X) の最大値を整数で一行に出力せよ。
入力例 1
5 10101 60 45 30 40 80
出力例 1
4
X=50 と設定すると、高橋君は 2,3,4 番目の人を子供、 1,5 番目の人を大人と判定します。
実際には 2,4 番目の人が子供、 1,3,5 番目の人が大人であるので、このとき、1,2,4,5 番目の合計 4 人に対して正しく判定できています。
よって、f(50)=4 です。
5 人全員に対して正しく判定できるような X は存在しないのでこのときが最大です。よって、4 を出力します。
入力例 2
3 000 1 2 3
出力例 2
3
例えば、X=10 とすると最大値 f(10)=3 を達成します。
全員が大人、または全員が子供である可能性もあることに注意してください。
入力例 3
5 10101 60 50 50 50 60
出力例 3
4
例えば、X=55 とすると最大値 f(55)=4 を達成します。
同じ体重の人が複数人存在する可能性もあることに注意してください。
Score : 300 points
Problem Statement
There are N people, each of whom is either a child or an adult. The i-th person has a weight of W_i.
Whether each person is a child or an adult is specified by a string S of length N consisting of 0 and 1.
If the i-th character of S is 0, then the i-th person is a child; if it is 1, then the i-th person is an adult.
When Takahashi the robot is given a real number X,
Takahashi judges a person with a weight less than X to be a child and a person with a weight more than or equal to X to be an adult.
For a real value X, let f(X) be the number of people whom Takahashi correctly judges whether they are children or adults.
Find the maximum value of f(X) for all real values of X.
Constraints
- 1\leq N\leq 2\times 10^5
- S is a string of length N consisting of
0and1. - 1\leq W_i\leq 10^9
- N and W_i are integers.
Input
Input is given from Standard Input in the following format:
N S W_1 W_2 \ldots W_N
Output
Print the maximum value of f(X) as an integer in a single line.
Sample Input 1
5 10101 60 45 30 40 80
Sample Output 1
4
When Takahashi is given X=50, it judges the 2-nd, 3-rd, and 4-th people to be children and the 1-st and 5-th to be adults.
In reality, the 2-nd and 4-th are children, and the 1-st, 3-rd, and 5-th are adults, so the 1-st, 2-nd, 4-th, and 5-th people are correctly judged.
Thus, f(50)=4.
This is the maximum since there is no X that judges correctly for all 5 people. Thus, 4 should be printed.
Sample Input 2
3 000 1 2 3
Sample Output 2
3
For example, X=10 achieves the maximum value f(10)=3.
Note that the people may be all children or all adults.
Sample Input 3
5 10101 60 50 50 50 60
Sample Output 3
4
For example, X=55 achieves the maximum value f(55)=4.
Note that there may be multiple people with the same weight.
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
xy -平面上にいくつかのイチゴが載った長方形のケーキがあります。 ケーキは、長方形領域 \lbrace (x, y) : 0 \leq x \leq W, 0 \leq y \leq H \rbrace をちょうど占めます。
ケーキには N 個のイチゴが載っており、i = 1, 2, \ldots, N について、i 番目のイチゴの座標は (p_i, q_i) です。 2 個以上のイチゴが同一の座標にあることはありません。
高橋君は、このケーキを包丁で下記の通りにいくつかのピースに切り分けます。
- まず、ケーキを通る y 軸に並行な A 本の異なる直線、直線 x = a_1 、直線 x = a_2 、\ldots 、直線 x = a_A のそれぞれにそってケーキを切る。
- 次に、ケーキを通る x 軸に並行な B 本の異なる直線、直線 y = b_1 、直線 y = b_2 、\ldots 、直線 y = b_B のそれぞれにそってケーキを切る。
その結果、ケーキは (A+1)(B+1) 個の長方形のピースに分割されます。 高橋君はそれらのうちのいずれか 1 個だけを選んで食べます。 高橋君が選んだピースに載っているイチゴの個数としてあり得る最小値と最大値をそれぞれ出力してください。
ここで、最終的にピースの縁となる位置にはイチゴが存在しないことが保証されます。 より形式的な説明は下記の制約を参照してください。
制約
- 3 \leq W, H \leq 10^9
- 1 \leq N \leq 2 \times 10^5
- 0 \lt p_i \lt W
- 0 \lt q_i \lt H
- i \neq j \implies (p_i, q_i) \neq (p_j, q_j)
- 1 \leq A, B \leq 2 \times 10^5
- 0 \lt a_1 \lt a_2 \lt \cdots \lt a_A \lt W
- 0 \lt b_1 \lt b_2 \lt \cdots \lt b_B \lt H
- p_i \not \in \lbrace a_1, a_2, \ldots, a_A \rbrace
- q_i \not \in \lbrace b_1, b_2, \ldots, b_B \rbrace
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
W H N p_1 q_1 p_2 q_2 \vdots p_N q_N A a_1 a_2 \ldots a_A B b_1 b_2 \ldots b_B
出力
高橋君が選んだピースに載っているイチゴの個数としてあり得る最小値 m と最大値 M をそれぞれ、下記の形式の通り空白区切りで出力せよ。
m M
入力例 1
7 6 5 6 1 3 1 4 2 1 5 6 2 2 2 5 2 3 4
出力例 1
0 2
全 9 個のピースの内訳は、イチゴが 0 個載ったものが 6 個、イチゴが 1 個載ったものが 1 個、イチゴが 2 個載ったものが 2 個です。 よって、それらのうちのいずれか 1 個だけを選んで食べるとき、選んだピースに載っているイチゴの個数としてあり得る最小値は 0 、最大値は 2 です。
入力例 2
4 4 4 1 1 3 1 3 3 1 3 1 2 1 2
出力例 2
1 1
どのピースにもイチゴが 1 個載っています。
Score : 400 points
Problem Statement
There is a rectangular cake with some strawberries on the xy-plane. The cake occupies the rectangular area \lbrace (x, y) : 0 \leq x \leq W, 0 \leq y \leq H \rbrace.
There are N strawberries on the cake, and the coordinates of the i-th strawberry are (p_i, q_i) for i = 1, 2, \ldots, N. No two strawberries have the same coordinates.
Takahashi will cut the cake into several pieces with a knife, as follows.
- First, cut the cake along A different lines parallel to the y-axis: lines x = a_1, x = a_2, \ldots, x = a_A.
- Next, cut the cake along B different lines parallel to the x-axis: lines y = b_1, y = b_2, \ldots, y = b_B.
As a result, the cake will be divided into (A+1)(B+1) rectangular pieces. Takahashi will choose just one of these pieces to eat. Print the minimum and maximum possible numbers of strawberries on the chosen piece.
Here, it is guaranteed that there are no strawberries along the edges of the final pieces. For a more formal description, refer to the constraints below.
Constraints
- 3 \leq W, H \leq 10^9
- 1 \leq N \leq 2 \times 10^5
- 0 \lt p_i \lt W
- 0 \lt q_i \lt H
- i \neq j \implies (p_i, q_i) \neq (p_j, q_j)
- 1 \leq A, B \leq 2 \times 10^5
- 0 \lt a_1 \lt a_2 \lt \cdots \lt a_A \lt W
- 0 \lt b_1 \lt b_2 \lt \cdots \lt b_B \lt H
- p_i \not \in \lbrace a_1, a_2, \ldots, a_A \rbrace
- q_i \not \in \lbrace b_1, b_2, \ldots, b_B \rbrace
- All input values are integers.
Input
The input is given from Standard Input in the following format:
W H N p_1 q_1 p_2 q_2 \vdots p_N q_N A a_1 a_2 \ldots a_A B b_1 b_2 \ldots b_B
Output
Print the minimum possible number of strawberries m and the maximum possible number M on the chosen piece in the following format, separated by a space.
m M
Sample Input 1
7 6 5 6 1 3 1 4 2 1 5 6 2 2 2 5 2 3 4
Sample Output 1
0 2
There are nine pieces in total: six with zero strawberries, one with one strawberry, and two with two strawberries. Therefore, when choosing just one of these pieces to eat, the minimum possible number of strawberries on the chosen piece is 0, and the maximum possible number is 2.
Sample Input 2
4 4 4 1 1 3 1 3 3 1 3 1 2 1 2
Sample Output 2
1 1
Each piece has one strawberry on it.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
0,1,2 からなる長さ N の数列 A=(A_1,A_2,\dots,A_N) と、
M, E, X からなる長さ N の文字列 S=S_1S_2\dots S_N が与えられます。
1 \leq i < j < k \leq N かつ S_iS_jS_k= MEX を満たす全ての整数の組 (i,j,k) に対する \text{mex}(A_i,A_j,A_k)
の総和を求めてください。
ここで、\text{mex}(A_i,A_j,A_k) は A_i,A_j,A_k のいずれとも一致しない最小の非負整数を意味します。
制約
- 3\leq N \leq 2\times 10^5
- N は整数
- A_i \in \lbrace 0,1,2\rbrace
- S は
M,E,Xからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N S
出力
答えを整数として出力せよ。
入力例 1
4 1 1 0 2 MEEX
出力例 1
3
S_iS_jS_k = MEX となる i,j,k\ (1 \leq i < j < k \leq N) の組は (i,j,k)=(1,2,4),(1,3,4) の 2 つです。
\text{mex}(A_1,A_2,A_4)=\text{mex}(1,1,2)=0,\text{mex}(A_1,A_3,A_4)=\text{mex}(1,0,2)=3 より答えは 0+3=3 です。
入力例 2
3 0 0 0 XXX
出力例 2
0
入力例 3
15 1 1 2 0 0 2 0 2 0 0 0 0 0 2 2 EXMMXXXEMEXEXMM
出力例 3
13
Score : 475 points
Problem Statement
You are given a length-N sequence A=(A_1,A_2,\dots,A_N) consisting of 0, 1, and 2,
and a length-N string S=S_1S_2\dots S_N consisting of M, E, and X.
Find the sum of
\text{mex}(A_i,A_j,A_k) over all tuples of integers (i,j,k) such that 1 \leq i < j < k \leq N and S_iS_jS_k= MEX.
Here, \text{mex}(A_i,A_j,A_k) denotes the minimum non-negative integer that equals neither A_i,A_j, nor A_k.
Constraints
- 3\leq N \leq 2\times 10^5
- N is an integer.
- A_i \in \lbrace 0,1,2\rbrace
- S is a string of length N consisting of
M,E, andX.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N S
Output
Print the answer as an integer.
Sample Input 1
4 1 1 0 2 MEEX
Sample Output 1
3
The tuples (i,j,k)\ (1 \leq i < j < k \leq N) such that S_iS_jS_k = MEX are the following two: (i,j,k)=(1,2,4),(1,3,4).
Since \text{mex}(A_1,A_2,A_4)=\text{mex}(1,1,2)=0 and \text{mex}(A_1,A_3,A_4)=\text{mex}(1,0,2)=3, the answer is 0+3=3.
Sample Input 2
3 0 0 0 XXX
Sample Output 2
0
Sample Input 3
15 1 1 2 0 0 2 0 2 0 0 0 0 0 2 2 EXMMXXXEMEXEXMM
Sample Output 3
13
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
以下のいずれかの条件を満たす文字列を正しい括弧列と定義します。
- 空文字列
- ある正しい括弧列 A が存在して、
(, A,)をこの順に連結した文字列 - ある空でない正しい括弧列 A, B が存在して、A, B をこの順に連結した文字列
( と ) のみからなる長さ N の文字列 S があります。
Q 個のクエリ \text{Query}_1,\text{Query}_2,\ldots,\text{Query}_Q が与えられるので、順番に処理してください。クエリには 2 種類があり、入力形式とクエリの内容は以下の通りです。
1 l r: S の l 文字目と r 文字目を入れ替える。2 l r: S の l 文字目から r 文字目までの連続部分文字列が正しい括弧列であるか判定する。
制約
- 1 \leq N,Q \leq 2 \times 10^5
- S は
(と)のみからなる長さ N の文字列 - 1 \leq l < r \leq N
- N,Q,l,r はいずれも整数
- 各クエリは
1 l r、2 l rのいずれかの形式で与えられる。 2 l rの形式のクエリは 1 つ以上与えられる。
入力
入力は以下の形式で標準入力から与えられる。
N Q
S
\text{Query}_1
\text{Query}_2
\vdots
\text{Query}_Q
出力
2 l r の形式の各クエリに対して、連続部分文字列が正しい括弧列である場合 Yes、そうでない場合 No と出力し、改行せよ。
入力例 1
5 3 (())( 2 1 4 2 1 2 2 4 5
出力例 1
Yes No No
1 つ目のクエリにおいて、(()) は正しい括弧列です。
2 つ目のクエリにおいて、(( は正しい括弧列ではありません。
3 つ目のクエリにおいて、)( は正しい括弧列ではありません。
入力例 2
5 3 (())( 2 1 4 1 1 4 2 1 4
出力例 2
Yes No
1 つ目のクエリにおいて、(()) は正しい括弧列です。
2 つ目のクエリによって、S は )()(( となります。
3 つ目のクエリにおいて、)()( は正しい括弧列ではありません。
入力例 3
8 8 (()(())) 2 2 7 2 2 8 1 2 5 2 3 4 1 3 4 1 3 5 1 1 4 1 6 8
出力例 3
Yes No No
Score : 500 points
Problem Statement
Let us define a correct parenthesis sequence as a string that satisfies one of the following conditions.
- It is an empty string.
- It is a concatenation of
(, A,), in this order, for some correct parenthesis sequence A. - It is a concatenation of A, B, in this order, for some correct parenthesis sequences A and B.
We have a string S of length N consisting of ( and ).
Given Q queries \text{Query}_1,\text{Query}_2,\ldots,\text{Query}_Q, process them in order. There are two kinds of queries, with the following formats and content.
1 l r: Swap the l-th and r-th characters of S.2 l r: Determine whether the contiguous substring from the l-th through the r-th character is a correct parenthesis sequence.
Constraints
- 1 \leq N,Q \leq 2 \times 10^5
- S is a string of length N consisting of
(and). - 1 \leq l < r \leq N
- N,Q,l,r are all integers.
- Each query is in the format
1 l ror2 l r. - There is at least one query in the format
2 l r.
Input
Input is given from Standard Input in the following format:
N Q
S
\text{Query}_1
\text{Query}_2
\vdots
\text{Query}_Q
Output
For each query in the format 2 l r, print Yes if the contiguous substring is a correct parenthesis sequence, and No otherwise, followed by a newline.
Sample Input 1
5 3 (())( 2 1 4 2 1 2 2 4 5
Sample Output 1
Yes No No
In the first query, (()) is a correct parenthesis sequence.
In the second query, (( is not a correct parenthesis sequence.
In the third query, )( is not a correct parenthesis sequence.
Sample Input 2
5 3 (())( 2 1 4 1 1 4 2 1 4
Sample Output 2
Yes No
In the first query, (()) is a correct parenthesis sequence.
In the second query, S becomes )()((.
In the third query, )()( is not a correct parenthesis sequence.
Sample Input 3
8 8 (()(())) 2 2 7 2 2 8 1 2 5 2 3 4 1 3 4 1 3 5 1 1 4 1 6 8
Sample Output 3
Yes No No