実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
長さ N の整数からなる数列 A=(A_1,\ldots,A_N),B=(B_1,\ldots,B_N) が与えられます。
以下の条件を全て満たす長さ N の数列 X=(X_1,\ldots,X_N) が存在するかを判定してください。
-
すべての i(1\leq i\leq N) について、X_i = A_i または X_i = B_i
-
すべての i(1\leq i\leq N-1) について、|X_i - X_{i+1}| \leq K
制約
- 1 \leq N \leq 2\times 10^5
- 0 \leq K \leq 10^9
- 1 \leq A_i,B_i \leq 10^9
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 \ldots A_N B_1 \ldots B_N
出力
条件を全て満たす X が存在するならば Yes
と、存在しないならば No
と出力せよ。
入力例 1
5 4 9 8 3 7 2 1 6 2 9 5
出力例 1
Yes
X=(9,6,3,7,5) が全ての条件を満たします。
入力例 2
4 90 1 1 1 100 1 2 3 100
出力例 2
No
条件を満たす X は存在しません。
入力例 3
4 1000000000 1 1 1000000000 1000000000 1 1000000000 1 1000000000
出力例 3
Yes
Score : 300 points
Problem Statement
You are given two sequences, each of length N, consisting of integers: A=(A_1, \ldots, A_N) and B=(B_1, \ldots, B_N).
Determine whether there is a sequence of length N, X=(X_1, \ldots, X_N), satisfying all of the conditions below.
-
X_i = A_i or X_i = B_i, for every i(1\leq i\leq N).
-
|X_i - X_{i+1}| \leq K, for every i(1\leq i\leq N-1).
Constraints
- 1 \leq N \leq 2\times 10^5
- 0 \leq K \leq 10^9
- 1 \leq A_i,B_i \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K A_1 \ldots A_N B_1 \ldots B_N
Output
If there is an X that satisfies all of the conditions, print Yes
; otherwise, print No
.
Sample Input 1
5 4 9 8 3 7 2 1 6 2 9 5
Sample Output 1
Yes
X=(9,6,3,7,5) satisfies all conditions.
Sample Input 2
4 90 1 1 1 100 1 2 3 100
Sample Output 2
No
No X satisfies all conditions.
Sample Input 3
4 1000000000 1 1 1000000000 1000000000 1 1000000000 1 1000000000
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
H 行 W 列のマス目の上に 0 個以上のセンサが配置されています。上から i 行目、左から j 列目のマス目を (i, j) と表記します。
センサが配置されているマス目の情報は長さ W の文字列 S_1, S_2, \ldots, S_H によって与えられ、S_i の j 文字目が #
のとき、またそのときに限り (i, j) にセンサが配置されています。
このセンサは上下左右斜めに隣接しているマス目に存在する他のセンサと連動し、一つのセンサとして動作します。
ただし、マス目 (x, y) と (x', y') が上下左右斜めに隣接しているとは、\max(|x-x'|,|y-y'|) = 1 であることを指します。
また、センサ A とセンサ B が連動し、センサ A とセンサ C が連動しているとき、センサ B とセンサ C も連動することに注意してください。
連動するセンサを一つのセンサと見なしたとき、このマス目の上にあるセンサの個数を求めてください。
制約
- 1 \leq H, W \leq 1000
- H, W は整数
- S_i は各文字が
#
または.
である長さ W の文字列
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H
出力
答えを出力せよ。
入力例 1
5 6 .##... ...#.. ....## #.#... ..#...
出力例 1
3
連動しているセンサを一つのセンサと見なしたとき、
- (1,2),(1,3),(2,4),(3,5),(3,6) にあるセンサが連動したもの
- (4,1) にあるセンサ
- (4,3),(5,3) にあるセンサが連動したもの
の 3 つのセンサが存在します。
入力例 2
3 3 #.# .#. #.#
出力例 2
1
入力例 3
4 2 .. .. .. ..
出力例 3
0
入力例 4
5 47 .#..#..#####..#...#..#####..#...#...###...##### .#.#...#.......#.#...#......##..#..#...#..#.... .##....#####....#....#####..#.#.#..#......##### .#.#...#........#....#......#..##..#...#..#.... .#..#..#####....#....#####..#...#...###...#####
出力例 4
7
Score : 300 points
Problem Statement
There are zero or more sensors placed on a grid of H rows and W columns. Let (i, j) denote the square in the i-th row from the top and the j-th column from the left.
Whether each square contains a sensor is given by the strings S_1, S_2, \ldots, S_H, each of length W. (i, j) contains a sensor if and only if the j-th character of S_i is #
.
These sensors interact with other sensors in the squares horizontally, vertically, or diagonally adjacent to them and operate as one sensor.
Here, a cell (x, y) and a cell (x', y') are said to be horizontally, vertically, or diagonally adjacent if and only if \max(|x-x'|,|y-y'|) = 1.
Note that if sensor A interacts with sensor B and sensor A interacts with sensor C, then sensor B and sensor C also interact.
Considering the interacting sensors as one sensor, find the number of sensors on this grid.
Constraints
- 1 \leq H, W \leq 1000
- H and W are integers.
- S_i is a string of length W where each character is
#
or.
.
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H
Output
Print the answer.
Sample Input 1
5 6 .##... ...#.. ....## #.#... ..#...
Sample Output 1
3
When considering the interacting sensors as one sensor, the following three sensors exist:
- The interacting sensors at (1,2),(1,3),(2,4),(3,5),(3,6)
- The sensor at (4,1)
- The interacting sensors at (4,3),(5,3)
Sample Input 2
3 3 #.# .#. #.#
Sample Output 2
1
Sample Input 3
4 2 .. .. .. ..
Sample Output 3
0
Sample Input 4
5 47 .#..#..#####..#...#..#####..#...#...###...##### .#.#...#.......#.#...#......##..#..#...#..#.... .##....#####....#....#####..#.#.#..#......##### .#.#...#........#....#......#..##..#...#..#.... .#..#..#####....#....#####..#...#...###...#####
Sample Output 4
7
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
整数 N と A
, B
, C
からなる長さ N の文字列 R,C が与えられるので、以下の問題を解いてください。
N \times N のマス目があり、最初全てのマスは空きマスです。
各マスに A
, B
, C
のうち高々 1 文字を書き込みます。( 空きマスのままにすることも許されます )
以下の条件を全て満たすことが可能であるか判定し、可能であれば書き込み方を 1 つ出力して下さい。
- 各行 / 各列 に
A
,B
,C
がちょうど 1 個ずつ含まれる - i 行目に書かれた文字の中で最も左にある文字は R の i 文字目と一致する
- i 列目に書かれた文字の中で最も上にある文字は C の i 文字目と一致する
制約
- N は 3 以上 5 以下の整数
- R,C は
A
,B
,C
からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N R C
出力
問題文中の条件を満たす書き込み方が存在しない場合、 No
と 1 行に出力してください。
そうでない場合、以下の形式に従い書き込み方を出力してください。
Yes A_1 A_2 \vdots A_N
まず、 1 行目に Yes
と出力してください。
続く N 行のうちの i 行目に、長さ N の文字列である A_i を出力してください。
- A_i の j 文字目が
.
であるとき、上から i 行目、左から j 列目のマスは空きマスであることを示します。 - A_i の j 文字目が
A
であるとき、上から i 行目、左から j 列目のマスにA
が書き込まれたことを示します。 - A_i の j 文字目が
B
であるとき、上から i 行目、左から j 列目のマスにB
が書き込まれたことを示します。 - A_i の j 文字目が
C
であるとき、上から i 行目、左から j 列目のマスにC
が書き込まれたことを示します。
正しい書き込み方が複数存在する場合、どれを出力しても構いません。
入力例 1
5 ABCBC ACAAB
出力例 1
Yes AC..B .BA.C C.BA. BA.C. ..CBA
出力例のマス目は以下の条件を全て満たすため、正解として扱われます。
- 全ての行に
A
,B
,C
がちょうど 1 個ずつ含まれる - 全ての列に
A
,B
,C
がちょうど 1 個ずつ含まれる - 各行に書かれた最も左の文字は上から順に
A
,B
,C
,B
,C
である - 各列に書かれた最も上の文字は左から順に
A
,C
,A
,A
,B
である
入力例 2
3 AAA BBB
出力例 2
No
この入力では、条件を満たす書き込み方は存在しません。
Score : 450 points
Problem Statement
You are given an integer N and strings R and C of length N consisting of A
, B
, and C
. Solve the following problem.
There is a N \times N grid. All cells are initially empty.
You can write at most one character from A
, B
, and C
in each cell. (You can also leave the cell empty.)
Determine if it is possible to satisfy all of the following conditions, and if it is possible, print one way to do so.
- Each row and each column contain exactly one
A
, oneB
, and oneC
. - The leftmost character written in the i-th row matches the i-th character of R.
- The topmost character written in the i-th column matches the i-th character of C.
Constraints
- N is an integer between 3 and 5, inclusive.
- R and C are strings of length N consisting of
A
,B
, andC
.
Input
The input is given from Standard Input in the following format:
N R C
Output
If there is no way to fill the grid to satisfy the conditions in the problem statement, print No
in one line.
Otherwise, print one such way to fill the grid in the following format:
Yes A_1 A_2 \vdots A_N
The first line should contain Yes
.
The i-th of the subsequent N lines should contain a string A_i of length N.
- If the j-th character of A_i is
.
, it indicates that the cell in the i-th row from the top and the j-th column from the left is empty. - If the j-th character of A_i is
A
, it indicates thatA
is written in the cell in the i-th row from the top and the j-th column from the left. - If the j-th character of A_i is
B
, it indicates thatB
is written in the cell in the i-th row from the top and the j-th column from the left. - If the j-th character of A_i is
C
, it indicates thatC
is written in the cell in the i-th row from the top and the j-th column from the left.
If there are multiple correct ways to fill the grid, you may print any of them.
Sample Input 1
5 ABCBC ACAAB
Sample Output 1
Yes AC..B .BA.C C.BA. BA.C. ..CBA
The grid in the output example satisfies all the following conditions, so it will be treated as correct.
- Each row contains exactly one
A
, oneB
, and oneC
. - Each column contains exactly one
A
, oneB
, and oneC
. - The leftmost characters written in the rows are
A
,B
,C
,B
,C
from top to bottom. - The topmost characters written in the columns are
A
,C
,A
,A
,B
from left to right.
Sample Input 2
3 AAA BBB
Sample Output 2
No
For this input, there is no way to fill the grid to satisfy the conditions.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
以下の操作を M 回行ってください。
- 各 i\ (1\leq i \leq N) について、 A_i に i を加算する。その後 A に含まれない最小の非負整数を求める。
制約
- 1\leq N,M \leq 2\times 10^5
- -10^9\leq A_i\leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_N
出力
M 行出力せよ。
i (1\leq i \leq M) 行目には i 回目の操作後に A に含まれない最小の非負整数を出力せよ。
入力例 1
3 3 -1 -1 -6
出力例 1
2 2 0
1 回目の操作では、数列 A は
(-1 + 1, -1 +2 ,-6+3) = (0,1,-3)
になります。 A に含まれない最小の非負整数は 2 です。
2 回目の操作では、数列 A は
(0 + 1, 1 +2 ,-3+3) = (1,3,0)
になります。 A に含まれない最小の非負整数は 2 です。
3 回目の操作では、数列 A は
(1 + 1, 3 +2 ,0+3) = (2,5,3)
になります。 A に含まれない最小の非負整数は 0 です。
入力例 2
5 6 -2 -2 -5 -7 -15
出力例 2
1 3 2 0 0 0
Score : 500 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N.
Perform the following operation M times:
- For each i\ (1\leq i \leq N), add i to A_i. Then, find the minimum non-negative integer not contained in A.
Constraints
- 1\leq N,M \leq 2\times 10^5
- -10^9\leq A_i\leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_N
Output
Print M lines.
The i-th (1\leq i \leq M) line should contain the minimum non-negative integer not contained in A after the i-th operation.
Sample Input 1
3 3 -1 -1 -6
Sample Output 1
2 2 0
The 1-st operation makes the sequence A
(-1 + 1, -1 +2 ,-6+3) = (0,1,-3).
The minimum non-negative integer not contained in A is 2.
The 2-nd operation makes the sequence A
(0 + 1, 1 +2 ,-3+3) = (1,3,0).
The minimum non-negative integer not contained in A is 2.
The 3-rd operation makes the sequence A
(1 + 1, 3 +2 ,0+3) = (2,5,3).
The minimum non-negative integer not contained in A is 0.
Sample Input 2
5 6 -2 -2 -5 -7 -15
Sample Output 2
1 3 2 0 0 0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
ABC250 は、 ABC1000 の開催を目指す高橋くんにとってちょうど 1/4 となる記念すべき回です。
そこで、高橋くんはピザを 1 枚買ってきて、そのピザのうちなるべく 1/4 に近い分量を食べて祝うことにしました。
高橋くんが買ってきたピザは凸 N 角形 (N \ge 4) の平らな形をしており、このピザを xy 平面上に置いた際、 i 番目の頂点の座標は (X_i,Y_i) でした。
高橋くんは、このピザを以下のように切って食べることにしました。
- まず、高橋くんはピザの頂点のうち隣接しない 2 頂点を選び、それらを通る直線に沿ってナイフを入れ、ピザを 2 つに切り分ける。
- その後、 2 つのピースのうち好きなものをどちらか 1 つ選んで食べる。
高橋くんが買ってきたピザの面積の 1/4 を a 、高橋くんが食べるピースの面積を b とした時、 8 \times |a-b| としてありえる最小値を求めてください。なお、この値は常に整数となることが示せます。
制約
- 入力は全て整数
- 4 \le N \le 10^5
- |X_i|, |Y_i| \le 4 \times 10^8
- 入力される頂点は反時計回りに凸 N 角形をなす。
入力
入力は以下の形式で標準入力から与えられる。
N X_1 Y_1 X_2 Y_2 \dots X_N Y_N
出力
答えを整数として出力せよ。
入力例 1
5 3 0 2 3 -1 3 -3 1 -1 -1
出力例 1
1
3 番目の頂点と 5 番目の頂点を通る直線に沿ってピザを切り分け、 4 番目の頂点を含む側のピースを食べたとします。
このとき、a=\frac{33}{2} \times \frac{1}{4} = \frac{33}{8} 、 b=4 、 8 \times |a-b|=1 であり、これがありえる最小値です。
入力例 2
4 400000000 400000000 -400000000 400000000 -400000000 -400000000 400000000 -400000000
出力例 2
1280000000000000000
入力例 3
6 -816 222 -801 -757 -165 -411 733 131 835 711 -374 979
出力例 3
157889
Score : 500 points
Problem Statement
ABC 250 is a commemorable quarter milestone for Takahashi, who aims to hold ABC 1000, so he is going to celebrate this contest by eating as close to 1/4 of a pizza he bought as possible.
The pizza that Takahashi bought has a planar shape of convex N-gon. When the pizza is placed on an xy-plane, the i-th vertex has coordinates (X_i, Y_i).
Takahashi has decided to cut and eat the pizza as follows.
- First, Takahashi chooses two non-adjacent vertices from the vertices of the pizza and makes a cut with a knife along the line passing through those two points, dividing the pizza into two pieces.
- Then, he chooses one of the pieces at his choice and eats it.
Let a be the quarter (=1/4) of the area of the pizza that Takahashi bought, and b be the area of the piece of pizza that Takahashi eats. Find the minimum possible value of 8 \times |a-b|. We can prove that this value is always an integer.
Constraints
- All values in input are integers.
- 4 \le N \le 10^5
- |X_i|, |Y_i| \le 4 \times 10^8
- The given points are the vertices of a convex N-gon in the counterclockwise order.
Input
Input is given from Standard Input in the following format:
N X_1 Y_1 X_2 Y_2 \dots X_N Y_N
Output
Print the answer as an integer.
Sample Input 1
5 3 0 2 3 -1 3 -3 1 -1 -1
Sample Output 1
1
Suppose that he makes a cut along the line passing through the 3-rd and the 5-th vertex and eats the piece containing the 4-th vertex.
Then, a=\frac{33}{2} \times \frac{1}{4} = \frac{33}{8}, b=4, and 8 \times |a-b|=1, which is minimum possible.
Sample Input 2
4 400000000 400000000 -400000000 400000000 -400000000 -400000000 400000000 -400000000
Sample Output 2
1280000000000000000
Sample Input 3
6 -816 222 -801 -757 -165 -411 733 131 835 711 -374 979
Sample Output 3
157889