A - Insert

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

長さ N の整数列 A と整数 K,X が与えられます。
整数列 AK 要素目の直後に整数 X1 つ挿入した整数列 B を出力してください。

制約

  • 入力は全て整数
  • 1 \le K \le N \le 100
  • 1 \le A_i,X \le 100

入力

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

N K X
A_1 A_2 \dots A_N

出力

整数列 AK 要素目の直後に整数 X1 つ挿入した整数列 B を、以下の形式で出力せよ。

B_1 B_2 \dots B_{N+1}

入力例 1

4 3 7
2 3 5 11

出力例 1

2 3 5 7 11

K=3, X=7, A=(2,3,5,11) のとき、 B=(2,3,5,7,11) です。


入力例 2

1 1 100
100

出力例 2

100 100

入力例 3

8 8 3
9 9 8 2 4 4 3 5

出力例 3

9 9 8 2 4 4 3 5 3

Score : 100 points

Problem Statement

You are given an integer sequence A of length N and integers K and X.
Print the integer sequence B obtained by inserting the integer X immediately after the K-th element of the sequence A.

Constraints

  • All input values are integers.
  • 1 \le K \le N \le 100
  • 1 \le A_i, X \le 100

Input

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

N K X
A_1 A_2 \dots A_N

Output

Print the integer sequence B obtained by inserting the integer X immediately after the K-th element of the sequence A, in the following format:

B_1 B_2 \dots B_{N+1}

Sample Input 1

4 3 7
2 3 5 11

Sample Output 1

2 3 5 7 11

For K=3, X=7, and A=(2,3,5,11), we get B=(2,3,5,7,11).


Sample Input 2

1 1 100
100

Sample Output 2

100 100

Sample Input 3

8 8 3
9 9 8 2 4 4 3 5

Sample Output 3

9 9 8 2 4 4 3 5 3
B - Intersection of Cuboids

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

あなたは3Dゲームの当たり判定を実装しようとしています。

3 次元空間内の直方体であって、2(a,b,c),(d,e,f) を結ぶ線分を対角線とし、全ての面が xy 平面、yz 平面、zx 平面のいずれかに平行なものを C(a,b,c,d,e,f) と表します。
(この定義により C(a,b,c,d,e,f) は一意に定まります)

2 つの直方体 C(a,b,c,d,e,f)C(g,h,i,j,k,l) が与えられるので、これらの共通部分の体積が正かどうか判定してください。

制約

  • 0 \leq a < d \leq 1000
  • 0 \leq b < e \leq 1000
  • 0 \leq c < f \leq 1000
  • 0 \leq g < j \leq 1000
  • 0 \leq h < k \leq 1000
  • 0 \leq i < l \leq 1000
  • 入力は全て整数

入力

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

a b c d e f
g h i j k l

出力

2 つの直方体の共通部分の体積が正なら Yes、そうでなければ No を出力せよ。


入力例 1

0 0 0 4 5 6
2 3 4 5 6 7

出力例 1

Yes

2 つの直方体の位置関係は下図のようになっており、共通部分の体積は 8 です。


入力例 2

0 0 0 2 2 2
0 0 2 2 2 4

出力例 2

No

2 つの直方体は面で接していますが、共通部分の体積は 0 です。


入力例 3

0 0 0 1000 1000 1000
10 10 10 100 100 100

出力例 3

Yes

Score : 250 points

Problem Statement

You are trying to implement collision detection in a 3D game.

In a 3-dimensional space, let C(a,b,c,d,e,f) denote the cuboid with a diagonal connecting (a,b,c) and (d,e,f), and with all faces parallel to the xy-plane, yz-plane, or zx-plane.
(This definition uniquely determines C(a,b,c,d,e,f).)

Given two cuboids C(a,b,c,d,e,f) and C(g,h,i,j,k,l), determine whether their intersection has a positive volume.

Constraints

  • 0 \leq a < d \leq 1000
  • 0 \leq b < e \leq 1000
  • 0 \leq c < f \leq 1000
  • 0 \leq g < j \leq 1000
  • 0 \leq h < k \leq 1000
  • 0 \leq i < l \leq 1000
  • All input values are integers.

Input

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

a b c d e f
g h i j k l

Output

Print Yes if the intersection of the two cuboids has a positive volume, and No otherwise.


Sample Input 1

0 0 0 4 5 6
2 3 4 5 6 7

Sample Output 1

Yes

The positional relationship of the two cuboids is shown in the figure below, and their intersection has a volume of 8.


Sample Input 2

0 0 0 2 2 2
0 0 2 2 2 4

Sample Output 2

No

The two cuboids touch at a face, where the volume of the intersection is 0.


Sample Input 3

0 0 0 1000 1000 1000
10 10 10 100 100 100

Sample Output 3

Yes
C - Make Them Narrow

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

長さ N の数列 A が与えられます。
A のうち丁度 K 要素を自由に選んで消し、残った要素を順序を保って連結した数列を B とします。
( B の最大値 ) - ( B の最小値 ) としてありうる最小値を求めてください。

制約

  • 入力は全て整数
  • 1 \le K < N \le 2 \times 10^5
  • 1 \le A_i \le 10^9

入力

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

N K
A_1 A_2 \dots A_N

出力

答えを整数として出力せよ。


入力例 1

5 2
3 1 5 4 9

出力例 1

2

A=(3,1,5,4,9) から丁度 2 要素を自由に選んで消すことを考えます。

  • 例えば 2 要素目の 15 要素目の 9 を消すと、消した後の数列 B=(3,5,4) となります。
    • このとき B の最大値は 5 、最小値は 3 なので ( B の最大値 ) - ( B の最小値 ) =2 となり、これは達成可能な最小値です。

入力例 2

6 5
1 1 1 1 1 1

出力例 2

0

入力例 3

8 3
31 43 26 6 18 36 22 13

出力例 3

18

Score : 250 points

Problem Statement

You are given a sequence A of length N.
Freely choose exactly K elements from A and remove them, then concatenate the remaining elements in their original order to form a new sequence B.
Find the minimum possible value of this: the maximum value of B minus the minimum value of B.

Constraints

  • All inputs are integers.
  • 1 \le K < N \le 2 \times 10^5
  • 1 \le A_i \le 10^9

Input

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

N K
A_1 A_2 \dots A_N

Output

Print the answer as an integer.


Sample Input 1

5 2
3 1 5 4 9

Sample Output 1

2

Consider removing exactly two elements from A=(3,1,5,4,9).

  • For example, if you remove the 2nd element 1 and the 5th element 9, the resulting sequence is B=(3,5,4).
    • In this case, the maximum value of B is 5 and the minimum value is 3, so (maximum value of B) - (minimum value of B) =2, which is the minimum possible value.

Sample Input 2

6 5
1 1 1 1 1 1

Sample Output 2

0

Sample Input 3

8 3
31 43 26 6 18 36 22 13

Sample Output 3

18
D - Go Stone Puzzle

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425

問題文

N+2 個のマスが横一列に並んでいます。左から i 番目のマスをマス i と表します。

マス 1 からマス N には石が 1 個ずつ置かれています。
1\leq i \leq N について、S_iW のときマス i に置かれている石の色は白であり、S_iB のときマス i に置かれている石の色は黒です。
マス N+1,N+2 には何も置かれていません。

あなたは以下の操作を好きな回数(0 回でもよい)行うことができます。

  • 石が 2 個並んでいる箇所を選び、その 2 個の石を順序を保って空きマスに移す。
    より正確には次の通り。1 以上 N+1 以下の整数 x であって、マス x,x+1 の両方に石が置かれているものを選ぶ。石の置かれていないマスを k,k+1 とする。マス x,x+1 にある石をそれぞれマス k,k+1 に移動する。

以下の状態にすることが可能か判定し、可能なら操作回数の最小値を求めてください。

  • マス 1 からマス N には石が 1 個ずつ置かれており、各 1\leq i \leq N について、T_iW のときマス i に置かれている石の色は白、T_iB のときマス i に置かれている石の色は黒である。

制約

  • 2 \leq N \leq 14
  • N は整数である
  • S,TB および W のみからなる長さ N の文字列である

入力

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

N
S
T

出力

目的の状態にすることが可能なら操作回数の最小値を出力せよ。不可能ならかわりに -1 を出力せよ。


入力例 1

6
BWBWBW
WWWBBB

出力例 1

4

石が置かれていないマスを . と表します。以下のようにして 4 回の操作で目的の状態にすることができ、これが最小回数です。

  • BWBWBW..
  • BW..BWBW
  • BWWBB..W
  • ..WBBBWW
  • WWWBBB..

入力例 2

6
BBBBBB
WWWWWW

出力例 2

-1

入力例 3

14
BBBWBWWWBBWWBW
WBWWBBWWWBWBBB

出力例 3

7

Score : 425 points

Problem Statement

There are N+2 cells arranged in a row. Let cell i denote the i-th cell from the left.

There is one stone placed in each of the cells from cell 1 to cell N.
For each 1 \leq i \leq N, the stone in cell i is white if S_i is W, and black if S_i is B.
Cells N+1 and N+2 are empty.

You can perform the following operation any number of times (possibly zero):

  • Choose a pair of adjacent cells that both contain stones, and move these two stones to the empty two cells while preserving their order.
    More precisely, choose an integer x such that 1 \leq x \leq N+1 and both cells x and x+1 contain stones. Let k and k+1 be the empty two cells. Move the stones from cells x and x+1 to cells k and k+1, respectively.

Determine if it is possible to achieve the following state, and if so, find the minimum number of operations required:

  • Each of the cells from cell 1 to cell N contains one stone, and for each 1 \leq i \leq N, the stone in cell i is white if T_i is W, and black if T_i is B.

Constraints

  • 2 \leq N \leq 14
  • N is an integer.
  • Each of S and T is a string of length N consisting of B and W.

Input

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

N
S
T

Output

If it is possible to achieve the desired state, print the minimum number of operations required. If it is impossible, print -1.


Sample Input 1

6
BWBWBW
WWWBBB

Sample Output 1

4

Using . to represent an empty cell, the desired state can be achieved in four operations as follows, which is the minimum:

  • BWBWBW..
  • BW..BWBW
  • BWWBB..W
  • ..WBBBWW
  • WWWBBB..

Sample Input 2

6
BBBBBB
WWWWWW

Sample Output 2

-1

Sample Input 3

14
BBBWBWWWBBWWBW
WBWWBBWWWBWBBB

Sample Output 3

7
E - Tree and Hamilton Path 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

AtCoder国には 1 から N の番号がついた N 個の街と 1 から N-1 の番号がついた N-1 本の道路があります。

道路 i は街 A_i と街 B_i を双方向に結び、長さは C_i です。どの街同士も、いくつかの道路を通って互いに行き来することができます。

いずれかの街を出発し、道路による移動で全ての街を 1 度以上訪れるための移動距離の最小値を求めてください。

制約

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq A_i,B_i \leq N
  • 1 \leq C_i \leq 10^9
  • 入力は全て整数である
  • どの街同士も、いくつかの道路を通って互いに行き来できる

入力

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

N
A_1 B_1 C_1
\vdots
A_{N-1} B_{N-1} C_{N-1}

出力

答えを出力せよ。


入力例 1

4
1 2 2
1 3 3
1 4 4

出力例 1

11

4 \to 1 \to 2 \to 1 \to 3 と移動すると移動距離の合計は 11 となり、これが最小値です。

最初の街に戻ってくる必要はないことに注意してください。


入力例 2

10
10 9 1000000000
9 8 1000000000
8 7 1000000000
7 6 1000000000
6 5 1000000000
5 4 1000000000
4 3 1000000000
3 2 1000000000
2 1 1000000000

出力例 2

9000000000

オーバーフローに注意してください。

Score : 500 points

Problem Statement

In the nation of AtCoder, there are N cities numbered 1 to N and N-1 roads numbered 1 to N-1.

Road i connects cities A_i and B_i bidirectionally, and its length is C_i. Any pair of cities can be reached from each other by traveling through some roads.

Find the minimum travel distance required to start from a city and visit all cities at least once using the roads.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq A_i, B_i \leq N
  • 1 \leq C_i \leq 10^9
  • All input values are integers.
  • Any pair of cities can be reached from each other by traveling through some roads.

Input

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

N
A_1 B_1 C_1
\vdots
A_{N-1} B_{N-1} C_{N-1}

Output

Print the answer.


Sample Input 1

4
1 2 2
1 3 3
1 4 4

Sample Output 1

11

If you travel as 4 \to 1 \to 2 \to 1 \to 3, the total travel distance is 11, which is the minimum.

Note that you do not need to return to the starting city.


Sample Input 2

10
10 9 1000000000
9 8 1000000000
8 7 1000000000
7 6 1000000000
6 5 1000000000
5 4 1000000000
4 3 1000000000
3 2 1000000000
2 1 1000000000

Sample Output 2

9000000000

Beware overflow.

F - x = a^b

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

1 以上 N 以下の正整数 x であって、ある正整数 a2 以上の 正整数 b を用いて x=a^b と表現できるものはいくつありますか?

制約

  • 入力は全て整数
  • 1 \le N \le 10^{18}

入力

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

N

出力

答えを整数として出力せよ。


入力例 1

99

出力例 1

12

問題文中の条件を満たす整数は 1,4,8,9,16,25,27,32,36,49,64,8112 個です。


入力例 2

1000000000000000000

出力例 2

1001003332

Score : 500 points

Problem Statement

How many integers x between 1 and N, inclusive, can be expressed as x = a^b using some positive integer a and a positive integer b not less than 2?

Constraints

  • All input values are integers.
  • 1 \le N \le 10^{18}

Input

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

N

Output

Print the answer as an integer.


Sample Input 1

99

Sample Output 1

12

The integers that satisfy the conditions in the problem statement are 1, 4, 8, 9, 16, 25, 27, 32, 36, 49, 64, 81: there are 12.


Sample Input 2

1000000000000000000

Sample Output 2

1001003332
G - Go Territory

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 600

問題文

2 次元平面上に N 個の石が置かれています。i 番目の石は座標 (X_i,Y_i) にあります。石は全て第一象限(軸上含む)の格子点にあります。

石の置かれていない格子点 (x,y) であって、上下左右のいずれかに 1 移動することを繰り返すことで、石の置かれている座標を通らずに (-1,-1) に到達することができないものの個数を求めてください。

より正確には、石の置かれていない格子点 (x,y) であって、以下の 4 条件を全て満たすような整数の組の有限列 (x_0,y_0),\ldots,(x_k,y_k) が存在しないものの個数を求めてください。

  • (x_0,y_0)=(x,y)
  • (x_k,y_k)=(-1,-1)
  • 全ての 0\leq i < k|x_i-x_{i+1}|+|y_i-y_{i+1}|=1
  • どの 0 \leq i \leq k でも、(x_i,y_i) に石はない

制約

  • 0 \leq N \leq 2\times 10^5
  • 0 \leq X_i,Y_i \leq 2\times 10^5
  • (X_i,Y_i) は相異なる
  • 入力は全て整数

入力

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

N
X_1 Y_1
\vdots
X_N Y_N

出力

条件を満たす格子点の個数を出力せよ。


入力例 1

5
1 0
0 1
2 3
1 2
2 1

出力例 1

1

(1,1) から (-1,-1) に到達することができません。


入力例 2

0

出力例 2

0

石が 1 つも置かれていないこともあります。


入力例 3

22
0 1
0 2
0 3
1 0
1 4
2 0
2 2
2 4
3 0
3 1
3 2
3 4
5 1
5 2
5 3
6 0
6 4
7 0
7 4
8 1
8 2
8 3

出力例 3

6

(6,1),(6,2),(6,3),(7,1),(7,2),(7,3)6 個が該当します。

Score : 600 points

Problem Statement

There are N stones placed on a 2-dimensional plane. The i-th stone is located at coordinates (X_i, Y_i). All stones are located at lattice points in the first quadrant (including the axes).

Count the number of lattice points (x, y) where no stone is placed and it is impossible to reach (-1, -1) from (x, y) by repeatedly moving up, down, left, or right by 1 without passing through coordinates where a stone is placed.

More precisely, count the number of lattice points (x, y) where no stone is placed, and there does not exist a finite sequence of integer pairs (x_0, y_0), \ldots, (x_k, y_k) that satisfies all of the following four conditions:

  • (x_0, y_0) = (x, y).
  • (x_k, y_k) = (-1, -1).
  • |x_i - x_{i+1}| + |y_i - y_{i+1}| = 1 for all 0 \leq i < k.
  • There is no stone at (x_i, y_i) for all 0 \leq i \leq k.

Constraints

  • 0 \leq N \leq 2 \times 10^5
  • 0 \leq X_i, Y_i \leq 2 \times 10^5
  • The pairs (X_i, Y_i) are distinct.
  • All input values are integers.

Input

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

N
X_1 Y_1
\vdots
X_N Y_N

Output

Print the number of lattice points that satisfy the conditions.


Sample Input 1

5
1 0
0 1
2 3
1 2
2 1

Sample Output 1

1

It is impossible to reach (-1, -1) from (1, 1).


Sample Input 2

0

Sample Output 2

0

There may be cases where no stones are placed.


Sample Input 3

22
0 1
0 2
0 3
1 0
1 4
2 0
2 2
2 4
3 0
3 1
3 2
3 4
5 1
5 2
5 3
6 0
6 4
7 0
7 4
8 1
8 2
8 3

Sample Output 3

6

There are six such points: (6, 1), (6, 2), (6, 3), (7, 1), (7, 2), (7, 3).