Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
縦 H マス, 横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i, j) と呼びます。
はじめ、グリッド上には、ある 縦横 2 マス以上 の部分長方形の内部にあるマスにクッキーが 1 枚ずつ置かれていて、それ以外のマスにはクッキーが置かれていません。
形式的に説明すると、以下の条件を全て満たす 4 つの整数の組 (a,b,c,d) がただ 1 つ存在します。
- 1 \leq a \lt b \leq H
- 1 \leq c \lt d \leq W
- グリッド上のマスのうち、a \leq i \leq b, c \leq j \leq d を満たす全てのマス (i, j) にはクッキーが 1 枚ずつ置かれていて、それ以外のマスにはクッキーが置かれていない。
ところが、すぬけ君がグリッド上のクッキーのどれか 1 枚を取って食べてしまいました。
すぬけ君がクッキーを取ったマスは、クッキーが置かれていない状態に変わります。
すぬけ君がクッキーを食べた後のグリッドの状態が入力として与えられます。
マス (i, j) の状態は文字 S_{i,j} として与えられて、# はクッキーが置かれているマスを, . はクッキーが置かれていないマスを意味します。
すぬけ君が食べたクッキーが元々置かれていたマスを答えてください。(答えは一意に定まります。)
制約
- 2 \leq H, W \leq 500
- S_{i,j} は
#または.
入力
入力は以下の形式で標準入力から与えられる。
H W
S_{1,1}S_{1,2}\dotsS_{1,W}
S_{2,1}S_{2,2}\dotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\dotsS_{H,W}
出力
すぬけ君が食べたクッキーが元々置かれていたマスを (i, j) とする。i, j をこの順に空白区切りで出力せよ。
入力例 1
5 6 ...... ..#.#. ..###. ..###. ......
出力例 1
2 4
はじめ、クッキーは (2, 3) を左上、(4, 5) を右下とする部分長方形の内部にあるマスに置かれていて、すぬけ君は (2, 4) にあるクッキーを食べたことがわかります。よって (2, 4) を出力します。
入力例 2
3 2 #. ## ##
出力例 2
1 2
はじめ、クッキーは (1, 1) を左上、(3, 2) を右下とする部分長方形の内部にあるマスに置かれていて、すぬけ君は (1, 2) にあるクッキーを食べたことがわかります。
入力例 3
6 6 ..#### ..##.# ..#### ..#### ..#### ......
出力例 3
2 5
Score : 300 points
Problem Statement
There is a grid with H rows and W columns. Let (i, j) denote the square at the i-th row from the top and the j-th column from the left.
Initially, there was one cookie on each square inside a rectangle whose height and width were at least 2 squares long, and no cookie on the other squares.
Formally, there was exactly one quadruple of integers (a,b,c,d) that satisfied all of the following conditions.
- 1 \leq a \lt b \leq H
- 1 \leq c \lt d \leq W
- There was one cookie on each square (i, j) such that a \leq i \leq b, c \leq j \leq d, and no cookie on the other squares.
However, Snuke took and ate one of the cookies on the grid.
The square that contained that cookie is now empty.
As the input, you are given the state of the grid after Snuke ate the cookie.
The state of the square (i, j) is given as the character S_{i,j}, where # means a square with a cookie, and . means a square without one.
Find the square that contained the cookie eaten by Snuke. (The answer is uniquely determined.)
Constraints
- 2 \leq H, W \leq 500
- S_{i,j} is
#or..
Input
The input is given from Standard Input in the following format:
H W
S_{1,1}S_{1,2}\dotsS_{1,W}
S_{2,1}S_{2,2}\dotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\dotsS_{H,W}
Output
Let (i, j) the square contained the cookie eaten by Snuke. Print i and j in this order, separated by a space.
Sample Input 1
5 6 ...... ..#.#. ..###. ..###. ......
Sample Output 1
2 4
Initially, cookies were on the squares inside the rectangle with (2, 3) as the top-left corner and (4, 5) as the bottom-right corner, and Snuke ate the cookie on (2, 4). Thus, you should print (2, 4).
Sample Input 2
3 2 #. ## ##
Sample Output 2
1 2
Initially, cookies were placed on the squares inside the rectangle with (1, 1) as the top-left corner and (3, 2) as the bottom-right corner, and Snuke ate the cookie at (1, 2).
Sample Input 3
6 6 ..#### ..##.# ..#### ..#### ..#### ......
Sample Output 3
2 5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の数列 A=(A_1,\ldots,A_N) が与えられます。
1\leq i,j \leq N である組 (i,j) であって、A_i-A_j=X となるものが存在するかどうか判定してください。
制約
- 2 \leq N \leq 2\times 10^5
- -10^9 \leq A_i \leq 10^9
- -10^9 \leq X \leq 10^9
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N X A_1 \ldots A_N
出力
1\leq i,j \leq N である組 (i,j) であって、A_i-A_j=X となるものが存在するとき Yes、存在しないとき No と出力せよ。
入力例 1
6 5 3 1 4 1 5 9
出力例 1
Yes
A_6-A_3=9-4=5 です。
入力例 2
6 -4 -2 -7 -1 -8 -2 -8
出力例 2
No
A_i-A_j=-4 となる組 (i,j) は存在しません。
入力例 3
2 0 141421356 17320508
出力例 3
Yes
A_1-A_1=0 です。
Score : 300 points
Problem Statement
You are given a sequence of N numbers: A=(A_1,\ldots,A_N).
Determine whether there is a pair (i,j) with 1\leq i,j \leq N such that A_i-A_j=X.
Constraints
- 2 \leq N \leq 2\times 10^5
- -10^9 \leq A_i \leq 10^9
- -10^9 \leq X \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N X A_1 \ldots A_N
Output
Print Yes if there is a pair (i,j) with 1\leq i,j \leq N such that A_i-A_j=X, and No otherwise.
Sample Input 1
6 5 3 1 4 1 5 9
Sample Output 1
Yes
We have A_6-A_3=9-4=5.
Sample Input 2
6 -4 -2 -7 -1 -8 -2 -8
Sample Output 2
No
There is no pair (i,j) such that A_i-A_j=-4.
Sample Input 3
2 0 141421356 17320508
Sample Output 3
Yes
We have A_1-A_1=0.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は 2 以上の整数が書かれた N 個のボールを持っており、これらを細長い筒の中に落としていきます。i \, (1 \leq i \leq N) 回目には、a_i が書かれたボールを落とします。
ボールは特殊な材質でできており、筒の中において k \, (k \geq 2) が書かれたボールが k 個連続すると、それら k 個のボールは全て消えてしまいます。
各 i \, (1 \leq i \leq N) について、i 個目のボールを筒の中に落とした後、筒の中に何個のボールがあるか求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 2 \leq a_i \leq 2 \times 10^5 \, (1 \leq i \leq N)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N a_1 \ldots a_N
出力
N 行出力せよ。i \, (1 \leq i \leq N) 行目には、i 個目のボールを筒の中に落とした後、筒の中にあるボールの個数を出力せよ。
入力例 1
5 3 2 3 2 2
出力例 1
1 2 3 4 3
筒の中は次のように変化します。
- 1 個目のボールを落とす。筒の中にあるボールに書かれた整数は 3 である。
- 2 個目のボールを落とす。筒の中にあるボールに書かれた整数は下から順に 3, 2 である。
- 3 個目のボールを落とす。筒の中にあるボールに書かれた整数は下から順に 3, 2, 3 である。
- 4 個目のボールを落とす。筒の中にあるボールに書かれた整数は下から順に 3, 2, 3, 2 である。
- 5 個目のボールを落とす。筒の中にあるボールに書かれた整数は下から順に 3, 2, 3, 2, 2 となるが、2 が書かれたボールが 2 個連続しているのでこれらは消え、下から順に 3, 2, 3 となる。

入力例 2
10 2 3 2 3 3 3 2 3 3 2
出力例 2
1 2 3 4 5 3 2 3 1 0
Score : 400 points
Problem Statement
Takahashi has N balls. Each ball has an integer not less than 2 written on it. He will insert them in a cylinder one by one. The integer written on the i-th ball is a_i.
The balls are made of special material. When k balls with k (k \geq 2) written on them line up in a row, all these k balls will disappear.
For each i (1 \leq i \leq N), find the number of balls after inserting the i-th ball.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 2 \leq a_i \leq 2 \times 10^5 \, (1 \leq i \leq N)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N a_1 \ldots a_N
Output
Print N lines. The i-th line (1 \leq i \leq N) should contain the number of balls after inserting the i-th ball.
Sample Input 1
5 3 2 3 2 2
Sample Output 1
1 2 3 4 3
The content of the cylinder changes as follows.
- After inserting the 1-st ball, the cylinder contains the ball with 3.
- After inserting the 2-nd ball, the cylinder contains 3, 2 from bottom to top.
- After inserting the 3-rd ball, the cylinder contains 3, 2, 3 from bottom to top.
- After inserting the 4-th ball, the cylinder contains 3, 2, 3, 2 from bottom to top.
- After inserting the 5-th ball, the cylinder momentarily has 3, 2, 3, 2, 2 from bottom to top. The two consecutive balls with 2 disappear, and the cylinder eventually contains 3, 2, 3 from bottom to top.

Sample Input 2
10 2 3 2 3 3 3 2 3 3 2
Sample Output 2
1 2 3 4 5 3 2 3 1 0
Time Limit: 6 sec / Memory Limit: 1024 MiB
配点 : 500 点
コンテスト開催当時はメモリ制限が2GBでしたが、ジャッジ環境が異なるため、現在はメモリ制限を1GBに設定しております。なお、このメモリ制限でもAC出来ることは確認しています。
問題文
ここに、 N \times N のチェス盤があります。このチェス盤の上から i 行目、左から j 列目にあるマスをマス (i,j) と呼びます。
チェス盤の情報は N 個の文字列 S_i として与えられます。
文字列 S_i の j 文字目である S_{i,j} には、以下の情報が含まれています。
- S_{i,j}=
.のとき マス (i,j) には何も置かれていない。 - S_{i,j}=
#のとき マス (i,j) には白のポーンが 1 つ置かれている。このポーンを動かしたり取り除いたりすることはできない。
この盤面のマス (A_x,A_y) に、白のビショップを 1 つ置きました。
この白のビショップをチェスのルール (注記参照) に従ってマス (A_x,A_y) からマス (B_x,B_y) に移動させるために必要な最小の手数を求めてください。
ただし、移動できない場合は代わりに -1 を出力してください。
注記
マス (i,j) に置かれている白の ビショップ は、 1 手で以下のルールに従って移動することができます。
-
各正整数 d について、以下の条件を全て満たせばマス (i+d,j+d) に移動できる。
- マス (i+d,j+d) が盤内に存在する
- 全ての正整数 l \le d について、 (i+l,j+l) に白のポーンがない
-
各正整数 d について、以下の条件を全て満たせばマス (i+d,j-d) に移動できる。
- マス (i+d,j-d) が盤内に存在する
- 全ての正整数 l \le d について、 (i+l,j-l) に白のポーンがない
-
各正整数 d について、以下の条件を全て満たせばマス (i-d,j+d) に移動できる。
- マス (i-d,j+d) が盤内に存在する
- 全ての正整数 l \le d について、 (i-l,j+l) に白のポーンがない
-
各正整数 d について、以下の条件を全て満たせばマス (i-d,j-d) に移動できる。
- マス (i-d,j-d) が盤内に存在する
- 全ての正整数 l \le d について、 (i-l,j-l) に白のポーンがない
制約
- 2 \le N \le 1500
- 1 \le A_x,A_y \le N
- 1 \le B_x,B_y \le N
- (A_x,A_y) \neq (B_x,B_y)
- S_i は
.および#からなる N 文字の文字列 - S_{A_x,A_y}=
. - S_{B_x,B_y}=
.
入力
入力は以下の形式で標準入力から与えられる。
N A_x A_y B_x B_y S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
5 1 3 3 5 ....# ...#. ..... .#... #....
出力例 1
3
以下のように移動させることで 3 手でビショップを (1,3) から (3,5) まで移動させることができます。 2 手以内でビショップを (1,3) から (3,5) まで移動させることはできません。
- (1,3) \rightarrow (2,2) \rightarrow (4,4) \rightarrow (3,5)
入力例 2
4 3 2 4 2 .... .... .... ....
出力例 2
-1
どのようにビショップを動かしても (3,2) から (4,2) に移動させることはできません。
入力例 3
18 18 1 1 18 .................. .####............. .#..#..####....... .####..#..#..####. .#..#..###...#.... .#..#..#..#..#.... .......####..#.... .............####. .................. .................. .####............. ....#..#..#....... .####..#..#..####. .#.....####..#.... .####.....#..####. ..........#..#..#. .............####. ..................
出力例 3
9
Score : 500 points
During the time of the contest, the memory limit was set to 2GB. However, due to a change in the judging environment, the memory limit has now been set to 1GB. Please note that it has been confirmed that solutions can still achieve an Acceptable Completion (AC) within this memory limit.
Problem Statement
We have an N \times N chessboard. Let (i, j) denote the square at the i-th row from the top and j-th column from the left of this board.
The board is described by N strings S_i.
The j-th character of the string S_i, S_{i,j}, means the following.
- If S_{i,j}=
., the square (i, j) is empty. - If S_{i,j}=
#, the square (i, j) is occupied by a white pawn, which cannot be moved or removed.
We have put a white bishop on the square (A_x, A_y).
Find the minimum number of moves needed to move this bishop from (A_x, A_y) to (B_x, B_y) according to the rules of chess (see Notes).
If it cannot be moved to (B_x, B_y), report -1 instead.
Notes
A white bishop on the square (i, j) can go to the following positions in one move.
-
For each positive integer d, it can go to (i+d,j+d) if all of the conditions are satisfied.
- The square (i+d,j+d) exists in the board.
- For every positive integer l \le d, (i+l,j+l) is not occupied by a white pawn.
-
For each positive integer d, it can go to (i+d,j-d) if all of the conditions are satisfied.
- The square (i+d,j-d) exists in the board.
- For every positive integer l \le d, (i+l,j-l) is not occupied by a white pawn.
-
For each positive integer d, it can go to (i-d,j+d) if all of the conditions are satisfied.
- The square (i-d,j+d) exists in the board.
- For every positive integer l \le d, (i-l,j+l) is not occupied by a white pawn.
-
For each positive integer d, it can go to (i-d,j-d) if all of the conditions are satisfied.
- The square (i-d,j-d) exists in the board.
- For every positive integer l \le d, (i-l,j-l) is not occupied by a white pawn.
Constraints
- 2 \le N \le 1500
- 1 \le A_x,A_y \le N
- 1 \le B_x,B_y \le N
- (A_x,A_y) \neq (B_x,B_y)
- S_i is a string of length N consisting of
.and#. - S_{A_x,A_y}=
. - S_{B_x,B_y}=
.
Input
Input is given from Standard Input in the following format:
N A_x A_y B_x B_y S_1 S_2 \vdots S_N
Output
Print the answer.
Sample Input 1
5 1 3 3 5 ....# ...#. ..... .#... #....
Sample Output 1
3
We can move the bishop from (1,3) to (3,5) in three moves as follows, but not in two or fewer moves.
- (1,3) \rightarrow (2,2) \rightarrow (4,4) \rightarrow (3,5)
Sample Input 2
4 3 2 4 2 .... .... .... ....
Sample Output 2
-1
There is no way to move the bishop from (3,2) to (4,2).
Sample Input 3
18 18 1 1 18 .................. .####............. .#..#..####....... .####..#..#..####. .#..#..###...#.... .#..#..#..#..#.... .......####..#.... .............####. .................. .................. .####............. ....#..#..#....... .####..#..#..####. .#.....####..#.... .####.....#..####. ..........#..#..#. .............####. ..................
Sample Output 3
9
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
xy 平面上に N 個の点 1,2,\dots,N があります。このうち点 i は座標 (X_i,Y_i) にあります。
あなたは、以下の操作を 0 回以上 K 回以下行うことができます。
- まず、 N 点の中からひとつを選択する。選ばれた点を k とし、この点が現在 (x,y) にあるものとする。
- 次に、以下の 4 つからひとつを選択し、実行する。
- 点 k を x 軸沿いに +1 だけ移動させる。点 k の座標は (x+1,y) となる。
- 点 k を x 軸沿いに -1 だけ移動させる。点 k の座標は (x-1,y) となる。
- 点 k を y 軸沿いに +1 だけ移動させる。点 k の座標は (x,y+1) となる。
- 点 k を y 軸沿いに -1 だけ移動させる。点 k の座標は (x,y-1) となる。
- 複数の点を同じ座標に存在させることも許されます。また、入力で複数の点が同じ座標に存在しうることに注意してください。
全ての操作が終わった後、 N 個全ての点を内部または周上に包含する、各辺が x 軸または y 軸に平行な正方形をひとつ書き込みます。
このとき、書き込む正方形の一辺の長さとしてありうる最小の値を求めてください。全ての点が常に格子点にあることから、この値は整数であることが示せます。
特に、全ての点を同じ座標に存在させられる時、答えは 0 であるものとします。
制約
- 入力は全て整数
- 1 \le N \le 2 \times 10^5
- 0 \le K \le 4 \times 10^{14}
- 0 \le X_i,Y_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N K X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
出力
答えを整数として出力せよ。
入力例 1
6 5 2 0 5 2 0 3 3 2 3 4 1 5
出力例 1
3
このケースについて、横を x 軸、縦を y 軸として図示したものが以下です。

例えば、図中の矢印に従って 4 度の移動を行った後、図中に示した一辺が 3 の正方形で全ての点を内部または周上に含むことができ、これが最小値であることが示せます。
入力例 2
4 400000000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
出力例 2
0
最初から全ての点が同じ座標に存在します。
例えば操作を 0 回行う (即ち、全く行わない) ことにより、全ての点を同じ座標に存在させられるので、この入力に対する答えは 0 です。
入力例 3
10 998244353 489733278 189351894 861289363 30208889 450668761 133103889 306319121 739571083 409648209 922270934 930832199 304946211 358683490 923133355 369972904 539399938 915030547 735320146 386219602 277971612
出力例 3
484373824
Score : 525 points
Problem Statement
There are N points labeled 1, 2, \dots, N on the xy-plane. Point i is located at coordinates (X_i, Y_i).
You can perform the following operation between 0 and K times, inclusive.
- First, choose one of the N points. Let k be the selected point, and assume it is currently at (x, y).
- Next, choose and execute one of the following four actions:
- Move point k along the x-axis by +1. The coordinates of point k become (x+1, y).
- Move point k along the x-axis by -1. The coordinates of point k become (x-1, y).
- Move point k along the y-axis by +1. The coordinates of point k become (x, y+1).
- Move point k along the y-axis by -1. The coordinates of point k become (x, y-1).
- It is allowed to have multiple points at the same coordinates. Note that the input may already have multiple points at the same coordinates.
After all your operations, draw one square that includes all the N points inside or on the circumference, with each side parallel to the x- or y-axis.
Find the minimum possible value for the length of a side of this square. This value can be shown to be an integer since all points are always on lattice points.
In particular, if all points can be made to exist at the same coordinates, the answer is considered to be 0.
Constraints
- All input values are integers.
- 1 \le N \le 2 \times 10^5
- 0 \le K \le 4 \times 10^{14}
- 0 \le X_i, Y_i \le 10^9
Input
Input is given from Standard Input in the following format:
N K X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
Output
Print the answer as an integer.
Sample Input 1
6 5 2 0 5 2 0 3 3 2 3 4 1 5
Sample Output 1
3
The figure below illustrates this case with the horizontal x-axis and the vertical y-axis.

For example, after performing four moves following the arrows in the figure, you can include all points inside or on the circumference of the square shown in the figure with a side length of 3, and this can be shown to be the minimum value.
Sample Input 2
4 400000000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 2
0
All points already exist at the same coordinates from the beginning.
For example, by performing zero operations, all points can be made to exist at the same coordinates, so the answer for this input is 0.
Sample Input 3
10 998244353 489733278 189351894 861289363 30208889 450668761 133103889 306319121 739571083 409648209 922270934 930832199 304946211 358683490 923133355 369972904 539399938 915030547 735320146 386219602 277971612
Sample Output 3
484373824