Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
H 行 W 列のグリッドがあります。
グリッドの各マスは陸か海のどちらかであり、
その情報は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H で与えられます。
上から i 行目、左から j 列目のマスを (i, j) で表すと、
S_i の j 文字目が . のときマス (i, j) は陸であり、# のときマス (i, j) は海です。
ここで、グリッドの外周のマス(すなわち、i = 1 、i = H 、j = 1 、j = W のうち少なくとも 1 個以上を満たすマス (i, j) )については、すべて海であることが制約として保証されます。
高橋君が乗った宇宙船が、グリッド上のあるマスに不時着してしまいました。
その後、高橋君は L 、R 、U 、D のみからなる長さ N の文字列 T で表される手順に沿って、グリッド上を N 回移動しました。
i = 1, 2, \ldots, N について、T の i 文字目は i 回目の移動の内容を下記の通り表します。
Lのとき、左に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i, j-1) である。Rのとき、右に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i, j+1) である。Uのとき、上に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i-1, j) である。Dのとき、下に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i+1, j) である。
高橋君の移動経路上のマス(不時着したマスおよび現在いるマスを含む)はいずれも海でないことがわかっています。 高橋君が現在いるマスとしてあり得るものの個数を出力してください。
制約
- H, W, N は整数
- 3 \leq H, W \leq 500
- 1 \leq N \leq 500
- T は
L、R、U、Dのみからなる長さ N の文字列 - S_i は
.と#のみからなる長さ W の文字列 - 高橋君が現在いるマスとしてあり得るものが少なくとも 1 個存在する。
- グリッドの外周のマスはすべて海である。
入力
入力は以下の形式で標準入力から与えられる。
H W N T S_1 S_2 \vdots S_H
出力
答えを出力せよ。
入力例 1
6 7 5 LULDR ####### #...#.# ##...## #.#...# #...#.# #######
出力例 1
2
下記の 2 つの場合がありえるため、高橋君が現在いるマスとしてあり得るものは (3, 4) と (4, 5) の 2 個です。
- マス (3, 5) に不時着し、(3, 5) \rightarrow (3, 4) \rightarrow (2, 4) \rightarrow (2, 3) \rightarrow (3, 3) \rightarrow (3, 4) と移動した場合
- マス (4, 6) に不時着し、(4, 6) \rightarrow (4, 5) \rightarrow (3, 5) \rightarrow (3, 4) \rightarrow (4, 4) \rightarrow (4, 5) と移動した場合
入力例 2
13 16 9 ULURDLURD ################ ##..##.#..####.# ###.#..#.....#.# #..##..#####.### #...#..#......## ###.##.#..#....# ##.#####....##.# ###.###.#.#.#..# ######.....##..# #...#.#.######.# ##..###..#..#.## #...#.#.#...#..# ################
出力例 2
6
Score: 250 points
Problem Statement
There is a grid with H rows and W columns.
Each cell of the grid is land or sea, which is represented by H strings S_1, S_2, \ldots, S_H of length W. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left, and (i, j) is land if the j-th character of S_i is ., and (i, j) is sea if the character is #.
The constraints guarantee that all cells on the perimeter of the grid (that is, the cells (i, j) that satisfy at least one of i = 1, i = H, j = 1, j = W) are sea.
Takahashi's spaceship has crash-landed on a cell in the grid. Afterward, he moved N times on the grid following the instructions represented by a string T of length N consisting of L, R, U, and D. For i = 1, 2, \ldots, N, the i-th character of T describes the i-th move as follows:
Lindicates a move of one cell to the left. That is, if he is at (i, j) before the move, he will be at (i, j-1) after the move.Rindicates a move of one cell to the right. That is, if he is at (i, j) before the move, he will be at (i, j+1) after the move.Uindicates a move of one cell up. That is, if he is at (i, j) before the move, he will be at (i-1, j) after the move.Dindicates a move of one cell down. That is, if he is at (i, j) before the move, he will be at (i+1, j) after the move.
It is known that all cells along his path (including the cell where he crash-landed and the cell he is currently on) are not sea. Print the number of cells that could be his current position.
Constraints
- H, W, and N are integers.
- 3 \leq H, W \leq 500
- 1 \leq N \leq 500
- T is a string of length N consisting of
L,R,U, andD. - S_i is a string of length W consisting of
.and#. - There is at least one cell that could be Takahashi's current position.
- All cells on the perimeter of the grid are sea.
Input
The input is given from Standard Input in the following format:
H W N T S_1 S_2 \vdots S_H
Output
Print the answer.
Sample Input 1
6 7 5 LULDR ####### #...#.# ##...## #.#...# #...#.# #######
Sample Output 1
2
The following two cases are possible, so there are two cells that could be Takahashi's current position: (3, 4) and (4, 5).
- He crash-landed on cell (3, 5) and moved (3, 5) \rightarrow (3, 4) \rightarrow (2, 4) \rightarrow (2, 3) \rightarrow (3, 3) \rightarrow (3, 4).
- He crash-landed on cell (4, 6) and moved (4, 6) \rightarrow (4, 5) \rightarrow (3, 5) \rightarrow (3, 4) \rightarrow (4, 4) \rightarrow (4, 5).
Sample Input 2
13 16 9 ULURDLURD ################ ##..##.#..####.# ###.#..#.....#.# #..##..#####.### #...#..#......## ###.##.#..#....# ##.#####....##.# ###.###.#.#.#..# ######.....##..# #...#.#.######.# ##..###..#..#.## #...#.#.#...#..# ################
Sample Output 2
6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 頂点の木 T があり、 i (1\leq i\leq N-1) 番目の辺は頂点 U_i と頂点 V_i を結んでいます。
T 上の相異なる 2 頂点 X,Y が与えられるので、 頂点 X から頂点 Y への単純パス上の頂点(端点含む)を順に列挙してください。
ただし、木上の任意の相異なる 2 頂点 a,b について、a から b への単純パスがただ一つ存在することが証明できます。
単純パスとは?
グラフ G 上の頂点 X,Y に対して、頂点列 v_1,v_2, \ldots, v_k であって、 v_1=X, v_k=Y かつ、1\leq i\leq k-1 に対して v_i と v_{i+1} が辺で結ばれているようなものを頂点 X から頂点 Y への パス と呼びます。 さらに、v_1,v_2, \ldots, v_k がすべて異なるようなものを頂点 X から頂点 Y への 単純パス と呼びます。制約
- 1\leq N\leq 2\times 10^5
- 1\leq X,Y\leq N
- X\neq Y
- 1\leq U_i,V_i\leq N
- 入力はすべて整数
- 与えられるグラフは木
入力
入力は以下の形式で標準入力から与えられる。
N X Y
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
出力
頂点 X から頂点 Y への単純パス上の頂点番号を順に空白区切りで出力せよ。
入力例 1
5 2 5 1 2 1 3 3 4 3 5
出力例 1
2 1 3 5
木 T は以下のような形であり、頂点 2 から頂点 5への単純パスは
頂点 2 \to 頂点 1 \to 頂点 3 \to 頂点 5 となります。
よって、2,1,3,5 をこの順に空白区切りで出力します。

入力例 2
6 1 2 3 1 2 5 1 2 4 1 2 6
出力例 2
1 2
木 T は以下のような形です。

Score : 300 points
Problem Statement
There is a tree T with N vertices. The i-th edge (1\leq i\leq N-1) connects vertex U_i and vertex V_i.
You are given two different vertices X and Y in T. List all vertices along the simple path from vertex X to vertex Y in order, including endpoints.
It can be proved that, for any two different vertices a and b in a tree, there is a unique simple path from a to b.
What is a simple path?
For vertices X and Y in a graph G, a path from vertex X to vertex Y is a sequence of vertices v_1,v_2, \ldots, v_k such that v_1=X, v_k=Y, and v_i and v_{i+1} are connected by an edge for each 1\leq i\leq k-1. Additionally, if all of v_1,v_2, \ldots, v_k are distinct, the path is said to be a simple path from vertex X to vertex Y.Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq X,Y\leq N
- X\neq Y
- 1\leq U_i,V_i\leq N
- All values in the input are integers.
- The given graph is a tree.
Input
The input is given from Standard Input in the following format:
N X Y
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
Output
Print the indices of all vertices along the simple path from vertex X to vertex Y in order, with spaces in between.
Sample Input 1
5 2 5 1 2 1 3 3 4 3 5
Sample Output 1
2 1 3 5
The tree T is shown below. The simple path from vertex 2 to vertex 5 is 2 \to 1 \to 3 \to 5.
Thus, 2,1,3,5 should be printed in this order, with spaces in between.

Sample Input 2
6 1 2 3 1 2 5 1 2 4 1 2 6
Sample Output 2
1 2
The tree T is shown below.

Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
非負整数列 A=(a_1,a_2,\ldots,a_N) が与えられます。
A の(添え字が相異なる) K 個の項の和として考えられる非負整数の集合を S とします。
S に含まれる D の倍数の最大値を求めてください。ただし、S に D の倍数が含まれない場合、代わりに -1 と出力してください。
制約
- 1 \leq K \leq N \leq 100
- 1 \leq D \leq 100
- 0 \leq a_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K D a_1 \ldots a_N
出力
答えを出力せよ。
入力例 1
4 2 2 1 2 3 4
出力例 1
6
A から 2 個の項を選ぶ方法を列挙すると
- a_1 と a_2 を選ぶ。選ばれた項の和は 1+2=3 となる。
- a_1 と a_3 を選ぶ。選ばれた項の和は 1+3=4 となる。
- a_1 と a_4 を選ぶ。選ばれた項の和は 1+4=5 となる。
- a_2 と a_3 を選ぶ。選ばれた項の和は 2+3=5 となる。
- a_2 と a_4 を選ぶ。選ばれた項の和は 2+4=6 となる。
- a_3 と a_4 を選ぶ。選ばれた項の和は 3+4=7 となる。
となり、S=\{3,4,5,6,7\} となります。S に含まれる 2 の倍数のうち最大のものは 6 なので、6 と出力します。
入力例 2
3 1 2 1 3 5
出力例 2
-1
この例では S=\{1,3,5\} です。S に含まれる非負整数はいずれも 2 の倍数でないため、-1 と出力します。
Score : 400 points
Problem Statement
You are given a sequence of non-negative integers A=(a_1,a_2,\ldots,a_N).
Let S be the set of non-negative integers that can be the sum of K terms in A (with distinct indices).
Find the greatest multiple of D in S. If there is no multiple of D in S, print -1 instead.
Constraints
- 1 \leq K \leq N \leq 100
- 1 \leq D \leq 100
- 0 \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 K D a_1 \ldots a_N
Output
Print the answer.
Sample Input 1
4 2 2 1 2 3 4
Sample Output 1
6
Here are all the ways to choose two terms in A.
- Choose a_1 and a_2, whose sum is 1+2=3.
- Choose a_1 and a_3, whose sum is 1+3=4.
- Choose a_1 and a_4, whose sum is 1+4=5.
- Choose a_2 and a_3, whose sum is 2+3=5.
- Choose a_2 and a_4, whose sum is 2+4=6.
- Choose a_3 and a_4, whose sum is 3+4=7.
Thus, we have S=\{3,4,5,6,7\}. The greatest multiple of 2 in S is 6, so you should print 6.
Sample Input 2
3 1 2 1 3 5
Sample Output 2
-1
In this example, we have S=\{1,3,5\}. Nothing in S is a multiple of 2, so you should print -1.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
長さ N の整数からなる数列 A=(A_1,\ldots,A_N) であって、以下の条件を全て満たすものは何通りありますか?
-
1\le A_i \le M (1 \le i \le N)
-
|A_i - A_{i+1}| \geq K (1 \le i \le N - 1)
ただし、答えは非常に大きくなることがあるので、答えを 998244353 で割った余りを求めてください。
制約
- 2 \leq N \leq 1000
- 1 \leq M \leq 5000
- 0 \leq K \leq M-1
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M K
出力
答えを 998244353 で割った余りを出力せよ。
入力例 1
2 3 1
出力例 1
6
条件を満たす数列は以下の 6 つです。
- (1,2)
- (1,3)
- (2,1)
- (2,3)
- (3,1)
- (3,2)
入力例 2
3 3 2
出力例 2
2
条件を満たす数列は以下の 2 つです。
- (1,3,1)
- (3,1,3)
入力例 3
100 1000 500
出力例 3
657064711
答えを 998244353 で割った余りを出力してください。
Score : 500 points
Problem Statement
How many integer sequences A=(A_1,\ldots,A_N) of length N satisfy all the conditions below?
-
1\le A_i \le M (1 \le i \le N)
-
|A_i - A_{i+1}| \geq K (1 \le i \le N - 1)
Since the count can be enormous, find it modulo 998244353.
Constraints
- 2 \leq N \leq 1000
- 1 \leq M \leq 5000
- 0 \leq K \leq M-1
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M K
Output
Print the count modulo 998244353.
Sample Input 1
2 3 1
Sample Output 1
6
The following 6 sequences satisfy the conditions.
- (1,2)
- (1,3)
- (2,1)
- (2,3)
- (3,1)
- (3,2)
Sample Input 2
3 3 2
Sample Output 2
2
The following 2 sequences satisfy the conditions.
- (1,3,1)
- (3,1,3)
Sample Input 3
100 1000 500
Sample Output 3
657064711
Print the count modulo 998244353.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
数直線上にりんごの木が並んでおり、 N 個のりんごが木から落ちてきます。
具体的には 1\leq i\leq N について、時刻 T_i に座標 X_i にりんごが落ちてきます。
高橋君は耐久性が D , 長さ W のカゴを持っており、一度だけ 次の行動を取ることができます。
正整数 S, L を選ぶ。高橋君は時刻 S-0.5 に L-0.5\leq x\leq L+W-0.5 の範囲を覆うようにカゴを設置し、時刻 S+D-0.5 に回収する。 高橋君はカゴを設置してから回収するまでの間に、カゴが設置されていた範囲に落ちてきたりんごをすべて得ることができる。
高橋君は一度設置したカゴを動かしたり、回収したカゴを再度設置することはできません。
高橋君が得られるりんごの数の最大値を求めてください。
制約
- 1 \leq N\leq 2\times 10^5
- 1 \leq D\leq 2\times 10^5
- 1 \leq W\leq 2\times 10^5
- 1 \leq T_i\leq 2\times 10^5
- 1 \leq X_i\leq 2\times 10^5
- (T_i,X_i) はすべて異なる。
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N D W T_1 X_1 T_2 X_2 \vdots T_N X_N
出力
高橋君が得られるりんごの数の最大値を出力せよ。
入力例 1
8 4 3 1 1 3 4 6 4 5 2 4 2 4 3 5 5 7 3
出力例 1
5
高橋君は S=3, L=2 を選ぶと、時刻 2.5 から 6.5 までカゴを 1.5\leq x\leq 4.5 の範囲に設置します。このとき、
- 時刻 T_2=3 に 座標 X_2=4 に落ちてくるりんご
- 時刻 T_3=6 に 座標 X_3=4 に落ちてくるりんご
- 時刻 T_4=5 に 座標 X_4=2 に落ちてくるりんご
- 時刻 T_5=4 に 座標 X_5=2 に落ちてくるりんご
- 時刻 T_6=4 に 座標 X_6=3 に落ちてくるりんご
の 5 個のりんごを得ることができます。
どのように行動しても 6 個以上のりんごを得ることはできないため、5 を出力します。
Score : 550 points
Problem Statement
There are apple trees lined up on a number line, and N apples fall from the trees.
Specifically, for each 1\leq i\leq N, an apple falls at coordinate X_i at time T_i.
Takahashi has a basket with durability D and length W, and he can take the following action exactly once.
Choose positive integers S and L. He sets up the basket to cover the range L-0.5\leq x\leq L+W-0.5 at time S-0.5, and retrieves it at time S+D-0.5. He gets all the apples that fell into the range covered by the basket between the time it was set up and the time it was retrieved.
He cannot move the basket once it has been set up, nor can he set it up again once it has been retrieved.
Find the maximum number of apples that he can get.
Constraints
- 1 \leq N\leq 2\times 10^5
- 1 \leq D\leq 2\times 10^5
- 1 \leq W\leq 2\times 10^5
- 1 \leq T_i\leq 2\times 10^5
- 1 \leq X_i\leq 2\times 10^5
- All pairs (T_i,X_i) are different.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N D W T_1 X_1 T_2 X_2 \vdots T_N X_N
Output
Print the maximum number of apples that Takahashi can get.
Sample Input 1
8 4 3 1 1 3 4 6 4 5 2 4 2 4 3 5 5 7 3
Sample Output 1
5
If Takahashi chooses S=3 and L=2, he will set up the basket to cover the range 1.5\leq x\leq 4.5 from time 2.5 to 6.5. Then, he gets the following five apples:
- The apple that falls at coordinate X_2=4 at time T_2=3
- The apple that falls at coordinate X_3=4 at time T_3=6
- The apple that falls at coordinate X_4=2 at time T_4=5
- The apple that falls at coordinate X_5=2 at time T_5=4
- The apple that falls at coordinate X_6=3 at time T_6=4
There is no way to get six or more apples, so print 5.