実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
5 枚のカードがあり、それぞれのカードには整数 A,B,C,D,E が書かれています。
この 5 枚組は以下の条件を満たすとき、またそのときに限って、フルハウスであると呼ばれます。
- 同じ数が書かれたカード 3 枚と、別の同じ数が書かれたカード 2 枚からなる。
5 枚組がフルハウスであるか判定してください。
制約
- 1 \leq A,B,C,D,E\leq 13
- A,B,C,D,E 全てが同じ値ではない
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
A B C D E
出力
フルハウスである場合 Yes を、そうでないとき No を出力せよ。
入力例 1
1 2 1 2 1
出力例 1
Yes
1 が書かれたカード 3 枚と、2 が書かれたカード 2 枚からなるため、これはフルハウスです。
入力例 2
12 12 11 1 2
出力例 2
No
フルハウスの条件を満たしません。
Score : 100 points
Problem Statement
We have five cards with integers A, B, C, D, and E written on them, one on each card.
This set of five cards is called a Full house if and only if the following condition is satisfied:
- the set has three cards with a same number written on them, and two cards with another same number written on them.
Determine whether the set is a Full house.
Constraints
- 1 \leq A,B,C,D,E\leq 13
- Not all of A, B, C, D, and E are the same.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
A B C D E
Output
If the set is a Full house, print Yes; otherwise, print No.
Sample Input 1
1 2 1 2 1
Sample Output 1
Yes
The set has three cards with 1 written on them and two cards with 2 written on them, so it is a Full house.
Sample Input 2
12 12 11 1 2
Sample Output 2
No
The condition is not satisfied.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
以下の条件を満たす正整数 x を 321-like Number と呼びます。
- x の各桁を上から見ると狭義単調減少になっている。
- すなわち、x が d 桁の整数だとすると、 1 \le i < d を満たす全ての整数 i について以下の条件を満たす。
- ( x の上から i 桁目 ) > ( x の上から i+1 桁目 )
なお、 1 桁の正整数は必ず 321-like Number であることに注意してください。
例えば、 321,96410,1 は 321-like Number ですが、 123,2109,86411 は 321-like Number ではありません。
N が入力として与えられるので、 N が 321-like Number なら Yes 、そうでないなら No と出力してください。
制約
- 入力は全て整数
- 1 \le N \le 99999
入力
入力は以下の形式で標準入力から与えられる。
N
出力
N が 321-like Number なら Yes 、そうでないなら No と出力せよ。
入力例 1
321
出力例 1
Yes
N=321 に対して、以下が成り立ちます。
- 上から 1 桁目の 3 は上から 2 桁目の 2 より大きい。
- 上から 2 桁目の 2 は上から 3 桁目の 1 より大きい。
以上より、 321 は 321-like Number です。
入力例 2
123
出力例 2
No
N=123 について、例えば以下が成り立ちます。
- 上から 1 桁目の 1 は上から 2 桁目の 2 より大きくない。
このことから、 123 は 321-like Number ではありません。
入力例 3
1
出力例 3
Yes
入力例 4
86411
出力例 4
No
Score : 100 points
Problem Statement
A positive integer x is called a 321-like Number when it satisfies the following condition.
- The digits of x are strictly decreasing from top to bottom.
- In other words, if x has d digits, it satisfies the following for every integer i such that 1 \le i < d:
- (the i-th digit from the top of x) > (the (i+1)-th digit from the top of x).
Note that all one-digit positive integers are 321-like Numbers.
For example, 321, 96410, and 1 are 321-like Numbers, but 123, 2109, and 86411 are not.
You are given N as input. Print Yes if N is a 321-like Number, and No otherwise.
Constraints
- All input values are integers.
- 1 \le N \le 99999
Input
The input is given from Standard Input in the following format:
N
Output
Print Yes if N is a 321-like Number, and No otherwise.
Sample Input 1
321
Sample Output 1
Yes
For N=321, the following holds:
- The first digit from the top, 3, is greater than the second digit from the top, 2.
- The second digit from the top, 2, is greater than the third digit from the top, 1.
Thus, 321 is a 321-like Number.
Sample Input 2
123
Sample Output 2
No
For N=123, the following holds:
- The first digit from the top, 1, is not greater than the second digit from the top, 2.
Thus, 123 is not a 321-like Number.
Sample Input 3
1
Sample Output 3
Yes
Sample Input 4
86411
Sample Output 4
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
選挙が行われています。
N 人が投票を行い、i\,(1 \leq i \leq N) 番目の人は S_i という名前の候補者に投票しました。
得票数が最大の候補者の名前を答えてください。なお、与えられる入力では得票数が最大の候補者は一意に定まります。
制約
- 1 \leq N \leq 100
- S_i は英小文字からなる長さ 1 以上 10 以下の文字列
- N は整数
- 得票数が最大の候補者は一意に定まる
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
得票数が最大の候補者の名前を出力せよ。
入力例 1
5 snuke snuke takahashi takahashi takahashi
出力例 1
takahashi
takahashi は 3 票、snuke は 2 票獲得しました。よって takahashi を出力します。
入力例 2
5 takahashi takahashi aoki takahashi snuke
出力例 2
takahashi
入力例 3
1 a
出力例 3
a
Score : 200 points
Problem Statement
An election is taking place.
N people voted. The i-th person (1 \leq i \leq N) cast a vote to the candidate named S_i.
Find the name of the candidate who received the most votes. The given input guarantees that there is a unique candidate with the most votes.
Constraints
- 1 \leq N \leq 100
- S_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
- N is an integer.
- There is a unique candidate with the most votes.
Input
Input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print the name of the candidate who received the most votes.
Sample Input 1
5 snuke snuke takahashi takahashi takahashi
Sample Output 1
takahashi
takahashi got 3 votes, and snuke got 2, so we print takahashi.
Sample Input 2
5 takahashi takahashi aoki takahashi snuke
Sample Output 2
takahashi
Sample Input 3
1 a
Sample Output 3
a
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
二次元平面上に N 個の点があります。i 個目の点の座標は (x_i,y_i) です。
この中から 2 個の点を選ぶとき、それらを結ぶ線分の長さの最大値を求めてください。
制約
- 2 \leq N \leq 100
- -1000 \leq x_i,y_i \leq 1000
- (x_i,y_i) \neq (x_j,y_j)\ (i \neq j)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
x_1 y_1
x_2 y_2
\hspace{0.4cm} \vdots
x_N y_N
出力
2 点を結ぶ線分の長さの最大値を出力せよ。
想定解との絶対誤差または相対誤差が 10^{-6} 以下であれば正解とみなされる。
入力例 1
3 0 0 0 1 1 1
出力例 1
1.4142135624
1 個目の点と 3 個目の点を選んだときそれらを結ぶ線分の長さは \sqrt 2 = 1.41421356237\dots となり、これが最大です。
入力例 2
5 315 271 -2 -621 -205 -511 -952 482 165 463
出力例 2
1455.7159750446
Score : 200 points
Problem Statement
There are N points in a two-dimensional plane. The coordinates of the i-th point are (x_i,y_i).
Find the maximum length of a segment connecting two of these points.
Constraints
- 2 \leq N \leq 100
- -1000 \leq x_i,y_i \leq 1000
- (x_i,y_i) \neq (x_j,y_j)\ (i \neq j)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
\hspace{0.4cm} \vdots
x_N y_N
Output
Print the maximum length of a segment connecting two of the points.
Your answer will be considered correct when the absolute or relative error from the judge's answer is at most 10^{-6}.
Sample Input 1
3 0 0 0 1 1 1
Sample Output 1
1.4142135624
For the 1-st and 3-rd points, the length of the segment connecting them is \sqrt 2 = 1.41421356237\dots, which is the maximum length.
Sample Input 2
5 315 271 -2 -621 -205 -511 -952 482 165 463
Sample Output 2
1455.7159750446
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
英大文字からなる長さ 3 の文字列 T が、英小文字からなる文字列 S の 空港コード であるとは、 T が S から次のいずれかの方法により得られることとします。
- S の長さ 3 の(連続とは限らない)部分列をとり、それを英大文字に変換したものを T とする
- S の長さ 2 の(連続とは限らない)部分列をとり、それを英大文字に変換したものの末尾に
Xを追加したものを T とする
文字列 S, T が与えられるので、 T が S の空港コードであるか判定してください。
制約
- S は英小文字からなる長さ 3 以上 10^5 以下の文字列
- T は英大文字からなる長さ 3 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
T が S の空港コードであるならば Yes を、そうでないならば No を出力せよ。
入力例 1
narita NRT
出力例 1
Yes
narita の部分列 nrt を英大文字に変換した文字列 NRT は、 narita の空港コードです。
入力例 2
losangeles LAX
出力例 2
Yes
losangeles の部分列 la を英大文字に変換した文字列 LA の末尾に X を追加したもの LAX は、 losangeles の空港コードです。
入力例 3
snuke RNG
出力例 3
No
Score: 300 points
Problem Statement
A string T of length 3 consisting of uppercase English letters is an airport code for a string S of lowercase English letters if and only if T can be derived from S by one of the following methods:
- Take a subsequence of length 3 from S (not necessarily contiguous) and convert it to uppercase letters to form T.
- Take a subsequence of length 2 from S (not necessarily contiguous), convert it to uppercase letters, and append
Xto the end to form T.
Given strings S and T, determine if T is an airport code for S.
Constraints
- S is a string of lowercase English letters with a length between 3 and 10^5, inclusive.
- T is a string of uppercase English letters with a length of 3.
Input
The input is given from Standard Input in the following format:
S T
Output
Print Yes if T is an airport code for S, and No otherwise.
Sample Input 1
narita NRT
Sample Output 1
Yes
The subsequence nrt of narita, when converted to uppercase, forms the string NRT, which is an airport code for narita.
Sample Input 2
losangeles LAX
Sample Output 2
Yes
The subsequence la of losangeles, when converted to uppercase and appended with X, forms the string LAX, which is an airport code for losangeles.
Sample Input 3
snuke RNG
Sample Output 3
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
英小文字からなる長さ N の文字列 S が与えられます。 S の各文字は色 1 、色 2 、\ldots 、色 M の M 色のうちのいずれかで塗られており、 i = 1, 2, \ldots, N について、S の i 文字目は色 C_i で塗られています。
各 i = 1, 2, \ldots, M について、この順番に下記の操作を行います。
- S の色 i で塗られた文字からなる部分を、右に 1 つ巡回シフトする。 すなわち、S の 色 i で塗られた文字の位置が先頭のものから順に p_1, p_2, p_3, \ldots, p_k 文字目であるとき、 S の p_1, p_2, p_3, \ldots, p_k 文字目を、それぞれ、S の p_k, p_1,p_2, \ldots, p_{k-1} 文字目で同時に置き換える。
上記の操作をすべて行った後の、最終的な S を出力してください。
なお、M 色あるどの色についても、その色で塗られた S の文字が必ず 1 つ以上存在することが、制約として保証されます。
制約
- 1 \leq M \leq N \leq 2 \times 10^5
- 1 \leq C_i \leq M
- N, M, C_i はすべて整数
- S は英小文字からなる長さ N の文字列
- 任意の整数 1 \leq i \leq M に対して、ある整数 1 \leq j \leq N が存在して C_j = i が成り立つ
入力
入力は以下の形式で標準入力から与えられる。
N M S C_1 C_2 \ldots C_N
出力
答えを出力せよ。
入力例 1
8 3 apzbqrcs 1 2 3 1 2 2 1 2
出力例 1
cszapqbr
はじめ、S = apzbqrcs です。
- i = 1 に対する操作では、S の 1, 4, 7 文字目からなる部分を右に 1 つ巡回シフトします。その結果、S =
cpzaqrbsとなります。 - i = 2 に対する操作では、S の 2, 5, 6, 8 文字目からなる部分を右に 1 つ巡回シフトします。その結果、S =
cszapqbrとなります。 - i = 3 に対する操作では、S の 3 文字目からなる部分を右に 1 つ巡回シフトします。その結果、S =
cszapqbrとなります(操作の前後で S は変わりません)。
よって、最終的な S である cszapqbr を出力します。
入力例 2
2 1 aa 1 1
出力例 2
aa
Score : 300 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters. Each character of S is painted in one of the M colors: color 1, color 2, ..., color M; for each i = 1, 2, \ldots, N, the i-th character of S is painted in color C_i.
For each i = 1, 2, \ldots, M in this order, let us perform the following operation.
- Perform a right circular shift by 1 on the part of S painted in color i. That is, if the p_1-th, p_2-th, p_3-th, \ldots, p_k-th characters are painted in color i from left to right, then simultaneously replace the p_1-th, p_2-th, p_3-th, \ldots, p_k-th characters of S with the p_k-th, p_1-th, p_2-th, \ldots, p_{k-1}-th characters of S, respectively.
Print the final S after the above operations.
The constraints guarantee that at least one character of S is painted in each of the M colors.
Constraints
- 1 \leq M \leq N \leq 2 \times 10^5
- 1 \leq C_i \leq M
- N, M, and C_i are all integers.
- S is a string of length N consisting of lowercase English letters.
- For each integer 1 \leq i \leq M, there is an integer 1 \leq j \leq N such that C_j = i.
Input
The input is given from Standard Input in the following format:
N M S C_1 C_2 \ldots C_N
Output
Print the answer.
Sample Input 1
8 3 apzbqrcs 1 2 3 1 2 2 1 2
Sample Output 1
cszapqbr
Initially, S = apzbqrcs.
- For i = 1, perform a right circular shift by 1 on the part of S formed by the 1-st, 4-th, 7-th characters, resulting in S =
cpzaqrbs. - For i = 2, perform a right circular shift by 1 on the part of S formed by the 2-nd, 5-th, 6-th, 8-th characters, resulting in S =
cszapqbr. - For i = 3, perform a right circular shift by 1 on the part of S formed by the 3-rd character, resulting in S =
cszapqbr(here, S is not changed).
Thus, you should print cszapqbr, the final S.
Sample Input 2
2 1 aa 1 1
Sample Output 2
aa
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
AtCoder 国は無限に広がる直交座標の上にあります。
AtCoder 国には N 個の街があり、 1,2,\dots,N と番号が付けられています。街 i は地点 (x_i, y_i) にあり、2 つの異なる番号の街が同じ座標にあることはありません。
AtCoder 国には転移魔法(以下、魔法と表記)があります。魔法は整数の組 (a,b) によって識別されていて、地点 (x,y) にいるときに魔法 (a,b) を使うと (x+a,y+b) にワープすることができます。
すぬけ君は、任意の整数の組 (a,b) を選んで魔法 (a,b) を覚えることができる大魔術師です。また、すぬけ君は何種類でも魔法を覚えることができます。
魔法を使って街と街の間を移動したくなったすぬけ君は、全ての相異なる街の組 (i,j) について次の行動を取れるようにいくつかの魔法を覚えることにしました。
- 覚えた魔法のうち 1 種類の魔法のみ を選ぶ。その後、選んだ魔法 のみ を繰り返し使って街 i から 街 j に移動する。
すぬけ君が上の条件を満たすように魔法を覚えるとき、少なくとも何種類の魔法を覚えればよいですか?
制約
- 2 \leq N \leq 500
- 0 \leq x_i \leq 10^9 (1 \leq i \leq N)
- 0 \leq y_i \leq 10^9 (1 \leq i \leq N)
- i \neq j ならば (x_i, y_i) \neq (x_j, y_j) である。
入力
入力は以下の形式で標準入力から与えられる。
N x_1 y_1 x_2 y_2 \vdots x_N y_N
出力
すぬけ君が覚える必要がある魔法の個数の最小値を出力せよ。
入力例 1
3 1 2 3 6 7 4
出力例 1
6
AtCoder 国の街の位置を図示したのが下の図です。(わかりやすさのために四隅の座標を表示しています。)

すぬけ君は次の 6 種類の魔法を覚えれば、すべての (i,j) (i \neq j) の組に対して街 i から 1 種類の魔法を 1 回使うことで街 j に着くことができるので条件を満たします。
- (2, 4)
- (-2, -4)
- (4, -2)
- (-4, 2)
- (-6, -2)
- (6, 2)
次の 6 種類の魔法も条件を満たします。このときすぬけ君は、すべての (i,j) (i \neq j) の組に対して街 i から 1 種類の魔法を 2 回使うことで街 j に着くことができます。
- (1, 2)
- (-1, -2)
- (2, -1)
- (-2, 1)
- (-3, -1)
- (3, 1)
条件を満たす魔法の組み合わせのうち 6 種類より少ないものは存在しないので、 6 を出力します。
入力例 2
3 1 2 2 2 4 2
出力例 2
2
次の 2 種類の魔法を覚えるのが最適です。
- (1, 0)
- (-1, 0)
入力例 3
4 0 0 0 1000000000 1000000000 0 1000000000 1000000000
出力例 3
8
Score : 400 points
Problem Statement
The Republic of AtCoder lies on a Cartesian coordinate plane.
It has N towns, numbered 1,2,\dots,N. Town i is at (x_i, y_i), and no two different towns are at the same coordinates.
There are teleportation spells in the nation. A spell is identified by a pair of integers (a,b), and casting a spell (a, b) at coordinates (x, y) teleports you to (x+a, y+b).
Snuke is a great magician who can learn the spell (a, b) for any pair of integers (a, b) of his choice. The number of spells he can learn is also unlimited.
To be able to travel between the towns using spells, he has decided to learn some number of spells so that it is possible to do the following for every pair of different towns (i, j).
- Choose just one spell among the spells learned. Then, repeatedly use just the chosen spell to get from Town i to Town j.
At least how many spells does Snuke need to learn to achieve the objective above?
Constraints
- 2 \leq N \leq 500
- 0 \leq x_i \leq 10^9 (1 \leq i \leq N)
- 0 \leq y_i \leq 10^9 (1 \leq i \leq N)
- (x_i, y_i) \neq (x_j, y_j) if i \neq j.
Input
Input is given from Standard Input in the following format:
N x_1 y_1 x_2 y_2 \vdots x_N y_N
Output
Print the minimum number of spells Snuke needs to learn.
Sample Input 1
3 1 2 3 6 7 4
Sample Output 1
6
The figure below illustrates the positions of the towns (along with the coordinates of the four corners).

If Snuke learns the six spells below, he can get from Town i to Town j by using one of the spells once for every pair (i,j) (i \neq j), achieving his objective.
- (2, 4)
- (-2, -4)
- (4, -2)
- (-4, 2)
- (-6, -2)
- (6, 2)
Another option is to learn the six spells below. In this case, he can get from Town i to Town j by using one of the spells twice for every pair (i,j) (i \neq j), achieving his objective.
- (1, 2)
- (-1, -2)
- (2, -1)
- (-2, 1)
- (-3, -1)
- (3, 1)
There is no combination of spells that consists of less than six spells and achieve the objective, so we should print 6.
Sample Input 2
3 1 2 2 2 4 2
Sample Output 2
2
The optimal choice is to learn the two spells below:
- (1, 0)
- (-1, 0)
Sample Input 3
4 0 0 0 1000000000 1000000000 0 1000000000 1000000000
Sample Output 3
8
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
1 以上 N-1 以下の整数からなる長さ M の数列 A=(A_1,A_2,\dots,A_M) が与えられます。 i=1,2,\dots,M について、以下の質問に答えてください。
- 数列 B=(B_1,B_2,\dots,B_N) がある。最初、各 j について B_j=j である。今から、k=1,2,\dots,i-1,i+1,\dots,M の順に以下の操作を行う
(すなわち、i を除いた 1 以上 M 以下の整数 k について、昇順に以下の操作を行う)。
- B_{A_k} と B_{A_k+1} の値を入れ替える。
- 全ての操作が終了した段階で、B_j=1 を満たす j の値を S_i と定義する。S_i を求めよ。
制約
- 2 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- 1 \leq A_i \leq N-1\ (1\leq i \leq M)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_M
出力
M 行出力せよ。 i\ (1\leq i \leq M) 行目には、S_i の値を整数として出力せよ。
入力例 1
5 4 1 2 3 2
出力例 1
1 3 2 4
i = 2 のとき、操作によって B は以下のように変化します。
- 最初、B = (1,2,3,4,5)
- k=1 として操作を行う。すなわち B_1 と B_2 の値を入れ替えて、B = (2,1,3,4,5)
- k=3 として操作を行う。すなわち B_3 と B_4 の値を入れ替えて、B = (2,1,4,3,5)
- k=4 として操作を行う。すなわち B_2 と B_3 の値を入れ替えて、B = (2,4,1,3,5)
全ての操作が終了した段階で B_3=1 であるため、S_2 = 3 です。
同様に、
- i=1 のとき:k=2,3,4 の順に操作を行うと B=(1,4,3,2,5) になるので、S_1=1
- i=3 のとき:k=1,2,4 の順に操作を行うと B=(2,1,3,4,5) になるので、S_3=2
- i=4 のとき:k=1,2,3 の順に操作を行うと B=(2,3,4,1,5) になるので、S_4=4
です。
入力例 2
3 3 2 2 2
出力例 2
1 1 1
入力例 3
10 10 1 1 1 9 4 4 2 1 3 3
出力例 3
2 2 2 3 3 3 1 3 4 4
Score : 500 points
Problem Statement
You are given a sequence of length M consisting of integers between 1 and N-1, inclusive: A=(A_1,A_2,\dots,A_M). Answer the following question for i=1, 2, \dots, M.
- There is a sequence B=(B_1,B_2,\dots,B_N). Initially, we have B_j=j for each j. Let us perform the following operation for k=1, 2, \dots, i-1, i+1, \dots, M in this order (in other words, for integers k between 1 and M except i in ascending order).
- Swap the values of B_{A_k} and B_{A_k+1}.
- After all the operations, let S_i be the value of j such that B_j=1. Find S_i.
Constraints
- 2 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- 1 \leq A_i \leq N-1\ (1\leq i \leq M)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_M
Output
Print M lines. The i-th line (1\leq i \leq M) should contain the value S_i as an integer.
Sample Input 1
5 4 1 2 3 2
Sample Output 1
1 3 2 4
For i = 2, the operations change B as follows.
- Initially, B = (1,2,3,4,5).
- Perform the operation for k=1. That is, swap the values of B_1 and B_2, making B = (2,1,3,4,5).
- Perform the operation for k=3. That is, swap the values of B_3 and B_4, making B = (2,1,4,3,5).
- Perform the operation for k=4. That is, swap the values of B_2 and B_3, making B = (2,4,1,3,5).
After all the operations, we have B_3=1, so S_2 = 3.
Similarly, we have the following.
- For i=1: performing the operation for k=2,3,4 in this order makes B=(1,4,3,2,5), so S_1=1.
- For i=3: performing the operation for k=1,2,4 in this order makes B=(2,1,3,4,5), so S_3=2.
- For i=4: performing the operation for k=1,2,3 in this order makes B=(2,3,4,1,5), so S_4=4.
Sample Input 2
3 3 2 2 2
Sample Output 2
1 1 1
Sample Input 3
10 10 1 1 1 9 4 4 2 1 3 3
Sample Output 3
2 2 2 3 3 3 1 3 4 4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 個の都市があり、都市 1, 都市 2, \ldots, 都市 N と番号づけられています。
いくつかの異なる都市の間は一方通行のテレポーターによって移動できます。
都市 i (1\leq i\leq N) からテレポーターによって直接移動できる都市は 0 と 1 からなる長さ M の文字列 S_i によって表されます。具体的には、1\leq j\leq N に対して、
- 1\leq j-i\leq M かつ S_i の (j-i) 文字目が
1ならば、都市 i から都市 j に直接移動できる。 - そうでない時、都市 i から都市 j へは直接移動できない。
k=2,3,\ldots, N-1 に対して次の問題を解いてください。
テレポータを繰り返し使用することによって、都市 k を通らずに都市 1 から 都市 N へ移動できるか判定し、 できるならばそのために必要なテレポーターの使用回数の最小値を、 できないならば -1 を出力せよ。
制約
- 3 \leq N \leq 10^5
- 1\leq M\leq 10
- M<N
- S_i は
0と1のみからなる長さ M の文字列 - i+j>N ならば S_i の j 文字目は
0 - N,M は整数
入力
入力は以下の形式で標準入力から与えられる。
N M S_1 S_2 \vdots S_N
出力
N-2 個の整数を空白区切りで一行に出力せよ。 i (1\leq i\leq N-2) 番目には、k=i+1 に対する問題の答えを出力せよ。
入力例 1
5 2 11 01 11 10 00
出力例 1
2 3 2
テレポータによって各都市からはそれぞれ以下の都市へ直接移動する事ができます。
- 都市 1 からは都市 2,3 へ移動できる。
- 都市 2 からは都市 4 へ移動できる。
- 都市 3 からは都市 4,5 へ移動できる。
- 都市 4 からは都市 5 へ移動できる。
- 都市 5 から移動できる都市は存在しない。
よって、都市 1 から都市 5 へ移動する方法は、
- 経路 1 : 都市 1 \to 都市 2 \to 都市 4 \to 都市 5
- 経路 2 : 都市 1 \to 都市 3 \to 都市 4 \to 都市 5
- 経路 3 : 都市 1 \to 都市 3 \to 都市 5
の 3 つがあり、
- 都市 2 を通らない経路は経路 2, 経路 3 の 2つであり、そのうちテレポーターの使用回数が最小となるのは経路 3 で、この時 2 回使用する。
- 都市 3 を通らない経路は経路 1 のみであり、この時テレポーターは 3 回使用する。
- 都市 4 を通らない経路は経路 3 のみであり、この時テレポーターは 2 回使用する。
となります。よって、2,3,2 をこの順に空白区切りで出力します。
入力例 2
6 3 101 001 101 000 100 000
出力例 2
-1 3 3 -1
都市 1 から都市 6 へ移動する方法は、都市 1 \to 都市 2 \to 都市 5 \to 都市 6 のみであるため、
k=2,5 の場合には都市 k を通らずに都市 1 から都市 6 へ移動する方法は存在せず、
k=3,4 の場合には上の方法が条件をみたし、テレポーターを 3 回使用します。
よって、-1,3,3,-1 をこの順に空白区切りで出力します。
テレポーターは一方通行であるため、
都市 3 から都市 4 へはテレポーターによって移動できますが、
都市 4 から都市 3 へは移動できず、
都市 1 \to 都市 4 \to 都市 3 \to 都市 6
のような移動はできない事に注意してください。
Score : 500 points
Problem Statement
There are N cities numbered city 1, city 2, \ldots, and city N.
There are also one-way teleporters that send you to different cities.
Whether a teleporter can send you directly from city i (1\leq i\leq N) to another is represented by a length-M string S_i consisting of 0 and 1. Specifically, for 1\leq j\leq N,
- if 1\leq j-i\leq M and the (j-i)-th character of S_i is
1, then a teleporter can send you directly from city i to city j; - otherwise, it cannot send you directly from city i to city j.
Solve the following problem for k=2,3,\ldots, N-1:
Can you travel from city 1 to city N without visiting city k by repeatedly using a teleporter? If you can, print the minimum number of times you need to use a teleporter; otherwise, print -1.
Constraints
- 3 \leq N \leq 10^5
- 1\leq M\leq 10
- M<N
- S_i is a string of length M consisting of
0and1. - If i+j>N, then the j-th character of S_i is
0. - N and M are integers.
Input
The input is given from Standard Input in the following format:
N M S_1 S_2 \vdots S_N
Output
Print (N-2) integers, separated by spaces, in a single line. The i-th (1\leq i\leq N-2) integer should be the answer to the problem for k=i+1.
Sample Input 1
5 2 11 01 11 10 00
Sample Output 1
2 3 2
A teleporter sends you
- from city 1 to cities 2 and 3;
- from city 2 to city 4;
- from city 3 to cities 4 and 5;
- from city 4 to city 5; and
- from city 5 to nowhere.
Therefore, there are three paths to travel from city 1 to city 5:
- path 1 : city 1 \to city 2 \to city 4 \to city 5;
- path 2 : city 1 \to city 3 \to city 4 \to city 5; and
- path 3 : city 1 \to city 3 \to city 5.
Among these paths,
- two paths, path 2 and path 3, do not visit city 2. Among them, path 3 requires the minimum number of teleporter uses (twice).
- Path 1 is the only path without city 3. It requires using a teleporter three times.
- Path 3 is the only path without city 4. It requires using a teleporter twice.
Thus, 2, 3, and 2, separated by spaces, should be printed.
Sample Input 2
6 3 101 001 101 000 100 000
Sample Output 2
-1 3 3 -1
The only path from city 1 to city 6 is city 1 \to city 2 \to city 5 \to city 6.
For k=2,5, there is no way to travel from city 1 to city 6 without visiting city k.
For k=3,4, the path above satisfies the condition; it requires using a teleporter three times.
Thus, -1, 3, 3, and -1, separated by spaces, should be printed.
Note that a teleporter is one-way;
a teleporter can send you from city 3 to city 4,
but not from city 4 to city 3,
so the following path, for example, is invalid:
city 1 \to city 4 \to city 3 \to city 6.