E - Yamanote Line Game

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君と青木君は 2 人で次の対戦ゲームをします。

高橋君が先手でゲームを始め、ゲームが終了するまでの間、 2 人は交互に 1 以上 2N+1 以下の整数を 1 つずつ宣言します。 どちらかが一度でも宣言した整数は、それ以降どちらも二度と宣言することが出来ません。 先に整数を宣言することが出来なくなった方のプレイヤーの負けとなり、負けなかった方のプレイヤーの勝ちとなります。

このゲームでは必ず高橋君が勝ちます。 高橋君の立場で実際にゲームを行い、ゲームに勝ってください。

制約

  • 1 \leq N \leq 1000
  • N は整数

入出力

この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジプログラムが入出力を介して対話を行う形式の問題)です。
あなたのプログラムが高橋君の立場で、ジャッジプログラムが青木君の立場でゲームを行います。

まず、あなたのプログラムに標準入力から正の整数 N が与えられます。 その後、ゲームが終了するまで下記の手順を繰り返します。

  1. あなたのプログラムが、高橋君が宣言する整数として、1 以上 2N+1 以下の整数を標準出力に出力します。(どちらかのプレイヤーによってすでに宣言されている整数を出力することは出来ません。)
  2. ジャッジプログラムによって、青木君が宣言する整数があなたのプログラムに標準入力から与えられます。(どちらかのプレイヤーによってすでに宣言されている整数が入力されることはありません。) ただし、青木君が宣言できる整数が残っていない場合は、代わりに 0 が与えられ高橋君の勝ちでゲームが終了します。

注意点

  • 出力を行うたびに標準出力をflushしてください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
  • 高橋君の勝ちでゲームが終了したあと、あなたのプログラムは直ちに終了しなければなりません。そうしなかった場合、ジャッジ結果が AC とならない可能性があります。
  • ゲームの途中で不正な出力を行った場合(例えば、すでにどちらかのプレイヤーによって宣言されている整数を出力した場合)は不正解となりますが、そのときのジャッジ結果は不定です。WA になるとは限りません。

入出力例

入力 出力 説明
2 まず整数 N が与えられます。
1 高橋君が 1 を宣言します。
3 青木君が 3 を宣言します。
2 高橋君が 2 を宣言します。
4 青木君が 4 を宣言します。
5 高橋君が 5 を宣言します。
0 青木君が宣言できる整数が残っていないため、高橋君の勝ちでゲームが終了します。

Score : 300 points

Problem Statement

Takahashi and Aoki will play the following game against each other.

Starting from Takahashi, the two alternatingly declare an integer between 1 and 2N+1 (inclusive) until the game ends. Any integer declared by either player cannot be declared by either player again. The player who is no longer able to declare an integer loses; the player who didn't lose wins.

In this game, Takahashi will always win. Your task is to actually play the game on behalf of Takahashi and win the game.

Constraints

  • 1 \leq N \leq 1000
  • N is an integer.

Input and Output

This task is an interactive task (in which your program and the judge program interact with each other via inputs and outputs).
Your program plays the game on behalf of Takahashi, and the judge program plays the game on behalf of Aoki.

First, your program is given a positive integer N from Standard Input. Then, the following procedures are repeated until the game ends.

  1. Your program outputs an integer between 1 and 2N+1 (inclusive) to Standard Output, which defines the integer that Takahashi declares. (You cannot output an integer that is already declared by either player.)
  2. The integer that Aoki declares is given by the judge program to your program from Standard Input. (No integer that is already declared by either player will be given.) If Aoki has no more integer to declare, 0 is given instead, which means that the game ended and Takahashi won.

Notes

  • After each output, you must flush Standard Output. Otherwise, you may get TLE.
  • After the game ended and Takahashi won, the program must be terminated immediately. Otherwise, the judge does not necessarily give AC.
  • If your program outputs something that violates the rules of the game (such as an integer that has already been declared by either player), your answer is considered incorrect. In such case, the verdict is indeterminate. It does not necessarily give WA.

Sample Input and Output

Input Output Description
2 First, an integer N is given.
1 Takahashi declares an integer 1.
3 Aoki declares an integer 3.
2 Takahashi declares an integer 2.
4 Aoki declares an integer 4.
5 Takahashi declares an integer 5.
0 Aoki has no more integer to declare, so Takahashi wins, and the game ends.
F - Min Difference

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

それぞれ N 個、M 個の正整数からなる 2 つの数列 A=(A_1,A_2, \ldots ,A_N)B=(B_1, \ldots ,B_M) が与えられます。

それぞれの数列から 1 つずつ要素を選んだときの 2 つの値の差の最小値、すなわち、 \displaystyle \min_{ 1\leq i\leq N}\displaystyle\min_{1\leq j\leq M} \lvert A_i-B_j\rvert を求めてください。

制約

  • 1 \leq N,M \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • 入力は全て整数である。

入力

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

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

出力

答えを出力せよ。


入力例 1

2 2
1 6
4 9

出力例 1

2

それぞれの数列から 1 つずつ要素を選んだときの 2 つの値の差としてあり得るのは、 \lvert 1-4\rvert=3\lvert 1-9\rvert=8\lvert 6-4\rvert=2\lvert 6-9\rvert=34 つです。 この中で最小である 2 を出力します。


入力例 2

1 1
10
10

出力例 2

0

入力例 3

6 8
82 76 82 82 71 70
17 39 67 2 45 35 22 24

出力例 3

3

Score : 300 points

Problem Statement

You are given two sequences: A=(A_1,A_2, \ldots ,A_N) consisting of N positive integers, and B=(B_1, \ldots ,B_M) consisting of M positive integers.

Find the minimum difference of an element of A and an element of B, that is, \displaystyle \min_{ 1\leq i\leq N}\displaystyle\min_{1\leq j\leq M} \lvert A_i-B_j\rvert.

Constraints

  • 1 \leq N,M \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

Print the answer.


Sample Input 1

2 2
1 6
4 9

Sample Output 1

2

Here is the difference for each of the four pair of an element of A and an element of B: \lvert 1-4\rvert=3, \lvert 1-9\rvert=8, \lvert 6-4\rvert=2, and \lvert 6-9\rvert=3. We should print the minimum of these values, or 2.


Sample Input 2

1 1
10
10

Sample Output 2

0

Sample Input 3

6 8
82 76 82 82 71 70
17 39 67 2 45 35 22 24

Sample Output 3

3
G - Circumferences

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

xy -平面上の N 個の円が与えられます。 i = 1, 2, \ldots, N について、i 番目の円は点 (x_i, y_i) を中心とする半径 r_i の円です。

N 個の円のうち少なくとも 1 つ以上の円の円周上にある点のみを通って、点 (s_x, s_y) から点 (t_x, t_y) に行くことができるかどうかを判定してください。

制約

  • 1 \leq N \leq 3000
  • -10^9 \leq x_i, y_i \leq 10^9
  • 1 \leq r_i \leq 10^9
  • (s_x, s_y)N 個の円のうち少なくとも 1 つ以上の円の円周上にある
  • (t_x, t_y)N 個の円のうち少なくとも 1 つ以上の円の円周上にある
  • 入力はすべて整数

入力

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

N
s_x s_y t_x t_y
x_1 y_1 r_1
x_2 y_2 r_2
\vdots
x_N y_N r_N

出力

(s_x, s_y) から点 (t_x, t_y) に行くことができる場合は Yes を、そうでない場合は No を出力せよ。 ジャッジは英小文字と英大文字を厳密に区別することに注意せよ。


入力例 1

4
0 -2 3 3
0 0 2
2 0 2
2 3 1
-3 3 3

出力例 1

Yes

例えば、下記の経路で点 (0, -2) から点 (3, 3) へ行くことができます。

  • (0, -2) から 1 つ目の円の円周上を反時計回りに通って点 (1, -\sqrt{3}) へ行く。
  • (1, -\sqrt{3}) から 2 つ目の円の円周上を時計回りに通って点 (2, 2) へ行く。
  • (2, 2) から 3 つ目の円の円周上を反時計回りに通って点 (3, 3) へ行く。

よって、Yes を出力します。


入力例 2

3
0 1 0 3
0 0 1
0 0 2
0 0 3

出力例 2

No

少なくとも 1 つ以上の円の円周上にある点のみを通って点 (0, 1) から点 (0, 3) に行くことはできないので No を出力します。

Score : 400 points

Problem Statement

You are given N circles on the xy-coordinate plane. For each i = 1, 2, \ldots, N, the i-th circle is centered at (x_i, y_i) and has a radius of r_i.

Determine whether it is possible to get from (s_x, s_y) to (t_x, t_y) by only passing through points that lie on the circumference of at least one of the N circles.

Constraints

  • 1 \leq N \leq 3000
  • -10^9 \leq x_i, y_i \leq 10^9
  • 1 \leq r_i \leq 10^9
  • (s_x, s_y) lies on the circumference of at least one of the N circles.
  • (t_x, t_y) lies on the circumference of at least one of the N circles.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
s_x s_y t_x t_y
x_1 y_1 r_1
x_2 y_2 r_2
\vdots
x_N y_N r_N

Output

If it is possible to get from (s_x, s_y) to (t_x, t_y), print Yes; otherwise, print No. Note that the judge is case-sensitive.


Sample Input 1

4
0 -2 3 3
0 0 2
2 0 2
2 3 1
-3 3 3

Sample Output 1

Yes

Here is one way to get from (0, -2) to (3, 3).

  • From (0, -2), pass through the circumference of the 1-st circle counterclockwise to reach (1, -\sqrt{3}).
  • From (1, -\sqrt{3}), pass through the circumference of the 2-nd circle clockwise to reach (2, 2).
  • From (2, 2), pass through the circumference of the 3-rd circle counterclockwise to reach (3, 3).

Thus, Yes should be printed.


Sample Input 2

3
0 1 0 3
0 0 1
0 0 2
0 0 3

Sample Output 2

No

It is impossible to get from (0, 1) to (0, 3) by only passing through points on the circumference of at least one of the circles, so No should be printed.

H - Small d and k

Time Limit: 3.5 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

N 頂点 M 辺の単純無向グラフがあり、各頂点には 1,\ldots,N と番号が付けられています。 i=1,\ldots,M に対し、 i 番目の辺は頂点 a_i と頂点 b_i を結びます。また、各頂点の次数は 3 以下です。

i=1,\ldots,Q に対し、次のクエリに答えてください。

  • 頂点 x_i との距離が k_i 以下であるような頂点の番号の総和を求めよ。

制約

  • 1 \leq N \leq 1.5 \times 10^5
  • 0 \leq M \leq \min (\frac{N(N-1)}{2},\frac{3N}{2})
  • 1 \leq a_i \lt b_i \leq N
  • i\neq j ならば (a_i,b_i) \neq (a_j,b_j)
  • 与えられるグラフの各頂点の次数は 3 以下
  • 1 \leq Q \leq 1.5 \times 10^5
  • 1 \leq x_i \leq N
  • 0 \leq k_i \leq 3
  • 入力はすべて整数

入力

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

N M
a_1 b_1
\vdots
a_M b_M
Q
x_1 k_1
\vdots
x_Q k_Q

出力

Q 行出力せよ。 i 行目には i 番目のクエリへの答えを出力せよ。


入力例 1

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

出力例 1

1
20
2
20
7
6
20

1 番目のクエリでは、頂点 1 との距離が 1 以下であるような頂点は頂点 1 のみなので 1 が答えです。
2 番目のクエリでは、頂点 2 との距離が 2 以下であるような頂点は頂点 2,3,4,5,6 なのでこれらの総和の 20 が答えになります。
3 番目以降のクエリも同様にして答えを求められます。

Score : 500 points

Problem Statement

We have a simple undirected graph with N vertices and M edges. The vertices are numbered 1,\ldots,N. For each i=1,\ldots,M, the i-th edge connects Vertex a_i and Vertex b_i. Additionally, the degree of each vertex is at most 3.

For each i=1,\ldots,Q, answer the following query.

  • Find the sum of indices of vertices whose distances from Vertex x_i are at most k_i.

Constraints

  • 1 \leq N \leq 1.5 \times 10^5
  • 0 \leq M \leq \min (\frac{N(N-1)}{2},\frac{3N}{2})
  • 1 \leq a_i \lt b_i \leq N
  • (a_i,b_i) \neq (a_j,b_j), if i\neq j.
  • The degree of each vertex in the graph is at most 3.
  • 1 \leq Q \leq 1.5 \times 10^5
  • 1 \leq x_i \leq N
  • 0 \leq k_i \leq 3
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
\vdots
a_M b_M
Q
x_1 k_1
\vdots
x_Q k_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.


Sample Input 1

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

Sample Output 1

1
20
2
20
7
6
20

For the 1-st query, the only vertex whose distance from Vertex 1 is at most 1 is Vertex 1, so the answer is 1.
For the 2-nd query, the vertices whose distances from Vertex 2 are at most 2 are Vertex 2, 3, 4, 5, and 6, so the answer is their sum, 20.
The 3-rd and subsequent queries can be answered similarly.

I - Tournament

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

1 から 2^N の番号がついた 2^N 人でじゃんけん大会を行います。

大会は以下の形式で行われます。

  • 参加者を人 1、人 2\ldots、人 2^N の順に横一列に並べる。
  • 現在の列の長さを 2M として、各 i\ (1\leq i \leq M) について左から 2i-1 番目の人と左から 2i 番目の人で試合を行い、負けた M 人を列から外す。これを N 回繰り返す。

ここで、人 i はちょうど j 回試合に勝つと C_{i,j} 円獲得できます。1 回も勝たなかった場合は何も貰えません。全ての試合の勝敗を自由に決められるとき、人 1、人 2\ldots、人 2^N が貰えるお金の合計の最大値を求めてください。

制約

  • 1 \leq N \leq 16
  • 1 \leq C_{i,j} \leq 10^9
  • 入力は全て整数

入力

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

N 
C_{1,1} C_{1,2} \ldots C_{1,N}
C_{2,1} C_{2,2} \ldots C_{2,N}
\vdots
C_{2^N,1} C_{2^N,2} \ldots C_{2^N,N}

出力

答えを出力せよ。


入力例 1

2
2 5
6 5
2 1
7 9

出力例 1

15

初めの列は (1,2,3,4) です。

1 と人 2 の試合で人 2 が勝ち、人 3 と人 4 の試合で人 4 が勝ったとすると、列は (2,4) になります。

次に人 2 と人 4 の試合で人 4 が勝ったとすると、列は (4) になり、これで大会が終了となります。

このとき、人 2 はちょうど 1 回勝ち、人 4 はちょうど 2 回勝ったので、貰えるお金の合計は 0+6+0+9=15 となります。 これが貰えるお金の合計の最大値です。


入力例 2

3
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1

出力例 2

4

Score : 500 points

Problem Statement

2^N people, numbered 1 to 2^N, will participate in a rock-paper-scissors tournament.

The tournament proceeds as follows:

  • The participants are arranged in a row in the order Person 1, Person 2, \ldots, Person 2^N from left to right.
  • Let 2M be the current length of the row. For each i\ (1\leq i \leq M), the (2i-1)-th and (2i)-th persons from the left play a game against each other. Then, the M losers are removed from the row. This process is repeated N times.

Here, if Person i wins exactly j games, they receive C_{i,j} yen (Japanese currency). A person winning zero games receives nothing. Find the maximum possible total amount of money received by the 2^N people if the results of all games can be manipulated freely.

Constraints

  • 1 \leq N \leq 16
  • 1 \leq C_{i,j} \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N 
C_{1,1} C_{1,2} \ldots C_{1,N}
C_{2,1} C_{2,2} \ldots C_{2,N}
\vdots
C_{2^N,1} C_{2^N,2} \ldots C_{2^N,N}

Output

Print the answer.


Sample Input 1

2
2 5
6 5
2 1
7 9

Sample Output 1

15

The initial row of the people is (1,2,3,4).

If Person 2 wins the game against Person 1, and Person 4 wins the game against Person 3, the row becomes (2,4).

Then, if Person 4 wins the game against Person 2, the row becomes (4), and the tournament ends.

Here, Person 2 wins exactly 1 game, and Person 4 wins exactly 2 games, so they receive 0+6+0+9=15 yen in total, which is the maximum possible sum.


Sample Input 2

3
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1

Sample Output 2

4