C - Rotate

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

NN 列のマス目が与えられます。上から i 行目、左から j 列目のマスには整数 A_{i,j} が書かれています。ここで、A_{i,j}01 であることが保証されます。

マス目の外側のマスに書かれた整数を時計回りに 1 個ずつずらしたときのマス目を出力してください。

ただし外側のマスとは、1 行目、N 行目、1 列目、N 列目のいずれか 1 つ以上に属するマスの集合のことを指します。

制約

  • 2 \le N \le 100
  • 0 \le A_{i,j} \le 1(1 \le i,j \le N)
  • 入力は全て整数

入力

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

N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}

出力

マス目の外側のマスに書かれた整数を時計回りに 1 個ずつずらしたときのマス目において、上から i 行目、左から j 列目のマスに書かれている整数を B_{i,j} と置く。このとき、以下の形式で出力せよ。

B_{1,1}B_{1,2}\dots B_{1,N}
B_{2,1}B_{2,2}\dots B_{2,N}
\vdots
B_{N,1}B_{N,2}\dots B_{N,N}

入力例 1

4
0101
1101
1111
0000

出力例 1

1010
1101
0111
0001

上から i 行目、左から j 列目のマスを (i,j) と呼ぶこととします。

外側のマスは (1,1) から時計回りに列挙すると (1,1),(1,2),(1,3),(1,4),(2,4),(3,4),(4,4),(4,3),(4,2),(4,1),(3,1),(2,1)12 個です。

これらのマスに書かれている整数を、時計回りに 1 個ずつ動かすと出力欄のようになります。


入力例 2

2
11
11

出力例 2

11
11

入力例 3

5
01010
01001
10110
00110
01010

出力例 3

00101
11000
00111
00110
10100

Score : 200 points

Problem Statement

You are given a grid with N rows and N columns. An integer A_{i, j} is written on the square at the i-th row from the top and j-th column from the left. Here, it is guaranteed that A_{i,j} is either 0 or 1.

Shift the integers written on the outer squares clockwise by one square each, and print the resulting grid.

Here, the outer squares are those in at least one of the 1-st row, N-th row, 1-st column, and N-th column.

Constraints

  • 2 \le N \le 100
  • 0 \le A_{i,j} \le 1(1 \le i,j \le N)
  • All input values are integers.

Input

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

N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}

Output

Let B_{i,j} be the integer written on the square at the i-th row from the top and j-th column from the left in the grid resulting from shifting the outer squares clockwise by one square each. Print them in the following format:

B_{1,1}B_{1,2}\dots B_{1,N}
B_{2,1}B_{2,2}\dots B_{2,N}
\vdots
B_{N,1}B_{N,2}\dots B_{N,N}

Sample Input 1

4
0101
1101
1111
0000

Sample Output 1

1010
1101
0111
0001

We denote by (i,j) the square at the i-th row from the top and j-th column from the left.

The outer squares, in clockwise order starting from (1,1), are the following 12 squares: (1,1),(1,2),(1,3),(1,4),(2,4),(3,4),(4,4),(4,3),(4,2),(4,1),(3,1), and (2,1).

The sample output shows the resulting grid after shifting the integers written on those squares clockwise by one square.


Sample Input 2

2
11
11

Sample Output 2

11
11

Sample Input 3

5
01010
01001
10110
00110
01010

Sample Output 3

00101
11000
00111
00110
10100
D - chess960

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

高橋君は chess960 と呼ばれるゲームで遊んでいます。 高橋君はランダムに決めた初期配置が chess960 の条件を満たすか確認するプログラムを書くことにしました。

長さ 8 の文字列 S が与えられます。S には K, Q がちょうど 1 文字ずつ、R, B, N がちょうど 2 文字ずつ含まれます。 S が以下の条件を全て満たしているか判定してください。

  • S において左から x,y\ (x < y) 文字目が B であるとする。このとき xy の偶奇が異なる。

  • K2 つの R の間にある。より形式的には、S において左から x,y\ (x < y) 文字目が R であり、 z 文字目が K であるとする。このとき、 x< z < y が成り立つ。

制約

  • S は 長さ 8 の文字列であり、K, Q がちょうど 1 文字ずつ、R, B, N がちょうど 2 文字ずつ含まれる。

入力

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

S

出力

S が条件を満たす場合 Yes を、そうでない場合 No を出力せよ。


入力例 1

RNBQKBNR

出力例 1

Yes

B は左から 3 番目、6 番目にあり、36 は偶奇が異なります。 また、K2 つの R の間にあります。よって条件を満たします。


入力例 2

KRRBBNNQ

出力例 2

No

K2 つの R の間にありません。


入力例 3

BRKRBQNN

出力例 3

No

Score : 200 points

Problem Statement

Takahashi is playing a game called Chess960. He has decided to write a code that determines if a random initial state satisfies the conditions of Chess960.

You are given a string S of length eight. S has exactly one K and Q, and exactly two R's, B's , and N's. Determine if S satisfies all of the following conditions.

  • Suppose that the x-th and y-th (x < y) characters from the left of S are B; then, x and y have different parities.

  • K is between two R's. More formally, suppose that the x-th and y-th (x < y) characters from the left of S are R and the z-th is K; then x< z < y.

Constraints

  • S is a string of length 8 that contains exactly one K and Q, and exactly two R's, B's , and N's.

Input

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

S

Output

Print Yes if S satisfies the conditions; print No otherwise.


Sample Input 1

RNBQKBNR

Sample Output 1

Yes

The 3-rd and 6-th characters are B, and 3 and 6 have different parities. Also, K is between the two R's. Thus, the conditions are fulfilled.


Sample Input 2

KRRBBNNQ

Sample Output 2

No

K is not between the two R's.


Sample Input 3

BRKRBQNN

Sample Output 3

No
E - Poem Online Judge

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

ポエムオンラインジャッジ (Poem Online Judge, 以下 POJ と表記) は提出された文字列に得点をつけるオンラインジャッジです。
POJ に N 回の提出がありました。早い方から i 番目の提出では文字列 S_i が提出されて、得点は T_i でした。(同じ文字列が複数回提出される場合もあります)
ただし、POJ では 同じ文字列を提出しても得点が等しいとは限らない のに注意してください。

N 回の提出のうち、その提出よりも早い提出であって文字列が一致するものが存在しないような提出を オリジナル であると呼びます。
また、オリジナルな提出の中で最も得点が高いものを 最優秀賞 と呼びます。ただし、そのような提出が複数ある場合は、最も提出が早いものを最優秀賞とします。

最優秀賞は早い方から何番目の提出ですか?

制約

  • 1 \leq N \leq 10^5
  • S_i は英小文字からなる文字列
  • S_i の長さは 1 以上 10 以下
  • 0 \leq T_i \leq 10^9
  • N, T_i は整数

入力

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

N
S_1 T_1
S_2 T_2
\vdots
S_N T_N

出力

答えを出力せよ。


入力例 1

3
aaa 10
bbb 20
aaa 30

出力例 1

2

以下では早い方から i 番目の提出を提出 i と呼びます。
オリジナルな提出は提出 1 と 提出 2 です。提出 3 は提出 1 と文字列が一致しているためオリジナルではありません。
オリジナルな提出のうち最も得点が高い提出は提出 2 です。よってこれが最優秀賞になります。


入力例 2

5
aaa 9
bbb 10
ccc 10
ddd 10
bbb 11

出力例 2

2

オリジナルな提出は提出 1・提出 2・提出 3・提出 4 です。
その中で最も得点が高い提出は提出 2・提出 3・提出 4 です。この場合はこの中でもっとも提出の早い提出 2 を最優秀賞とします。
このように、オリジナルな提出の中で最も得点が高い提出が複数ある場合は、さらにその中で最も提出が早いものを最優秀賞とするのに注意してください。


入力例 3

10
bb 3
ba 1
aa 4
bb 1
ba 5
aa 9
aa 2
ab 6
bb 5
ab 3

出力例 3

8

Score : 300 points

Problem Statement

Poem Online Judge (POJ) is an online judge that gives scores to submitted strings.
There were N submissions to POJ. In the i-th earliest submission, string S_i was submitted, and a score of T_i was given. (The same string may have been submitted multiple times.)
Note that POJ may not necessarily give the same score to submissions with the same string.

A submission is said to be an original submission if the string in the submission is never submitted in any earlier submission.
A submission is said to be the best submission if it is an original submission with the highest score. If there are multiple such submissions, only the earliest one is considered the best submission.

Find the index of the best submission.

Constraints

  • 1 \leq N \leq 10^5
  • S_i is a string consisting of lowercase English characters.
  • S_i has a length between 1 and 10, inclusive.
  • 0 \leq T_i \leq 10^9
  • N and T_i are integers.

Input

Input is given from Standard Input in the following format:

N
S_1 T_1
S_2 T_2
\vdots
S_N T_N

Output

Print the answer.


Sample Input 1

3
aaa 10
bbb 20
aaa 30

Sample Output 1

2

We will refer to the i-th earliest submission as Submission i.
Original submissions are Submissions 1 and 2. Submission 3 is not original because it has the same string as that in Submission 1.
Among the original submissions, Submission 2 has the highest score. Therefore, this is the best submission.


Sample Input 2

5
aaa 9
bbb 10
ccc 10
ddd 10
bbb 11

Sample Output 2

2

Original submissions are Submissions 1, 2, 3, and 4.
Among them, Submissions 2, 3, and 4 have the highest scores. In this case, the earliest submission among them, Submission 2, is the best.
As in this sample, beware that if multiple original submissions have the highest scores, only the one with the earliest among them is considered the best submission.


Sample Input 3

10
bb 3
ba 1
aa 4
bb 1
ba 5
aa 9
aa 2
ab 6
bb 5
ab 3

Sample Output 3

8
F - Make Takahashi Happy

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

HW 列のマス目があります。 1 \leq i \leq H かつ 1 \leq j \leq W を満たす 2 つの整数 i, j について、 上から i 行目、左から j 列目のマス(以下、(i, j) と表す)には、整数 A_{i, j} が書かれています。

いま、高橋君は (1, 1) にいます。 これから高橋君は「いまいるマスから右または下に隣接するマスに移動する」ことを繰り返して、(H, W) まで移動します。 ただし、その過程でマス目の外部に移動することは出来ません。

その結果、高橋君が通ったマス(始点 (1, 1) と終点 (H, W) を含む)に書かれた整数がすべて異なるとき、高橋君は嬉しくなります。 高橋君の移動経路として考えられるもののうち、高橋君が嬉しくなるものの個数を出力してください。

制約

  • 2 \leq H, W \leq 10
  • 1 \leq A_{i, j} \leq 10^9
  • 入力はすべて整数

入力

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

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

出力

答えを出力せよ。


入力例 1

3 3
3 2 2
2 1 3
1 5 4

出力例 1

3

高橋君の移動経路として考えられるものは下記の 6 通りです。

  • (1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 2, 3, 4 であり、高橋君は嬉しくなりません
  • (1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 3, 4 であり、高橋君は嬉しくなりません
  • (1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 5, 4 であり、高橋君は嬉しくなります
  • (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 3, 4 であり、高橋君は嬉しくなりません
  • (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 5, 4 であり、高橋君は嬉しくなります
  • (1, 1) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 2) \rightarrow (3, 3):通ったマスに書かれた整数は 3, 2, 1, 5, 4 であり、高橋君は嬉しくなります

よって、高橋君が嬉しくなる移動経路は、上で 3, 5, 6 番目に述べた 3 個です。


入力例 2

10 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100

出力例 2

48620

この例では、高橋君は考えられるどの経路を通っても嬉しくなります。

Score : 300 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns. For two integers i and j such that 1 \leq i \leq H and 1 \leq j \leq W, the square at the i-th row from the top and j-th column from the left (which we denote by (i, j)) has an integer A_{i, j} written on it.

Takahashi is currently at (1,1). From now on, he repeats moving to an adjacent square to the right of or below his current square until he reaches (H, W). When he makes a move, he is not allowed to go outside the grid.

Takahashi will be happy if the integers written on the squares he visits (including initial (1, 1) and final (H, W)) are distinct. Find the number of his possible paths that make him happy.

Constraints

  • 2 \leq H, W \leq 10
  • 1 \leq A_{i, j} \leq 10^9
  • All values in the input are integers.

Input

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

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

Output

Print the answer.


Sample Input 1

3 3
3 2 2
2 1 3
1 5 4

Sample Output 1

3

There are six possible paths:

  • (1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 2, 3, 4, so he will not be happy.
  • (1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 3, 4, so he will not be happy.
  • (1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 5, 4, so he will be happy.
  • (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 3, 4, so he will not be happy.
  • (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 5, 4, so he will be happy.
  • (1, 1) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 2) \rightarrow (3, 3): the integers written on the squares he visits are 3, 2, 1, 5, 4, so he will be happy.

Thus, the third, fifth, and sixth paths described above make him happy.


Sample Input 2

10 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100

Sample Output 2

48620

In this example, every possible path makes him happy.

G - Cheating Gomoku Narabe

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

HW 列のグリッドがあります。上から i 行目、左から j 列目のマスをマス (i, j) と呼びます。

各マスには ox. のうちいずれかの文字が書かれています。 各マスに書かれた文字は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H で表され、 マス (i, j) に書かれた文字は、文字列 S_ij 文字目と一致します。

このグリッドに対して、下記の操作を 0 回以上好きな回数だけ繰り返します。

  • . が書かれているマスを 1 個選び、そのマスに書かれた文字を o に変更する。

その結果、縦方向または横方向に連続した K 個のマスであってそれらに書かれた文字がすべて o であるようなものが存在する( すなわち、下記の 2 つの条件のうち少なくとも一方を満たす)ようにすることが可能かを判定し、可能な場合はそのために行う操作回数の最小値を出力してください。

  • 1 \leq i \leq H かつ 1 \leq j \leq W-K+1 を満たす整数の組 (i, j) であって、マス (i, j), (i, j+1), \ldots, (i, j+K-1) に書かれた文字が o であるものが存在する。
  • 1 \leq i \leq H-K+1 かつ 1 \leq j \leq W を満たす整数の組 (i, j) であって、マス (i, j), (i+1, j), \ldots, (i+K-1, j) に書かれた文字が o であるものが存在する。

制約

  • H, W, K は整数
  • 1 \leq H
  • 1 \leq W
  • H \times W \leq 2 \times 10^5
  • 1 \leq K \leq \max\lbrace H, W \rbrace
  • S_iox. のみからなる長さ W の文字列

入力

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

H W K
S_1
S_2
\vdots
S_H

出力

問題文中の条件を満たすことが不可能な場合は -1 を、可能な場合はそのために行う操作回数の最小値を出力せよ。


入力例 1

3 4 3
xo.x
..o.
xx.o

出力例 1

2

操作を 2 回行って、例えばマス (2, 1) とマス (2, 2) に書かれた文字をそれぞれ o に変更することで問題文中の条件を満たすことができ、これが最小の操作回数です。


入力例 2

4 2 3
.o
.o
.o
.o

出力例 2

0

操作を一度も行わなくても問題文中の条件を満たします。


入力例 3

3 3 3
x..
..x
.x.

出力例 3

-1

問題文中の条件を満たすことは不可能なので、-1 を出力します。


入力例 4

10 12 6
......xo.o..
x...x.....o.
x...........
..o...x.....
.....oo.....
o.........x.
ox.oox.xx..x
....o...oox.
..o.....x.x.
...o........

出力例 4

3

Score: 400 points

Problem Statement

There is 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 contains one of the characters o, x, and .. The characters written in each cell are represented by H strings S_1, S_2, \ldots, S_H of length W; the character written in cell (i, j) is the j-th character of the string S_i.

For this grid, you may repeat the following operation any number of times, possibly zero:

  • Choose one cell with the character . and change the character in that cell to o.

Determine if it is possible to have a sequence of K horizontally or vertically consecutive cells with o written in all cells (in other words, satisfy at least one of the following two conditions). If it is possible, print the minimum number of operations required to achieve this.

  • There is an integer pair (i, j) satisfying 1 \leq i \leq H and 1 \leq j \leq W-K+1 such that the characters in cells (i, j), (i, j+1), \ldots, (i, j+K-1) are all o.
  • There is an integer pair (i, j) satisfying 1 \leq i \leq H-K+1 and 1 \leq j \leq W such that the characters in cells (i, j), (i+1, j), \ldots, (i+K-1, j) are all o.

Constraints

  • H, W, and K are integers.
  • 1 \leq H
  • 1 \leq W
  • H \times W \leq 2 \times 10^5
  • 1 \leq K \leq \max\lbrace H, W \rbrace
  • S_i is a string of length W consisting of the characters o, x, and ..

Input

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

H W K
S_1
S_2
\vdots
S_H

Output

If it is impossible to satisfy the condition in the problem statement, print -1. Otherwise, print the minimum number of operations required to do so.


Sample Input 1

3 4 3
xo.x
..o.
xx.o

Sample Output 1

2

By operating twice, for example, changing the characters in cells (2, 1) and (2, 2) to o, you can satisfy the condition in the problem statement, and this is the minimum number of operations required.


Sample Input 2

4 2 3
.o
.o
.o
.o

Sample Output 2

0

The condition is satisfied without performing any operations.


Sample Input 3

3 3 3
x..
..x
.x.

Sample Output 3

-1

It is impossible to satisfy the condition, so print -1.


Sample Input 4

10 12 6
......xo.o..
x...x.....o.
x...........
..o...x.....
.....oo.....
o.........x.
ox.oox.xx..x
....o...oox.
..o.....x.x.
...o........

Sample Output 4

3