A - Prefix and Suffix

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 200

問題文

すぬけ君は次の条件を満たす文字列に興味があります。

  • 長さ N 以上である。
  • 先頭 N 文字が文字列 s に一致する。
  • 末尾 N 文字が文字列 t に一致する。

条件を満たす文字列のうち、最も短いものの長さを求めてください。

制約

  • 1≤N≤100
  • s, t は長さ N である。
  • s, t は英小文字のみからなる。

入力

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

N
s
t

出力

条件を満たす文字列のうち、最も短いものの長さを出力せよ。


入力例 1

3
abc
cde

出力例 1

5

最も短い文字列は abcde です。


入力例 2

1
a
z

出力例 2

2

最も短い文字列は az です。


入力例 3

4
expr
expr

出力例 3

4

最も短い文字列は expr です。

Score : 200 points

Problem Statement

Snuke is interested in strings that satisfy the following conditions:

  • The length of the string is at least N.
  • The first N characters equal to the string s.
  • The last N characters equal to the string t.

Find the length of the shortest string that satisfies the conditions.

Constraints

  • 1≤N≤100
  • The lengths of s and t are both N.
  • s and t consist of lowercase English letters.

Input

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

N
s
t

Output

Print the length of the shortest string that satisfies the conditions.


Sample Input 1

3
abc
cde

Sample Output 1

5

The shortest string is abcde.


Sample Input 2

1
a
z

Sample Output 2

2

The shortest string is az.


Sample Input 3

4
expr
expr

Sample Output 3

4

The shortest string is expr.

B - Median Pyramid Easy

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 400

問題文

N 段のピラミッドがあります。 段は上から順に 1, 2, ..., N と番号が振られています。 各 1≤i≤N について、i 段目には 2i-1 個のブロックが横一列に並んでいます。 また、各段の中央のブロックに注目すると、これらは縦一列に並んでいます。

N=4 段のピラミッド

すぬけ君は N 段目のブロックに (1, 2, ..., 2N-1) を並べ替えたもの(順列)を書き込みました。 さらに、次のルールに従い、残りすべてのブロックに整数を書き込みました。

  • あるブロックに書き込まれる整数は、そのブロックの左下、真下、右下のブロックに書き込まれた整数の中央値である。

ブロックに整数を書き込む例

その後、すぬけ君はすべてのブロックに書き込まれた整数を消してしまいました。 すぬけ君は、1 段目のブロックに書き込まれた整数が x であったことだけを覚えています。

N 段目のブロックに書き込まれた順列としてあり得るものが存在するか判定し、存在するならばひとつ求めてください。

制約

  • 2≤N≤10^5
  • 1≤x≤2N-1

入力

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

N x

出力

N 段目のブロックに書き込まれた順列としてあり得るものが存在しないならば、No を出力せよ。

存在するならば、Yes を出力した後、2N-1 行出力せよ。 このうち i 行目には、順列の i 番目の整数を出力せよ。


入力例 1

4 4

出力例 1

Yes
1
6
3
7
4
5
2

問題文中の図の例です。


入力例 2

2 1

出力例 2

No

N 段目のブロックにどのような順列を書き込んでも、1 段目のブロックに書き込まれる整数は 2 となります。

Score : 400 points

Problem Statement

We have a pyramid with N steps, built with blocks. The steps are numbered 1 through N from top to bottom. For each 1≤i≤N, step i consists of 2i-1 blocks aligned horizontally. The pyramid is built so that the blocks at the centers of the steps are aligned vertically.

A pyramid with N=4 steps

Snuke wrote a permutation of (1, 2, ..., 2N-1) into the blocks of step N. Then, he wrote integers into all remaining blocks, under the following rule:

  • The integer written into a block b must be equal to the median of the three integers written into the three blocks directly under b, or to the lower left or lower right of b.

Writing integers into the blocks

Afterwards, he erased all integers written into the blocks. Now, he only remembers that the integer written into the block of step 1 was x.

Construct a permutation of (1, 2, ..., 2N-1) that could have been written into the blocks of step N, or declare that Snuke's memory is incorrect and such a permutation does not exist.

Constraints

  • 2≤N≤10^5
  • 1≤x≤2N-1

Input

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

N x

Output

If no permutation of (1, 2, ..., 2N-1) could have been written into the blocks of step N, print No.

Otherwise, print Yes in the first line, then print 2N-1 lines in addition.

The i-th of these 2N-1 lines should contain the i-th element of a possible permutation.


Sample Input 1

4 4

Sample Output 1

Yes
1
6
3
7
4
5
2

This case corresponds to the figure in the problem statement.


Sample Input 2

2 1

Sample Output 2

No

No matter what permutation was written into the blocks of step N, the integer written into the block of step 1 would be 2.

C - Rabbit Exercise

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 800

問題文

N 匹のうさぎがいます。 うさぎ達は 1 から N まで番号が振られています。 最初、うさぎ i は数直線上の座標 x_i にいます。

うさぎ達は体操をすることにしました。 1 セット分の体操は、次のような合計 M 回のジャンプからなります。 j 回目のジャンプでは、うさぎ a_j (2≤a_j≤N-1) がジャンプします。 このとき、うさぎ a_j-1 かうさぎ a_j+1 のどちらかが等確率で選ばれ(これをうさぎ x とします)、うさぎ a_j はうさぎ x に関して対称な座標へジャンプします。

以上の合計 M 回のジャンプを 1 セット分の体操として、うさぎ達は K セット分の体操を続けて繰り返します。 各うさぎについて、最終的な座標の期待値を求めてください。

制約

  • 3≤N≤10^5
  • x_i は整数である。
  • |x_i|≤10^9
  • 1≤M≤10^5
  • 2≤a_j≤N-1
  • 1≤K≤10^{18}

入力

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

N
x_1 x_2 ... x_N
M K
a_1 a_2 ... a_M

出力

N 行出力せよ。 i 行目には、うさぎ i の最終的な座標の期待値を出力せよ。 絶対誤差または相対誤差が 10^{-9} 以下ならば正解となる。


入力例 1

3
-1 0 2
1 1
2

出力例 1

-1.0
1.0
2.0

うさぎ 2 がジャンプします。 うさぎ 1 に関して対称な座標へジャンプすると、座標 -2 へ移動します。 うさぎ 3 に関して対称な座標へジャンプすると、座標 4 へ移動します。 よって、うさぎ 2 の最終的な座標の期待値は 0.5×(-2)+0.5×4=1.0 です。


入力例 2

3
1 -1 1
2 2
2 2

出力例 2

1.0
-1.0
1.0

x_i は相異なるとは限りません。


入力例 3

5
0 1 3 6 10
3 10
2 3 4

出力例 3

0.0
3.0
7.0
8.0
10.0

Score : 800 points

Problem Statement

There are N rabbits on a number line. The rabbits are conveniently numbered 1 through N. The coordinate of the initial position of rabbit i is x_i.

The rabbits will now take exercise on the number line, by performing sets described below. A set consists of M jumps. The j-th jump of a set is performed by rabbit a_j (2≤a_j≤N-1). For this jump, either rabbit a_j-1 or rabbit a_j+1 is chosen with equal probability (let the chosen rabbit be rabbit x), then rabbit a_j will jump to the symmetric point of its current position with respect to rabbit x.

The rabbits will perform K sets in succession. For each rabbit, find the expected value of the coordinate of its eventual position after K sets are performed.

Constraints

  • 3≤N≤10^5
  • x_i is an integer.
  • |x_i|≤10^9
  • 1≤M≤10^5
  • 2≤a_j≤N-1
  • 1≤K≤10^{18}

Input

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

N
x_1 x_2 ... x_N
M K
a_1 a_2 ... a_M

Output

Print N lines. The i-th line should contain the expected value of the coordinate of the eventual position of rabbit i after K sets are performed. The output is considered correct if the absolute or relative error is at most 10^{-9}.


Sample Input 1

3
-1 0 2
1 1
2

Sample Output 1

-1.0
1.0
2.0

Rabbit 2 will perform the jump. If rabbit 1 is chosen, the coordinate of the destination will be -2. If rabbit 3 is chosen, the coordinate of the destination will be 4. Thus, the expected value of the coordinate of the eventual position of rabbit 2 is 0.5×(-2)+0.5×4=1.0.


Sample Input 2

3
1 -1 1
2 2
2 2

Sample Output 2

1.0
-1.0
1.0

x_i may not be distinct.


Sample Input 3

5
0 1 3 6 10
3 10
2 3 4

Sample Output 3

0.0
3.0
7.0
8.0
10.0
D - Median Pyramid Hard

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1300

問題文

N 段のピラミッドがあります。 段は上から順に 1, 2, ..., N と番号が振られています。 各 1≤i≤N について、i 段目には 2i-1 個のブロックが横一列に並んでいます。 また、各段の中央のブロックに注目すると、これらは縦一列に並んでいます。

N=4 段のピラミッド

すぬけ君は N 段目のブロックに (1, 2, ..., 2N-1) を並べ替えたもの(順列)を書き込みました。 さらに、次のルールに従い、残りすべてのブロックに整数を書き込みました。

  • あるブロックに書き込まれる整数は、そのブロックの左下、真下、右下のブロックに書き込まれた整数の中央値である。

ブロックに整数を書き込む例

その後、すぬけ君はすべてのブロックに書き込まれた整数を消してしまいました。 すぬけ君は、N 段目のブロックに書き込まれた順列が (a_1, a_2, ..., a_{2N-1}) であったことだけを覚えています。

1 段目のブロックに書き込まれた整数を求めてください。

制約

  • 2≤N≤10^5
  • (a_1, a_2, ..., a_{2N-1}) は (1, 2, ..., 2N-1) の順列である。

入力

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

N
a_1 a_2 ... a_{2N-1}

出力

1 段目のブロックに書き込まれた整数を出力せよ。


入力例 1

4
1 6 3 7 4 5 2

出力例 1

4

問題文中の図の例です。


入力例 2

2
1 2 3

出力例 2

2

Score : 1300 points

Problem Statement

We have a pyramid with N steps, built with blocks. The steps are numbered 1 through N from top to bottom. For each 1≤i≤N, step i consists of 2i-1 blocks aligned horizontally. The pyramid is built so that the blocks at the centers of the steps are aligned vertically.

A pyramid with N=4 steps

Snuke wrote a permutation of (1, 2, ..., 2N-1) into the blocks of step N. Then, he wrote integers into all remaining blocks, under the following rule:

  • The integer written into a block b must be equal to the median of the three integers written into the three blocks directly under b, or to the lower left or lower right of b.

Writing integers into the blocks

Afterwards, he erased all integers written into the blocks. Now, he only remembers that the permutation written into the blocks of step N was (a_1, a_2, ..., a_{2N-1}).

Find the integer written into the block of step 1.

Constraints

  • 2≤N≤10^5
  • (a_1, a_2, ..., a_{2N-1}) is a permutation of (1, 2, ..., 2N-1).

Input

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

N
a_1 a_2 ... a_{2N-1}

Output

Print the integer written into the block of step 1.


Sample Input 1

4
1 6 3 7 4 5 2

Sample Output 1

4

This case corresponds to the figure in the problem statement.


Sample Input 2

2
1 2 3

Sample Output 2

2
E - Rotate 3x3

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1500

問題文

3 マス、横 N マスのマス目があります。 上から i マス目、左から j マス目のマスを (i, j) と表します。 最初、マス (i, j) には整数 i+3j-3 が書かれています。

N=5 のマス目

すぬけ君は次の操作を何回か行うことができます。

  • 3×3 マスの正方形を選び、正方形内の整数の配置を 180° 回転する。

操作列の例(青い正方形が操作を行った部分)

すぬけ君の目標は、マス (i, j) に整数 a_{i,j} が書かれているようにすることです。 すぬけ君が目標を達成できるか判定してください。

制約

  • 5≤N≤10^5
  • 1≤a_{i,j}≤3N
  • a_{i,j} はすべて相異なる。

入力

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

N
a_{1,1} a_{1,2} ... a_{1,N}
a_{2,1} a_{2,2} ... a_{2,N}
a_{3,1} a_{3,2} ... a_{3,N}

出力

すぬけ君が目標を達成できるならば Yes を、できないならば No を出力せよ。


入力例 1

5
9 6 15 12 1
8 5 14 11 2
7 4 13 10 3

出力例 1

Yes

問題文中の図の例です。


入力例 2

5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

出力例 2

No

入力例 3

5
1 4 7 10 13
2 5 8 11 14
3 6 9 12 15

出力例 3

Yes

最初から目標の配置になっています。


入力例 4

6
15 10 3 4 9 16
14 11 2 5 8 17
13 12 1 6 7 18

出力例 4

Yes

入力例 5

7
21 12 1 16 13 6 7
20 11 2 17 14 5 8
19 10 3 18 15 4 9

出力例 5

No

Score : 1500 points

Problem Statement

We have a grid with 3 rows and N columns. The cell at the i-th row and j-th column is denoted (i, j). Initially, each cell (i, j) contains the integer i+3j-3.

A grid with N=5 columns

Snuke can perform the following operation any number of times:

  • Choose a 3×3 subrectangle of the grid. The placement of integers within the subrectangle is now rotated by 180°.

An example sequence of operations (each chosen subrectangle is colored blue)

Snuke's objective is to manipulate the grid so that each cell (i, j) contains the integer a_{i,j}. Determine whether it is achievable.

Constraints

  • 5≤N≤10^5
  • 1≤a_{i,j}≤3N
  • All a_{i,j} are distinct.

Input

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

N
a_{1,1} a_{1,2} ... a_{1,N}
a_{2,1} a_{2,2} ... a_{2,N}
a_{3,1} a_{3,2} ... a_{3,N}

Output

If Snuke's objective is achievable, print Yes. Otherwise, print No.


Sample Input 1

5
9 6 15 12 1
8 5 14 11 2
7 4 13 10 3

Sample Output 1

Yes

This case corresponds to the figure in the problem statement.


Sample Input 2

5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

Sample Output 2

No

Sample Input 3

5
1 4 7 10 13
2 5 8 11 14
3 6 9 12 15

Sample Output 3

Yes

The objective is already achieved with the initial placement of the integers.


Sample Input 4

6
15 10 3 4 9 16
14 11 2 5 8 17
13 12 1 6 7 18

Sample Output 4

Yes

Sample Input 5

7
21 12 1 16 13 6 7
20 11 2 17 14 5 8
19 10 3 18 15 4 9

Sample Output 5

No
F - Blackout

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1700

問題文

縦、横ともに N マスのマス目があります。 上から i マス目、左から j マス目のマスを (i, j) と表します。

最初、M 個のマスが黒く塗られており、それ以外のマスはすべて白です。 具体的には、マス (a_1, b_1), (a_2, b_2), ..., (a_M, b_M) が黒く塗られています。

すぬけ君は次のルールに従い、可能な限りマスを黒く塗っていきます。

  • ある 1≤x,y,z≤N について、マス (x, y), (y, z) がともに黒で、マス (z, x) が白ならば、マス (z, x) を黒く塗る。

すぬけ君が可能な限りマスを黒く塗ったとき、最終的に黒いマスは何個になるかを求めてください。

制約

  • 1≤N≤10^5
  • 1≤M≤10^5
  • 1≤a_i,b_i≤N
  • (a_i, b_i) はすべて相異なる。

入力

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

N M
a_1 b_1
a_2 b_2
:
a_M b_M

出力

すぬけ君が可能な限りマスを黒く塗ったとき、最終的に黒いマスは何個になるかを出力せよ。


入力例 1

3 2
1 2
2 3

出力例 1

3

次のようにマスを黒く塗っていくことができます。

  • マス (1, 2), (2, 3) がともに黒で、マス (3, 1) が白なので、マス (3, 1) を黒く塗る。

入力例 2

2 2
1 1
1 2

出力例 2

4

次のようにマスを黒く塗っていくことができます。

  • マス (1, 1), (1, 2) がともに黒で、マス (2, 1) が白なので、マス (2, 1) を黒く塗る。
  • マス (2, 1), (1, 2) がともに黒で、マス (2, 2) が白なので、マス (2, 2) を黒く塗る。

入力例 3

4 3
1 2
1 3
4 4

出力例 3

3

新たにマスを黒く塗ることができません。

Score : 1700 points

Problem Statement

We have a grid with N rows and N columns. The cell at the i-th row and j-th column is denoted (i, j).

Initially, M of the cells are painted black, and all other cells are white. Specifically, the cells (a_1, b_1), (a_2, b_2), ..., (a_M, b_M) are painted black.

Snuke will try to paint as many white cells black as possible, according to the following rule:

  • If two cells (x, y) and (y, z) are both black and a cell (z, x) is white for integers 1≤x,y,z≤N, paint the cell (z, x) black.

Find the number of black cells when no more white cells can be painted black.

Constraints

  • 1≤N≤10^5
  • 1≤M≤10^5
  • 1≤a_i,b_i≤N
  • All pairs (a_i, b_i) are distinct.

Input

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

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

Print the number of black cells when no more white cells can be painted black.


Sample Input 1

3 2
1 2
2 3

Sample Output 1

3

It is possible to paint one white cell black, as follows:

  • Since cells (1, 2) and (2, 3) are both black and a cell (3, 1) is white, paint the cell (3, 1) black.

Sample Input 2

2 2
1 1
1 2

Sample Output 2

4

It is possible to paint two white cells black, as follows:

  • Since cells (1, 1) and (1, 2) are both black and a cell (2, 1) is white, paint the cell (2, 1) black.
  • Since cells (2, 1) and (1, 2) are both black and a cell (2, 2) is white, paint the cell (2, 2) black.

Sample Input 3

4 3
1 2
1 3
4 4

Sample Output 3

3

No white cells can be painted black.