A - Irreversible operation

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 300

問題文

N 個のオセロの石が一列に並んでいます。 それぞれの石の状態は長さ N の文字列 S によって表されており、 S_i=B のとき左から i 番目の石の表面は黒色、 S_i=W のとき左から i 番目の石の表面は白色となっています。

ここで、以下の操作を行うことを考えます。

  • 左から i 番目の石の表面が黒色、左から i+1 番目の石の表面が白色であるような i (1 \leq i < N) を一つ選び、 その 2 つの石をともに裏返す。つまり、左から i 番目の石の表面が白色、左から i+1 番目の石の表面が黒色になるようにする。

最大で何回この操作を行うことができるか求めてください。

制約

  • 1 \leq |S| \leq 2\times 10^5
  • S_i=B または W

入力

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

S

出力

先の操作を行うことができる回数の最大値を出力せよ。


入力例 1

BBW

出力例 1

2

以下のようにして 2 回の操作を行うことができます。

  • 左から 2 番目、3 番目の石を裏返す。
  • 左から 1 番目、2 番目の石を裏返す。

入力例 2

BWBWBW

出力例 2

6

Score : 300 points

Problem Statement

There are N Reversi pieces arranged in a row. (A Reversi piece is a disc with a black side and a white side.) The state of each piece is represented by a string S of length N. If S_i=B, the i-th piece from the left is showing black; If S_i=W, the i-th piece from the left is showing white.

Consider performing the following operation:

  • Choose i (1 \leq i < N) such that the i-th piece from the left is showing black and the (i+1)-th piece from the left is showing white, then flip both of those pieces. That is, the i-th piece from the left is now showing white and the (i+1)-th piece from the left is now showing black.

Find the maximum possible number of times this operation can be performed.

Constraints

  • 1 \leq |S| \leq 2\times 10^5
  • S_i=B or W

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum possible number of times the operation can be performed.


Sample Input 1

BBW

Sample Output 1

2

The operation can be performed twice, as follows:

  • Flip the second and third pieces from the left.
  • Flip the first and second pieces from the left.

Sample Input 2

BWBWBW

Sample Output 2

6
B - Powers of two

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 600

問題文

高橋君は正整数が書かれたボールを N 個持っています。i 番目のボールに書かれている正整数は A_i です。 高橋君は N 個のボールからいくつかのペアを作って、それぞれのペアのボールに書かれた数の和が 2 べきとなるようにしたいです。 ただし、同じボールが複数のペアに属することはできません。 最大でいくつのペアが作れるか求めてください。

ただし、正整数が 2 べきであるとは、ある非負整数 t を用いて 2^t と書けることを指します。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • A_i は整数

入力

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

N
A_1 A_2 ... A_N

出力

2 つのボールに書かれた数の和が 2 べきとなるペアの個数として考えられる最大値を出力せよ。


入力例 1

3
1 2 3

出力例 1

1

1 番目のボールと 3 番目のボールをペアにすることで、書かれた数の和が 4 となるペアを 1 つ作ることができます。 2 番目のボール同士をペアにできないことに注意してください。


入力例 2

5
3 11 14 5 13

出力例 2

2

Score : 600 points

Problem Statement

Takahashi has N balls with positive integers written on them. The integer written on the i-th ball is A_i. He would like to form some number of pairs such that the sum of the integers written on each pair of balls is a power of 2. Note that a ball cannot belong to multiple pairs. Find the maximum possible number of pairs that can be formed.

Here, a positive integer is said to be a power of 2 when it can be written as 2^t using some non-negative integer t.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible number of pairs such that the sum of the integers written on each pair of balls is a power of 2.


Sample Input 1

3
1 2 3

Sample Output 1

1

We can form one pair whose sum of the written numbers is 4 by pairing the first and third balls. Note that we cannot pair the second ball with itself.


Sample Input 2

5
3 11 14 5 13

Sample Output 2

2
C - Lexicographic constraints

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 700

問題文

N 個の文字列が一列に並んでおり、どの隣り合う 2 つの文字列に対しても、 左に書いてある文字列の方が右に書いてある文字列よりも辞書順で小さいことが分かっています。 つまり、左から i 番目の文字列を S_i としたときに、辞書順で S_1<S_2<...<S_N が成り立っています。

S_i の長さが A_i であると分かっているとき、S_1,S_2,...,S_N に含まれる文字の種類数として考えられる最小の値を求めてください。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • A_i は整数

Note

文字列は英字アルファベットからなる必要はない。無限に多くの文字があり、辞書式順序がそれらについて定まっているとして良い。


入力

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

N
A_1 A_2 ... A_N

出力

いずれかの文字列に含まれる文字の種類数として考えられる最小の値を出力せよ。


入力例 1

3
3 2 1

出力例 1

2

例えば、S_1=abc, S_2=bb, S_3=c のときはS_1,S_2,...,S_N に含まれる文字の種類数は 3 になります。

しかし、文字列をうまく選ぶと、文字の種類数を 2 にすることができます。


入力例 2

5
2 3 2 1 2

出力例 2

2

Score : 700 points

Problem Statement

There are N strings arranged in a row. It is known that, for any two adjacent strings, the string to the left is lexicographically smaller than the string to the right. That is, S_1<S_2<...<S_N holds lexicographically, where S_i is the i-th string from the left.

At least how many different characters are contained in S_1,S_2,...,S_N, if the length of S_i is known to be A_i?

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • A_i is an integer.

Note

The strings do not necessarily consist of English alphabet; there can be arbitrarily many different characters (and the lexicographic order is defined for those characters).


Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible number of different characters contained in the strings.


Sample Input 1

3
3 2 1

Sample Output 1

2

The number of different characters contained in S_1,S_2,...,S_N would be 3 when, for example, S_1=abc, S_2=bb and S_3=c.

However, if we choose the strings properly, the number of different characters can be 2.


Sample Input 2

5
2 3 2 1 2

Sample Output 2

2
D - Grid game

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 800

問題文

高橋君と青木君は HW 列のマス目を使ってゲームをします。 このマス目上には N 個の障害物があり、i 番目の障害物は (X_i,Y_i) にあります。 ただし、ij 列目 (1 \leq i \leq H, 1 \leq j \leq W) にあるマスを (i,j) で表すことにします。 また、どの障害物も (1,1) にはなく、(1,1) には 1 つの駒が置いてあります。

そこで、高橋君と青木君は高橋君から始めて、交互に以下の行動のいずれかを行います。

  • 駒を隣り合うマスに動かす。 ただし、駒の位置を (x,y) としたとき、高橋君は (x+1,y) にのみ、青木君は (x,y+1) にのみ駒を動かすことができる。 また、動かすことのできるマスが存在しない、もしくは、動かす予定のマス目に障害物がある場合はこの行動をとることはできない。
  • 駒を動かさず、マス目を元の状態のまま行動を終える。

2 回連続で駒が動かされなかった場合、そこでゲームを終了します。

高橋君はできるだけ多くの回数の行動 (駒を動かさないのも含む) を行ってゲームを終えたいですが、 青木君はできるだけ少ない回数の行動を行ってゲームを終えたいです。 このとき、高橋君が行うことになる行動の回数は何回か求めてください。

制約

  • 1 \leq H,W \leq 2\times 10^5
  • 0 \leq N \leq 2\times 10^5
  • 1 \leq X_i \leq H
  • 1 \leq Y_i \leq W
  • i \neq j のとき (X_i,Y_i) \neq (X_j,Y_j)
  • (X_i,Y_i) \neq (1,1)
  • X_i,Y_i は整数

入力

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

H W N
X_1 Y_1
:
X_N Y_N

出力

高橋君が行うことになる行動の回数を出力せよ。


入力例 1

3 3 1
3 2

出力例 1

2

ゲームの一例は以下のようになります。

  • 高橋君が駒を (2,1) に動かす。
  • 青木君が駒を動かさない。
  • 高橋君が駒を (3,1) に動かす。
  • 青木君が駒を動かさない。
  • 高橋君が駒を動かさない。

この場合は高橋君は 3 回の行動を行いますが、 両者が最適に行動すれば 2 回しか高橋君は行動せずにゲームが終了します。


入力例 2

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

出力例 2

6

入力例 3

100000 100000 0

出力例 3

100000

Score : 800 points

Problem Statement

Takahashi and Aoki will play a game using a grid with H rows and W columns of square cells. There are N obstacles on this grid; the i-th obstacle is at (X_i,Y_i). Here, we represent the cell at the i-th row and j-th column (1 \leq i \leq H, 1 \leq j \leq W) by (i,j). There is no obstacle at (1,1), and there is a piece placed there at (1,1).

Starting from Takahashi, he and Aoki alternately perform one of the following actions:

  • Move the piece to an adjacent cell. Here, let the position of the piece be (x,y). Then Takahashi can only move the piece to (x+1,y), and Aoki can only move the piece to (x,y+1). If the destination cell does not exist or it is occupied by an obstacle, this action cannot be taken.
  • Do not move the piece, and end his turn without affecting the grid.

The game ends when the piece does not move twice in a row.

Takahashi would like to perform as many actions (including not moving the piece) as possible before the game ends, while Aoki would like to perform as few actions as possible before the game ends. How many actions will Takahashi end up performing?

Constraints

  • 1 \leq H,W \leq 2\times 10^5
  • 0 \leq N \leq 2\times 10^5
  • 1 \leq X_i \leq H
  • 1 \leq Y_i \leq W
  • If i \neq j, (X_i,Y_i) \neq (X_j,Y_j)
  • (X_i,Y_i) \neq (1,1)
  • X_i and Y_i are integers.

Input

Input is given from Standard Input in the following format:

H W N
X_1 Y_1
:
X_N Y_N

Output

Print the number of actions Takahashi will end up performing.


Sample Input 1

3 3 1
3 2

Sample Output 1

2

For example, the game proceeds as follows:

  • Takahashi moves the piece to (2,1).
  • Aoki does not move the piece.
  • Takahashi moves the piece to (3,1).
  • Aoki does not move the piece.
  • Takahashi does not move the piece.

Takahashi performs three actions in this case, but if both players play optimally, Takahashi will perform only two actions before the game ends.


Sample Input 2

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

Sample Output 2

6

Sample Input 3

100000 100000 0

Sample Output 3

100000
E - Wandering TKHS

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 1200

問題文

高橋君の会社は N 個の部屋からなり、各部屋には 1 から N の番号が割り振られています。 また、N-1 本の通路があり、i 番目の通路は部屋 a_i と部屋 b_i を繋いでいます。 どの 2 つの部屋の間もこれらの通路のみを通って移動できることが分かっています。

今高橋君はある部屋に迷い込んでしまいました。この部屋を r とします。 そこで、高橋君は自分の部屋である部屋 1 に戻るために、以下の方法で移動することを繰り返すことにしました。

  • 今まで訪れた部屋に隣接する部屋の中でまだ訪れていない部屋の内、番号が一番小さい部屋に移動する。

部屋 1 に戻るまでに行う必要のある移動の回数を c_r とします。 c_2,c_3,...,c_N をすべて求めてください。 ただし、1 回の移動でいくつの通路を通るとしても、移動は 1 回として数えられることに注意してください。

制約

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq a_i,b_i \leq N
  • a_i \neq b_i
  • 入力で与えられるグラフは木である。

入力

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

N
a_1 b_1
:
a_{N-1} b_{N-1}

出力

r に対して c_r を以下の形式に従って出力せよ。

c_2 c_3 ... c_N

入力例 1

6
1 5
5 6
6 2
6 3
6 4

出力例 1

5 5 5 1 5

例えば部屋 2 に迷い込んでいた場合、高橋君は以下のように移動します。

  • 部屋 6 に移動する。
  • 部屋 3 に移動する。
  • 部屋 4 に移動する。
  • 部屋 5 に移動する。
  • 部屋 1 に移動する。

入力例 2

6
1 2
2 3
3 4
4 5
5 6

出力例 2

1 2 3 4 5

入力例 3

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

出力例 3

7 5 3 1 3 4 7 4 5

Score : 1200 points

Problem Statement

Takahashi's office has N rooms. Each room has an ID from 1 to N. There are also N-1 corridors, and the i-th corridor connects Room a_i and Room b_i. It is known that we can travel between any two rooms using only these corridors.

Takahashi has got lost in one of the rooms. Let this room be r. He decides to get back to his room, Room 1, by repeatedly traveling in the following manner:

  • Travel to the room with the smallest ID among the rooms that are adjacent to the rooms already visited, but not visited yet.

Let c_r be the number of travels required to get back to Room 1. Find all of c_2,c_3,...,c_N. Note that, no matter how many corridors he passes through in a travel, it still counts as one travel.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq a_i,b_i \leq N
  • a_i \neq b_i
  • The graph given as input is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

Print c_r for each r, in the following format:

c_2 c_3 ... c_N

Sample Input 1

6
1 5
5 6
6 2
6 3
6 4

Sample Output 1

5 5 5 1 5

For example, if Takahashi was lost in Room 2, he will travel as follows:

  • Travel to Room 6.
  • Travel to Room 3.
  • Travel to Room 4.
  • Travel to Room 5.
  • Travel to Room 1.

Sample Input 2

6
1 2
2 3
3 4
4 5
5 6

Sample Output 2

1 2 3 4 5

Sample Input 3

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

Sample Output 3

7 5 3 1 3 4 7 4 5
F - Construction of a tree

実行時間制限: 4 sec / メモリ制限: 1024 MB

配点 : 2200

問題文

\{1,2,...,N\} の部分集合が N-1 個与えられます。i 番目の集合を E_i とします。

各集合 E_i から相異なる 2 要素 u_i,v_i を選び、\{1,2,..,N\} を頂点集合、 (u_1,v_1),(u_2,v_2),...,(u_{N-1},v_{N-1}) を辺集合とするような、N 頂点 N-1 辺グラフ T を考えます。 うまく u_i,v_i を定めることにより、T を木にすることができるかどうか判定してください。 さらに可能な場合は、T が実際に木となるような (u_1,v_1),(u_2,v_2),...,(u_{N-1},v_{N-1}) を一つ求めてください。

制約

  • 2 \leq N \leq 10^5
  • E_i\{1,2,..,N\} の部分集合である。
  • |E_i| \geq 2
  • |E_i| の和は 2 \times 10^5 以下である。

入力

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

N
c_1 w_{1,1} w_{1,2} ... w_{1,c_1}
:
c_{N-1} w_{N-1,1} w_{N-1,2} ... w_{N-1,c_{N-1}}

ただし、c_iE_i の要素数を指し、w_{i,1},...,w_{i,c_i}E_ic_i 個の元である。 また、2 \leq c_i \leq N, 1 \leq w_{i,j} \leq N, w_{i,j} \neq w_{i,k} (1 \leq j < k \leq c_i) である。

出力

T を木とすることができない場合は -1 を出力せよ。そうでない場合は以下の形式に従って条件を満たす (u_i,v_i) を出力せよ。

u_1 v_1
:
u_{N-1} v_{N-1}

入力例 1

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

出力例 1

1 2
1 3
3 4
4 5

入力例 2

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

出力例 2

-1

入力例 3

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

出力例 3

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

Score : 2200 points

Problem Statement

You are given N-1 subsets of \{1,2,...,N\}. Let the i-th set be E_i.

Let us choose two distinct elements u_i and v_i from each set E_i, and consider a graph T with N vertices and N-1 edges, whose vertex set is \{1,2,..,N\} and whose edge set is (u_1,v_1),(u_2,v_2),...,(u_{N-1},v_{N-1}). Determine if T can be a tree by properly deciding u_i and v_i. If it can, additionally find one instance of (u_1,v_1),(u_2,v_2),...,(u_{N-1},v_{N-1}) such that T is actually a tree.

Constraints

  • 2 \leq N \leq 10^5
  • E_i is a subset of \{1,2,..,N\}.
  • |E_i| \geq 2
  • The sum of |E_i| is at most 2 \times 10^5.

Input

Input is given from Standard Input in the following format:

N
c_1 w_{1,1} w_{1,2} ... w_{1,c_1}
:
c_{N-1} w_{N-1,1} w_{N-1,2} ... w_{N-1,c_{N-1}}

Here, c_i stands for the number of elements in E_i, and w_{i,1},...,w_{i,c_i} are the c_i elements in c_i. Here, 2 \leq c_i \leq N, 1 \leq w_{i,j} \leq N, and w_{i,j} \neq w_{i,k} (1 \leq j < k \leq c_i) hold.

Output

If T cannot be a tree, print -1; otherwise, print the choices of (u_i,v_i) that satisfy the condition, in the following format:

u_1 v_1
:
u_{N-1} v_{N-1}

Sample Input 1

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

Sample Output 1

1 2
1 3
3 4
4 5

Sample Input 2

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

Sample Output 2

-1

Sample Input 3

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

Sample Output 3

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