A - Attack

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

体力が A の敵がいます。あなたは、1 回攻撃をすることで敵の体力を B 減らすことが出来ます。

敵の体力を 0 以下にするためには、最小で何回攻撃をする必要があるでしょうか?

制約

  • 1 \le A,B \le 10^{18}
  • A, B は整数である。

入力

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

A B

出力

答えを出力せよ。


入力例 1

7 3

出力例 1

3

3 回攻撃すると敵の体力が -2 となります。

2 回攻撃しただけでは敵の体力は 1 であるため、3 回攻撃する必要があります。


入力例 2

123456789123456789 987654321

出力例 2

124999999

入力例 3

999999999999999998 2

出力例 3

499999999999999999

Score : 100 points

Problem Statement

There is an enemy with stamina A. Every time you attack the enemy, its stamina reduces by B.

At least how many times do you need to attack the enemy to make its stamina 0 or less?

Constraints

  • 1 \le A,B \le 10^{18}
  • A and B are integers.

Input

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

A B

Output

Print the answer.


Sample Input 1

7 3

Sample Output 1

3

Attacking three times make the enemy's stamina -2.

Attacking only twice makes the stamina 1, so you need to attack it three times.


Sample Input 2

123456789123456789 987654321

Sample Output 2

124999999

Sample Input 3

999999999999999998 2

Sample Output 3

499999999999999999
B - Find snuke

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 250

問題文

H マス \timesW マスのマス目があり、各マスに 1 つずつ英小文字が書き込まれています。 上から i 行目かつ左から j 列目のマスを (i,j) で表します。

マス目に書き込まれている英小文字は H 個の長さ W の文字列 S_1,S_2,\ldots, S_H によって与えられ、 S_ij 文字目が、(i, j) に書き込まれた英小文字を表します。

マス目の中に、s, n, u, k, e この順に(縦・横・ななめのいずれかの方向に) 連続して並んでいる 場所がただ 1 つ存在します。
そのような場所を見つけ、そのマスの位置を出力の形式に従って出力してください。

ただし、s, n, u, k, e この順に(縦・横・ななめのいずれかの方向に) 連続して並んでいる場所とは、 5 つのマスの組 (A_1,A_2,A_3,A_4,A_5) であって、次をすべてみたすものをさします。

  • A_1,A_2,A_3,A_4,A_5 に書き込まれた英小文字はそれぞれ s, n, u, k, e である。
  • 1\leq i\leq 4 について、A_iA_{i+1} は頂点または辺を共有している。
  • A_1,A_2,A_3,A_4,A_5 の中心はこの順に一直線上に等間隔で並んでいる。

制約

  • 5\leq H\leq 100
  • 5\leq W\leq 100
  • H,W は整数
  • S_i は英小文字のみからなる長さ W の文字列
  • 与えられるマス目の中に条件をみたす場所がただ 1 つ存在する

入力

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

H W
S_1
S_2
\vdots
S_H

出力

次の形式にしたがって、5 行出力せよ。

条件をみたす場所のうち s, n, u, k, e が書かれたマスがそれぞれ (R_1,C_1), (R_2,C_2)\ldots,(R_5,C_5) であるとき、 i 行目には R_iC_i をこの順に空白区切りで出力せよ。

すなわち、以下のように出力せよ。

R_1 C_1
R_2 C_2
\vdots
R_5 C_5

以下の入力例も参考にせよ。


入力例 1

6 6
vgxgpu
amkxks
zhkbpp
hykink
esnuke
zplvfj

出力例 1

5 2
5 3
5 4
5 5
5 6

この時、(A_1,A_2,A_3,A_4,A_5)=((5,2),(5,3),(5,4),(5,5),(5,6)) とすると、
それぞれのマスに書き込まれた英小文字は s, n, u, k, e であり、
1\leq i\leq 4 について、A_iA_{i+1} は辺を共有しており、
各マスの中心は一直線上に存在するため、条件をみたしています。


入力例 2

5 5
ezzzz
zkzzz
ezuzs
zzznz
zzzzs

出力例 2

5 5
4 4
3 3
2 2
1 1

(A_1,A_2,A_3,A_4,A_5)=((5,5),(4,4),(3,3),(2,2),(1,1)) が条件をみたしています。
例えば、(A_1,A_2,A_3,A_4,A_5)=((3,5),(4,4),(3,3),(2,2),(3,1)) は、1,2 つめの条件をみたしていますが、
マスの中心が一直線上に存在しないため、3 つめの条件をみたしていません。


入力例 3

10 10
kseeusenuk
usesenesnn
kskekeeses
nesnusnkkn
snenuuenke
kukknkeuss
neunnennue
sknuessuku
nksneekknk
neeeuknenk

出力例 3

9 3
8 3
7 3
6 3
5 3

Score : 250 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns. Each cell has a lowercase English letter written on it. We denote by (i, j) the cell at the i-th row from the top and j-th column from the left.

The letters written on the grid are represented by H strings S_1,S_2,\ldots, S_H, each of length W. The j-th letter of S_i represents the letter written on (i, j).

There is a unique set of contiguous cells (going vertically, horizontally, or diagonally) in the grid with s, n, u, k, and e written on them in this order.
Find the positions of such cells and print them in the format specified in the Output section.

A tuple of five cells (A_1,A_2,A_3,A_4,A_5) is said to form a set of contiguous cells (going vertically, horizontally, or diagonally) with s, n, u, k, and e written on them in this order if and only if all of the following conditions are satisfied.

  • A_1,A_2,A_3,A_4 and A_5 have letters s, n, u, k, and e written on them, respectively.
  • For all 1\leq i\leq 4, cells A_i and A_{i+1} share a corner or a side.
  • The centers of A_1,A_2,A_3,A_4, and A_5 are on a common line at regular intervals.

Constraints

  • 5\leq H\leq 100
  • 5\leq W\leq 100
  • H and W are integers.
  • S_i is a string of length W consisting of lowercase English letters.
  • The given grid has a unique conforming set of cells.

Input

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

H W
S_1
S_2
\vdots
S_H

Output

Print five lines in the following format.

Let (R_1,C_1), (R_2,C_2)\ldots,(R_5,C_5) be the cells in the sought set with s, n, u, k, and e written on them, respectively. The i-th line should contain R_i and C_i in this order, separated by a space.

In other words, print them in the following format:

R_1 C_1
R_2 C_2
\vdots
R_5 C_5

See also Sample Inputs and Outputs below.


Sample Input 1

6 6
vgxgpu
amkxks
zhkbpp
hykink
esnuke
zplvfj

Sample Output 1

5 2
5 3
5 4
5 5
5 6

Tuple (A_1,A_2,A_3,A_4,A_5)=((5,2),(5,3),(5,4),(5,5),(5,6)) satisfies the conditions.
Indeed, the letters written on them are s, n, u, k, and e;
for all 1\leq i\leq 4, cells A_i and A_{i+1} share a side;
and the centers of the cells are on a common line.


Sample Input 2

5 5
ezzzz
zkzzz
ezuzs
zzznz
zzzzs

Sample Output 2

5 5
4 4
3 3
2 2
1 1

Tuple (A_1,A_2,A_3,A_4,A_5)=((5,5),(4,4),(3,3),(2,2),(1,1)) satisfies the conditions.
However, for example, (A_1,A_2,A_3,A_4,A_5)=((3,5),(4,4),(3,3),(2,2),(3,1)) violates the third condition because the centers of the cells are not on a common line, although it satisfies the first and second conditions.


Sample Input 3

10 10
kseeusenuk
usesenesnn
kskekeeses
nesnusnkkn
snenuuenke
kukknkeuss
neunnennue
sknuessuku
nksneekknk
neeeuknenk

Sample Output 3

9 3
8 3
7 3
6 3
5 3
C - Almost Equal

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 250

問題文

英小文字からなる長さ M の文字列 NS_1,S_2,\dots,S_N が与えられます。ここで、S_i は互いに異なります。

これらを並び替えた文字列の列 T_1,T_2,\dots,T_N であって、以下の条件を満たすものが存在するか判定してください。

  • 1 \le i \le N-1 を満たす全ての整数 i に対して、T_i1 文字だけ別の英小文字に変えて T_{i+1} にすることが出来る。

制約

  • 2 \le N \le 8
  • 1 \le M \le 5
  • S_i は英小文字からなる長さ M の文字列である。(1 \le i \le N)
  • S_i は互いに異なる。

入力

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

N M
S_1
S_2
\vdots
S_N

出力

問題文の条件を満たす列が存在するならば Yes を、そうでないならば No を出力せよ。


入力例 1

4 4
bbed
abcd
abed
fbed

出力例 1

Yes

abcd , abed , bbed , fbed の順に並び替えると条件を満たします。


入力例 2

2 5
abcde
abced

出力例 2

No

どのように並び替えても条件を満たすことは出来ません。


入力例 3

8 4
fast
face
cast
race
fact
rice
nice
case

出力例 3

Yes

Score : 250 points

Problem Statement

You are given N strings S_1,S_2,\dots,S_N, each of length M, consisting of lowercase English letter. Here, S_i are pairwise distinct.

Determine if one can rearrange these strings to obtain a new sequence of strings T_1,T_2,\dots,T_N such that:

  • for all integers i such that 1 \le i \le N-1, one can alter exactly one character of T_i to another lowercase English letter to make it equal to T_{i+1}.

Constraints

  • 2 \le N \le 8
  • 1 \le M \le 5
  • S_i is a string of length M consisting of lowercase English letters. (1 \le i \le N)
  • S_i are pairwise distinct.

Input

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

N M
S_1
S_2
\vdots
S_N

Output

Print Yes if one can obtain a conforming sequence; print No otherwise.


Sample Input 1

4 4
bbed
abcd
abed
fbed

Sample Output 1

Yes

One can rearrange them in this order: abcd, abed, bbed, fbed. This sequence satisfies the condition.


Sample Input 2

2 5
abcde
abced

Sample Output 2

No

No matter how the strings are rearranged, the condition is never satisfied.


Sample Input 3

8 4
fast
face
cast
race
fact
rice
nice
case

Sample Output 3

Yes
D - Impartial Gift

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

高橋君は青木君とすぬけ君に 1 つずつ贈り物を送ることにしました。
青木君への贈り物の候補は N 個あり、 それぞれの価値は A_1, A_2, \ldots,A_N です。
すぬけ君への贈り物の候補は M 個あり、 それぞれの価値は B_1, B_2, \ldots,B_M です。

高橋君は 2 人への贈り物の価値の差が D 以下になるようにしたいと考えています。

条件をみたすように贈り物を選ぶことが可能か判定し、可能な場合はそのような選び方における贈り物の価値の和の最大値を求めてください。

制約

  • 1\leq N,M\leq 2\times 10^5
  • 1\leq A_i,B_i\leq 10^{18}
  • 0\leq D \leq 10^{18}
  • 入力はすべて整数

入力

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

N M D
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

出力

高橋君が条件をみたすように贈り物を選ぶことができる場合、 条件をみたし、かつ価値の和が最大になるように贈り物を選んだ時の価値の和を出力せよ。 高橋君が条件をみたすように選ぶことができない場合、-1 を出力せよ。


入力例 1

2 3 2
3 10
2 5 15

出力例 1

8

高橋君は贈り物の価値の差を 2 以下にする必要があります。
青木君に価値 3, すぬけ君に価値 5 の贈り物を渡すと条件をみたし、価値の和としてはこのときが最大となります。
よって、3+5=8 を出力します。


入力例 2

3 3 0
1 3 3
6 2 7

出力例 2

-1

条件をみたすように贈り物を選ぶことは不可能です。 また、同一人物に対して、同じ価値の贈り物が複数存在することもあります。


入力例 3

1 1 1000000000000000000
1000000000000000000
1000000000000000000

出力例 3

2000000000000000000

答えが 32 bit整数型の範囲に収まらないことがあることに注意してください。


入力例 4

8 6 1
2 5 6 5 2 1 7 9
7 2 5 5 2 4

出力例 4

14

Score : 400 points

Problem Statement

Takahashi has decided to give one gift to Aoki and one gift to Snuke.
There are N candidates of gifts for Aoki, and their values are A_1, A_2, \ldots,A_N.
There are M candidates of gifts for Snuke, and their values are B_1, B_2, \ldots,B_M.

Takahashi wants to choose gifts so that the difference in values of the two gifts is at most D.

Determine if he can choose such a pair of gifts. If he can, print the maximum sum of values of the chosen gifts.

Constraints

  • 1\leq N,M\leq 2\times 10^5
  • 1\leq A_i,B_i\leq 10^{18}
  • 0\leq D \leq 10^{18}
  • All values in the input are integers.

Input

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

N M D
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

If he can choose gifts to satisfy the condition, print the maximum sum of values of the chosen gifts. If he cannot satisfy the condition, print -1.


Sample Input 1

2 3 2
3 10
2 5 15

Sample Output 1

8

The difference of values of the two gifts should be at most 2.
If he gives a gift with value 3 to Aoki and another with value 5 to Snuke, the condition is satisfied, achieving the maximum possible sum of values.
Thus, 3+5=8 should be printed.


Sample Input 2

3 3 0
1 3 3
6 2 7

Sample Output 2

-1

He cannot choose gifts to satisfy the condition. Note that the candidates of gifts for a person may contain multiple gifts with the same value.


Sample Input 3

1 1 1000000000000000000
1000000000000000000
1000000000000000000

Sample Output 3

2000000000000000000

Note that the answer may not fit into a 32-bit integer type.


Sample Input 4

8 6 1
2 5 6 5 2 1 7 9
7 2 5 5 2 4

Sample Output 4

14
E - Isolation

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 425

問題文

最初 N 頂点 0 辺の無向グラフがあり、各頂点には 1 から N まで番号がついています。
Q 個のクエリが与えられるので、順に処理し、各クエリの後における「他のどの頂点とも辺で結ばれていない頂点」の数を出力してください。

i 個目のクエリは \mathrm{query}_i であり、各クエリは次の 2 種類いずれかです。

  • 1 u v: 頂点 u と頂点 v を辺で結ぶ。このクエリが与えられる直前の時点で、頂点 u と頂点 v は辺で結ばれていない事が保証される。

  • 2 v : 頂点 v と他の頂点を結ぶ辺をすべて削除する。(頂点 v 自体は削除しない。)

制約

  • 2 \leq N\leq 3\times 10^5
  • 1 \leq Q\leq 3\times 10^5
  • 1 番目の種類のクエリにおいて、1\leq u,v\leq N, u\neq v
  • 2 番目の種類のクエリにおいて、1\leq v\leq N
  • 1 番目の種類のクエリの直前の時点で、そのクエリの u,v について頂点 u と頂点 v は辺で結ばれていない。
  • 入力はすべて整数

入力

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

N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

出力

Q 行出力せよ。
i 行目 (1\leq i\leq Q) には、i 個目のクエリを処理した後の「他のどの頂点とも辺で結ばれていない頂点」の数を出力せよ。


入力例 1

3 7
1 1 2
1 1 3
1 2 3
2 1
1 1 2
2 2
1 1 2

出力例 1

1
0
0
1
0
3
1

1 個目のクエリの後で、頂点 1 と頂点 2 は互いに結ばれており、頂点 3 のみが他のどの頂点とも辺で結ばれていません。
よって、1 行目には 1 を出力します。

また、3 個目のクエリの後でどの相異なる 2 頂点の間も辺で結ばれていますが、4 個目のクエリによって、 頂点 1 と他の頂点を結ぶ辺、すなわち 頂点 1 と頂点 2 を結ぶ辺および頂点 1 と頂点 3 を結ぶ辺が削除されます。 この結果として、頂点 2 と頂点 3 は互いに結ばれているが、頂点 1 は他のどの頂点とも辺で結ばれていない状態となります。
よって、3 行目には 0 を、4 行目には 1 を出力します。


入力例 2

2 1
2 1

出力例 2

2

2 番目の種類のクエリを行う直前の時点で、すでにその頂点と他の頂点を結ぶ辺が 1 本も存在しないこともあります。

Score : 425 points

Problem Statement

There is an undirected graph with N vertices numbered 1 through N, and initially with 0 edges.
Given Q queries, process them in order. After processing each query, print the number of vertices that are not connected to any other vertices by an edge.

The i-th query, \mathrm{query}_i, is of one of the following two kinds.

  • 1 u v: connect vertex u and vertex v with an edge. It is guaranteed that, when this query is given, vertex u and vertex v are not connected by an edge.

  • 2 v: remove all edges that connect vertex v and the other vertices. (Vertex v itself is not removed.)

Constraints

  • 2 \leq N\leq 3\times 10^5
  • 1 \leq Q\leq 3\times 10^5
  • For each query of the first kind, 1\leq u,v\leq N and u\neq v.
  • For each query of the second kind, 1\leq v\leq N.
  • Right before a query of the first kind is given, there is no edge between vertices u and v.
  • All values in the input are integers.

Input

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

N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Output

Print Q lines.
The i-th line (1\leq i\leq Q) should contain the number of vertices that are not connected to any other vertices by an edge.


Sample Input 1

3 7
1 1 2
1 1 3
1 2 3
2 1
1 1 2
2 2
1 1 2

Sample Output 1

1
0
0
1
0
3
1

After the first query, vertex 1 and vertex 2 are connected to each other by an edge, but vertex 3 is not connected to any other vertices.
Thus, 1 should be printed in the first line.

After the third query, all pairs of different vertices are connected by an edge.
However, the fourth query asks to remove all edges that connect vertex 1 and the other vertices, specifically to remove the edge between vertex 1 and vertex 2, and another between vertex 1 and vertex 3. As a result, vertex 2 and vertex 3 are connected to each other, while vertex 1 is not connected to any other vertices by an edge.
Thus, 0 and 1 should be printed in the third and fourth lines, respectively.


Sample Input 2

2 1
2 1

Sample Output 2

2

When the query of the second kind is given, there may be no edge that connects that vertex and the other vertices.

F - Merge Set

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 500

問題文

黒板に 1 以上 M 以下の整数からなる集合 NS_1,S_2,\dots,S_N が書かれています。ここで、S_i = \lbrace S_{i,1},S_{i,2},\dots,S_{i,A_i} \rbrace です。

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

  • 1 個以上の共通した要素を持つ 2 個の集合 X,Y を選ぶ。X,Y2 個を黒板から消し、新たに X\cup Y を黒板に書く。

ここで、X\cup Y とは XY の少なくともどちらかに含まれている要素のみからなる集合を意味します。

1M が両方含まれる集合を作ることが出来るか判定してください。出来るならば、必要な操作回数の最小値を求めてください。

制約

  • 1 \le N \le 2 \times 10^5
  • 2 \le M \le 2 \times 10^5
  • 1 \le \sum_{i=1}^{N} A_i \le 5 \times 10^5
  • 1 \le S_{i,j} \le M(1 \le i \le N,1 \le j \le A_i)
  • S_{i,j} \neq S_{i,k}(1 \le j < k \le A_i)
  • 入力は全て整数である。

入力

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

N M
A_1
S_{1,1} S_{1,2} \dots S_{1,A_1}
A_2
S_{2,1} S_{2,2} \dots S_{2,A_2}
\vdots
A_N
S_{N,1} S_{N,2} \dots S_{N,A_N}

出力

1M が両方含まれる集合を作ることが出来るならば必要な操作回数の最小値を、出来ないならば -1 を出力せよ。


入力例 1

3 5
2
1 2
2
2 3
3
3 4 5

出力例 1

2

まず、\lbrace 1,2 \rbrace\lbrace 2,3 \rbrace を選んで消し、\lbrace 1,2,3 \rbrace を追加します。

そして、\lbrace 1,2,3 \rbrace\lbrace 3,4,5 \rbrace を選んで消し、\lbrace 1,2,3,4,5 \rbrace を追加します。

すると 2 回の操作で 1M を両方含む集合を作ることが出来ます。1 回の操作では目標を達成できないため、答えは 2 です。


入力例 2

1 2
2
1 2

出力例 2

0

始めから S_11,M を共に含むため、必要な操作回数の最小値は 0 回です。


入力例 3

3 5
2
1 3
2
2 4
3
2 4 5

出力例 3

-1

入力例 4

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

出力例 4

2

Score : 500 points

Problem Statement

On a blackboard, there are N sets S_1,S_2,\dots,S_N consisting of integers between 1 and M. Here, S_i = \lbrace S_{i,1},S_{i,2},\dots,S_{i,A_i} \rbrace.

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

  • choose two sets X and Y with at least one common element. Erase them from the blackboard, and write X\cup Y on the blackboard instead.

Here, X\cup Y denotes the set consisting of the elements contained in at least one of X and Y.

Determine if one can obtain a set containing both 1 and M. If it is possible, find the minimum number of operations required to obtain it.

Constraints

  • 1 \le N \le 2 \times 10^5
  • 2 \le M \le 2 \times 10^5
  • 1 \le \sum_{i=1}^{N} A_i \le 5 \times 10^5
  • 1 \le S_{i,j} \le M(1 \le i \le N,1 \le j \le A_i)
  • S_{i,j} \neq S_{i,k}(1 \le j < k \le A_i)
  • All values in the input are integers.

Input

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

N M
A_1
S_{1,1} S_{1,2} \dots S_{1,A_1}
A_2
S_{2,1} S_{2,2} \dots S_{2,A_2}
\vdots
A_N
S_{N,1} S_{N,2} \dots S_{N,A_N}

Output

If one can obtain a set containing both 1 and M, print the minimum number of operations required to obtain it; if it is impossible, print -1 instead.


Sample Input 1

3 5
2
1 2
2
2 3
3
3 4 5

Sample Output 1

2

First, choose and remove \lbrace 1,2 \rbrace and \lbrace 2,3 \rbrace to obtain \lbrace 1,2,3 \rbrace.

Then, choose and remove \lbrace 1,2,3 \rbrace and \lbrace 3,4,5 \rbrace to obtain \lbrace 1,2,3,4,5 \rbrace.

Thus, one can obtain a set containing both 1 and M with two operations. Since one cannot achieve the objective by performing the operation only once, the answer is 2.


Sample Input 2

1 2
2
1 2

Sample Output 2

0

S_1 already contains both 1 and M, so the minimum number of operations required is 0.


Sample Input 3

3 5
2
1 3
2
2 4
3
2 4 5

Sample Output 3

-1

Sample Input 4

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

Sample Output 4

2
G - Sort from 1 to 4

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 625

問題文

全ての要素が 1 以上 4 以下の整数である、長さ N の数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

高橋君は次の操作を何回でも (0 回でも良い) 繰り返し行う事ができます。

  • 1\leq i<j\leq N をみたす整数の組 (i,j) を選び、A_iA_j を交換する。

数列 A を広義単調増加にするために必要な操作回数の最小値を求めてください。
ただし、数列 A が広義単調増加であるとは、1\leq i\leq N-1 をみたすすべての整数について A_i\leq A_{i+1} が成り立つことをさします。

制約

  • 2 \leq N \leq 2\times 10^5
  • 1\leq A_i \leq 4
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

数列 A を広義単調増加にするために必要な操作回数の最小値を一行に出力せよ。


入力例 1

6
3 4 1 1 2 4

出力例 1

3

次のようにして 3 回の操作で A を広義単調増加にすることができます。

  • (i,j)=(2,3) を選び、A_2A_3 を交換する。A=(3,1,4,1,2,4) となる。
  • (i,j)=(1,4) を選び、A_1A_4 を交換する。A=(1,1,4,3,2,4) となる。
  • (i,j)=(3,5) を選び、A_3A_5 を交換する。A=(1,1,2,3,4,4) となる。

2 回以下の操作で A を広義単調増加にすることはできないため、このとき操作回数が最小となります。
よって、3 を出力します。


入力例 2

4
2 3 4 1

出力例 2

3

Score : 625 points

Problem Statement

You are given a sequence A=(A_1,A_2,\ldots,A_N) of length N, consisting of integers between 1 and 4.

Takahashi may perform the following operation any number of times (possibly zero):

  • choose a pair of integer (i,j) such that 1\leq i<j\leq N, and swap A_i and A_j.

Find the minimum number of operations required to make A non-decreasing.
A sequence is said to be non-decreasing if and only if A_i\leq A_{i+1} for all 1\leq i\leq N-1.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • 1\leq A_i \leq 4
  • All values in the input are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print the minimum number of operations required to make A non-decreasing in a single line.


Sample Input 1

6
3 4 1 1 2 4

Sample Output 1

3

One can make A non-decreasing with the following three operations:

  • Choose (i,j)=(2,3) to swap A_2 and A_3, making A=(3,1,4,1,2,4).
  • Choose (i,j)=(1,4) to swap A_1 and A_4, making A=(1,1,4,3,2,4).
  • Choose (i,j)=(3,5) to swap A_3 and A_5, making A=(1,1,2,3,4,4).

This is the minimum number of operations because it is impossible to make A non-decreasing with two or fewer operations.
Thus, 3 should be printed.


Sample Input 2

4
2 3 4 1

Sample Output 2

3
Ex - Ball Collector

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 625

問題文

N 頂点の木があります。i(1 \le i \le N-1) 本目の辺は、頂点 U_iV_i を結ぶ無向辺です。頂点 i(1 \le i \le N) には、A_i が書かれたボールと B_i が書かれたボールが 1 個ずつあります。

v = 2,3,\dots,N に対して、以下の問題を解いてください。(各問題は独立です。)

  • 頂点 1 から頂点 v まで最短経路で移動します。このとき、通った各頂点(頂点 1,v も含む)において、ボールを 1 個ずつ選んで取ります。最終的に持っているボールに書かれている整数の種類数の最大値を求めてください。

制約

  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i,B_i \le N
  • 与えられるグラフは木である。
  • 入力は全て整数である。

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}

出力

v=2,3,\dots,N に対して、答えを空白区切りで出力せよ。


入力例 1

4
1 2
2 3
3 1
1 2
1 2
2 3
3 4

出力例 1

2 3 3

例えば、v=4 のときは通る頂点は 1,2,3,4 であり、それぞれ A_1,B_2,B_3,B_4(=1,3,1,2) が書かれているボールを選ぶと種類数が 3 となり、これが最大となります。


入力例 2

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

出力例 2

4 3 2 3 4 3 4 2 3

Score : 625 points

Problem Statement

We have a tree with N vertices. The i-th (1 \le i \le N-1) edge is an undirected edge between vertices U_i and V_i. Vertex i (1 \le i \le N) has a ball with A_i written on it and another with B_i.

For each v = 2,3,\dots,N, answer the following question. (Each query is independent.)

  • Consider traveling from vertex 1 to vertex v on the shortest path. Every time you visit a vertex (including vertices 1 and v), you pick up one ball placed there. Find the maximum number of distinct integers written on the picked-up balls.

Constraints

  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i,B_i \le N
  • The given graph is a tree.
  • All values in the input are integers.

Input

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}

Output

Print the answers for v=2,3,\dots,N, separated by spaces.


Sample Input 1

4
1 2
2 3
3 1
1 2
1 2
2 3
3 4

Sample Output 1

2 3 3

For example, when v=4, you visit vertices 1,2,3, and 4. By choosing balls with A_1,B_2,B_3,B_4(=1,3,1,2) written on them, the number of distinct integers on the balls is 3, which is the maximum.


Sample Input 2

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

Sample Output 2

4 3 2 3 4 3 4 2 3