実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
長さ N の数列 A = (A_1, A_2, \dots, A_N) が与えられます。
あなたは次の操作をちょうど K 回行います。
- A の先頭の要素を取り除き、A の末尾に 0 を挿入する。
操作を行った後の A の要素をすべて出力してください。
制約
- 1 \leq N \leq 100
- 1 \leq K \leq 100
- 1 \leq A_i \leq 100
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_N
出力
操作を行った後の A の要素を空白区切りで 1 行に出力せよ。
入力例 1
3 2 2 7 8
出力例 1
8 0 0
操作を行う前は A = (2, 7, 8) です。
操作を 1 回行った時点では A = (7, 8, 0) です。
操作を 2 回行った時点では A = (8, 0, 0) です。
よって (8, 0, 0) が答えです。
入力例 2
3 4 9 9 9
出力例 2
0 0 0
入力例 3
9 5 1 2 3 4 5 6 7 8 9
出力例 3
6 7 8 9 0 0 0 0 0
Score : 100 points
Problem Statement
You are given a sequence A = (A_1, A_2, \dots, A_N) of length N.
You perform the following operation exactly K times:
- remove the initial element of A and append a 0 to the tail of A.
Print all the elements of A after the operations.
Constraints
- 1 \leq N \leq 100
- 1 \leq K \leq 100
- 1 \leq A_i \leq 100
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_N
Output
Print the elements of A after the operations in one line, separated by spaces.
Sample Input 1
3 2 2 7 8
Sample Output 1
8 0 0
Before the operations, A = (2, 7, 8).
After performing the operation once, A = (7, 8, 0).
After performing the operation twice, A = (8, 0, 0).
Thus, (8, 0, 0) is the answer.
Sample Input 2
3 4 9 9 9
Sample Output 2
0 0 0
Sample Input 3
9 5 1 2 3 4 5 6 7 8 9
Sample Output 3
6 7 8 9 0 0 0 0 0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
<, =, > のみからなる文字列 S が与えられます。
S が 双方向矢印型 の文字列であるか判定してください。
ただし、文字列 S が双方向矢印型の文字列であるとは、
ある正整数 k が存在して、
S が 1 個の < 、k 個の = 、1 個の > をこの順に連結した長さ (k+2) の文字列であることをいいます。
制約
- S は
<,=,>のみからなる長さ 3 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S が 双方向矢印型 の文字列ならば Yes を、そうでないならば No を出力せよ。
入力例 1
<====>
出力例 1
Yes
<====> は、1 個の < 、4 個の = 、1 個の > をこの順に連結した文字列であり、双方向矢印型の文字列です。
よって、Yes を出力します。
入力例 2
==>
出力例 2
No
==> は双方向矢印型の文字列の条件をみたしていません。
よって、No を出力します。
入力例 3
<>>
出力例 3
No
Scoring: 100 points
Problem Statement
You are given a string S consisting of <, =, and >.
Determine whether S is a bidirectional arrow string.
A string S is a bidirectional arrow string if and only if there is a positive integer k such that S is a concatenation of one <, k =s, and one >, in this order, with a length of (k+2).
Constraints
- S is a string of length between 3 and 100, inclusive, consisting of
<,=, and>.
Input
The input is given from Standard Input in the following format:
S
Output
If S is a bidirectional arrow string, print Yes; otherwise, print No.
Sample Input 1
<====>
Sample Output 1
Yes
<====> is a concatenation of one <, four =s, and one >, in this order, so it is a bidirectional arrow string.
Hence, print Yes.
Sample Input 2
==>
Sample Output 2
No
==> does not meet the condition for a bidirectional arrow string.
Hence, print No.
Sample Input 3
<>>
Sample Output 3
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
次の図に示す、各マスが黒または白に塗られた縦 15 行 \times 横 15 列のグリッドにおいて、 上から R 行目、左から C 列目のマスが何色かを出力して下さい。

制約
- 1 \leq R, C \leq 15
- R, C は整数
入力
入力は以下の形式で標準入力から与えられる。
R C
出力
図のグリッドにおいて上から R 行目、左から C 列目のマスが黒色の場合は black と、白色の場合は white と出力せよ。
ジャッジは英小文字と英大文字を厳密に区別することに注意せよ。
入力例 1
3 5
出力例 1
black
図のグリッドにおいて上から 3 行目、左から 5 列目のマスは黒色です。
よって、black と出力します。
入力例 2
4 5
出力例 2
white
図のグリッドにおいて上から 4 行目、左から 5 列目のマスは白色です。
よって、white と出力します。
Score : 200 points
Problem Statement
Print the color of the cell at the R-th row from the top and C-th column from the left in the following grid with 15 vertical rows and 15 horizontal columns.

Constraints
- 1 \leq R, C \leq 15
- R and C are integers.
Input
Input is given from Standard Input in the following format:
R C
Output
In the grid above, if the color of the cell at the R-th row from the top and C-th column from the left is black, then print black; if the cell is white, then print white. Note that the judge is case-sensitive.
Sample Input 1
3 5
Sample Output 1
black
In the grid above, the cell at the 3-rd row from the top and 5-th column from the left is black. Thus, black should be printed.
Sample Input 2
4 5
Sample Output 2
white
In the grid above, the cell at the 4-th row from the top and 5-th column from the left is white. Thus, white should be printed.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
長さ N の整数列 A = (A_1, A_2, \dots, A_N), B = (B_1, B_2, \dots, B_N) が与えられます。
A の要素はすべて異なります。B の要素もすべて異なります。
次の 2 つを出力してください。
- A にも B にも含まれ、その位置も一致している整数の個数。言い換えると、A_i = B_i を満たす整数 i の個数。
- A にも B にも含まれるが、その位置は異なる整数の個数。言い換えると、A_i = B_j, i \neq j を満たす整数の組 (i, j) の個数。
制約
- 1 \leq N \leq 1000
- 1 \leq A_i \leq 10^9
- 1 \leq B_i \leq 10^9
- A_1, A_2, \dots, A_N はすべて異なる。
- B_1, B_2, \dots, B_N はすべて異なる。
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
出力
答えを 2 行出力せよ。1 行目には 1. の個数、2 行目には 2. の個数を出力せよ。
入力例 1
4 1 3 5 2 2 3 1 4
出力例 1
1 2
A にも B にも含まれ、その位置も一致している整数は A_2 = B_2 = 3 の 1 個です。
A にも B にも含まれるが、その位置は異なる整数は A_1 = B_3 = 1 と A_4 = B_1 = 2 の 2 個です。
入力例 2
3 1 2 3 4 5 6
出力例 2
0 0
1., 2. ともに条件を満たす整数は存在しません。
入力例 3
7 4 8 1 7 9 5 6 3 5 1 7 8 2 6
出力例 3
3 2
Score : 200 points
Problem Statement
You are given integer sequences, each of length N: A = (A_1, A_2, \dots, A_N) and B = (B_1, B_2, \dots, B_N).
All elements of A are different. All elements of B are different, too.
Print the following two values.
- The number of integers contained in both A and B, appearing at the same position in the two sequences. In other words, the number of integers i such that A_i = B_i.
- The number of integers contained in both A and B, appearing at different positions in the two sequences. In other words, the number of pairs of integers (i, j) such that A_i = B_j and i \neq j.
Constraints
- 1 \leq N \leq 1000
- 1 \leq A_i \leq 10^9
- 1 \leq B_i \leq 10^9
- A_1, A_2, \dots, A_N are all different.
- B_1, B_2, \dots, B_N are all different.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
Output
Print the answers in two lines: the answer to1. in the first line, and the answer to2. in the second line.
Sample Input 1
4 1 3 5 2 2 3 1 4
Sample Output 1
1 2
There is one integer contained in both A and B, appearing at the same position in the two sequences: A_2 = B_2 = 3.
There are two integers contained in both A and B, appearing at different positions in the two sequences: A_1 = B_3 = 1 and A_4 = B_1 = 2.
Sample Input 2
3 1 2 3 4 5 6
Sample Output 2
0 0
In both 1. and 2., no integer satisfies the condition.
Sample Input 3
7 4 8 1 7 9 5 6 3 5 1 7 8 2 6
Sample Output 3
3 2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
1,2,\dots,N がちょうど 3 回ずつ現れる長さ 3N の数列 A=(A_1,A_2,\dots,A_{3N}) が与えられます。
i=1,2,\dots,N について、A の中にある i のうち真ん中にあるものの添字を f(i) と定めます。 1,2,\dots,N を f(i) の昇順に並べ替えてください。
f(i) の定義は厳密には以下の通りです。
- A_j = i を満たす j が j=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma) であるとする。このとき、f(i) = \beta である。
制約
- 1\leq N \leq 10^5
- 1 \leq A_j \leq N
- i=1,2,\dots,N それぞれについて、A の中に i はちょうど 3 回現れる
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 A_2 \dots A_{3N}
出力
1,2,\dots,N を f(i) の昇順に並べ替えてできる長さ N の数列を空白区切りで出力せよ。
入力例 1
3 1 1 3 2 3 2 2 3 1
出力例 1
1 3 2
- A の中にある 1 は A_1,A_2,A_9 なので、f(1) = 2 です。
- A の中にある 2 は A_4,A_6,A_7 なので、f(2) = 6 です。
- A の中にある 3 は A_3,A_5,A_8 なので、f(3) = 5 です。
よって、f(1) < f(3) < f(2) であるため 1,3,2 の順に出力します。
入力例 2
1 1 1 1
出力例 2
1
入力例 3
4 2 3 4 3 4 1 3 1 1 4 2 2
出力例 3
3 4 1 2
Score : 250 points
Problem Statement
You are given a sequence A=(A_1,A_2,\dots,A_{3N}) of length 3N where each of 1,2,\dots, and N occurs exactly three times.
For i=1,2,\dots,N, let f(i) be the index of the middle occurrence of i in A. Sort 1,2,\dots,N in ascending order of f(i).
Formally, f(i) is defined as follows.
- Suppose that those j such that A_j = i are j=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma). Then, f(i) = \beta.
Constraints
- 1\leq N \leq 10^5
- 1 \leq A_j \leq N
- i occurs in A exactly three times, for each i=1,2,\dots,N.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 \dots A_{3N}
Output
Print the sequence of length N obtained by sorting 1,2,\dots,N in ascending order of f(i), separated by spaces.
Sample Input 1
3 1 1 3 2 3 2 2 3 1
Sample Output 1
1 3 2
- 1 occurs in A at A_1,A_2,A_9, so f(1) = 2.
- 2 occurs in A at A_4,A_6,A_7, so f(2) = 6.
- 3 occurs in A at A_3,A_5,A_8, so f(3) = 5.
Thus, f(1) < f(3) < f(2), so 1,3, and 2 should be printed in this order.
Sample Input 2
1 1 1 1
Sample Output 2
1
Sample Input 3
4 2 3 4 3 4 1 3 1 1 4 2 2
Sample Output 3
3 4 1 2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
AtCoder 鉄道のとある路線には N 個の駅が存在し、始点から終点に向かって i \, (1 \leq i \leq N) 番目の駅の名前は S_i です。
普通列車は全ての駅に止まりますが、急行列車は全ての駅に止まるとは限りません。具体的には、急行列車は M \, (M \leq N) 個の駅にのみ止まり、j \, (1 \leq j \leq M) 番目に止まる駅の名前は T_j です。
ただし、T_1 = S_1 かつ T_M = S_N、すなわち急行列車は始点と終点の両方に止まることが保証されます。
N 個の駅それぞれについて、その駅に急行列車が止まるかどうか判定してください。
制約
- 2 \leq M \leq N \leq 10^5
- N, M は整数
- S_i \, (1 \leq i \leq N) は英小文字のみからなる 1 文字以上 10 文字以下の文字列
- S_i \neq S_j \, (i \neq j)
- T_1 = S_1 かつ T_M = S_N
- (T_1, \dots, T_M) は (S_1, \dots, S_N) から 0 個以上の文字列を選んで取り除き、残った文字列を元の順序で並べることで得られる
入力
入力は以下の形式で標準入力から与えられる。
N M S_1 \ldots S_N T_1 \ldots T_M
出力
N 行出力せよ。i \, (1 \leq i \leq N) 行目には、始点から終点に向かって i 番目の駅に急行列車が止まるなら Yes、そうでないなら No と出力せよ。
入力例 1
5 3 tokyo kanda akiba okachi ueno tokyo akiba ueno
出力例 1
Yes No Yes No Yes
入力例 2
7 7 a t c o d e r a t c o d e r
出力例 2
Yes Yes Yes Yes Yes Yes Yes
急行列車が全ての駅に止まることもあります。
Score : 300 points
Problem Statement
There are N stations on a certain line operated by AtCoder Railway. The i-th station (1 \leq i \leq N) from the starting station is named S_i.
Local trains stop at all stations, while express trains may not. Specifically, express trains stop at only M \, (M \leq N) stations, and the j-th stop (1 \leq j \leq M) is the station named T_j.
Here, it is guaranteed that T_1 = S_1 and T_M = S_N, that is, express trains stop at both starting and terminal stations.
For each of the N stations, determine whether express trains stop at that station.
Constrains
- 2 \leq M \leq N \leq 10^5
- N and M are integers.
- S_i (1 \leq i \leq N) is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
- S_i \neq S_j \, (i \neq j)
- T_1 = S_1 and T_M = S_N.
- (T_1, \dots, T_M) is obtained by removing zero or more strings from (S_1, \dots, S_N) and lining up the remaining strings without changing the order.
Input
Input is given from Standard Input in the following format:
N M S_1 \ldots S_N T_1 \ldots T_M
Output
Print N lines. The i-th line (1 \leq i \leq N) should contain Yes if express trains stop at the i-th station from the starting station, and No otherwise.
Sample Input 1
5 3 tokyo kanda akiba okachi ueno tokyo akiba ueno
Sample Output 1
Yes No Yes No Yes
Sample Input 2
7 7 a t c o d e r a t c o d e r
Sample Output 2
Yes Yes Yes Yes Yes Yes Yes
Express trains may stop at all stations.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
数直線上に 1 から N の番号がつけられた N 匹の蟻がいます。 蟻 i (1 \leq i \leq N) ははじめ座標 X_i にいて、正負どちらかの方向を向いています。はじめに全ての蟻は相異なる座標にいます。各蟻が向いている方向は長さ N の 01 文字列 S で表され、S_i が 0 のとき蟻 i は負の方向を向いており、 1 のとき蟻 i は正の方向を向いています。
現在を時刻 0 とし、時刻 (T+0.1) までの (T+0.1) 単位時間にわたって、N 匹の蟻がそれぞれの向いている方向に向かって単位時間あたり 1 の速さで移動します。 複数の蟻が同じ座標に到達すると、それらの蟻はすれ違い、方向や速度を変えずに通り過ぎます。 (T+0.1) 単位時間が経過したとき、すべての蟻は停止します。
1 \leq i < j \leq N を満たし、今から時刻 (T+0.1) までに蟻 i と蟻 j がすれ違う整数の組 (i,j) の個数を求めてください。
制約
- 2 \leq N \leq 2 \times 10^{5}
- 1 \leq T \leq 10^{9}
- S は
0と1からなる長さ N の文字列 - -10^{9} \leq X_i \leq 10^{9} (1 \leq i \leq N)
- X_i \neq X_j (1 \leq i < j \leq N)
- N,T,X_i (1 \leq i \leq N) は整数
入力
入力は以下の形式で標準入力から与えられる。
N T S X_1 X_2 ... X_N
出力
答えを出力せよ。
入力例 1
6 3 101010 -5 -1 0 1 2 4
出力例 1
5
以下の 5 つの蟻の組み合わせがすれ違います。
- 蟻 3 と蟻 4 が時刻 0.5 にすれ違う。
- 蟻 5 と蟻 6 が時刻 1 にすれ違う。
- 蟻 1 と蟻 2 が時刻 2 にすれ違う。
- 蟻 3 と蟻 6 が時刻 2 にすれ違う。
- 蟻 1 と蟻 4 が時刻 3 にすれ違う。
これ以外の蟻の組み合わせはすれ違うことはないため、5 を出力します。
入力例 2
13 656320850 0100110011101 -900549713 -713494784 -713078652 -687818593 -517374932 -498415009 -472742091 -390030458 -379340552 -237481538 -44636942 352721061 695864366
出力例 2
14
Score : 350 points
Problem Statement
There are N ants on a number line, labeled 1 to N. Ant i (1 \leq i \leq N) starts at coordinate X_i and faces either a positive or negative direction. Initially, all ants are at distinct coordinates. The direction each ant is facing is represented by a binary string S of length N, where ant i is facing the negative direction if S_i is 0 and the positive direction if S_i is 1.
Let the current time be 0, and the ants move in their respective directions at a speed of 1 unit per unit time for (T+0.1) units of time until time (T+0.1). If multiple ants reach the same coordinate, they pass through each other without changing direction or speed. After (T+0.1) units of time, all ants stop.
Find the number of pairs (i, j) such that 1 \leq i < j \leq N and ants i and j pass each other from now before time (T+0.1).
Constraints
- 2 \leq N \leq 2 \times 10^{5}
- 1 \leq T \leq 10^{9}
- S is a string of length N consisting of
0and1. - -10^{9} \leq X_i \leq 10^{9} (1 \leq i \leq N)
- X_i \neq X_j (1 \leq i < j \leq N)
- N, T, and X_i (1 \leq i \leq N) are integers.
Input
The input is given from Standard Input in the following format:
N T S X_1 X_2 ... X_N
Output
Print the answer.
Sample Input 1
6 3 101010 -5 -1 0 1 2 4
Sample Output 1
5
The following five pairs of ants pass each other:
- Ant 3 and ant 4 pass each other at time 0.5.
- Ant 5 and ant 6 pass each other at time 1.
- Ant 1 and ant 2 pass each other at time 2.
- Ant 3 and ant 6 pass each other at time 2.
- Ant 1 and ant 4 pass each other at time 3.
No other pairs of ants pass each other, so print 5.
Sample Input 2
13 656320850 0100110011101 -900549713 -713494784 -713078652 -687818593 -517374932 -498415009 -472742091 -390030458 -379340552 -237481538 -44636942 352721061 695864366
Sample Output 2
14
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 個の頂点と M 本の辺からなる無向グラフが与えられます。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ無向辺であり、
a_i = 1 ならばはじめは通行可能、a_i = 0 ならばはじめは通行不能です。
また、頂点 s_1 、頂点 s_2 、\ldots 、頂点 s_K の K 個の頂点にはスイッチがあります。
高橋君は、はじめ頂点 1 におり、「下記の移動とスイッチを押すの 2 つの行動のどちらかを行うこと」を好きなだけ繰り返します。
- 移動 : いまいる頂点と通行可能な辺を介して隣接する頂点を 1 つ選び、その頂点に移動する。
- スイッチを押す : いまいる頂点にスイッチがあるならば、そのスイッチを押す。その結果、グラフ上のすべての辺の通行可能・通行不能の状態が反転する。すなわち、通行可能である辺は通行不能に、通行不能である辺は通行可能に変化する。
高橋君が頂点 N に到達することが可能かどうかを判定し、可能な場合は頂点 N に到達するまでに行う移動の回数としてあり得る最小値を出力してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 0 \leq K \leq N
- 1 \leq u_i, v_i \leq N
- u_i \neq v_i
- a_i \in \lbrace 0, 1\rbrace
- 1 \leq s_1 \lt s_2 \lt \cdots \lt s_K \leq N
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M K u_1 v_1 a_1 u_2 v_2 a_2 \vdots u_M v_M a_M s_1 s_2 \ldots s_K
出力
高橋君が頂点 N に到達することが不可能な場合は -1 を、 可能な場合は頂点 N に到達するまでに行う移動の回数としてあり得る最小値を出力せよ。
入力例 1
5 5 2 1 3 0 2 3 1 5 4 1 2 1 1 1 4 0 3 4
出力例 1
5
高橋君は、下記の手順で行動することで頂点 N に到達できます。
- 頂点 1 から頂点 2 に移動する。
- 頂点 2 から頂点 3 に移動する。
- 頂点 3 にあるスイッチを押す。これによって、グラフ上のすべての辺の通行可能・通行不能の状態が反転する。
- 頂点 3 から頂点 1 に移動する。
- 頂点 1 から頂点 4 に移動する。
- 頂点 4 にあるスイッチを押す。これによって、グラフ上のすべての辺の通行可能・通行不能の状態が再び反転する。
- 頂点 4 から頂点 5 に移動する。
この手順における移動の回数は 5 回であり、これが考えられる最小の回数です。
入力例 2
4 4 2 4 3 0 1 2 1 1 2 0 2 1 1 2 4
出力例 2
-1
与えられるグラフは、連結でないことや、多重辺を含むことがあります。 この入力例では、高橋君はどのように行動しても頂点 N に到達することはできないので、-1 を出力します。
Score : 500 points
Problem Statement
You are given an undirected graph consisting of N vertices and M edges.
For i = 1, 2, \ldots, M, the i-th edge is an undirected edge connecting vertex u_i and v_i that is initially passable if a_i = 1 and initially impassable if a_i = 0.
Additionally, there are switches on K of the vertices: vertex s_1, vertex s_2, \ldots, vertex s_K.
Takahashi is initially on vertex 1, and will repeat performing one of the two actions below, Move or Hit Switch, which he may choose each time, as many times as he wants.
- Move: Choose a vertex adjacent to the vertex he is currently on via an edge, and move to that vertex.
- Hit Switch: If there is a switch on the vertex he is currently on, hit it. This will invert the passability of every edge in the graph. That is, a passable edge will become impassable, and vice versa.
Determine whether Takahashi can reach vertex N, and if he can, print the minimum possible number of times he performs Move before reaching vertex N.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 0 \leq K \leq N
- 1 \leq u_i, v_i \leq N
- u_i \neq v_i
- a_i \in \lbrace 0, 1\rbrace
- 1 \leq s_1 \lt s_2 \lt \cdots \lt s_K \leq N
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M K u_1 v_1 a_1 u_2 v_2 a_2 \vdots u_M v_M a_M s_1 s_2 \ldots s_K
Output
If Takahashi cannot reach vertex N, print -1; if he can, print the minimum possible number of times he performs Move before reaching vertex N.
Sample Input 1
5 5 2 1 3 0 2 3 1 5 4 1 2 1 1 1 4 0 3 4
Sample Output 1
5
Takahashi can reach vertex N as follows.
- Move from vertex 1 to vertex 2.
- Move from vertex 2 to vertex 3.
- Hit the switch on vertex 3. This inverts the passability of every edge in the graph.
- Move from vertex 3 to vertex 1.
- Move from vertex 1 to vertex 4.
- Hit the switch on vertex 4. This again inverts the passability of every edge in the graph.
- Move from vertex 4 to vertex 5.
Here, Move is performed five times, which is the minimum possible number.
Sample Input 2
4 4 2 4 3 0 1 2 1 1 2 0 2 1 1 2 4
Sample Output 2
-1
The given graph may be disconnected or contain multi-edges. In this sample input, there is no way for Takahashi to reach vertex N, so you should print -1.
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
数直線の原点に高橋君がいます。高橋君は座標 X にあるゴールに移動しようとしています。
また、数直線上に N 枚の壁と N 本のハンマーがあります。
- 座標 Y_1,Y_2,\dots,Y_N にはそれぞれタイプ 1,2,\dots,N の壁があります。
- 最初、高橋君は壁を超えて移動することができません。
- 座標 Z_1,Z_2,\dots,Z_N にはそれぞれタイプ 1,2,\dots,N のハンマーがあります。
- 高橋君はハンマーのある座標に着くとそこにあるハンマーを手に入れます。
- タイプ i のハンマーはタイプ i の壁を破壊するための専用のもので、タイプ i のハンマーを手に入れた後でなら、タイプ i の壁を破壊して通過できるようになります。
高橋君がゴールに到達することが可能か判定し、可能であれば移動距離の最小値を求めてください。
制約
- 入力は全て整数
- 1 \le N \le 1500
- 1 \le |X|,|Y_i|,|Z_i| \le 10^9
- 合計 2 \times N + 1 個の座標 X,Y_i,Z_i は相異なる
入力
入力は以下の形式で標準入力から与えられる。
N X Y_1 Y_2 \dots Y_N Z_1 Z_2 \dots Z_N
出力
高橋君がゴールに到達することが可能であれば移動距離の最小値を整数として出力せよ。
不可能であれば -1 と出力せよ。
入力例 1
3 10 -2 8 -5 5 -10 3
出力例 1
40
以下の手順により、移動距離 40 で高橋くんがゴールに到達でき、これが移動距離の最小です。
- 座標 0 から高橋君が行動を開始する。
- 座標 3 に行く。タイプ 3 のハンマーを手に入れる。
- 座標 5 に行く。タイプ 1 のハンマーを手に入れる。
- 座標 -2 に行く。タイプ 1 の壁を破壊する。
- 座標 -5 に行く。タイプ 3 の壁を破壊する。
- 座標 -10 に行く。タイプ 2 のハンマーを手に入れる。
- 座標 8 に行く。タイプ 2 の壁を破壊する。
- 座標 10 に行く。ここがゴールである。
入力例 2
5 -1 10 -20 30 -40 50 -10 20 -30 40 -50
出力例 2
1
ゴールに移動するために、ハンマーを手に入れる必要も壁を破壊する必要もない場合もあります。
入力例 3
1 100 30 60
出力例 3
-1
高橋君がタイプ 1 のハンマーを手に入れることは不可能であり、ゴールに辿り着くこともできません。
入力例 4
4 865942261 703164879 -531670946 -874856231 -700164975 -941120316 599462305 -649785130 665402307
出力例 4
4078987507
Score : 500 points
Problem Statement
Takahashi is at the origin of a number line. Takahashi wants to reach the goal at coordinate X.
Also, there are N walls and N hammers on the number line.
- At coordinates Y_1,Y_2,\dots,Y_N are walls of types 1,2,\dots,N, respectively.
- Initially, Takahashi cannot get over the walls.
- At coordinates Z_1,Z_2,\dots,Z_N are hammers of types 1,2,\dots,N, respectively.
- When he arrives at a coordinate with a hammer, he obtains the hammer.
- The hammer of type i is dedicated to destroying the wall of type i. After he obtains the hammer of type i, he can destroy the wall of type i and get over it.
Determine if he can reach the goal. If he can, find the minimum distance he travels.
Constraints
- All values in the input are integers.
- 1 \le N \le 1500
- 1 \le |X|,|Y_i|,|Z_i| \le 10^9
- The (2 \times N + 1) coordinates X,Y_i and Z_i are distinct.
Input
The input is given from Standard Input in the following format:
N X Y_1 Y_2 \dots Y_N Z_1 Z_2 \dots Z_N
Output
If Takahashi can reach the goal, print the minimum possible distance he travels as an integer.
Otherwise, print -1.
Sample Input 1
3 10 -2 8 -5 5 -10 3
Sample Output 1
40
Takahashi can reach the goal by traveling a distance of 40 as follows, which is the minimum possible:
- He starts at coordinate 0.
- He moves to coordinate 3 to obtain the hammer of type 3.
- He moves to coordinate 5 to obtain the hammer of type 1.
- He moves to coordinate -2 to destroy the wall of type 1.
- He moves to coordinate -5 to destroy the wall of type 3.
- He moves to coordinate -10 to obtain the hammer of type 2.
- He moves to coordinate 8 to destroy the wall of type 2.
- He moves to coordinate 10, which is the goal.
Sample Input 2
5 -1 10 -20 30 -40 50 -10 20 -30 40 -50
Sample Output 2
1
It may not be required that he obtains a hammer or destroys a wall to reach the goal.
Sample Input 3
1 100 30 60
Sample Output 3
-1
Takahashi cannot obtain the hammer of type 1, and neither can he reach the goal.
Sample Input 4
4 865942261 703164879 -531670946 -874856231 -700164975 -941120316 599462305 -649785130 665402307
Sample Output 4
4078987507