実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 150 点
問題文
A, B, C の三兄弟がいます。この 3 人の年齢関係は、3 つの文字 S_{\mathrm{AB}},S_{\mathrm{AC}},S_{\mathrm{BC}} によって与えられ、それぞれ以下を意味します。
- S_{\mathrm{AB}} が
<
の場合 A は B より年下であり、>
の場合 A は B より年上である。 - S_{\mathrm{AC}} が
<
の場合 A は C より年下であり、>
の場合 A は C より年上である。 - S_{\mathrm{BC}} が
<
の場合 B は C より年下であり、>
の場合 B は C より年上である。
三兄弟の次男、つまり二番目に年上の人は誰ですか?
制約
- S_{\mathrm{AB}},S_{\mathrm{AC}},S_{\mathrm{BC}} はそれぞれ
<
または>
- 入力に矛盾は含まれない。つまり、与えられた大小関係を全て満たす年齢関係が必ず存在する入力のみが与えられる。
入力
入力は以下の形式で標準入力から与えられる。
S_{\mathrm{AB}} S_{\mathrm{AC}} S_{\mathrm{BC}}
出力
三兄弟の次男、つまり二番目に年上の人の名前を出力せよ。
入力例 1
< < <
出力例 1
B
A が B より年下であり、B が C より年下であることから、C が長男、B が次男、A が三男であることがわかります。よって答えは B
です。
入力例 2
< < >
出力例 2
C
Score : 150 points
Problem Statement
There are three brothers named A
, B
, and C
. The age relationships among them are given by three characters S_{\mathrm{AB}}, S_{\mathrm{AC}}, S_{\mathrm{BC}}, which mean the following:
- If S_{\mathrm{AB}} is
<
, then A is younger than B; if it is>
, then A is older than B. - If S_{\mathrm{AC}} is
<
, then A is younger than C; if it is>
, then A is older than C. - If S_{\mathrm{BC}} is
<
, then B is younger than C; if it is>
, then B is older than C.
Who is the middle brother, that is, the second oldest among the three?
Constraints
- Each of S_{\mathrm{AB}}, S_{\mathrm{AC}}, S_{\mathrm{BC}} is
<
or>
. - The input contains no contradictions; that is, there always exists an age relationship that satisfies all given inequalities.
Input
The input is given from Standard Input in the following format:
S_{\mathrm{AB}} S_{\mathrm{AC}} S_{\mathrm{BC}}
Output
Print the name of the middle brother, that is, the second oldest among the three.
Sample Input 1
< < <
Sample Output 1
B
Since A is younger than B, and B is younger than C, we can determine that C is the oldest, B is the middle, and A is the youngest. Hence, the answer is B
.
Sample Input 2
< < >
Sample Output 2
C
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
AtCoder 王国では、長男に必ず「太郎」という名前を付けます。長男以外には「太郎」という名前は付けません。 長男とは、各家で生まれた男の子のうち最も早く生まれた者を指します。
AtCoder 王国には N 戸の家があり、M 人の赤子が生まれました。また、M 人の赤子が生まれる前には、N 戸のどの家も赤子が生まれたことはありませんでした。
赤子の情報が生まれの時系列順に与えられます。
i 番目に生まれた赤子は、A_i 番目の家で生まれ、B_i が M
のとき男の子、F
のとき女の子です。
M 人の赤子それぞれについて、付けられた名前が「太郎」か判定してください。
制約
- 1\leq N,M\leq 100
- 1\leq A_i\leq N
- B_i は
M
またはF
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 \vdots A_M B_M
出力
M 行出力せよ。
i\ (1\leq i \leq M) 行目には、i 番目に生まれた赤子の名前が「太郎」ならば Yes
を、そうでない場合 No
を出力せよ。
入力例 1
2 4 1 M 1 M 2 F 2 M
出力例 1
Yes No No Yes
1 番目に生まれた赤子は、家 1 で生まれた男の子のうち最も早く生まれた者なので「太郎」です。
一方、2 番目に生まれた赤子は、家 1 で生まれた男の子のうち最も早く生まれた者ではないので「太郎」ではありません。
3 番目に生まれた赤子は、女の子なので「太郎」ではありません。
4 番目に生まれた赤子は、家 2 で生まれた男の子のうち最も早く生まれた者なので「太郎」です。3 番目に生まれた赤子も家 2 で生まれていますが、男の子のうち最も早く生まれた者を「太郎」と名付けることに注意してください。
入力例 2
4 7 2 M 3 M 1 F 4 F 4 F 1 F 2 M
出力例 2
Yes Yes No No No No No
Score : 200 points
Problem Statement
In the Kingdom of AtCoder, the eldest son is always given the name Taro. No one else is given the name Taro. The eldest son is the earliest born male child in each family.
There are N families in the Kingdom, and M babies were born. Before the M babies were born, none of the N families had had any babies.
Information about the babies is given in chronological order of their birth.
The i-th baby born was born in family A_i, and the baby is male if B_i is M
, and female if it is F
.
Determine for each of the M babies whether the name given is Taro.
Constraints
- 1\leq N,M\leq 100
- 1\leq A_i\leq N
- B_i is
M
orF
. - All numbers in the input are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 \vdots A_M B_M
Output
Print M lines.
The i-th line (1\leq i \leq M) should contain Yes
if the name given to the i-th baby is Taro, and No
otherwise.
Sample Input 1
2 4 1 M 1 M 2 F 2 M
Sample Output 1
Yes No No Yes
The first baby is the earliest born boy in family 1, so he is named Taro.
The second baby is not the earliest born boy in family 1, so he is not named Taro.
The third baby is a girl, so she is not named Taro.
The fourth baby is the earliest born boy in family 2, so he is named Taro. Note that the third baby is also born in family 2, but it is the earliest born boy who is named Taro.
Sample Input 2
4 7 2 M 3 M 1 F 4 F 4 F 1 F 2 M
Sample Output 2
Yes Yes No No No No No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
頂点 1, 頂点 2,\ldots, 頂点 N の N 個の頂点からなる単純無向グラフ G,H が与えられます。 G には M _ G 本の辺があり、i 本目 (1\leq i\leq M _ G) の辺は頂点 u _ i と頂点 v _ i を結んでいます。 H には M _ H 本の辺があり、i 本目 (1\leq i\leq M _ H) の辺は頂点 a _ i と頂点 b _ i を結んでいます。
あなたは、グラフ H に対して次の操作を 0 回以上の好きな回数繰り返すことができます。
- 1\leq i\lt j\leq N を満たす整数の組 (i,j) をひとつ選ぶ。A _ {i,j} 円を支払って、頂点 i と頂点 j を結ぶ辺がなければ追加し、あれば取り除く。
G と H を同型にするために少なくとも何円支払う必要があるか求めてください。
単純無向グラフとは?
単純無向グラフとは、自己ループや多重辺を含まず、辺に向きの無いグラフのことをいいます。
グラフの同型とは?
N 頂点のグラフ G と H が同型であるとは、ある (1,2,\ldots,N) の並べ替え (P _ 1,P _ 2,\ldots,P _ N) が存在して、どの 1\leq i\lt j\leq N に対しても
- G に頂点 i, 頂点 j を結ぶ辺が存在するとき、かつそのときに限り H に頂点 P _ i, 頂点 P _ j を結ぶ辺が存在する
制約
- 1\leq N\leq8
- 0\leq M _ G\leq\dfrac{N(N-1)}2
- 0\leq M _ H\leq\dfrac{N(N-1)}2
- 1\leq u _ i\lt v _ i\leq N\ (1\leq i\leq M _ G)
- (u _ i,v _ i)\neq(u _ j,v _ j)\ (1\leq i\lt j\leq M _ G)
- 1\leq a _ i\lt b _ i\leq N\ (1\leq i\leq M _ H)
- (a _ i,b _ i)\neq(a _ j,b _ j)\ (1\leq i\lt j\leq M _ H)
- 1\leq A _ {i,j}\leq 10 ^ 6\ (1\leq i\lt j\leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M _ G u _ 1 v _ 1 u _ 2 v _ 2 \vdots u _ {M _ G} v _ {M _ G} M _ H a _ 1 b _ 1 a _ 2 b _ 2 \vdots a _ {M _ H} b _ {M _ H} A _ {1,2} A _ {1,3} \ldots A _ {1,N} A _ {2,3} \ldots A _ {2,N} \vdots A _ {N-1,N}
出力
答えを出力せよ。
入力例 1
5 4 1 2 2 3 3 4 4 5 4 1 2 1 3 1 4 1 5 3 1 4 1 5 9 2 6 5 3
出力例 1
9
与えられたグラフはそれぞれ以下のようになります。
たとえば、H に対して次のような 4 つの操作を順に行うことで、9 円を支払ってG と H を同型にすることができます。
- (i,j)=(1,3) として操作を行う。H には頂点 1 と頂点 3 を結ぶ辺があるので、1 円を支払ってこれを取り除く。
- (i,j)=(2,5) として操作を行う。H には頂点 2 と頂点 5 を結ぶ辺はないので、2 円を支払ってこれを追加する。
- (i,j)=(1,5) として操作を行う。H には頂点 1 と頂点 5 を結ぶ辺があるので、1 円を支払ってこれを取り除く。
- (i,j)=(3,5) として操作を行う。H には頂点 3 と頂点 5 を結ぶ辺はないので、5 円を支払ってこれを追加する。
操作の結果、H は以下のようになります。
支払う金額を 8 円以下にして G と H を同型にすることはできないため、9
を出力してください。
入力例 2
5 3 1 2 2 3 3 4 4 1 2 2 3 3 4 4 5 9 1 1 1 1 1 1 1 1 9
出力例 2
3
たとえば、(i,j)=(2,3),(2,4),(3,4) とした 3 回の操作を行うことで G と H を同型にすることができます。
入力例 3
5 3 1 2 2 3 3 4 4 1 2 2 3 3 4 4 5 5 4 4 4 4 4 4 4 4 5
出力例 3
5
たとえば、(i,j)=(4,5) とした 1 回の操作を行うことで G と H を同型にすることができます。
入力例 4
2 0 0 371
出力例 4
0
G や H には辺が含まれていないこともあります。 また、一度も操作をする必要がないこともあります。
入力例 5
8 13 1 8 5 7 4 6 1 5 7 8 1 6 1 2 5 8 2 6 5 6 6 7 3 7 4 8 15 3 5 1 7 4 6 3 8 7 8 1 2 5 6 1 6 1 5 1 4 2 8 2 6 2 4 4 7 1 3 7483 1694 5868 3296 9723 5299 4326 5195 4088 5871 1384 2491 6562 1149 6326 2996 9845 7557 4041 7720 1554 5060 8329 8541 3530 4652 3874 3748
出力例 5
21214
Score : 300 points
Problem Statement
You are given simple undirected graphs G and H, each with N vertices: vertices 1, 2, \ldots, N. Graph G has M_G edges, and its i-th edge (1\leq i\leq M_G) connects vertices u_i and v_i. Graph H has M_H edges, and its i-th edge (1\leq i\leq M_H) connects vertices a_i and b_i.
You can perform the following operation on graph H any number of times, possibly zero.
- Choose a pair of integers (i,j) satisfying 1\leq i<j\leq N. Pay A_{i,j} yen, and if there is no edge between vertices i and j in H, add one; if there is, remove it.
Find the minimum total cost required to make G and H isomorphic.
What is a simple undirected graph?
A simple undirected graph is a graph without self-loops or multi-edges, where edges have no direction.
What does it mean for graphs to be isomorphic?
Two graphs G and H with N vertices are isomorphic if and only if there exists a permutation (P_1,P_2,\ldots,P_N) of (1,2,\ldots,N) such that for all 1\leq i\lt j\leq N:
- an edge exists between vertices i and j in G if and only if an edge exists between vertices P_i and P_j in H.
Constraints
- 1\leq N\leq8
- 0\leq M _ G\leq\dfrac{N(N-1)}2
- 0\leq M _ H\leq\dfrac{N(N-1)}2
- 1\leq u _ i\lt v _ i\leq N\ (1\leq i\leq M _ G)
- (u _ i,v _ i)\neq(u _ j,v _ j)\ (1\leq i\lt j\leq M _ G)
- 1\leq a _ i\lt b _ i\leq N\ (1\leq i\leq M _ H)
- (a _ i,b _ i)\neq(a _ j,b _ j)\ (1\leq i\lt j\leq M _ H)
- 1\leq A _ {i,j}\leq 10 ^ 6\ (1\leq i\lt j\leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M _ G u _ 1 v _ 1 u _ 2 v _ 2 \vdots u _ {M _ G} v _ {M _ G} M _ H a _ 1 b _ 1 a _ 2 b _ 2 \vdots a _ {M _ H} b _ {M _ H} A _ {1,2} A _ {1,3} \ldots A _ {1,N} A _ {2,3} \ldots A _ {2,N} \vdots A _ {N-1,N}
Output
Print the answer.
Sample Input 1
5 4 1 2 2 3 3 4 4 5 4 1 2 1 3 1 4 1 5 3 1 4 1 5 9 2 6 5 3
Sample Output 1
9
The given graphs are as follows:
For example, you can perform the following four operations on H to make it isomorphic to G at a cost of 9 yen.
- Choose (i,j)=(1,3). There is an edge between vertices 1 and 3 in H, so pay 1 yen to remove it.
- Choose (i,j)=(2,5). There is no edge between vertices 2 and 5 in H, so pay 2 yen to add it.
- Choose (i,j)=(1,5). There is an edge between vertices 1 and 5 in H, so pay 1 yen to remove it.
- Choose (i,j)=(3,5). There is no edge between vertices 3 and 5 in H, so pay 5 yen to add it.
After these operations, H becomes:
You cannot make G and H isomorphic at a cost less than 9 yen, so print 9
.
Sample Input 2
5 3 1 2 2 3 3 4 4 1 2 2 3 3 4 4 5 9 1 1 1 1 1 1 1 1 9
Sample Output 2
3
For example, performing the operations (i,j)=(2,3),(2,4),(3,4) on H will make it isomorphic to G.
Sample Input 3
5 3 1 2 2 3 3 4 4 1 2 2 3 3 4 4 5 5 4 4 4 4 4 4 4 4 5
Sample Output 3
5
For example, performing the operation (i,j)=(4,5) once will make G and H isomorphic.
Sample Input 4
2 0 0 371
Sample Output 4
0
Note that G and H may have no edges.
Also, it is possible that no operations are needed.
Sample Input 5
8 13 1 8 5 7 4 6 1 5 7 8 1 6 1 2 5 8 2 6 5 6 6 7 3 7 4 8 15 3 5 1 7 4 6 3 8 7 8 1 2 5 6 1 6 1 5 1 4 2 8 2 6 2 4 4 7 1 3 7483 1694 5868 3296 9723 5299 4326 5195 4088 5871 1384 2491 6562 1149 6326 2996 9845 7557 4041 7720 1554 5060 8329 8541 3530 4652 3874 3748
Sample Output 5
21214
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
数直線上に N 個の村があります。i 番目の村は座標 X_i にあり、P_i 人の村人がいます。
Q 個のクエリに答えてください。i 番目のクエリは以下の形式です。
- 整数 L_i,R_i が与えられる。座標が L_i 以上 R_i 以下の村に住んでいる村人の人数の総数を求めよ。
制約
- 1\leq N,Q\leq 2\times 10^5
- -10^9\leq X_1 < X_2 < \ldots < X_N \leq 10^9
- 1\leq P_i\leq 10^9
- -10^9\leq L_i \leq R_i \leq 10^9
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N X_1 \ldots X_N P_1 \ldots P_N Q L_1 R_1 \vdots L_Q R_Q
出力
Q 行出力せよ。
i\ (1\leq i \leq Q) 行目には、i 番目のクエリに対する答えを出力せよ。
入力例 1
4 1 3 5 7 1 2 3 4 4 1 1 2 6 0 10 2 2
出力例 1
1 5 10 0
1 番目のクエリについて考えます。座標が 1 以上 1 以下の村は、座標 1 にある村で、村人は 1 人います。よって答えは 1 です。
2 番目のクエリについて考えます。座標が 2 以上 6 以下の村は、座標 3 にある村と座標 5 にある村で、村人はそれぞれ 2 人と 3 人います。よって答えは 2+3=5 です。
入力例 2
7 -10 -5 -3 -1 0 1 4 2 5 6 5 2 1 7 8 -7 7 -1 5 -10 -4 -8 10 -5 0 -10 5 -8 7 -8 -3
出力例 2
26 15 7 26 18 28 26 11
Score : 350 points
Problem Statement
There are N villages on a number line. The i-th village is located at coordinate X_i, and has P_i villagers.
Answer Q queries. The i-th query is in the following format:
- Given integers L_i and R_i, find the total number of villagers living in villages located between coordinates L_i and R_i, inclusive.
Constraints
- 1\leq N,Q\leq 2\times 10^5
- -10^9\leq X_1 < X_2 < \ldots < X_N \leq 10^9
- 1\leq P_i\leq 10^9
- -10^9\leq L_i \leq R_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X_1 \ldots X_N P_1 \ldots P_N Q L_1 R_1 \vdots L_Q R_Q
Output
Print Q lines.
The i-th line(1\leq i \leq Q) should contain the answer to the i-th query.
Sample Input 1
4 1 3 5 7 1 2 3 4 4 1 1 2 6 0 10 2 2
Sample Output 1
1 5 10 0
Consider the first query. The villages between coordinates 1 and 1 are the village at coordinate 1, with 1 villager. Hence, the answer is 1.
Consider the second query. The villages between coordinates 2 and 6 are the villages at coordinates 3 and 5, with 2 and 3 villagers, respectively. Hence, the answer is 2+3=5.
Sample Input 2
7 -10 -5 -3 -1 0 1 4 2 5 6 5 2 1 7 8 -7 7 -1 5 -10 -4 -8 10 -5 0 -10 5 -8 7 -8 -3
Sample Output 2
26 15 7 26 18 28 26 11
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。また、f(l,r) を以下で定義します。
- (A_l,A_{l+1},\ldots,A_{r-1},A_{r}) に含まれる値の種類数
次の式の値を求めてください。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq A_i\leq N
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 \ldots A_N
出力
答えを出力せよ。
入力例 1
3 1 2 2
出力例 1
8
f(1,2) について考えます。(A_1,A_2)=(1,2) に含まれる値の種類数は 2 なので f(1,2)=2 です。
f(2,3) について考えます。(A_2,A_3)=(2,2) に含まれる値の種類数は 1 なので f(2,3)=1 です。
f の総和は 8 となります。
入力例 2
9 5 4 2 2 3 2 4 4 1
出力例 2
111
Score : 475 points
Problem Statement
You are given a sequence of integers A = (A_1, A_2, \ldots, A_N) of length N. Define f(l, r) as:
- the number of distinct values in the subsequence (A_l, A_{l+1}, \ldots, A_r).
Evaluate the following expression:
Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq A_i\leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 \ldots A_N
Output
Print the answer.
Sample Input 1
3 1 2 2
Sample Output 1
8
Consider f(1,2). The subsequence (A_1, A_2) = (1,2) contains 2 distinct values, so f(1,2)=2.
Consider f(2,3). The subsequence (A_2, A_3) = (2,2) contains 1 distinct value, so f(2,3)=1.
The sum of f is 8.
Sample Input 2
9 5 4 2 2 3 2 4 4 1
Sample Output 2
111
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 550 点
問題文
東西に続く道があり、道の上には N 人の高橋くんがいます。 道は原点と呼ばれる点から東西に十分長く続いています。
i 番目 (1\leq i\leq N) の高橋くんは、はじめ原点から東に X _ i メートル進んだところにいます。
高橋くんたちは道の上を東西に動くことができます。 具体的には、次の移動を好きなだけ行うことができます。
- 高橋くんを一人選ぶ。移動する先に他の高橋くんがいない場合、選んだ高橋くんを 1 メートル東に、もしくは西に移動させる。
高橋くんたちには合計 Q 個の用事があり、i 個目 (1\leq i\leq Q) の用事は次の形式で表されます。
- T _ i 番目の高橋くんが座標 G _ i に到着する。
Q 個の用事を先頭から順にすべて完了するために必要な移動回数の最小値を求めてください。
制約
- 1\leq N\leq2\times10 ^ 5
- 0\leq X _ 1\lt X _ 2\lt\dotsb\lt X _ N\leq10 ^ 8
- 1\leq Q\leq2\times10 ^ 5
- 1\leq T _ i\leq N\ (1\leq i\leq Q)
- 0\leq G _ i\leq10 ^ 8\ (1\leq i\leq Q)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X _ 1 X _ 2 \ldots X _ N Q T _ 1 G _ 1 T _ 2 G _ 2 \vdots T _ Q G _ Q
出力
答えを出力せよ。
入力例 1
5 10 20 30 40 50 4 3 45 4 20 1 35 2 60
出力例 1
239
高橋くんたちの最適な行動は以下のようになります(それぞれの高橋くんの座標は正確に描かれているとは限りません)。
それぞれの用事では、高橋くんたちは次のように移動しています。
- 4 番目の高橋くんが 6 回東に進み、3 番目の高橋くんが 15 回東に進む。
- 2 番目の高橋くんが 2 回西に進み、3 番目の高橋くんが 26 回西に進み、4 番目の高橋くんが 26 回西に進む。
- 4 番目の高橋くんが 18 回東に進み、3 番目の高橋くんが 18 回東に進み、2 番目の高橋くんが 18 回東に進み、1 番目の高橋くんが 25 回東に進む。
- 5 番目の高橋くんが 13 回東に進み、4 番目の高橋くんが 24 回東に進み、3 番目の高橋くんが 24 回東に進み、2 番目の高橋くんが 24 回東に進む。
高橋くんたちの移動回数の合計は 21+54+79+85=239 回となります。
移動回数の合計を 238 回以下としてすべての用事を完了することはできないため、239
を出力してください。
入力例 2
8 0 1 2 3 4 5 6 100000000 6 1 100000000 8 0 1 100000000 8 4 1 100000000 5 21006578
出力例 2
4294967297
途中で一部の高橋くんが原点より西側や、原点より 10 ^ 8+1 メートル以上東に進んだところに移動する必要がある場合があることに注意してください。
また、答えが 2 ^ {32} を超える場合があることに注意してください。
入力例 3
12 1558 3536 3755 3881 4042 4657 5062 7558 7721 8330 8542 9845 8 9 1694 7 3296 12 5299 5 5195 5 5871 1 2491 8 1149 8 2996
出力例 3
89644
Score : 550 points
Problem Statement
There is a road extending east and west, and N persons are on the road. The road extends infinitely long to the east and west from a point called the origin.
The i-th person (1\leq i\leq N) is initially at a position X_i meters east from the origin.
The persons can move along the road to the east or west. Specifically, they can perform the following movement any number of times.
- Choose one person. If there is no other person at the destination, move the chosen person 1 meter east or west.
They have Q tasks in total, and the i-th task (1\leq i\leq Q) is as follows.
- The T_i-th person arrives at coordinate G_i.
Find the minimum total number of movements required to complete all Q tasks in order.
Constraints
- 1\leq N\leq2\times10^5
- 0\leq X_1 < X_2 < \dotsb < X_N \leq10^8
- 1\leq Q\leq2\times10^5
- 1\leq T_i\leq N\ (1\leq i\leq Q)
- 0\leq G_i\leq10^8\ (1\leq i\leq Q)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X_1 X_2 \ldots X_N Q T_1 G_1 T_2 G_2 \vdots T_Q G_Q
Output
Print the answer.
Sample Input 1
5 10 20 30 40 50 4 3 45 4 20 1 35 2 60
Sample Output 1
239
An optimal sequence of movements for the persons is as follows (the positions of the persons are not necessarily drawn to scale):
For each task, the persons move as follows.
- The 4th person moves 6 steps east, and the 3rd person moves 15 steps east.
- The 2nd person moves 2 steps west, the 3rd person moves 26 steps west, and the 4th person moves 26 steps west.
- The 4th person moves 18 steps east, the 3rd person moves 18 steps east, the 2nd person moves 18 steps east, and the 1st person moves 25 steps east.
- The 5th person moves 13 steps east, the 4th person moves 24 steps east, the 3rd person moves 24 steps east, and the 2nd person moves 24 steps east.
The total number of movements is 21+54+79+85=239.
You cannot complete all tasks with a total movement count of 238 or less, so print 239
.
Sample Input 2
8 0 1 2 3 4 5 6 100000000 6 1 100000000 8 0 1 100000000 8 4 1 100000000 5 21006578
Sample Output 2
4294967297
Note that some persons may need to move to the west of the origin or more than 10^8 meters to the east of it.
Also, note that the answer may exceed 2^{32}.
Sample Input 3
12 1558 3536 3755 3881 4042 4657 5062 7558 7721 8330 8542 9845 8 9 1694 7 3296 12 5299 5 5195 5 5871 1 2491 8 1149 8 2996
Sample Output 3
89644
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 575 点
問題文
(1,2,\ldots,N) の並べ替え P=(P _ 1,P _ 2,\ldots,P _ N),A=(A _ 1,A _ 2,\ldots,A _ N) が与えられます。
あなたは、次の操作を 0 回以上好きな回数行うことができます。
- i=1,2,\ldots,N に対して一斉に A _ i を A _ {P _ i} で置き換える。
得られる A としてありえるもののうち、辞書順で最小のものを出力してください。
辞書順の大小とは?
長さ N の列 A=(A _ 1,A _ 2,\ldots,A _ N),B=(B _ 1,B _ 2,\ldots,B _ N) について、辞書順で A が B より小さいとは、次のことが成り立つことをいいます。
- ある整数 i\ (1\leq i\leq N) が存在し、A _ i\lt B _ i が成り立ち、1\leq j\lt i を満たすすべての整数 j に対して A _ j=B _ j が成り立つ。
制約
- 1\leq N\leq2\times10 ^ 5
- 1\leq P _ i\leq N\ (1\leq i\leq N)
- P _ i\neq P _ j\ (1\leq i\lt j\leq N)
- 1\leq A _ i\leq N\ (1\leq i\leq N)
- A _ i\neq A _ j\ (1\leq i\lt j\leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N P _ 1 P _ 2 \ldots P _ N A _ 1 A _ 2 \ldots A _ N
出力
0 回以上の操作の結果としてあり得る A のうち辞書順で最小のものを (A _ 1,A _ 2,\ldots,A _ N) として、A _ 1,A _ 2,\ldots,A _ N をこの順に空白を区切りとして 1 行に出力せよ。
入力例 1
6 3 1 5 6 2 4 4 3 1 6 2 5
出力例 1
1 4 2 5 3 6
はじめ、A=(4,3,1,6,2,5) です。
ここから操作を繰り返すと、以下のようになります。
- A=(1,4,2,5,3,6) となる。
- A=(2,1,3,6,4,5) となる。
- A=(3,2,4,5,1,6) となる。
- A=(4,3,1,6,2,5) となる。
以降、4 回操作を行うたびにもとの A に戻ります。
よって、このうち辞書順で最小である 1 4 2 5 3 6
を出力してください。
入力例 2
8 3 5 8 7 2 6 1 4 1 2 3 4 5 6 7 8
出力例 2
1 2 3 4 5 6 7 8
1 度も操作をしなくても構いません。
入力例 3
26 24 14 4 20 15 19 16 11 23 22 12 18 21 3 6 8 26 2 25 7 13 1 5 9 17 10 15 3 10 1 13 19 22 24 20 4 14 23 7 26 25 18 11 6 9 12 2 21 5 16 8 17
出力例 3
4 1 22 18 20 13 14 6 15 11 3 26 2 12 5 23 9 10 25 24 7 17 16 21 19 8
Score : 575 points
Problem Statement
You are given permutations P = (P_1, P_2, \ldots, P_N) and A = (A_1, A_2, \ldots, A_N) of (1,2,\ldots,N).
You can perform the following operation any number of times, possibly zero:
- replace A_i with A_{P_i} simultaneously for all i=1,2,\ldots,N.
Print the lexicographically smallest A that can be obtained.
What is lexicographical order?
For sequences of length N, A = (A_1, A_2, \ldots, A_N) and B = (B_1, B_2, \ldots, B_N), A is lexicographically smaller than B if and only if:
- there exists an integer i\ (1\leq i\leq N) such that A_i < B_i, and A_j = B_j for all 1\leq j < i.
Constraints
- 1\leq N\leq2\times10^5
- 1\leq P_i\leq N\ (1\leq i\leq N)
- P_i\neq P_j\ (1\leq i<j\leq N)
- 1\leq A_i\leq N\ (1\leq i\leq N)
- A_i\neq A_j\ (1\leq i<j\leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N P_1 P_2 \ldots P_N A_1 A_2 \ldots A_N
Output
Let (A_1, A_2, \ldots, A_N) be the lexicographically smallest A that can be obtained. Print A_1, A_2, \ldots, A_N in this order, separated by spaces, in one line.
Sample Input 1
6 3 1 5 6 2 4 4 3 1 6 2 5
Sample Output 1
1 4 2 5 3 6
Initially, A = (4, 3, 1, 6, 2, 5).
Repeating the operation yields the following.
- A = (1, 4, 2, 5, 3, 6)
- A = (2, 1, 3, 6, 4, 5)
- A = (3, 2, 4, 5, 1, 6)
- A = (4, 3, 1, 6, 2, 5)
After this, A will revert to the original state every four operations.
Therefore, print the lexicographically smallest among these, which is 1 4 2 5 3 6
.
Sample Input 2
8 3 5 8 7 2 6 1 4 1 2 3 4 5 6 7 8
Sample Output 2
1 2 3 4 5 6 7 8
You may choose to perform no operations.
Sample Input 3
26 24 14 4 20 15 19 16 11 23 22 12 18 21 3 6 8 26 2 25 7 13 1 5 9 17 10 15 3 10 1 13 19 22 24 20 4 14 23 7 26 25 18 11 6 9 12 2 21 5 16 8 17
Sample Output 3
4 1 22 18 20 13 14 6 15 11 3 26 2 12 5 23 9 10 25 24 7 17 16 21 19 8