C - Yellow and Red Card

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

1 から N までの番号がついた N 人の選手がサッカーの試合をします。
選手が反則を犯したとき、その選手には イエローカードレッドカード のどちらかが提示されます。
以下の条件のうち一方を満たした選手は 退場処分 と呼ばれるペナルティを受けます。

  • イエローカードを累計 2 回提示される。
  • レッドカードを提示される。

なお、退場処分を受けた選手にそれ以降カードが提示されることはありません。

あなたはこの試合を観戦します。はじめ、すべての選手はカードを 1 回も提示されていません。
Q 個のイベントが発生するので、イベントで聞かれる質問に正しく答えてください。
イベントは 3 種類あり、c x (c1, 2, 3 のいずれか) という形式で入力から与えられます。イベントの説明は次の通りです。

  • 1 x : 選手 x にイエローカードが提示される。
  • 2 x : 選手 x にレッドカードが提示される。
  • 3 x : あなたは選手 x が退場処分を受けたかを質問される。選手 x が退場処分を受けていれば Yes と、そうでなければ No と答える。

制約

  • 1 \leq N \leq 100
  • 1 \leq Q \leq 100
  • 全てのイベントにおいて 1 \leq x \leq N
  • 3 種類目のイベントは少なくとも 1 個以上存在する
  • すでに退場処分を受けた選手にカードが提示されることはない
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。ただし、\text{event}_ii 番目に発生するイベントを意味する。

N Q
\text{event}_1
\text{event}_2
\vdots
\text{event}_Q

イベントは次の 3 つのいずれかの形式で入力される。

1 x
2 x
3 x

出力

入力で与えられる 3 種類目のイベントの個数を X として、X 行出力せよ。
i 行目には、3 種類目のイベントのうち i 番目のもので聞かれる質問について、選手 x が退場処分を受けていれば Yes を、そうでなければ No を出力せよ。


入力例 1

3 9
3 1
3 2
1 2
2 1
3 1
3 2
1 2
3 2
3 3

出力例 1

No
No
Yes
No
Yes
No

イベントを時系列順にすべて説明すると次の通りです。

1 番目のイベントでは、あなたは選手 1 が退場処分を受けたかを質問されます。選手 1 は退場処分を受けていないので No を出力します。
2 番目のイベントでは、あなたは選手 2 が退場処分を受けたかを質問されます。選手 2 は退場処分を受けていないので No を出力します。
3 番目のイベントでは、選手 2 にイエローカードが提示されます。
4 番目のイベントでは、選手 1 にレッドカードが提示されます。選手 1 は退場処分を受けます。
5 番目のイベントでは、あなたは選手 1 が退場処分を受けたかを質問されます。選手 1 は退場処分を受けたので Yes を出力します。
6 番目のイベントでは、あなたは選手 2 が退場処分を受けたかを質問されます。選手 2 は退場処分を受けていないので No を出力します。
7 番目のイベントでは、選手 2 にイエローカードが提示されます。選手 2 は退場処分を受けます。
8 番目のイベントでは、あなたは選手 2 が退場処分を受けたかを質問されます。選手 2 は退場処分を受けたので Yes を出力します。
9 番目のイベントでは、あなたは選手 3 が退場処分を受けたかを質問されます。選手 3 は退場処分を受けていないので No を出力します。

Score : 200 points

Problem Statement

N players numbered 1 to N will play a soccer game.
When a player commits an offense, that player will receive a yellow card or a red card.
A player who satisfies one of the following conditions will be removed from the game.

  • Accumulates two yellow cards.
  • Receives a red card.

Once a player is removed, that player will no longer receive any cards.

You will watch this game. Initially, the players have not received any cards.
There will be Q events. Correctly answer the questions asked in the events.
There are three kinds of events, which are given in the format c x from the input, where c is 1, 2, or 3. The events are as follows.

  • 1 x: Player x receives a yellow card.
  • 2 x: Player x receives a red card.
  • 3 x: You are asked whether player x has been removed from the game. Answer Yes or No.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq Q \leq 100
  • 1 \leq x \leq N in all events.
  • There is at least one event of the third kind.
  • A player who has been removed will no longer receive any cards.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format, where \text{event}_i denotes the i-th event.

N Q
\text{event}_1
\text{event}_2
\vdots
\text{event}_Q

Each event is in one of the following formats:

1 x
2 x
3 x

Output

Print X lines, where X is the number of events of the third kind in the input.
The i-th line should contain Yes if, for the i-th event of the third kind, player x has been removed from the game, and No otherwise.


Sample Input 1

3 9
3 1
3 2
1 2
2 1
3 1
3 2
1 2
3 2
3 3

Sample Output 1

No
No
Yes
No
Yes
No

Here are all the events in chronological order.

In the 1-st event, you are asked whether player 1 has been removed from the game. Player 1 has not been removed, so you should print No.
In the 2-nd event, you are asked whether player 2 has been removed from the game. Player 2 has not been removed, so you should print No.
In the 3-rd event, player 2 receives a yellow card.
In the 4-th event, player 1 receives a red card and is removed from the game.
In the 5-th event, you are asked whether player 1 has been removed from the game. Player 1 has been removed, so you should print Yes.
In the 6-th event, you are asked whether player 2 has been removed from the game. Player 2 has not been removed, so you should print No.
In the 7-th event, player 2 receives a yellow card and is removed from the game.
In the 8-th event, you are asked whether player 2 has been removed from the game. Player 2 has been removed, so you should print Yes.
In the 9-th event, you are asked whether player 3 has been removed from the game. Player 3 has not been removed, so you should print No.

D - String Shifting

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

空でない文字列に対し、先頭の文字を末尾に移動する操作を左シフト、末尾の文字を先頭に移動する操作を右シフトと呼びます。

例えば、abcde に対して左シフトを 1 回行うと bcdea となり、右シフトを 2 回行うと deabc となります。

英小文字からなる空でない文字列 S が与えられます。S に対し左シフトと右シフトをそれぞれ好きな回数(0 回でもよい)行って得られる文字列のうち、辞書順で最小のものと最大のものを求めてください。

辞書順とは?

辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、英小文字からなる相異なる文字列 S, T の大小を判定するアルゴリズムを以下に説明します。

以下では「 Si 文字目の文字」を S_i のように表します。また、 ST より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。

  1. S, T のうち長さが大きくない方の文字列の長さを L とします。i=1,2,\dots,L に対して S_iT_i が一致するか調べます。
  2. S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_jT_j を比較して、S_jT_j よりアルファベット順で小さい場合は S \lt T 、そうでない場合は S \gt T と決定して、アルゴリズムを終了します。
  3. S_i \neq T_i である i が存在しない場合、ST の長さを比較して、ST より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。

制約

  • S は英小文字からなる。
  • S の長さは 1 以上 1000 以下である。

入力

入力は以下の形式で標準入力から与えられる。

S

出力

2 行にわたって出力せよ。S に対し左シフトと右シフトをそれぞれ好きな回数(0 回でもよい)行って得られる文字列のうち、辞書順で最小のものと最大のものをそれぞれ S_{\min}, S_{\max} とおいたとき、1 行目には S_{\min} を、2 行目には S_{\max} を出力せよ。


入力例 1

aaba

出力例 1

aaab
baaa

操作によって、aaab , aaba , abaa , baaa4 通りの文字列を得ることができます。 これらのうち辞書順で最小、最大のものはそれぞれ aaab , baaa です。


入力例 2

z

出力例 2

z
z

どのように操作を行っても、得られる文字列は z のみです。


入力例 3

abracadabra

出力例 3

aabracadabr
racadabraab

Score : 200 points

Problem Statement

On a non-empty string, a left shift moves the first character to the end of the string, and a right shift moves the last character to the beginning of the string.

For example, a left shift on abcde results in bcdea, and two right shifts on abcde result in deabc.

You are given a non-empty S consisting of lowercase English letters. Among the strings that can be obtained by performing zero or more left shifts and zero or more right shifts on S, find the lexicographically smallest string and the lexicographically largest string.

What is the lexicographical order?

Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. As a more formal definition, here is the algorithm to determine the lexicographical order between different strings S and T.

Below, let S_i denote the i-th character of S. Also, if S is lexicographically smaller than T, we will denote that fact as S \lt T; if S is lexicographically larger than T, we will denote that fact as S \gt T.

  1. Let L be the smaller of the lengths of S and T. For each i=1,2,\dots,L, we check whether S_i and T_i are the same.
  2. If there is an i such that S_i \neq T_i, let j be the smallest such i. Then, we compare S_j and T_j. If S_j comes earlier than T_j in alphabetical order, we determine that S \lt T and quit; if S_j comes later than T_j, we determine that S \gt T and quit.
  3. If there is no i such that S_i \neq T_i, we compare the lengths of S and T. If S is shorter than T, we determine that S \lt T and quit; if S is longer than T, we determine that S \gt T and quit.

Constraints

  • S consists of lowercase English letters.
  • The length of S is between 1 and 1000 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Print two lines. The first line should contain S_{\min}, and the second line should contain S_{\max}. Here, S_{\min} and S_{\max} are respectively the lexicographically smallest and largest strings obtained by performing zero or more left shifts and zero or more right shifts on S.


Sample Input 1

aaba

Sample Output 1

aaab
baaa

By performing shifts, we can obtain four strings: aaab, aaba, abaa, baaa. The lexicographically smallest and largest among them are aaab and baaa, respectively.


Sample Input 2

z

Sample Output 2

z
z

Any sequence of operations results in z.


Sample Input 3

abracadabra

Sample Output 3

aabracadabr
racadabraab
E - Count ABC Again

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 350

問題文

長さ N の文字列 S が与えられます。クエリが Q 個与えられるので、順番に処理してください。

i 番目のクエリは以下の通りです。

  • 整数 X_i と文字 C_i が与えられるので、 SX_i 番目の文字を C_i に置き換える。その後、 S に文字列 ABC が部分文字列として何箇所含まれるかを出力する。

ここで、 S部分文字列 とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。
例えば、ababc の部分文字列ですが、acabc の部分文字列ではありません。

制約

  • 3\le N\le 2\times 10^5
  • 1\le Q\le 2\times 10^5
  • S は英大文字からなる長さ N の文字列
  • 1\le X_i\le N
  • C_i は英大文字

入力

入力は以下の形式で標準入力から与えられる。

N Q
S
X_1 C_1
X_2 C_2
\vdots
X_Q C_Q

出力

Q 行出力せよ。 i 行目 (1\le i\le Q) には i 番目のクエリに対する答えを出力せよ。


入力例 1

7 4
ABCDABC
4 B
3 A
5 C
4 G

出力例 1

2
1
1
0

各クエリを処理した後の S は次のようになります。

  • 1 つ目のクエリを処理後: S= ABCBABC となる。この中に ABC は部分文字列として 2 回登場する。
  • 2 つ目のクエリを処理後: S= ABABABC となる。この中に ABC は部分文字列として 1 回登場する。
  • 3 つ目のクエリを処理後: S= ABABCBC となる。この中に ABC は部分文字列として 1 回登場する。
  • 4 つ目のクエリを処理後: S= ABAGCBC となる。この中に ABC は部分文字列として 0 回登場する。

入力例 2

3 3
ABC
1 A
2 B
3 C

出力例 2

1
1
1

クエリの処理前と処理後で S が変わらない場合もあります。


入力例 3

15 10
BBCCBCACCBACACA
9 C
11 B
5 B
11 B
4 A
8 C
8 B
5 B
7 B
14 B

出力例 3

0
0
0
0
1
1
2
2
1
1

Score : 350 points

Problem Statement

You are given a string S of length N. You are also given Q queries, which you should process in order.

The i-th query is as follows:

  • Given an integer X_i and a character C_i, replace the X_i-th character of S with C_i. Then, print the number of times the string ABC appears as a substring in S.

Here, a substring of S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S.
For example, ab is a substring of abc, but ac is not a substring of abc.

Constraints

  • 3 \le N \le 2 \times 10^5
  • 1 \le Q \le 2 \times 10^5
  • S is a string of length N consisting of uppercase English letters.
  • 1 \le X_i \le N
  • C_i is an uppercase English letter.

Input

The input is given from Standard Input in the following format:

N Q
S
X_1 C_1
X_2 C_2
\vdots
X_Q C_Q

Output

Print Q lines. The i-th line (1 \le i \le Q) should contain the answer to the i-th query.


Sample Input 1

7 4
ABCDABC
4 B
3 A
5 C
4 G

Sample Output 1

2
1
1
0

After processing each query, S becomes as follows.

  • After the first query: S= ABCBABC. In this string, ABC appears twice as a substring.
  • After the second query: S= ABABABC. In this string, ABC appears once as a substring.
  • After the third query: S= ABABCBC. In this string, ABC appears once as a substring.
  • After the fourth query: S= ABAGCBC. In this string, ABC appears zero times as a substring.

Sample Input 2

3 3
ABC
1 A
2 B
3 C

Sample Output 2

1
1
1

There are cases where S does not change through processing a query.


Sample Input 3

15 10
BBCCBCACCBACACA
9 C
11 B
5 B
11 B
4 A
8 C
8 B
5 B
7 B
14 B

Sample Output 3

0
0
0
0
1
1
2
2
1
1
F - Final Day

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

N 人の生徒が 4 日間にわたる試験を受けています。

それぞれの日に行われる試験は 300 点満点です。すなわち、4 日間を通した試験の満点は 1200 点です。

現在 3 日目までの試験が終わり、これから 4 日目の試験が行われようとしています。i \, (1 \leq i \leq N) 番目の生徒は j \, (1 \leq j \leq 3) 日目の試験で P_{i, j} 点獲得しました。

それぞれの生徒について、4 日目の試験後に上位 K 位以内に入っていることがあり得るかどうか判定してください。
ただし、4 日目の試験後の生徒の順位は、その生徒よりも 4 日間の合計点が高い生徒の人数に 1 を加えた値として定めます。

制約

  • 1 \leq K \leq N \leq 10^5
  • 0 \leq P_{i, j} \leq 300 \, (1 \leq i \leq N, 1 \leq j \leq 3)
  • 入力は全て整数である。

入力

入力は以下の形式で標準入力から与えられる。

N K
P_{1,1} P_{1,2} P_{1,3}
\vdots
P_{N,1} P_{N,2} P_{N,3}

出力

N 行出力せよ。i \, (1 \leq i \leq N) 行目には、i 番目の生徒が 4 日目の試験後に上位 K 位以内に入っていることがあり得るならば Yes と、そうでないならば No と出力せよ。


入力例 1

3 1
178 205 132
112 220 96
36 64 20

出力例 1

Yes
Yes
No

4 日目に全員が 100 点を取ると、1 番目の生徒が 1 位になります。 4 日目に 2 番目の生徒が 100 点を取り、それ以外の生徒が 0 点を取ると、2 番目の生徒が 1 位になります。 3 番目の生徒が 1 位になることはあり得ません。


入力例 2

2 1
300 300 300
200 200 200

出力例 2

Yes
Yes

入力例 3

4 2
127 235 78
192 134 298
28 56 42
96 120 250

出力例 3

Yes
Yes
No
Yes

Score : 300 points

Problem Statement

N students are taking a 4-day exam.

There is a 300-point test on each day, for a total of 1200 points.

The first three days of the exam are already over, and the fourth day is now about to begin. The i-th student (1 \leq i \leq N) got P_{i, j} points on the j-th day (1 \leq j \leq 3).

For each student, determine whether it is possible that he/she is ranked in the top K after the fourth day.
Here, the rank of a student after the fourth day is defined as the number of students whose total scores over the four days are higher than that of the student, plus 1.

Constraints

  • 1 \leq K \leq N \leq 10^5
  • 0 \leq P_{i, j} \leq 300 \, (1 \leq i \leq N, 1 \leq j \leq 3)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_{1,1} P_{1,2} P_{1,3}
\vdots
P_{N,1} P_{N,2} P_{N,3}

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain Yes if it is possible that the i-th student is ranked in the top K after the fourth day, and No otherwise.


Sample Input 1

3 1
178 205 132
112 220 96
36 64 20

Sample Output 1

Yes
Yes
No

If every student scores 100 on the fourth day, the 1-st student will rank 1-st.
If the 2-nd student scores 100 and the other students score 0 on the fourth day, the 2-nd student will rank 1-st.
The 3-rd student will never rank 1-st.


Sample Input 2

2 1
300 300 300
200 200 200

Sample Output 2

Yes
Yes

Sample Input 3

4 2
127 235 78
192 134 298
28 56 42
96 120 250

Sample Output 3

Yes
Yes
No
Yes
G - Magical Cookies

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

H \times W 枚のクッキーが HW 列に並んでいます。
上から i 行目・左から j 列目のクッキーの色は英小文字 c_{i,j} で表されます。

これから、以下の手続きを行います。

1. 各行に対して次の操作を行う : その行に 2 枚以上のクッキーが残っており、それらの色がすべて同じならば、それらに印をつける。

2. 各列に対して次の操作を行う : その列に 2 枚以上のクッキーが残っており、それらの色がすべて同じならば、それらに印をつける。

3. 印のついたクッキーがあればそれらをすべて取り除いて 1. に戻り、なければ手続きを終了する。

手続きを終了した時点で残っているクッキーの枚数を求めてください。

制約

  • 2 \leq H, W \leq 2000
  • c_{i,j} は英小文字である

入力

入力は以下の形式で標準入力から与えられる。

H W
c_{1,1}c_{1,2} \ldots c_{1,W}
c_{2,1}c_{2,2} \ldots c_{2,W}
\vdots
c_{H,1}c_{H,2} \ldots c_{H,W}

出力

答えを出力せよ。


入力例 1

4 3
aaa
aaa
abc
abd

出力例 1

2

以下で示す順で手続きを行います。

  • 1. により、1, 2 行目のクッキーに印をつける。
  • 2. により、1 列目のクッキーに印をつける。
  • 3. により、印を付けたクッキーを取り除く。

この時点でクッキーは以下のようになっています。ただし、クッキーを取り除いた箇所は . で表しています。

...
...
.bc
.bd
  • 1. により、何もしない。
  • 2. により、2 列目のクッキーに印をつける。
  • 3. により、印を付けたクッキーを取り除く。

この時点でクッキーは以下のようになっています。ただし、クッキーを取り除いた箇所は . で表しています。

...
...
..c
..d
  • 1. により、何もしない。
  • 2. により、何もしない。
  • 3. により、印がついているクッキーが存在しないので手続きを終了する。

最終的に残っているクッキーの枚数は 2 枚です。


入力例 2

2 5
aaaaa
abcde

出力例 2

4

入力例 3

3 3
ooo
ooo
ooo

出力例 3

0

Score : 400 points

Problem Statement

There are H \times W cookies in H rows and W columns.
The color of the cookie at the i-row from the top and j-th column from the left is represented by a lowercase English letter c_{i,j}.

We will perform the following procedure.

1. For each row, perform the following operation: if there are two or more cookies remaining in the row and they all have the same color, mark them.

2. For each column, perform the following operation: if there are two or more cookies remaining in the column and they all have the same color, mark them.

3. If there are any marked cookies, remove them all and return to 1; otherwise, terminate the procedure.

Find the number of cookies remaining at the end of the procedure.

Constraints

  • 2 \leq H, W \leq 2000
  • c_{i,j} is a lowercase English letter.

Input

The input is given from Standard Input in the following format:

H W
c_{1,1}c_{1,2} \ldots c_{1,W}
c_{2,1}c_{2,2} \ldots c_{2,W}
\vdots
c_{H,1}c_{H,2} \ldots c_{H,W}

Output

Print the answer.


Sample Input 1

4 3
aaa
aaa
abc
abd

Sample Output 1

2

The procedure is performed as follows.

  • 1. Mark the cookies in the first and second rows.
  • 2. Mark the cookies in the first column.
  • 3. Remove the marked cookies.

At this point, the cookies look like the following, where . indicates a position where the cookie has been removed.

...
...
.bc
.bd
  • 1. Do nothing.
  • 2. Mark the cookies in the second column.
  • 3. Remove the marked cookies.

At this point, the cookies look like the following, where . indicates a position where the cookie has been removed.

...
...
..c
..d
  • 1. Do nothing.
  • 2. Do nothing.
  • 3. No cookies are marked, so terminate the procedure.

The final number of cookies remaining is 2.


Sample Input 2

2 5
aaaaa
abcde

Sample Output 2

4

Sample Input 3

3 3
ooo
ooo
ooo

Sample Output 3

0