Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 266 点
問題文
高橋君は、古い宝の地図を手に入れました。
地図には N 行 M 列のグリッド状の区画が描かれており、いくつかのマスには宝が埋まっている印が付いています。
高橋君は、宝が埋まっているすべてのマスを含むような長方形の領域を選び、その中を調査しようとしています。ここで長方形の領域とは、ある連続する行の範囲(上から r_1 行目から r_2 行目まで、r_1 \leq r_2)と、ある連続する列の範囲(左から c_1 列目から c_2 列目まで、c_1 \leq c_2)によって定まる、その両方の範囲に含まれるすべてのマスからなる領域のことです。この領域に含まれるマスの数は (r_2 - r_1 + 1) \times (c_2 - c_1 + 1) 個です。r_1 = r_2 かつ c_1 = c_2 の場合(マス 1 つだけからなる領域)も長方形の領域に含まれます。
調査の手間を最小限にするため、高橋君は、宝が埋まっているすべてのマスを含む長方形の領域のうち、含まれるマスの数が最小となるものを求めたいです。
条件を満たす長方形領域に含まれるマスの数の最小値を求めてください。
制約
- 1 \leq N \leq 1000
- 1 \leq M \leq 1000
- N, M は整数
- S_i(1 \leq i \leq N)は
.と#からなる長さ M の文字列であり、地図の上から i 行目の状態を表す #は全体で少なくとも 1 つ存在する
入力
N M S_1 S_2 \vdots S_N
- 1 行目には、地図の行数を表す整数 N と列数を表す整数 M が、スペース区切りで与えられる。
- 続く N 行にわたって、地図の各行の状態を表す長さ M の文字列 S_i が与えられる。
- S_i の j 文字目が
#のとき、上から i 行目・左から j 列目のマスに宝が埋まっていることを表す。 - S_i の j 文字目が
.のとき、そのマスには宝が埋まっていないことを表す。
出力
宝があるすべてのマスを含む長方形領域に含まれるマスの数の最小値を 1 行で出力せよ。
入力例 1
3 4 .... .##. .#..
出力例 1
4
入力例 2
5 6 ...... .#.... ...... ....#. ......
出力例 2
12
入力例 3
8 10 .......... ..#....... .......... .......... .......#.. .......... ..#....... ..........
出力例 3
36
Score : 266 pts
Problem Statement
Takahashi has obtained an old treasure map.
The map depicts a grid of N rows and M columns, and some cells are marked with signs indicating that treasure is buried there.
Takahashi wants to select a rectangular region that contains all cells where treasure is buried, and investigate the area within it. Here, a rectangular region is defined by a contiguous range of rows (from the r_1-th row to the r_2-th row from the top, where r_1 \leq r_2) and a contiguous range of columns (from the c_1-th column to the c_2-th column from the left, where c_1 \leq c_2), consisting of all cells that fall within both ranges. The number of cells contained in this region is (r_2 - r_1 + 1) \times (c_2 - c_1 + 1). A region consisting of just a single cell (when r_1 = r_2 and c_1 = c_2) is also considered a rectangular region.
To minimize the effort of investigation, Takahashi wants to find, among all rectangular regions that contain every cell where treasure is buried, the one with the minimum number of cells.
Find the minimum number of cells contained in a rectangular region that satisfies the condition.
Constraints
- 1 \leq N \leq 1000
- 1 \leq M \leq 1000
- N, M are integers
- S_i (1 \leq i \leq N) is a string of length M consisting of
.and#, representing the state of the i-th row from the top of the map - There is at least one
#in total
Input
N M S_1 S_2 \vdots S_N
- The first line contains an integer N representing the number of rows and an integer M representing the number of columns of the map, separated by a space.
- Over the following N lines, a string S_i of length M representing the state of each row of the map is given.
- If the j-th character of S_i is
#, it indicates that treasure is buried in the cell at the i-th row from the top and the j-th column from the left. - If the j-th character of S_i is
., it indicates that no treasure is buried in that cell.
Output
Print on one line the minimum number of cells contained in a rectangular region that includes all cells where treasure is buried.
Sample Input 1
3 4 .... .##. .#..
Sample Output 1
4
Sample Input 2
5 6 ...... .#.... ...... ....#. ......
Sample Output 2
12
Sample Input 3
8 10 .......... ..#....... .......... .......... .......#.. .......... ..#....... ..........
Sample Output 3
36
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 333 点
問題文
高橋君は、一直線の花壇に設置するスプリンクラーの配置を計画しています。
花壇は数直線上に沿って伸びており、花壇上には N 個の植え込みが左から順に配置されています。i 番目の植え込みの座標は X_i で、そこには P_i 本の花が植えられています。すべての植え込みは正の座標にあります。
高橋君は座標 0 の位置に水源を持っており、ここから正の方向に向かって水を送るパイプを伸ばし、パイプの先端にスプリンクラーを 1 台設置します。スプリンクラーは設置地点から正の方向に距離 D 未満の範囲に水を撒きます。なお、パイプは水をスプリンクラーまで送る役割のみを持ち、パイプの通る区間には散水されません。
具体的には、スプリンクラーを座標 s(s は 0 以上の任意の実数)に設置した場合、座標が s 以上 s + D 未満の範囲にある植え込みの花すべてに水やりができます。すなわち、植え込みの座標 X_i が s \leq X_i < s + D を満たすとき、その植え込みの P_i 本の花は水やりできたとみなします。この条件を満たさない植え込みの花は水やりできません。
高橋君は、スプリンクラーの設置位置 s を最適に選ぶことで、水やりできない花の本数を最小化したいと考えています。スプリンクラーは必ず 1 台設置しなければなりません。
水やりできない花の本数の最小値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq D \leq 10^9
- 1 \leq X_1 < X_2 < \cdots < X_N \leq 10^9
- 1 \leq P_i \leq 10^9
- 入力はすべて整数
入力
N D X_1 P_1 X_2 P_2 : X_N P_N
- 1 行目には、植え込みの数を表す整数 N と、スプリンクラーの散水距離を表す整数 D が、スペース区切りで与えられる。
- 2 行目から N + 1 行目では、各植え込みの座標と花の本数が与えられる。
- 1 + i 行目では、i 番目の植え込みの座標を表す整数 X_i と、その植え込みに植えられている花の本数を表す整数 P_i が、スペース区切りで与えられる。
出力
水やりできない花の本数の最小値を 1 行で出力せよ。
入力例 1
5 4 1 3 3 5 5 2 7 4 10 6
出力例 1
10
入力例 2
4 3 2 10 5 20 6 5 9 15
出力例 2
25
入力例 3
12 10 1 4 4 8 9 3 13 20 18 7 21 15 27 6 31 12 36 5 42 18 48 9 55 11
出力例 3
76
入力例 4
25 50 3 12 17 40 28 7 44 25 61 33 79 18 95 50 123 9 146 21 170 60 199 14 225 32 260 45 288 11 319 27 347 36 380 8 415 55 451 19 490 24 530 42 575 13 621 31 670 48 725 16
出力例 4
591
入力例 5
1 1000000000 1000000000 1000000000
出力例 5
0
Score : 333 pts
Problem Statement
Takahashi is planning the placement of a sprinkler to be installed in a straight flower bed.
The flower bed stretches along a number line, and N shrubs are arranged on it from left to right. The i-th shrub is located at coordinate X_i and has P_i flowers planted in it. All shrubs are located at positive coordinates.
Takahashi has a water source at coordinate 0. From here, he will extend a pipe in the positive direction to supply water and install exactly one sprinkler at the end of the pipe. The sprinkler waters the area in the positive direction within a distance strictly less than D from its installation point. Note that the pipe only serves to deliver water to the sprinkler, and no watering occurs along the interval where the pipe passes.
Specifically, if the sprinkler is installed at coordinate s (where s is any non-negative real number), all flowers in the shrubs located at coordinates greater than or equal to s and less than s + D can be watered. That is, if the coordinate X_i of a shrub satisfies s \leq X_i < s + D, the P_i flowers in that shrub are considered watered. Flowers in shrubs that do not satisfy this condition cannot be watered.
Takahashi wants to minimize the number of flowers that cannot be watered by choosing the sprinkler installation position s optimally. Exactly one sprinkler must be installed.
Find the minimum number of flowers that cannot be watered.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq D \leq 10^9
- 1 \leq X_1 < X_2 < \cdots < X_N \leq 10^9
- 1 \leq P_i \leq 10^9
- All input values are integers.
Input
N D X_1 P_1 X_2 P_2 : X_N P_N
- The first line contains an integer N, representing the number of shrubs, and an integer D, representing the watering distance of the sprinkler, separated by a space.
- The next N lines (from the 2nd to the (N+1)-th line) provide the coordinates and the number of flowers for each shrub.
- The (1+i)-th line contains an integer X_i, representing the coordinate of the i-th shrub, and an integer P_i, representing the number of flowers in that shrub, separated by a space.
Output
Print the minimum number of flowers that cannot be watered in a single line.
Sample Input 1
5 4 1 3 3 5 5 2 7 4 10 6
Sample Output 1
10
Sample Input 2
4 3 2 10 5 20 6 5 9 15
Sample Output 2
25
Sample Input 3
12 10 1 4 4 8 9 3 13 20 18 7 21 15 27 6 31 12 36 5 42 18 48 9 55 11
Sample Output 3
76
Sample Input 4
25 50 3 12 17 40 28 7 44 25 61 33 79 18 95 50 123 9 146 21 170 60 199 14 225 32 260 45 288 11 319 27 347 36 380 8 415 55 451 19 490 24 530 42 575 13 621 31 670 48 725 16
Sample Output 4
591
Sample Input 5
1 1000000000 1000000000 1000000000
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は商店街で開催されている「お買い物マラソン」に参加しています。
商店街には N 軒のお店が左から右へ一列に並んでいます。各お店ではそれぞれ1種類の商品が売られており、左から i 番目のお店の商品の満足度は A_i 、価格は B_i です。
お買い物マラソンのルールは次の通りです。高橋君は商店街の中から 連続する区間 を1つ選び、その区間に含まれるすべてのお店で商品を1つずつ購入しなければなりません。具体的には、整数の組 (l, r)( 1 \leq l \leq r \leq N )を選び、左から l 番目から r 番目までのすべてのお店で買い物をします。
ただし、高橋君の所持金には限りがあり、選んだ区間のお店の価格の合計 B_l + B_{l+1} + \cdots + B_r が K 以下でなければなりません。
高橋君は、この条件を満たす区間の中で、満足度の合計 A_l + A_{l+1} + \cdots + A_r を最大化したいです。
なお、条件を満たす区間が1つも存在しない場合(すなわち、どの1軒のお店を選んでもその価格が K を超える場合)は、高橋君は何も購入できず、満足度の合計は 0 となります。
高橋君が得られる満足度の合計の最大値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^{14}
- 1 \leq A_i \leq 10^9
- 1 \leq B_i \leq 10^9
- 入力はすべて整数である。
入力
N K A_1 B_1 A_2 B_2 \vdots A_N B_N
- 1 行目には、お店の数 N と所持金の上限 K が、スペース区切りで与えられる。
- 続く N 行のうち i 行目( 1 \leq i \leq N )には、左から i 番目のお店の商品の満足度 A_i と価格 B_i が、スペース区切りで与えられる。
出力
高橋君が得られる満足度の合計の最大値を1行で出力してください。
入力例 1
5 10 3 2 5 3 2 4 8 1 1 5
出力例 1
18
入力例 2
3 2 5 3 8 4 3 7
出力例 2
0
入力例 3
10 15 4 3 7 2 1 5 9 1 3 4 6 2 2 7 8 3 5 1 10 6
出力例 3
26
入力例 4
20 50 12 3 8 7 15 2 6 9 20 4 3 8 11 1 7 5 14 6 9 3 5 10 18 2 4 7 16 4 2 6 13 3 10 8 1 5 19 2 7 4
出力例 4
108
入力例 5
1 1000000000000 1000000000 1
出力例 5
1000000000
Score : 366 pts
Problem Statement
Takahashi is participating in a "Shopping Marathon" held at a shopping street.
There are N shops lined up in a row from left to right in the shopping street. Each shop sells one type of product. The satisfaction of the product at the i-th shop from the left is A_i, and its price is B_i.
The rules of the Shopping Marathon are as follows. Takahashi must choose one contiguous interval from the shopping street and purchase exactly one product from every shop within that interval. Specifically, he chooses a pair of integers (l, r) (1 \leq l \leq r \leq N) and shops at every store from the l-th to the r-th from the left.
However, Takahashi's budget is limited, so the total price of the shops in the chosen interval B_l + B_{l+1} + \cdots + B_r must be at most K.
Takahashi wants to maximize the total satisfaction A_l + A_{l+1} + \cdots + A_r among all intervals satisfying this condition.
If no interval satisfies the condition (that is, if the price of every single shop exceeds K), Takahashi cannot purchase anything, and the total satisfaction is 0.
Find the maximum total satisfaction Takahashi can obtain.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 10^{14}
- 1 \leq A_i \leq 10^9
- 1 \leq B_i \leq 10^9
- All input values are integers.
Input
N K A_1 B_1 A_2 B_2 \vdots A_N B_N
- The first line contains the number of shops N and the budget limit K, separated by a space.
- The i-th of the following N lines (1 \leq i \leq N) contains the satisfaction A_i and price B_i of the product at the i-th shop from the left, separated by a space.
Output
Print the maximum total satisfaction Takahashi can obtain, in a single line.
Sample Input 1
5 10 3 2 5 3 2 4 8 1 1 5
Sample Output 1
18
Sample Input 2
3 2 5 3 8 4 3 7
Sample Output 2
0
Sample Input 3
10 15 4 3 7 2 1 5 9 1 3 4 6 2 2 7 8 3 5 1 10 6
Sample Output 3
26
Sample Input 4
20 50 12 3 8 7 15 2 6 9 20 4 3 8 11 1 7 5 14 6 9 3 5 10 18 2 4 7 16 4 2 6 13 3 10 8 1 5 19 2 7 4
Sample Output 4
108
Sample Input 5
1 1000000000000 1000000000 1
Sample Output 5
1000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は、庭に花壇を作ろうとしています。庭には N 個の区画が一列に並んでおり、左から順に区画 1, 2, \ldots, N と番号が付いています。各区画 i には「美しさ」 D_i と「費用」 C_i が設定されています。
高橋君は、これらの区画のうちいくつかを選んで花を植えます。各区画は選ぶか選ばないかのいずれかであり、同じ区画を複数回選ぶことはできません。1 つも選ばなくてもかまいません。ただし、以下のルールがあります:
- 花を植えるための総予算は W です。選んだ区画の費用の合計が W 以下でなければなりません。
- 隣接する区画に連続して花を植えると、統一感によるボーナスが発生します。具体的には、選んだ区画の番号の集合をいくつかの極大連続区間に分けて考えます。ここで極大連続区間とは、選んだ区画の番号の集合の部分集合であって、含まれる番号が連続しており、かつその両端を 1 だけ拡張した番号は選んだ区画の集合に含まれないような区間のことです。各極大連続区間について、その区間に含まれる区画の数を L とすると、
- L \geq 2 ならば、その区間から L \times B のボーナス美しさが得られます。
- L = 1 ならば、孤立した 1 区画のみでは統一感が生まれないため、ボーナス美しさは 0 です。
ここで B は与えられる非負整数の定数です。すべての極大連続区間から得られるボーナス美しさを合算したものが、全体のボーナス美しさとなります。区画を 1 つも選ばない場合、極大連続区間は存在せず、ボーナス美しさは 0 です。
- 例えば、区画 \{2, 3, 4, 7, 8\} を選んだ場合、極大連続区間は \{2, 3, 4\}(長さ 3)と \{7, 8\}(長さ 2)です。ボーナス美しさは 3 \times B + 2 \times B = 5B です。
- 例えば、区画 \{3, 5\} を選んだ場合、極大連続区間は \{3\}(長さ 1)と \{5\}(長さ 1)で、ボーナス美しさは 0 + 0 = 0 です。
高橋君は、選んだ区画の費用の合計が W 以下となるように区画を選んだとき、選んだ区画の美しさの合計 + ボーナス美しさ を最大化したいと考えています。区画を 1 つも選ばない場合、この値は 0 です。
この最大値を求めてください。
制約
- 1 \leq N \leq 2000
- 1 \leq W \leq 10000
- 0 \leq B \leq 10^6
- 1 \leq D_i \leq 10^6(1 \leq i \leq N)
- 1 \leq C_i \leq W(1 \leq i \leq N)
- 入力はすべて整数である。
入力
N W B D_1 C_1 D_2 C_2 \vdots D_N C_N
- 1 行目には、区画の数を表す整数 N 、総予算を表す整数 W 、連続ボーナスの係数を表す非負整数 B が、スペース区切りで与えられる。
- 2 行目から N + 1 行目では、各区画の美しさと費用が与えられる。
- 1 + i 行目では、区画 i の美しさ D_i と費用 C_i がスペース区切りで与えられる。
出力
選んだ区画の費用の合計が W 以下となるように区画を選んだときの、選んだ区画の美しさの合計とボーナス美しさの和の最大値を 1 行で出力せよ。
入力例 1
5 7 3 4 2 6 3 2 1 8 4 5 2
出力例 1
24
入力例 2
6 10 0 7 3 4 2 9 5 6 4 3 1 8 6
出力例 2
20
入力例 3
12 25 7 15 4 8 3 20 6 5 2 13 5 18 7 9 3 25 8 6 2 14 4 11 5 22 6
出力例 3
121
入力例 4
25 100 50 120 8 75 5 210 14 95 7 160 11 40 3 185 13 130 9 55 4 240 16 105 8 90 6 170 12 65 5 200 15 110 7 45 3 155 10 220 17 80 6 145 9 60 4 190 13 100 7 250 18
出力例 4
2215
入力例 5
1 1 1000000 1000000 1
出力例 5
1000000
Score : 400 pts
Problem Statement
Takahashi is planning to build flowerbeds in his garden. The garden consists of N plots arranged in a row, numbered 1, 2, \ldots, N from left to right. Each plot i has a "beauty" D_i and a "cost" C_i.
Takahashi will select some of these plots to plant flowers in. Each plot can either be chosen or not, and the same plot cannot be chosen multiple times. He may choose to select no plots at all. However, the following rules apply:
- The total budget for planting flowers is W. The sum of the costs of the chosen plots must be at most W.
- Planting flowers in adjacent plots generates a bonus due to unity. Specifically, consider partitioning the set of chosen plot numbers into maximal contiguous intervals. Here, a maximal contiguous interval is a subset of the set of chosen plot numbers whose elements are contiguous, and neither extending the left endpoint by subtracting 1 nor extending the right endpoint by adding 1 yields a plot number contained in the chosen set. For each maximal contiguous interval, let L be the number of plots contained in it:
- If L \geq 2, a bonus beauty of L \times B is obtained from that interval.
- If L = 1, an isolated plot alone does not create unity, so the bonus beauty is 0.
Here, B is a given non-negative integer constant. The total bonus beauty is the sum of the bonus beauty obtained from all maximal contiguous intervals. If no plots are chosen, there are no maximal contiguous intervals, and the bonus beauty is 0.
- For example, if plots \{2, 3, 4, 7, 8\} are chosen, the maximal contiguous intervals are \{2, 3, 4\} (length 3) and \{7, 8\} (length 2). The bonus beauty is 3 \times B + 2 \times B = 5B.
- For example, if plots \{3, 5\} are chosen, the maximal contiguous intervals are \{3\} (length 1) and \{5\} (length 1), and the bonus beauty is 0 + 0 = 0.
Takahashi wants to maximize (sum of beauty of chosen plots) + (total bonus beauty) such that the total cost of the chosen plots is at most W. If no plots are chosen, this value is 0.
Find this maximum value.
Constraints
- 1 \leq N \leq 2000
- 1 \leq W \leq 10000
- 0 \leq B \leq 10^6
- 1 \leq D_i \leq 10^6 (1 \leq i \leq N)
- 1 \leq C_i \leq W (1 \leq i \leq N)
- All input values are integers.
Input
N W B D_1 C_1 D_2 C_2 \vdots D_N C_N
- The first line contains an integer N representing the number of plots, an integer W representing the total budget, and a non-negative integer B representing the adjacency bonus coefficient, separated by spaces.
- The 2-nd through (N + 1)-th lines give the beauty and cost of each plot.
- The (1 + i)-th line contains two space-separated integers D_i and C_i, representing the beauty and cost of plot i.
Output
Print the maximum value of the sum of the beauty of the chosen plots and the total bonus beauty such that the total cost of the chosen plots is at most W in a single line.
Sample Input 1
5 7 3 4 2 6 3 2 1 8 4 5 2
Sample Output 1
24
Sample Input 2
6 10 0 7 3 4 2 9 5 6 4 3 1 8 6
Sample Output 2
20
Sample Input 3
12 25 7 15 4 8 3 20 6 5 2 13 5 18 7 9 3 25 8 6 2 14 4 11 5 22 6
Sample Output 3
121
Sample Input 4
25 100 50 120 8 75 5 210 14 95 7 160 11 40 3 185 13 130 9 55 4 240 16 105 8 90 6 170 12 65 5 200 15 110 7 45 3 155 10 220 17 80 6 145 9 60 4 190 13 100 7 250 18
Sample Output 4
2215
Sample Input 5
1 1 1000000 1000000 1
Sample Output 5
1000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 433 点
問題文
高橋君は、 N \times M のグリッド状に区切られた倉庫のフロアを管理しています。グリッドの各マスは、壁(#)または床(.)のいずれかです。
このフロアには K 台のロボット掃除機が配置されています。各ロボット掃除機は、毎ターン上下左右のいずれか 1 マスに移動します。ロボット掃除機は以下のルールに従って動作するようプログラムされています。
- 各ロボット掃除機には、移動の指示を表す長さ L_i の命令列 S_i が与えられている。命令列は
U(上)、D(下)、L(左)、R(右)の文字からなる。 - ターン t において、ロボット掃除機 i は命令列の ((t - 1) \bmod L_i) + 1 番目の文字が示す方向に 1 マス移動しようとする。
- 移動先がグリッドの範囲外、または壁である場合、その移動は無視され、ロボット掃除機はそのターンではその場にとどまる。
高橋君は、 T ターン後に各ロボット掃除機がどの位置にいるかを知りたいと考えています。
ロボット掃除機 i の初期位置 (R_i, C_i) (上から R_i 行目、左から C_i 列目)、命令列 S_i 、およびターン数 T が与えられるので、 T ターン後の各ロボット掃除機の位置を求めてください。
なお、複数のロボット掃除機が同じマスに存在することは許されており、ロボット掃除機同士は互いに影響を与えません。
制約
- 1 \leq N \leq 2000
- 1 \leq M \leq 2000
- N M \leq 10^6
- 1 \leq K \leq 10^5
- 1 \leq T \leq 10^{18}
- g_i は
.または#からなる長さ M の文字列である - 1 \leq R_i \leq N
- 1 \leq C_i \leq M
- ロボット掃除機 i の初期位置 (R_i, C_i) は床(
.)である - 1 \leq L_i \leq 1000
- \displaystyle\sum_{i=1}^{K} L_i \leq 2 \times 10^5
- S_i は
U,D,L,Rからなる長さ L_i の文字列である - 相異なる命令列全体の集合を \mathcal{S} とし、A = \displaystyle\sum_{S \in \mathcal{S}} |S| とする。このとき、N M (A + K) \leq 2 \times 10^7 を満たす
- 入力はすべて整数( N, M, K, T, R_i, C_i, L_i )および文字列( g_i, S_i )で与えられる
入力
N M K T g_1 g_2 : g_N R_1 C_1 L_1 S_1 R_2 C_2 L_2 S_2 : R_K C_K L_K S_K
- 1 行目には、グリッドの行数 N 、列数 M 、ロボット掃除機の台数 K 、シミュレーションするターン数 T が、スペース区切りで与えられる。
- 2 行目から N 行にわたって、グリッドの情報が与えられる。 g_i は M 文字の文字列で、
.が床、#が壁を表す。 - 続く K 行で、各ロボット掃除機の情報が与えられる。 1 + N + j 行目には、ロボット掃除機 j の初期位置の行 R_j 、列 C_j 、命令列の長さ L_j 、命令列 S_j がスペース区切りで与えられる。
出力
K 行出力せよ。 i 行目には、ロボット掃除機 i の T ターン後の位置を、行番号と列番号をスペース区切りで出力せよ。
入力例 1
3 4 2 5 .... .#.. .... 1 1 4 RRRD 3 4 3 ULL
出力例 1
2 4 1 2
入力例 2
2 3 3 4 .#. ... 1 1 1 R 1 3 2 DL 2 2 4 LRUD
出力例 2
1 1 2 1 2 2
入力例 3
8 10 6 123456789012 .......... .####..#.. .#....#... .#.#..#.#. ...#...... ##...###.. ..#....... .....#.... 1 1 4 RDDR 3 4 6 URDLDR 5 10 3 LLL 8 1 8 UURRDDLL 6 3 5 DRRUL 2 7 4 DDLU
出力例 3
8 10 6 5 5 5 7 2 6 5 7 1
入力例 4
20 30 15 987654321987654321 .............................. .####.....######......####.... .#..#.....#....#......#..#.... .#..#.....#....#......#..#.... .####.....######......####.... .............................. .....#####..........#####..... .....#..................#..... .....#..##########..#...#..... .....#..................#..... .....#####..........#####..... .............................. ..########..####..########.... ..#......#..#..#..#......#.... ..########..####..########.... .............................. ####....####....####....####.. .............................. ..#..#..#..#..#..#..#..#..#... .............................. 1 1 4 RRRD 1 30 5 LLLDD 6 15 10 DDDRRRUUUL 8 10 12 RRRRDDLLLLUU 9 7 9 DRDRULULL 12 1 16 RRRRRRDDLLLLLLUU 13 1 7 RDRDRDL 14 15 6 UDLRRL 16 30 20 LLLLUUUURRRRDDDDLLLL 18 5 3 DDD 20 30 4 ULDR 7 1 8 RRRRRRRR 11 30 10 LLLLLDDUUU 19 1 5 RDRUR 4 29 11 LLUUURRRDDD
出力例 4
10 24 20 1 9 23 8 11 9 7 10 2 20 29 14 14 14 17 20 5 19 30 7 5 1 1 19 30 4 30
入力例 5
1 1 1 1000000000000000000 . 1 1 1 U
出力例 5
1 1
Score : 433 pts
Problem Statement
Takahashi manages the floor of a warehouse divided into an N \times M grid. Each cell of the grid is either a wall (#) or a floor (.).
There are K robot vacuum cleaners placed on this floor. Each robot vacuum cleaner moves one cell up, down, left, or right every turn. The robot vacuum cleaners are programmed to operate according to the following rules:
- Each robot vacuum cleaner i is given a command sequence S_i of length L_i representing movement instructions. The command sequence consists of the characters
U(up),D(down),L(left), andR(right). - At turn t, robot vacuum cleaner i attempts to move one cell in the direction indicated by the ((t - 1) \bmod L_i) + 1-th character of its command sequence.
- If the destination is outside the grid or is a wall, the movement is ignored, and the robot vacuum cleaner stays in place for that turn.
Takahashi wants to know the position of each robot vacuum cleaner after T turns.
Given the initial position (R_i, C_i) (row R_i from the top, column C_i from the left) of robot vacuum cleaner i, its command sequence S_i, and the number of turns T, determine the position of each robot vacuum cleaner after T turns.
Note that multiple robot vacuum cleaners are allowed to occupy the same cell, and robot vacuum cleaners do not affect each other.
Constraints
- 1 \leq N \leq 2000
- 1 \leq M \leq 2000
- N M \leq 10^6
- 1 \leq K \leq 10^5
- 1 \leq T \leq 10^{18}
- g_i is a string of length M consisting of
.or# - 1 \leq R_i \leq N
- 1 \leq C_i \leq M
- The initial position (R_i, C_i) of robot vacuum cleaner i is a floor cell (
.) - 1 \leq L_i \leq 1000
- \displaystyle\sum_{i=1}^{K} L_i \leq 2 \times 10^5
- S_i is a string of length L_i consisting of
U,D,L,R - Let \mathcal{S} be the set of all distinct command sequences, and let A = \displaystyle\sum_{S \in \mathcal{S}} |S|. Then N M (A + K) \leq 2 \times 10^7 holds.
- All inputs are integers (N, M, K, T, R_i, C_i, L_i) and strings (g_i, S_i)
Input
N M K T g_1 g_2 : g_N R_1 C_1 L_1 S_1 R_2 C_2 L_2 S_2 : R_K C_K L_K S_K
- The first line contains the number of rows N, the number of columns M, the number of robot vacuum cleaners K, and the number of turns to simulate T, separated by spaces.
- The next N lines give the grid information. g_i is a string of M characters, where
.represents a floor and#represents a wall. - The following K lines give the information for each robot vacuum cleaner. The (1 + N + j)-th line contains the initial row R_j, column C_j, command sequence length L_j, and command sequence S_j of robot vacuum cleaner j, separated by spaces.
Output
Output K lines. The i-th line should contain the row number and column number of robot vacuum cleaner i after T turns, separated by a space.
Sample Input 1
3 4 2 5 .... .#.. .... 1 1 4 RRRD 3 4 3 ULL
Sample Output 1
2 4 1 2
Sample Input 2
2 3 3 4 .#. ... 1 1 1 R 1 3 2 DL 2 2 4 LRUD
Sample Output 2
1 1 2 1 2 2
Sample Input 3
8 10 6 123456789012 .......... .####..#.. .#....#... .#.#..#.#. ...#...... ##...###.. ..#....... .....#.... 1 1 4 RDDR 3 4 6 URDLDR 5 10 3 LLL 8 1 8 UURRDDLL 6 3 5 DRRUL 2 7 4 DDLU
Sample Output 3
8 10 6 5 5 5 7 2 6 5 7 1
Sample Input 4
20 30 15 987654321987654321 .............................. .####.....######......####.... .#..#.....#....#......#..#.... .#..#.....#....#......#..#.... .####.....######......####.... .............................. .....#####..........#####..... .....#..................#..... .....#..##########..#...#..... .....#..................#..... .....#####..........#####..... .............................. ..########..####..########.... ..#......#..#..#..#......#.... ..########..####..########.... .............................. ####....####....####....####.. .............................. ..#..#..#..#..#..#..#..#..#... .............................. 1 1 4 RRRD 1 30 5 LLLDD 6 15 10 DDDRRRUUUL 8 10 12 RRRRDDLLLLUU 9 7 9 DRDRULULL 12 1 16 RRRRRRDDLLLLLLUU 13 1 7 RDRDRDL 14 15 6 UDLRRL 16 30 20 LLLLUUUURRRRDDDDLLLL 18 5 3 DDD 20 30 4 ULDR 7 1 8 RRRRRRRR 11 30 10 LLLLLDDUUU 19 1 5 RDRUR 4 29 11 LLUUURRRDDD
Sample Output 4
10 24 20 1 9 23 8 11 9 7 10 2 20 29 14 14 14 17 20 5 19 30 7 5 1 1 19 30 4 30
Sample Input 5
1 1 1 1000000000000000000 . 1 1 1 U
Sample Output 5
1 1