Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
横一列に 4 つのマスが並んでいます。
各文字が 0 または 1 である長さ 4 の文字列 S が与えられます。
S の i 文字目が 1 であるとき、左から i 番目のマスには 1 人の人がおり、
S の i 文字目が 0 であるとき、左から i 番目のマスには人がいません。
全ての人が一斉に、1 つ右隣のマスへ移動します。この移動により、もともと右端のマスにいた人は消えます。
移動後の各マスに人がいるかどうかを、S と同様のルールで文字列として出力してください。
制約
- S は
0,1のみからなる長さ 4 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
長さ 4 の文字列であって、移動後、左から i 番目のマスに人がいるならば i 文字目が 1、いないならば 0 であるようなものを出力せよ。
入力例 1
1011
出力例 1
0101
移動により、左から 1 番目のマスにいた人は左から 2 番目のマスに、
左から 3 番目のマスにいた人は左から 4 番目のマスに移動し、
左から 4 番目のマス(右端のマス)にいた人は消えます。
入力例 2
0000
出力例 2
0000
入力例 3
1111
出力例 3
0111
Score : 100 points
Problem Statement
There are 4 squares lined up horizontally.
You are given a string S of length 4 consisting of 0 and 1.
If the i-th character of S is 1, there is a person in the i-th square from the left;
if the i-th character of S is 0, there is no person in the i-th square from the left.
Now, everyone will move to the next square to the right simultaneously. By this move, the person who was originally in the rightmost square will disappear.
Determine if there will be a person in each square after the move. Print the result as a string in the same format as S. (See also Sample Input / Output for clarity.)
Constraints
- S is a string of length 4 consisting of
0and1.
Input
Input is given from Standard Input in the following format:
S
Output
Print a string of length 4 such that the i-th character is 1 if there will be a person in the i-th square from the left after the move, and 0 otherwise.
Sample Input 1
1011
Sample Output 1
0101
After the move, the person who was originally in the 1-st square will move to the 2-nd square,
the person in the 3-rd square to the 4-th square,
and the person in the 4-th square will disappear.
Sample Input 2
0000
Sample Output 2
0000
Sample Input 3
1111
Sample Output 3
0111
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
消毒液の入ったボトルがあり、その消毒液によってちょうど M 本の手を消毒することができます。
N 人の宇宙人が順に手の消毒を行いに来ます。
i 人目 (1\leq i\leq N) の宇宙人は H_i 本の手を持っており、それぞれ自身のすべての手を 1 回ずつ消毒したいと考えています。
何人目の宇宙人までがすべての手を消毒できるか求めてください。
ただし、ある宇宙人が消毒を始める時点で、自身のすべての手を消毒する分の消毒液が残っていなかったとしても、その宇宙人はその消毒液を使い切ってしまうものとします。
制約
- 1\leq N,M\leq 100
- 1\leq H_i\leq 100
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M H_1 H_2 \ldots H_N
出力
何人目の宇宙人までが自身のすべての手を消毒できるか出力せよ。
入力例 1
5 10 2 3 2 5 3
出力例 1
3
次の手順で宇宙人は自身の手を消毒します。
- 1 人目の宇宙人は自身の 2 本の手を消毒します。残りの消毒液によって、10-2=8 本の手を消毒できます。
- 2 人目の宇宙人は自身の 3 本の手を消毒します。残りの消毒液によって、8-3=5 本の手を消毒できます。
- 3 人目の宇宙人は自身の 2 本の手を消毒します。残りの消毒液によって、5-2=3 本の手を消毒できます。
- 4 人目の宇宙人は 5 本の手を持っていますが、消毒液は 3 本分しかないため消毒液を使い切り、かつ自身のすべての手を消毒できません。
よって、3 人目の宇宙人までが自身のすべての手を消毒できるため、3 を出力します。
入力例 2
5 10 2 3 2 3 5
出力例 2
4
入力例 3
1 5 1
出力例 3
1
すべての宇宙人が自身の手を消毒することができます。
Score : 100 points
Problem Statement
There is a bottle of disinfectant that can disinfect exactly M hands.
N aliens come one by one to disinfect their hands.
The i-th alien (1 \leq i \leq N) has H_i hands and wants to disinfect all of their hands once.
Determine how many aliens can disinfect all of their hands.
Here, even if there is not enough disinfectant left for an alien to disinfect all of their hands when they start, they will use up the remaining disinfectant.
Constraints
- 1 \leq N, M \leq 100
- 1 \leq H_i \leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M H_1 H_2 \ldots H_N
Output
Print the number of aliens who can disinfect all of their hands.
Sample Input 1
5 10 2 3 2 5 3
Sample Output 1
3
The aliens disinfect their hands in the following steps:
- The first alien disinfects their two hands. The remaining disinfectant can disinfect 10-2=8 hands.
- The second alien disinfects their three hands. The remaining disinfectant can disinfect 8-3=5 hands.
- The third alien disinfects their two hands. The remaining disinfectant can disinfect 5-2=3 hands.
- The fourth alien has five hands, but there is only enough disinfectant for three hands, so they use up the disinfectant without disinfecting all of their hands.
Thus, the first three aliens can disinfect all of their hands, so print 3.
Sample Input 2
5 10 2 3 2 3 5
Sample Output 2
4
Sample Input 3
1 5 1
Sample Output 3
1
All aliens can disinfect their hands.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
1,\ldots,N の番号のついた N 人の選手がゲームを行いました。選手 i のスコアは A_i であり、スコアが小さい方が上位になります。
ブービー賞に該当する選手、すなわち、下位から 2 番目の選手の番号を求めてください。
制約
- 2 \leq N \leq 2\times 10^5
- 1 \leq A_i \leq 10^9
- A_i は相異なる
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N A_1 \ldots A_N
出力
答えを出力せよ。
入力例 1
6 1 123 12345 12 1234 123456
出力例 1
3
6 人中 5 位になるのは、選手 3 です。
入力例 2
5 3 1 4 15 9
出力例 2
5
Score : 200 points
Problem Statement
N players, who are numbered 1, \ldots, N, have played a game. Player i has scored A_i, and a player with a smaller score ranks higher.
The player who ranks the second lowest will receive a booby prize. Who is this player? Answer with an integer representing the player.
Constraints
- 2 \leq N \leq 2\times 10^5
- 1 \leq A_i \leq 10^9
- A_i are distinct.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A_1 \ldots A_N
Output
Print the answer.
Sample Input 1
6 1 123 12345 12 1234 123456
Sample Output 1
3
It is Player 3 who ranks fifth among the six players.
Sample Input 2
5 3 1 4 15 9
Sample Output 2
5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
正の整数 X に対して、X を 2 進表記したときに 末尾 に連続する 0 の個数(の最大値)を \text{ctz}(X) で表します。
ただし、X を 2 進表記したとき末尾が 1 ならば \text{ctz}(X)=0 です。
正の整数 N が与えられるので、\text{ctz}(N) を出力してください。
制約
- 1\leq N\leq 10^9
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
\text{ctz}(N) を出力せよ。
入力例 1
2024
出力例 1
3
2024 を 2 進表記すると 11111101000 であり、末尾から 0 が 3 個連続しているため、\text{ctz}(2024)=3 です。
よって、3 を出力します。
入力例 2
18
出力例 2
1
18 を 2 進表記すると 10010 であるため、\text{ctz}(18)=1 です。
0 は末尾から連続する必要があることに注意してください。
入力例 3
5
出力例 3
0
Score: 200 points
Problem Statement
For a positive integer X, let \text{ctz}(X) be the (maximal) number of consecutive zeros at the end of the binary notation of X.
If the binary notation of X ends with a 1, then \text{ctz}(X)=0.
You are given a positive integer N. Print \text{ctz}(N).
Constraints
- 1\leq N\leq 10^9
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print \text{ctz}(N).
Sample Input 1
2024
Sample Output 1
3
2024 is 11111101000 in binary, with three consecutive 0s from the end, so \text{ctz}(2024)=3.
Thus, print 3.
Sample Input 2
18
Sample Output 2
1
18 is 10010 in binary, so \text{ctz}(18)=1.
Note that we count the trailing zeros.
Sample Input 3
5
Sample Output 3
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
H 行 W 列のマス目が与えられます。
以下、上から i 行目 (1\leq i\leq H) かつ左から j 列目 (1\leq j\leq W) のマスをマス (i,j) で表します。
マス目の状態は H 個の長さ W の文字列 S_1,S_2, \ldots, S_H によって以下のように表されます。
- S_i の j 文字目が
#のとき、マス (i,j) は黒く塗られている。 - S_i の j 文字目が
.のとき、マス (i,j) は白く塗られている。 - S_i の j 文字目が
?のとき、マス (i,j) は塗られていない。
高橋君はまだ塗られていないマスをそれぞれ白または黒で塗ることで、黒マス全体が長方形をなすようにしたいです。
より具体的には、ある 4 つの整数の組 (a,b,c,d) (1\leq a\leq b\leq H, 1\leq c\leq d\leq W) が存在して、次が成り立つようにしたいです。
マス (i,j) (1\leq i\leq H, 1\leq j\leq W) は、 a\leq i\leq b かつ c\leq j\leq d をみたすとき、黒く塗られている。
そうでないとき、白く塗られている。
そのようなことが可能か判定してください。
制約
- 1\leq H,W\leq 1000
- H, W は整数
- S_i は
#,.,?のみからなる長さ W の文字列 - 黒く塗られたマスが 1 つ以上存在する。
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H
出力
まだ塗られていないマスをそれぞれ白または黒で塗ることで、黒マス全体が長方形をなすようにできるならば Yes を、そうでないならば No を出力せよ。
入力例 1
3 5 .#?#. .?#?. ?...?
出力例 1
Yes
マス目は以下の状態になっています。? のマスがまだ塗られていないマスです。

マス (1,3), (2,2), (2,4) を黒く塗り、マス (3,1), (3,5) を白く塗ることで、 以下のように黒マス全体が長方形をなすようにできます。

よって、Yes を出力します。
入力例 2
3 3 ?## #.# ##?
出力例 2
No
黒マス全体が長方形をなすためには、少なくともマス (2,2) を黒く塗る必要がありますがすでに白く塗られています。
よって、黒マス全体が長方形をなすようにマス目を塗ることはできないため、No を出力します。
入力例 3
1 1 #
出力例 3
Yes
Score : 300 points
Problem Statement
You are given a grid of H rows and W columns.
Let (i,j) denote the cell at row i (1 \leq i \leq H) from the top and column j (1 \leq j \leq W) from the left.
The state of the grid is represented by H strings S_1, S_2, \ldots, S_H, each of length W, as follows:
- If the j-th character of S_i is
#, cell (i,j) is painted black. - If the j-th character of S_i is
., cell (i,j) is painted white. - If the j-th character of S_i is
?, cell (i,j) is not yet painted.
Takahashi wants to paint each not-yet-painted cell white or black so that all the black cells form a rectangle.
More precisely, he wants there to exist a quadruple of integers (a,b,c,d) (1 \leq a \leq b \leq H, 1 \leq c \leq d \leq W) such that:
For each cell (i,j) (1 \leq i \leq H, 1 \leq j \leq W), if a \leq i \leq b and c \leq j \leq d, the cell is black;
otherwise, the cell is white.
Determine whether this is possible.
Constraints
- 1 \leq H, W \leq 1000
- H and W are integers.
- Each S_i is a string of length W consisting of
#,.,?. - There is at least one cell that is already painted black.
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H
Output
If it is possible to paint all the not-yet-painted cells so that the black cells form a rectangle, print Yes; otherwise, print No.
Sample Input 1
3 5 .#?#. .?#?. ?...?
Sample Output 1
Yes
The grid is in the following state. ? indicates a cell that are not yet painted.

By painting cells (1,3), (2,2), and (2,4) black and cells (3,1) and (3,5) white, the black cells can form a rectangle as follows:

Therefore, print Yes.
Sample Input 2
3 3 ?## #.# ##?
Sample Output 2
No
To form a rectangle with all black cells, you would need to paint cell (2,2) black, but it is already painted white.
Therefore, it is impossible to make all black cells form a rectangle, so print No.
Sample Input 3
1 1 #
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君が運営する SNS「Twidai」にはユーザー 1 からユーザー N までの N 人のユーザーがいます。 Twidai では、ユーザーは別のユーザーをフォローすることや、フォローを解除することができます。
Twidai がサービスを開始してから、Q 回の操作が行われました。 i 回目 (1\leq i\leq Q) の操作は 3 つの整数 T _ i, A _ i, B _ i で表され、それぞれ次のような操作を表します。
- T _ i=1 のとき:ユーザー A _ i がユーザー B _ i をフォローしたことを表す。この操作の時点でユーザー A _ i がユーザー B _ i をフォローしている場合、ユーザーのフォロー状況に変化はない。
- T _ i=2 のとき:ユーザー A _ i がユーザー B _ i のフォローを解除したことを表す。この操作の時点でユーザー A _ i がユーザー B _ i をフォローしていない場合、ユーザーのフォロー状況に変化はない。
- T _ i=3 のとき:ユーザー A _ i とユーザー B _ i が互いにフォローしているかをチェックすることを表す。この操作の時点でユーザー A _ i がユーザー B _ i をフォローしており、かつユーザー B _ i がユーザー A _ i をフォローしているとき、このチェックに対して
Yesと答え、そうでないときこのチェックに対してNoと答える必要がある。
サービス開始時には、どのユーザーも他のユーザーをフォローしていません。
すべての T _ i=3 であるような操作に対して、i が小さいほうから順番に正しい答えを出力してください。
制約
- 2 \leq N \leq 10 ^ 9
- 1 \leq Q \leq 2\times10 ^ 5
- T _ i=1,2,3\ (1\leq i\leq Q)
- 1 \leq A _ i \leq N\ (1\leq i\leq Q)
- 1 \leq B _ i \leq N\ (1\leq i\leq Q)
- A _ i\neq B _ i\ (1\leq i\leq Q)
- T _ i=3 となる i\ (1\leq i\leq Q) が存在する
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q T _ 1 A _ 1 B _ 1 T _ 2 A _ 2 B _ 2 \vdots T _ Q A _ Q B _ Q
出力
T _ i=3 であるような i\ (1\leq i\leq Q) の個数を X として、X 行出力せよ。 j\ (1\leq j\leq X) 行目には j 番目の T _ i=3 であるような操作に対する答えを出力せよ。
入力例 1
3 9 1 1 2 3 1 2 1 2 1 3 1 2 1 2 3 1 3 2 3 1 3 2 1 2 3 1 2
出力例 1
No Yes No No
Twidai には 3 人のユーザーがいます。 9 回の操作はそれぞれ次のようになっています。
- ユーザー 1 がユーザー 2 をフォローします。そのほかにフォローしている/されているユーザーはいません。
- ユーザー 1 とユーザー 2 が互いにフォローしているかチェックします。ユーザー 1 はユーザー 2 をフォローしていますが、ユーザー 2 はユーザー 1 をフォローしていません。この操作への正しい答えは
Noです。 - ユーザー 2 がユーザー 1 をフォローします。
- ユーザー 1 とユーザー 2 が互いにフォローしているかチェックします。ユーザー 1 はユーザー 2 をフォローしており、ユーザー 2 はユーザー 1 をフォローしています。この操作への正しい答えは
Yesです。 - ユーザー 2 がユーザー 3 をフォローします。
- ユーザー 3 がユーザー 2 をフォローします。
- ユーザー 1 とユーザー 3 が互いにフォローしているかチェックします。ユーザー 1 はユーザー 3 をフォローしておらず、ユーザー 3 もユーザー 1 をフォローしていません。この操作への正しい答えは
Noです。 - ユーザー 1 がユーザー 2 のフォローを解除します。
- ユーザー 1 とユーザー 2 が互いにフォローしているかチェックします。ユーザー 2 はユーザー 1 をフォローしていますが、ユーザー 1 はユーザー 2 をフォローしていません。この操作への正しい答えは
Noです。
入力例 2
2 8 1 1 2 1 2 1 3 1 2 1 1 2 1 1 2 1 1 2 2 1 2 3 1 2
出力例 2
Yes No
同じユーザーに対して何度もフォロー操作をする場合があります。
入力例 3
10 30 3 1 6 3 5 4 1 6 1 3 1 7 3 8 4 1 1 6 2 4 3 1 6 5 1 5 6 1 1 8 1 8 1 2 3 10 1 7 6 3 5 6 1 6 7 3 6 7 1 9 5 3 8 6 3 3 8 2 6 9 1 7 1 3 10 8 2 9 2 1 10 9 2 6 10 2 6 8 3 1 6 3 1 8 2 8 5 1 9 10
出力例 3
No No No No Yes Yes No No No Yes Yes
Score : 300 points
Problem Statement
Takahashi runs an SNS "Twidai," which has N users from user 1 through user N. In Twidai, users can follow or unfollow other users.
Q operations have been performed since Twidai was launched. The i-th (1\leq i\leq Q) operation is represented by three integers T_i, A_i, and B_i, whose meanings are as follows:
- If T_i = 1: it means that user A_i follows user B_i. If user A_i is already following user B_i at the time of this operation, it does not make any change.
- If T_i = 2: it means that user A_i unfollows user B_i. If user A_i is not following user B_i at the time of this operation, it does not make any change.
- If T_i = 3: it means that you are asked to determine if users A_i and B_i are following each other. You need to print
Yesif user A_i is following user B_i and user B_i is following user A_i, andNootherwise.
When the service was launched, no users were following any users.
Print the correct answers for all operations such that T_i = 3 in ascending order of i.
Constraints
- 2 \leq N \leq 10 ^ 9
- 1 \leq Q \leq 2\times10 ^ 5
- T _ i=1,2,3\ (1\leq i\leq Q)
- 1 \leq A _ i \leq N\ (1\leq i\leq Q)
- 1 \leq B _ i \leq N\ (1\leq i\leq Q)
- A _ i\neq B _ i\ (1\leq i\leq Q)
- There exists i\ (1\leq i\leq Q) such that T _ i=3.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N Q T _ 1 A _ 1 B _ 1 T _ 2 A _ 2 B _ 2 \vdots T _ Q A _ Q B _ Q
Output
Print X lines, where X is the number of i's (1\leq i\leq Q) such that T _ i=3. The j-th (1\leq j\leq X) line should contain the answer to the j-th operation such that T _ i=3.
Sample Input 1
3 9 1 1 2 3 1 2 1 2 1 3 1 2 1 2 3 1 3 2 3 1 3 2 1 2 3 1 2
Sample Output 1
No Yes No No
Twidai has three users. The nine operations are as follows.
- User 1 follows user 2. No other users are following or followed by any users.
- Determine if users 1 and 2 are following each other. User 1 is following user 2, but user 2 is not following user 1, so
Nois the correct answer to this operation. - User 2 follows user 1.
- Determine if users 1 and 2 are following each other. User 1 is following user 2, and user 2 is following user 1, so
Yesis the correct answer to this operation. - User 2 follows user 3.
- User 3 follows user 2.
- Determine if users 1 and 3 are following each other. User 1 is not following user 3, and user 3 is not following user 1, so
Nois the correct answer to this operation. - User 1 unfollows user 2.
- Determine if users 1 and 2 are following each other. User 2 is following user 1, but user 1 is not following user 2, so
Nois the correct answer to this operation.
Sample Input 2
2 8 1 1 2 1 2 1 3 1 2 1 1 2 1 1 2 1 1 2 2 1 2 3 1 2
Sample Output 2
Yes No
A user may follow the same user multiple times.
Sample Input 3
10 30 3 1 6 3 5 4 1 6 1 3 1 7 3 8 4 1 1 6 2 4 3 1 6 5 1 5 6 1 1 8 1 8 1 2 3 10 1 7 6 3 5 6 1 6 7 3 6 7 1 9 5 3 8 6 3 3 8 2 6 9 1 7 1 3 10 8 2 9 2 1 10 9 2 6 10 2 6 8 3 1 6 3 1 8 2 8 5 1 9 10
Sample Output 3
No No No No Yes Yes No No No Yes Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
ある日、映画館を訪れた高橋君は、館内の全ての床に、最寄りの非常口の方向を示す矢印を描くことにしました。
縦 H マス、横 W マスのグリッドが与えられます。グリッドの上から i 行目、左から j 列目のマスを (i, j) と呼びます。
各マスの状態は、次のいずれかの文字 S_{i,j} で表されます。
.: 通路マス#: 壁マスE: 非常口
任意の通路マス (i, j) に対して、(i, j) と最寄りの非常口との距離 d(i, j) を次のように定義します。
- (i, j) からスタートして、上下左右に隣接する 壁マスでないマス へ移動することを繰り返して非常口に到達する際の、必要な最小の移動回数。
ここで、入力として与えられるグリッドにおいて、全ての通路マスに対し d(i, j) が定義可能であることが保証されます。すなわち、すべての通路マス (i, j) において、通路マスのみを経由することで到達可能な非常口が少なくとも 1 つ存在します。
このとき、以下の条件を満たすように、すべての通路マスに対して「上下左右いずれかの方向の矢印」を書き込んでください。
- 各通路マス (i, j) について、(i,j) にいる状態から開始して、以下の操作を d(i, j) 回行うことで非常口に到達することができる。
- 現在いるマスに書かれた矢印の方向に従って 1 マス移動する。(ただし、壁マスやグリッドの外へは移動できない。)
制約
- 2 \leq H \leq 1000
- 2 \leq W \leq 1000
- S_{i,j} は
.,#またはE - 全ての通路マス (i, j) について、そこからいずれかの非常口に到達することが可能である
- H, W は整数である
入力
入力は以下の形式で標準入力から与えられる。
H W
S_{1,1}S_{1,2}\dots S_{1,W}
S_{2,1}S_{2,2}\dots S_{2,W}
\vdots
S_{H,1}S_{H,2}\dots S_{H,W}
出力
矢印を書き込んだ後のマス (i, j) の状態を T_{i,j} とする。T_{i,j} は次のいずれかである。
^: 上矢印が書かれた通路マスv: 下矢印が書かれた通路マス<: 左矢印が書かれた通路マス>: 右矢印が書かれた通路マス#: 壁マスE: 非常口
これらを以下の形式で出力せよ。
T_{1,1}T_{1,2}\dots T_{1,W}
T_{2,1}T_{2,2}\dots T_{2,W}
\vdots
T_{H,1}T_{H,2}\dots T_{H,W}
答えが複数ある場合は、どれを出力しても正答とみなされる。
入力例 1
3 4 ...E .#.. ....
出力例 1
>>>E ^#>^ >>>^
出力例の (2, 3) について問題文の条件が成立することを確認します。(2, 3) と最寄りの非常口の距離は 2 です。そして、出力例のように書き込まれた矢印に従って移動することで 2 回の移動で非常口に到達することができます。
他の通路マスについても問題文の条件が成立することが確認できます。
入力例 2
3 2 ## ## ##
出力例 2
## ## ##
通路マスおよび非常口が存在しない場合もあります。
入力例 3
7 20 .................... ..#..#..####..#E##.. ..#..#..#..#..#..... ..E###..#..#..####.. .....#..#..E.....#.. .....#..####..####.. ....................
出力例 3
>v<<<<<>>>>>>>>v<<<< >v#^<#^^####v^#E##vv >v#^<#v^#>v#vv#^<<<< >>E###vv#>v#vv####^< >>^<<#vv#>>E<<<<<#^< >>^<<#vv####^<####^< >>^<<<<<>>>>^<<<<<^<
Score : 400 points
Problem Statement
One day, Takahashi visited a cinema and decided to draw arrows on every floor tile, each pointing toward the nearest emergency exit.
You are given a grid with H rows and W columns. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.
Each cell is represented by one of the following characters S_{i,j}:
.: corridor cell#: wall cellE: emergency exit
For any corridor cell (i, j), define d(i, j), the distance to the nearest emergency exit, as follows:
- Starting from (i, j), repeatedly move to an adjacent non-wall cell in one of the four cardinal directions (up, down, left, right) until you reach an emergency exit. d(i, j) is the minimum number of moves required.
It is guaranteed that d(i, j) is definable for every corridor cell in the given grid; that is, every corridor cell (i, j) has at least one emergency exit reachable by passing through only corridor cells.
Write exactly one arrow (up, down, left, or right) in every corridor cell so that the following condition holds:
- For every corridor cell (i, j), if you start at (i, j) and perform the following action d(i, j) times, you reach an emergency exit:
- Move one cell in the direction of the arrow written in your current cell. (You cannot move into a wall cell or outside the grid.)
Constraints
- 2 \le H \le 1000
- 2 \le W \le 1000
- Each S_{i,j} is
.,#, orE. - Every corridor cell (i, j) has at least one reachable emergency exit.
- H and W are integers.
Input
The input is given from Standard Input in the following format:
H W
S_{1,1}S_{1,2}\dots S_{1,W}
S_{2,1}S_{2,2}\dots S_{2,W}
\vdots
S_{H,1}S_{H,2}\dots S_{H,W}
Output
Let T_{i,j} be the state of cell (i, j) after writing the arrows. T_{i,j} is one of the following:
^: corridor cell with an upward arrowv: corridor cell with a downward arrow<: corridor cell with a leftward arrow>: corridor cell with a rightward arrow#: wall cellE: emergency exit
Output the grid in the following format:
T_{1,1}T_{1,2}\dots T_{1,W}
T_{2,1}T_{2,2}\dots T_{2,W}
\vdots
T_{H,1}T_{H,2}\dots T_{H,W}
If multiple solutions exist, any of them will be accepted.
Sample Input 1
3 4 ...E .#.. ....
Sample Output 1
>>>E ^#>^ >>>^
Let us verify the condition for (2, 3) in the sample output. The distance from (2, 3) to the nearest emergency exit is 2, and following the arrows written in the sample output leads to an emergency exit in two moves.
The condition can be verified for every other corridor cell as well.
Sample Input 2
3 2 ## ## ##
Sample Output 2
## ## ##
There may be cases with no corridor cells or emergency exits.
Sample Input 3
7 20 .................... ..#..#..####..#E##.. ..#..#..#..#..#..... ..E###..#..#..####.. .....#..#..E.....#.. .....#..####..####.. ....................
Sample Output 3
>v<<<<<>>>>>>>>v<<<< >v#^<#^^####v^#E##vv >v#^<#v^#>v#vv#^<<<< >>E###vv#>v#vv####^< >>^<<#vv#>>E<<<<<#^< >>^<<#vv####^<####^< >>^<<<<<>>>>^<<<<<^<
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
この問題は インタラクティブな問題 (あなたの作成したプログラムとジャッジシステムが入出力を介して対話を行う形式の問題)です。
1 から N の番号がついた N 頂点からなる木 G が与えられます。i 番目の辺は頂点 U_i と頂点 V_i を結んでいます。
この木 G を使って、あなたと高橋君がゲームをします。まず、あなたは先手後手を決めます。その後、先手から順に交互に以下の操作を行います。
- 1 \leq i < j \leq N を満たす整数の組 (i,j) であって、以下の条件をともに満たすものを選び、頂点 i と頂点 j を結ぶ辺を G に追加する。
- G は頂点 i と頂点 j を結ぶ辺を持たない
- 頂点 i と頂点 j を結ぶ辺を G に追加しても、奇閉路ができない
操作を行えなくなった方が負けであり、負けでない方が勝ちです。実際に高橋君とゲームをして勝ってください。
奇閉路とは?
G の頂点の列 (v_0,v_1,\ldots,v_k) が以下の条件を全て満たすとき、かつ、そのときに限りこの列を G の奇閉路といいます。
- k は奇数である。
- v_0=v_k である。
- 全ての 1\leq i \leq k に対して、v_{i-1} と v_{i} を結ぶ辺が存在する。
制約
- 2 \leq N \leq 100
- 1 \leq U_i < V_i \leq N
- 与えられるグラフは木である
- 入力は全て整数である
入出力
この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジシステムが入出力を介して対話を行う形式の問題)です。
最初に、N および G の情報を標準入力から受け取ってください。以下の形式で与えられます。
N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
次に、あなたが先手と後手のどちらを選ぶか決めてください。先手を選ぶ場合 First、後手を選ぶ場合 Second と標準出力に出力してください。
その後ゲームが始まります。
あなたの手番では、操作のために選んだ整数の組 (i,j) を、i,j の順に空白区切りで標準出力に出力してください。
i j
高橋君の手番では、2 つの整数 i,j が順に空白区切りで標準入力に与えられます。
i j
(i,j)=(-1,-1) のとき、あなたがゲームに勝利しゲームが終了したことを意味します。この場合、直ちにプログラムを終了してください。
それ以外のとき、(i,j) は高橋君が操作のために選んだ整数の組を表します。
注意点
- 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
- 対話の途中で誤った出力形式による出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。
- ゲームが終了したらただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。
入出力例
| 入力 | 出力 | 説明 |
|---|---|---|
41 22 33 4 | まず整数 N および G の情報が与えられます。 | |
First | あなたは先手を選びました。 | |
1 4 | あなたは (1,4) に対して操作を行いました。 | |
-1 -1 | 高橋君が操作を行えなくなったためゲームを終了します。ジャッジ結果は になります。 |
Score : 425 points
Problem Statement
This problem is an interactive problem (in which your program and the judge system communicate via input and output).
You are given a tree G with N vertices numbered 1 to N. The i-th edge connects vertices U_i and V_i.
You will play a game with Takahashi using this tree G. First, you decide who is first and who is second. Then, starting from the first player, take turns performing the following operation:
- Choose a pair of integers (i,j) with 1 \leq i < j \leq N that satisfies both of the following conditions, then add an edge connecting vertices i and j to G.
- G does not already have an edge connecting vertices i and j.
- Adding an edge connecting vertices i and j does not create an odd cycle.
A player who cannot perform this operation loses, and the other player wins. Play this game against Takahashi and win.
What is an odd cycle?
A sequence of vertices (v_0,v_1,\ldots,v_k) of G is called an odd cycle if and only if all of the following conditions are satisfied:
- k is odd.
- v_0=v_k.
- For every 1\leq i \leq k, there is an edge connecting v_{i-1} and v_{i}.
Constraints
- 2 \leq N \leq 100
- 1 \leq U_i < V_i \leq N
- The given graph is a tree.
- All input values are integers.
Interaction
This is an interactive problem (in which your program and the judge system communicate via input and output).
First, read N and the edges of G from Standard Input, given in the following format:
N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
Then, decide whether you go first or second. If you choose first, print First; if second, print Second.
Then, the game begins.
On your turn, output the chosen pair (i,j) by printing two integers i and j in this order, separated by a space:
i j
On Takahashi's turn, two integers i,j will be given in order, separated by a space, via Standard Input:
i j
If (i,j)=(-1,-1), it means he can no longer make a move and you win. Immediately terminate your program in this case.
Otherwise, (i,j) is the pair of integers he has chosen.
Notes
- After each output, be sure to print a newline and flush Standard Output. Otherwise, you may get TLE.
- If you produce output in an incorrect format or your program ends prematurely, the judge result is indeterminate.
- Once the game finishes, terminate your program immediately. Otherwise, the judge result is indeterminate.
Sample Interaction
| Input | Output | Explanation |
|---|---|---|
41 22 33 4 |
First, you receive N and the edges of G. | |
First |
You choose to go first. | |
1 4 |
You add an edge between vertices 1 and 4. | |
-1 -1 |
Takahashi can no longer move, so you win. The judge result will be . |
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 行 N 列のマス目があり、上から i \, (1 \leq i \leq N) 行目、左から j \, (1 \leq j \leq N) 列目のマスを (i, j) と表します。
マス (i, j) には非負整数 a_{i, j} が書かれています。
マス (i, j) にいるとき、マス (i+1, j), (i, j+1) のいずれかに移動することができます。ただし、マス目の外に出るような移動はできません。
マス (1, 1) から移動を繰り返してマス (N, N) にたどり着く方法であって、通ったマス(マス (1, 1), (N, N) を含む)に書かれた整数の排他的論理和が 0 となるようなものの総数を求めてください。
排他的論理和とは
整数 a, b の排他的論理和 a \oplus b は、以下のように定義されます。- a \oplus b を二進表記した際の 2^k \, (k \geq 0) の位の数は、a, b を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
一般に k 個の整数 p_1, \dots, p_k の排他的論理和は (\cdots ((p_1 \oplus p_2) \oplus p_3) \oplus \cdots \oplus p_k) と定義され、これは p_1, \dots, p_k の順番によらないことが証明できます。
制約
- 2 \leq N \leq 20
- 0 \leq a_{i, j} \lt 2^{30} \, (1 \leq i, j \leq N)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
a_{1, 1} \ldots a_{1, N}
\vdots
a_{N, 1} \ldots a_{N, N}
出力
答えを出力せよ。
入力例 1
3 1 5 2 7 0 5 4 2 3
出力例 1
2
次の二通りの方法が条件を満たします。
- (1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3) \rightarrow (3, 3)
- (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3)
入力例 2
2 1 2 2 1
出力例 2
0
入力例 3
10 1 0 1 0 0 1 0 0 0 1 0 0 0 1 0 1 0 1 1 0 1 0 0 0 1 0 1 0 0 0 0 1 0 0 0 1 1 0 0 1 0 0 1 1 0 1 1 0 1 0 1 0 0 0 1 0 0 1 1 0 1 1 1 0 0 0 1 1 0 0 0 1 1 0 0 1 1 0 1 0 1 0 1 1 0 0 0 0 0 0 1 0 1 1 0 0 1 1 1 0
出力例 3
24307
Score : 500 points
Problem Statement
There is a grid with N rows and N columns. We denote by (i, j) the square at the i-th (1 \leq i \leq N) row from the top and j-th (1 \leq j \leq N) column from the left.
Square (i, j) has a non-negative integer a_{i, j} written on it.
When you are at square (i, j), you can move to either square (i+1, j) or (i, j+1). Here, you are not allowed to go outside the grid.
Find the number of ways to travel from square (1, 1) to square (N, N) such that the exclusive logical sum of the integers written on the squares visited (including (1, 1) and (N, N)) is 0.
What is the exclusive logical sum?
The exclusive logical sum a \oplus b of two integers a and b is defined as follows.- The 2^k's place (k \geq 0) in the binary notation of a \oplus b is 1 if exactly one of the 2^k's places in the binary notation of a and b is 1; otherwise, it is 0.
In general, the exclusive logical sum of k integers p_1, \dots, p_k is defined as (\cdots ((p_1 \oplus p_2) \oplus p_3) \oplus \cdots \oplus p_k). We can prove that it is independent of the order of p_1, \dots, p_k.
Constraints
- 2 \leq N \leq 20
- 0 \leq a_{i, j} \lt 2^{30} \, (1 \leq i, j \leq N)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N
a_{1, 1} \ldots a_{1, N}
\vdots
a_{N, 1} \ldots a_{N, N}
Output
Print the answer.
Sample Input 1
3 1 5 2 7 0 5 4 2 3
Sample Output 1
2
The following two ways satisfy the condition:
- (1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3) \rightarrow (3, 3);
- (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3).
Sample Input 2
2 1 2 2 1
Sample Output 2
0
Sample Input 3
10 1 0 1 0 0 1 0 0 0 1 0 0 0 1 0 1 0 1 1 0 1 0 0 0 1 0 1 0 0 0 0 1 0 0 0 1 1 0 0 1 0 0 1 1 0 1 1 0 1 0 1 0 0 0 1 0 0 1 1 0 1 1 1 0 0 0 1 1 0 0 0 1 1 0 0 1 1 0 1 0 1 0 1 1 0 0 0 0 0 0 1 0 1 1 0 0 1 1 1 0
Sample Output 3
24307