E - Loong Tracking

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君は座標平面上で龍を操作するゲームを作成しました。

龍は 1 から N までの番号がついた N 個のパーツからなり、パーツ 1 と呼びます。

最初パーツ i は座標 (i,0) にあります。以下のクエリを Q 個処理してください。

  • 1 C: 頭を方向 C1 移動させる。ここで、CR, L, U, D のいずれかであり、それぞれ x 軸正方向、x 軸負方向、y 軸正方向、y 軸負方向を意味する。頭以外の全てのパーツは前のパーツに追従するように動く。すなわち、パーツ i\ \ (2\leq i \leq N) は移動前にパーツ i-1 があった座標に移動する。
  • 2 p: パーツ p のある座標を求める。

制約

  • 2 \leq N \leq 10^6
  • 1 \leq Q \leq 2\times 10^5
  • 1 種類目のクエリにおいて、CR, L, U, D のいずれか
  • 2 種類目のクエリにおいて、1\leq p \leq N
  • 入力に含まれる数値は全て整数

入力

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

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

各クエリは以下の 2 種類のいずれかの形式である。

1 C
2 p

出力

2 種類目のクエリの回数を q として q 行出力せよ。
i 行目には、i 番目のそのようなクエリに対する答えの座標を (x,y) としたとき、x,y を空白区切りで出力せよ。


入力例 1

5 9
2 3
1 U
2 3
1 R
1 D
2 3
1 L
2 1
2 5

出力例 1

3 0
2 0
1 1
1 0
1 0

2 種類目のクエリを処理する各タイミングにおいて、パーツの位置は次のようになっています。

図

複数のパーツが同じ座標に存在しうることに注意してください。

Score : 300 points

Problem Statement

Takahashi has created a game where the player controls a dragon on a coordinate plane.

The dragon consists of N parts numbered 1 to N, with part 1 being called the head.

Initially, part i is located at the coordinates (i,0). Process Q queries as follows.

  • 1 C: Move the head by 1 in direction C. Here, C is one of R, L, U, and D, which represent the positive x-direction, negative x-direction, positive y-direction, and negative y-direction, respectively. Each part other than the head moves to follow the part in front of it. That is, part i (2\leq i \leq N) moves to the coordinates where part i-1 was before the move.
  • 2 p: Find the coordinates of part p.

Constraints

  • 2 \leq N \leq 10^6
  • 1 \leq Q \leq 2\times 10^5
  • For the first type of query, C is one of R, L, U, and D.
  • For the second type of query, 1\leq p \leq N.
  • All numerical input values are integers.

Input

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

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

Each query is in one of the following two formats:

1 C
2 p

Output

Print q lines, where q is the number of queries of the second type.
The i-th line should contain x and y separated by a space, where (x,y) are the answer to the i-th such query.


Sample Input 1

5 9
2 3
1 U
2 3
1 R
1 D
2 3
1 L
2 1
2 5

Sample Output 1

3 0
2 0
1 1
1 0
1 0

At each time when processing the second type of query, the parts are at the following positions:

Figure

Note that multiple parts may exist at the same coordinates.

F - Festival

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

AtCoder 王国では、これから N 日間のお祭りが開催されます。そのうち、A_1 日目、A_2 日目、\dotsA_M 日目の M 日では花火が上がります。ここで、お祭りの最終日には花火が上がることが保証されます。(つまり、A_M=N が保証されます。)

i=1,2,\dots,N に対して、以下の問題を解いてください。

  • i 日目以降で初めて花火が上がるのは、i 日目から数えて何日後か?ただし、i 日目に花火が上がる場合 0 日後とする。

制約

  • 1 \le M \le N \le 2 \times 10^5
  • 1 \le A_1 < A_2 < \dots < A_M = N
  • 入力は全て整数

入力

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

N M
A_1 A_2 \dots A_M

出力

N 行出力せよ。

i(1 \le i \le N) 行目には、i 日目以降で初めて花火が上がるのは、i 日目から数えて何日後かを整数として出力せよ。


入力例 1

3 2
2 3

出力例 1

1
0
0

AtCoder 王国ではお祭りを 3 日間開催し、2,3 日目に花火が上がります。

  • 1 日目以降で初めて花火が上がるのは 2 日目なので、1 日目から数えて 1 日後です。
  • 2 日目以降で初めて花火が上がるのは 2 日目なので、2 日目から数えて 0 日後です。
  • 3 日目以降で初めて花火が上がるのは 3 日目なので、3 日目から数えて 0 日後です。

入力例 2

8 5
1 3 4 7 8

出力例 2

0
1
0
0
2
1
0
0

Score : 250 points

Problem Statement

The AtCoder Kingdom holds a festival for N days. On M of these days, namely on the A_1-th, A_2-th, \dots, A_M-th days, fireworks will be launched. It is guaranteed that fireworks will be launched on the last day of the festival. (In other words, A_M=N is guaranteed.)

For each i=1,2,\dots,N, solve the following problem.

  • How many days later from the i-th day will fireworks be launched for the first time on or after the i-th day? If fireworks are launched on the i-th day, it is considered to be 0 days later.

Constraints

  • 1 \le M \le N \le 2 \times 10^5
  • 1 \le A_1 < A_2 < \dots < A_M = N
  • All input values are integers.

Input

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

N M
A_1 A_2 \dots A_M

Output

Print N lines.

The i-th line (1 \le i \le N) should contain an integer representing the number of days from the i-th day until fireworks are launched for the first time on or after the i-th day.


Sample Input 1

3 2
2 3

Sample Output 1

1
0
0

The kingdom holds a festival for 3 days, and fireworks are launched on the 2-nd and 3-rd days.

  • From the 1-st day, the first time fireworks are launched is the 2-nd day of the festival, which is 1 day later.
  • From the 2-nd day, the first time fireworks are launched is the 2-nd day of the festival, which is 0 days later.
  • From the 3-rd day, the first time fireworks are launched is the 3-rd day of the festival, which is 0 days later.

Sample Input 2

8 5
1 3 4 7 8

Sample Output 2

0
1
0
0
2
1
0
0
G - Synchronized Players

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

NN 列のグリッドがあり、各マスは空きマスか障害物のあるマスのいずれかです。グリッドの上から i 行目、左から j 列目のマスを (i, j) と表記します。

また、2 人のプレイヤーがグリッド上の相異なる空きマス上におり、各マスの情報は N 個の長さ N の文字列 S_1, S_2, \ldots, S_N として以下の形式で与えられます。

  • S_ij 文字目が P であるとき、(i, j) は空きマスであり、プレイヤーがいる

  • S_ij 文字目が . であるとき、(i, j) は空きマスであり、プレイヤーがいない

  • S_ij 文字目が # であるとき、(i, j) は障害物のあるマスである

以下の操作を繰り返し 2 人のプレイヤーを同じマスに集めるために必要な操作回数の最小値を求めてください。ただし、操作の繰り返しにより 2 人のプレイヤーを同じマスに集めることができない場合には -1 を出力してください。

  • 上下左右のいずれかの方向を決める。そして各プレイヤーはともにその方向に隣接するマスへの移動を試みる。各プレイヤーは移動先のマスが存在し、かつ空きマスであるならば移動し、そうでないならば移動しない。

制約

  • N2 以上 60 以下の整数
  • S_i は長さ NP, ., # からなる文字列
  • S_ij 文字目が P であるような組 (i, j) の個数はちょうど 2

入力

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

N
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

5
....#
#..#.
.P...
..P..
....#

出力例 1

3

はじめに (3, 2) にいるプレイヤーをプレイヤー 1(4, 3) にいるプレイヤーをプレイヤー 2 とします。

例えば以下のようにすることで、3 回の操作で 2 人のプレイヤーが同じマスに集まります。

  • 左を選択する。プレイヤー 1(3, 1) に移動し、プレイヤー 2(4, 2) に移動する。

  • 上を選択する。プレイヤー 1 は移動せず、プレイヤー 2(3, 2) に移動する。

  • 左を選択する。プレイヤー 1 は移動せず、プレイヤー 2(3, 1) に移動する。


入力例 2

2
P#
#P

出力例 2

-1

入力例 3

10
..........
..........
..........
..........
....P.....
.....P....
..........
..........
..........
..........

出力例 3

10

Score: 400 points

Problem Statement

There is an N \times N grid, where each cell is either empty or contains an obstacle. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.

There are also two players on distinct empty cells of the grid. The information about each cell is given as N strings S_1, S_2, \ldots, S_N of length N, in the following format:

  • If the j-th character of S_i is P, then (i, j) is an empty cell with a player on it.

  • If the j-th character of S_i is ., then (i, j) is an empty cell without a player.

  • If the j-th character of S_i is #, then (i, j) contains an obstacle.

Find the minimum number of moves required to bring the two players to the same cell by repeating the following operation. If it is impossible to bring the two players to the same cell by repeating the operation, print -1.

  • Choose one of the four directions: up, down, left, or right. Then, each player attempts to move to the adjacent cell in that direction. Each player moves if the destination cell exists and is empty, and does not move otherwise.

Constraints

  • N is an integer between 2 and 60, inclusive.
  • S_i is a string of length N consisting of P, ., and #.
  • There are exactly two pairs (i, j) where the j-th character of S_i is P.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

5
....#
#..#.
.P...
..P..
....#

Sample Output 1

3

Let us call the player starting at (3, 2) Player 1 and the player starting at (4, 3) Player 2.

For example, doing the following brings the two players to the same cell in three moves:

  • Choose left. Player 1 moves to (3, 1), and Player 2 moves to (4, 2).

  • Choose up. Player 1 does not move, and Player 2 moves to (3, 2).

  • Choose left. Player 1 does not move, and Player 2 moves to (3, 1).


Sample Input 2

2
P#
#P

Sample Output 2

-1

Sample Input 3

10
..........
..........
..........
..........
....P.....
.....P....
..........
..........
..........
..........

Sample Output 3

10
H - Maximum Glutton

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

高橋君はすぬけ君のために N 個の料理を作りました。 料理には 1 から N までの番号がつけられていて、料理 i甘さA_iしょっぱさB_i です。

高橋君はこれらの料理を好きな順番で並べることができます。 すぬけ君は料理を並べられた順に食べていきますが、ある時点においてそれまでに食べた料理の甘さの合計が X を超えるかしょっぱさの合計が Y を超えた場合、それ以降の料理は食べません。

高橋君は、すぬけ君にできるだけ多くの料理を食べてほしいと思っています。 高橋君がうまく料理を並べたとき、すぬけ君が最大で何個の料理を食べることになるか求めてください。

制約

  • 1\leq N \leq 80
  • 1\leq A_i,B_i \leq 10000
  • 1\leq X,Y \leq 10000
  • 入力は全て整数

入力

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

N X Y
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

答えを整数として出力せよ。


入力例 1

4 8 4
1 5
3 2
4 1
5 3

出力例 1

3

高橋君が料理を 2,3,1,4 の順番で並べた場合のすぬけ君の行動を考えます。

  • まず料理 2 を食べる。ここまでに食べた料理の甘さの合計は 3、しょっぱさの合計は 2 である。
  • 次に料理 3 を食べる。ここまでに食べた料理の甘さの合計は 7、しょっぱさの合計は 3 である。
  • 次に料理 1 を食べる。ここまでに食べた料理の甘さの合計は 8、しょっぱさの合計は 8 である。
  • しょっぱさの合計が Y=4 を超えたので、これ以降の料理は食べない。

よって、この並び方の場合すぬけ君は 3 個の料理を食べることになります。

高橋君が料理をどのように並べてもすぬけ君が 4 つ全ての料理を食べることはないので、答えは 3 です。


入力例 2

2 1 1
3 2
3 2

出力例 2

1

入力例 3

2 100 100
3 2
3 2

出力例 3

2

入力例 4

6 364 463
230 381
154 200
328 407
339 94
193 10
115 309

出力例 4

3

Score : 475 points

Problem Statement

Takahashi has prepared N dishes for Snuke. The dishes are numbered from 1 to N, and dish i has a sweetness of A_i and a saltiness of B_i.

Takahashi can arrange these dishes in any order he likes. Snuke will eat the dishes in the order they are arranged, but if at any point the total sweetness of the dishes he has eaten so far exceeds X or the total saltiness exceeds Y, he will not eat any further dishes.

Takahashi wants Snuke to eat as many dishes as possible. Find the maximum number of dishes Snuke will eat if Takahashi arranges the dishes optimally.

Constraints

  • 1 \leq N \leq 80
  • 1 \leq A_i, B_i \leq 10000
  • 1 \leq X, Y \leq 10000
  • All input values are integers.

Input

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

N X Y
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the answer as an integer.


Sample Input 1

4 8 4
1 5
3 2
4 1
5 3

Sample Output 1

3

Consider the scenario where Takahashi arranges the dishes in the order 2, 3, 1, 4.

  • First, Snuke eats dish 2. The total sweetness so far is 3, and the total saltiness is 2.
  • Next, Snuke eats dish 3. The total sweetness so far is 7, and the total saltiness is 3.
  • Next, Snuke eats dish 1. The total sweetness so far is 8, and the total saltiness is 8.
  • The total saltiness has exceeded Y=4, so Snuke will not eat any further dishes.

Thus, in this arrangement, Snuke will eat three dishes.

No matter how Takahashi arranges the dishes, Snuke will not eat all four dishes, so the answer is 3.


Sample Input 2

2 1 1
3 2
3 2

Sample Output 2

1

Sample Input 3

2 100 100
3 2
3 2

Sample Output 3

2

Sample Input 4

6 364 463
230 381
154 200
328 407
339 94
193 10
115 309

Sample Output 4

3
I - Guess The Number 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

この問題は インタラクティブな問題(あなたが作成したプログラムとジャッジプログラムが標準入出力を介して対話を行う形式の問題)です。

あなたとジャッジは下記の手順を行います。 手順はフェイズ 1 とフェイズ 2 からなり、まずフェイズ 1 を行った直後、続けてフェイズ 2 を行います。

(フェイズ 1

  • ジャッジが 1 以上 10^9 以下の整数 N を決める。この整数は隠されている。
  • あなたは 1 以上 110 以下の整数 M を出力する。
  • さらにあなたは、すべての i = 1, 2, \ldots, M について 1 \leq A_i \leq M を満たす、長さ M の整数列 A=(A_1,A_2,\ldots,A_M) を出力する。

(フェイズ 2

  • ジャッジから、長さ M の整数列 B=(B_1,B_2,\ldots,B_M) が与えられる。ここで、 B_i = f^N(i) である。 f(i)1 以上 M 以下の整数 i に対し f(i)=A_i で定められ、 f^N(i)if(i) で置き換える操作を N 回行った際に得られる整数である。
  • あなたは、B の情報から、ジャッジが決めた整数 N を特定し、N を出力する。

上記の手順を行った後、直ちにプログラムを終了することで正解となります。

制約

  • N1 以上 10^9 以下の整数

入出力

この問題はインタラクティブな問題(あなたが作成したプログラムとジャッジプログラムが標準入出力を介して対話を行う形式の問題)です。

(フェイズ 1

  • まず、1 以上 110 以下の整数 M を出力してください。出力後、必ず改行してください。
M
  • その後、空白区切りで 1 以上 M 以下の整数からなる長さ M の整数列 A=(A_1,A_2,\ldots,A_M) を出力してください。出力後、必ず改行してください。
A_1 A_2 \ldots A_M

(フェイズ 2

  • まず、長さ M の整数列 B=(B_1,B_2,\ldots,B_M) が入力から与えられます。
B_1 B_2 \ldots B_M
  • 整数 N を求め、出力してください。出力後、必ず改行してください。
N

不正な出力がなされた場合、ジャッジは -1 を出力します。この時、提出はすでに不正解と判定されています。ジャッジプログラムはこの時点で終了するため、あなたのプログラムも終了するのが望ましいです。

注意点

  • 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
  • 対話の途中で不正な出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。
  • 答えを出力したら(または -1 を受け取ったら)直ちにプログラムを正常終了してください。そうしなかった場合、ジャッジ結果は不定です。
  • 特に、余計な改行も不正なフォーマットの出力とみなされるので注意してください。

入出力例

以下は、N = 2 の場合の入出力例です。

入力 出力 説明
ジャッジは N=2 と決めました。N は入力としては与えられず、隠されています。
4 M を出力します。
2 3 4 4 A=(2,3,4,4) を出力します。
3 4 4 4 f^2(1)=3,f^2(2)=4,f^2(3)=4,f^2(4)=4 なので、ジャッジは B=(3,4,4,4) をあなたのプログラムに与えます。
2 B から N=2 であると特定しました。 N を出力し、プログラムを正常終了させてください。

Score : 500 points

Problem Statement

This is an interactive task, where your and the judge's programs interact via Standard Input and Output.

You and the judge will follow the procedure below. The procedure consists of phases 1 and 2; phase 1 is immediately followed by phase 2.

(Phase 1)

  • The judge decides an integer N between 1 and 10^9 (inclusive), which is hidden.
  • You print an integer M between 1 and 110 (inclusive).
  • You also print an integer sequence A=(A_1,A_2,\ldots,A_M) of length M such that 1 \leq A_i \leq M for all i = 1, 2, \ldots, M.

(Phase 2)

  • The judge gives you an integer sequence B=(B_1,B_2,\ldots,B_M) of length M. Here, B_i = f^N(i). f(i) is defined by f(i)=A_i for all integers i between 1 and M (inclusive), and f^N(i) is the integer resulting from replacing i with f(i) N times.
  • Based on the given B, you identify the integer N that the judge has decided, and print N.

After the procedure above, terminate the program immediately to be judged correct.

Constraints

  • N is an integer between 1 and 10^9 (inclusive).

Input and Output

This is an interactive task, where your and the judge's programs interact via Standard Input and Output.

(Phase 1)

  • First, print an integer M between 1 and 110 (inclusive). It must be followed by a newline.
M
  • Then, print a sequence A=(A_1,A_2,\ldots,A_M) of length M consisting of integers between 1 and M (inclusive), with spaces in between. It must be followed by a newline.
A_1 A_2 \ldots A_M

(Phase 2)

  • First, an integer sequence B=(B_1,B_2,\ldots,B_M) of length M is given from the input.
B_1 B_2 \ldots B_M
  • Find the integer N and print it. It must be followed by a newline.
N

If you print something illegal, the judge prints -1. In that case, your submission is already considered incorrect. Since the judge program terminates at this point, it is desirable that your program terminates too.

Notes

  • After each output, add a newline and then flush Standard Output. Otherwise, you may get a TLE verdict.
  • If an invalid output is printed during the interaction, or if the program terminates halfway, the verdict will be indeterminate.
  • After you print the answer (or you receive -1), immediately terminate the program normally. Otherwise, the verdict will be indeterminate.
  • Note that an excessive newline is also considered an invalid input.

Sample Interaction

The following is a sample interaction with N = 2.

Input Output Description
The judge has decided that N=2. N is hidden: it is not given as an input.
4 You print M.
2 3 4 4 You print A=(2,3,4,4).
3 4 4 4 f^2(1)=3,f^2(2)=4,f^2(3)=4, and f^2(4)=4, so the judge gives B=(3,4,4,4) to your program.
2 Based on B, you have identified that N=2. Print N and terminate the program normally.