Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
H 行 W 列のグリッドがあります。グリッドの上から i 番目、左から j 番目のマスをマス (i, j) と表記します。
マス (i, j) は C_{i, j} が .
のとき空きマスであり、#
のとき空きマスではありません。
高橋君は現在マス (S_i, S_j) におり、i = 1, 2, \ldots, |X| の順に以下のルールに従って行動します。
- X の i 文字目が
L
のとき、高橋君が現在いるマスの 1 つ左のマスが存在し、そのマスが空きマスならば 1 つ左のマスに移動する。そうでないならば、現在いるマスに留まる。 - X の i 文字目が
R
のとき、高橋君が現在いるマスの 1 つ右のマスが存在し、そのマスが空きマスならば 1 つ右のマスに移動する。そうでないならば、現在いるマスに留まる。 - X の i 文字目が
U
のとき、高橋君が現在いるマスの 1 つ上のマスが存在し、そのマスが空きマスならば 1 つ上のマスに移動する。そうでないならば、現在いるマスに留まる。 - X の i 文字目が
D
のとき、高橋君が現在いるマスの 1 つ下のマスが存在し、そのマスが空きマスならば 1 つ下のマスに移動する。そうでないならば、現在いるマスに留まる。
一連の行動を終えた後高橋君がどのマスにいるか出力してください。
制約
- 1 \leq H, W \leq 50
- 1 \leq S_i \leq H
- 1 \leq S_j \leq W
- H, W, S_i, S_j は整数
- C_{i, j} は
.
または#
- C_{S_i, S_j} =
.
- X は
L
,R
,U
,D
からなる長さ 1 以上 50 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
H W S_i S_j C_{1, 1}C_{1, 2}\ldotsC_{1, W} C_{2, 1}C_{2, 2}\ldotsC_{2, W} \vdots C_{H, 1}C_{H, 2}\ldotsC_{H, W} X
出力
高橋君が一連の行動を終えた後にいるマスをマス (x, y) として、x と y をこの順に空白区切りで出力せよ。
入力例 1
2 3 2 1 .#. ... ULDRU
出力例 1
2 2
高橋君ははじめマス (2, 1) にいます。高橋君の一連の行動は以下のようになります。
- X の 1 文字目は
U
であり、マス (2, 1) の 1 つ上のマスは存在し、そのマスは空きマスであるため 1 つ上のマスであるマス (1, 1) に移動する。 - X の 2 文字目は
L
であり、マス (1, 1) の 1 つ左のマスは存在しないためマス (1, 1) に留まる。 - X の 3 文字目は
D
であり、マス (1, 1) の 1 つ下のマスは存在し、そのマスは空きマスであるため 1 つ下のマスであるマス (2, 1) に移動する。 - X の 4 文字目は
R
であり、マス (2, 1) の 1 つ右のマスは存在し、そのマスは空きマスであるため 1 つ右のマスであるマス (2, 2) に移動する。 - X の 5 文字目は
U
であり、マス (2, 2) の 1 つ上のマスは存在するが、そのマスは空きマスではないためマス (2, 2) に留まる。
したがって一連の行動を終えた後に高橋君がいるマスはマス (2, 2) です。
入力例 2
4 4 4 2 .... .#.. ...# .... DUUUURULRD
出力例 2
2 4
入力例 3
6 6 1 1 .##### ###### ###### ###### ###### ###### RURLDLULLRULRDL
出力例 3
1 1
Score : 200 points
Problem Statement
There is a grid with H rows and W columns. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left.
Cell (i, j) is empty if C_{i, j} is .
, and not empty if C_{i, j} is #
.
Takahashi is currently at cell (S_i, S_j), and he will act according to the following rules for i = 1, 2, \ldots, |X| in order.
- If the i-th character of X is
L
, and the cell to the left of his current cell exists and is empty, he moves to the cell to the left. Otherwise, he stays in the current cell. - If the i-th character of X is
R
, and the cell to the right of his current cell exists and is empty, he moves to the cell to the right. Otherwise, he stays in the current cell. - If the i-th character of X is
U
, and the cell above his current cell exists and is empty, he moves to the cell above. Otherwise, he stays in the current cell. - If the i-th character of X is
D
, and the cell below his current cell exists and is empty, he moves to the cell below. Otherwise, he stays in the current cell.
Print the cell where he is after completing the series of actions.
Constraints
- 1 \leq H, W \leq 50
- 1 \leq S_i \leq H
- 1 \leq S_j \leq W
- H, W, S_i, S_j are integers.
- C_{i, j} is
.
or#
. - C_{S_i, S_j} =
.
- X is a string of length between 1 and 50, inclusive, consisting of
L
,R
,U
,D
.
Input
The input is given from Standard Input in the following format:
H W S_i S_j C_{1, 1}C_{1, 2}\ldotsC_{1, W} C_{2, 1}C_{2, 2}\ldotsC_{2, W} \vdots C_{H, 1}C_{H, 2}\ldotsC_{H, W} X
Output
Let (x, y) be the cell where Takahashi is after completing the series of actions. Print x and y, separated by a space.
Sample Input 1
2 3 2 1 .#. ... ULDRU
Sample Output 1
2 2
Takahashi starts at cell (2, 1). His series of actions are as follows:
- The 1st character of X is
U
, and the cell above (2, 1) exists and is an empty cell, so he moves to the cell above, which is (1, 1). - The 2nd character of X is
L
, and the cell to the left of (1, 1) does not exist, so he stays at (1, 1). - The 3rd character of X is
D
, and the cell below (1, 1) exists and is an empty cell, so he moves to the cell below, which is (2, 1). - The 4th character of X is
R
, and the cell to the right of (2, 1) exists and is an empty cell, so he moves to the cell to the right, which is (2, 2). - The 5th character of X is
U
, and the cell above (2, 2) exists but is not an empty cell, so he stays at (2, 2).
Therefore, after completing the series of actions, he is at cell (2, 2).
Sample Input 2
4 4 4 2 .... .#.. ...# .... DUUUURULRD
Sample Output 2
2 4
Sample Input 3
6 6 1 1 .##### ###### ###### ###### ###### ###### RURLDLULLRULRDL
Sample Output 3
1 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
以下のような N 個の整数列 A_0,\ldots,A_{N-1} を求めてください。
- 各 i (0\leq i \leq N-1) について、A_i の長さは i+1 である。
-
各 i,j (0\leq i \leq N-1, 0 \leq j \leq i) について、A_i の j+1 番目の値 a_{i,j} は次のように定められる。
- j=0 または j=i の時、a_{i,j}=1
- それ以外の時、a_{i,j} = a_{i-1,j-1} + a_{i-1,j}
制約
- 1 \leq N \leq 30
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
N 行出力せよ。 i 行目には A_{i-1} の値を順に空白区切りで出力せよ。
入力例 1
3
出力例 1
1 1 1 1 2 1
入力例 2
10
出力例 2
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1
Score : 200 points
Problem Statement
Find the N integer sequences A_0,\ldots,A_{N-1} defined as follows.
- For each i (0\leq i \leq N-1), the length of A_i is i+1.
- For each i and j (0\leq i \leq N-1, 0 \leq j \leq i), the (j+1)-th term of A_i, denoted by a_{i,j}, is defined as follows.
- a_{i,j}=1, if j=0 or j=i.
- a_{i,j} = a_{i-1,j-1} + a_{i-1,j}, otherwise.
Constraints
- 1 \leq N \leq 30
- N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print N lines. The i-th line should contain the terms of A_{i-1} separated by spaces.
Sample Input 1
3
Sample Output 1
1 1 1 1 2 1
Sample Input 2
10
Sample Output 2
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
整数列 A = (A_1, A_2, \ldots, A_{|A|}) に対し、A が チルダ型 とは以下の 4 つの条件をすべて満たすことであると定義します。
- A の長さ |A| は 4 以上である
- A_1 < A_2 である
- A_{i - 1} < A_i > A_{i + 1} を満たす 2 \leq i < |A| なる整数 i はちょうど 1 個である
- A_{i - 1} > A_i < A_{i + 1} を満たす 2 \leq i < |A| なる整数 i はちょうど 1 個である
数列 (1, 2, \ldots, N) を並べ替えて得られる数列 P = (P_1, P_2, \ldots, P_N) が与えられます。P の連続部分列であってチルダ型である数列の個数を求めてください。
制約
- 4 \leq N \leq 3 \times 10^5
- P = (P_1, P_2, \ldots, P_N) は (1, 2, \ldots, N) を並べ替えて得られる数列
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N P_1 P_2 \ldots P_N
出力
答えを出力せよ。
入力例 1
6 1 3 6 4 2 5
出力例 1
2
数列 (1, 3, 6, 4, 2, 5) の連続部分列のうちチルダ型であるものは (3, 6, 4, 2, 5), (1, 3, 6, 4, 2, 5) の 2 つのみです。
入力例 2
6 1 2 3 4 5 6
出力例 2
0
入力例 3
12 11 3 8 9 5 2 10 4 1 6 12 7
出力例 3
4
Score : 350 points
Problem Statement
For an integer sequence A = (A_1, A_2, \ldots, A_{|A|}), we say that A is tilde-shaped if it satisfies all of the following four conditions:
- The length |A| is at least 4.
- A_1 < A_2.
- There exists exactly one integer i with 2 \leq i < |A| such that A_{i-1} < A_i > A_{i+1}.
- There exists exactly one integer i with 2 \leq i < |A| such that A_{i-1} > A_i < A_{i+1}.
You are given a permutation P = (P_1, P_2, \ldots, P_N) of (1, 2, \ldots, N). Find the number of (contiguous) subarrays of P that are tilde-shaped.
Constraints
- 4 \leq N \leq 3 \times 10^5
- P = (P_1, P_2, \ldots, P_N) is a permutation of (1, 2, \ldots, N).
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N P_1 P_2 \ldots P_N
Output
Output the answer.
Sample Input 1
6 1 3 6 4 2 5
Sample Output 1
2
Among the subarrays of (1, 3, 6, 4, 2, 5), exactly two are tilde-shaped: (3, 6, 4, 2, 5) and (1, 3, 6, 4, 2, 5).
Sample Input 2
6 1 2 3 4 5 6
Sample Output 2
0
Sample Input 3
12 11 3 8 9 5 2 10 4 1 6 12 7
Sample Output 3
4
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
1 から N の番号がついた N 個の箱と 1 から N の番号がついた N 個の荷物があります。荷物 i (1 \leq i \leq N) は箱 A_i の中にあり、重さは W_i です。
あなたは荷物を一つ選び、他の箱の中に移動させる操作を 0 回以上繰り返し行うことができます。1 回の操作で移動させる荷物の重さが w であるとき、w のコストがかかります。
全ての箱に荷物が 1 つずつ入っている状態にするためにかかるコストの総和の最小値を求めてください。
制約
- 1 \leq N \leq 10^{5}
- 1 \leq A_i \leq N (1 \leq i \leq N)
- 1 \leq W_i \leq 10^{4} (1 \leq i \leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N W_1 W_2 \ldots W_N
出力
全ての箱に荷物が 1 つずつ入っている状態にするためにかかるコストの総和の最小値を出力せよ。
入力例 1
5 2 2 3 3 5 33 40 2 12 16
出力例 1
35
以下の 2 回の荷物の移動で、すべての箱に荷物が 1 つずつ入っている状態にすることができます。
- 荷物 1 を箱 2 から箱 1 に移す。このとき、コストは 33 である。
- 荷物 3 を箱 3 から箱 4 に移す。このとき、コストは 2 である。
この 2 回の荷物の移動は合計 35 のコストかかります。 35 未満のコストですべての箱に荷物が 1 つずつ入っている状態にすることはできないため、 35 を出力します。
入力例 2
12 3 6 7 4 12 4 8 11 11 1 8 11 3925 9785 9752 3587 4013 1117 3937 7045 6437 6208 3391 6309
出力例 2
17254
Score : 250 points
Problem Statement
There are N boxes numbered 1 to N and N items numbered 1 to N. Item i (1 \leq i \leq N) is in box A_i and has a weight of W_i.
You can repeatedly perform the operation of choosing an item and moving it to another box zero or more times. If the weight of the item being moved is w, the cost of the operation is w.
Find the minimum total cost required to make each box contain exactly one item.
Constraints
- 1 \leq N \leq 10^{5}
- 1 \leq A_i \leq N (1 \leq i \leq N)
- 1 \leq W_i \leq 10^{4} (1 \leq i \leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N W_1 W_2 \ldots W_N
Output
Print the minimum total cost required to make each box contain exactly one item.
Sample Input 1
5 2 2 3 3 5 33 40 2 12 16
Sample Output 1
35
With the following two moves, you can make each box contain exactly one item:
- Move item 1 from box 2 to box 1. The cost is 33.
- Move item 3 from box 3 to box 4. The cost is 2.
The total cost of these two moves is 35. It is impossible to make each box contain exactly one item with a cost less than 35, so print 35.
Sample Input 2
12 3 6 7 4 12 4 8 11 11 1 8 11 3925 9785 9752 3587 4013 1117 3937 7045 6437 6208 3391 6309
Sample Output 2
17254
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
文字列 S があり、初め S= 1
です。
以下の形式のクエリが Q 個与えられるので順に処理してください。
1 x
: S の末尾に数字 x を追加する2
: S の先頭の数字を削除する3
: S を十進数表記の数とみなした値を 998244353 で割った余りを出力する
制約
- 1 \leq Q \leq 6 \times 10^5
- 1 番目の形式のクエリについて、x \in \{1,2,3,4,5,6,7,8,9\}
- 2 番目の形式のクエリは S が 2 文字以上の時にのみ与えられる
- 3 番目の形式のクエリが 1 個以上存在する
入力
入力は以下の形式で標準入力から与えられる。
Q \mathrm{query}_1 \vdots \mathrm{query}_Q
ただし \mathrm{query}_i は i 番目のクエリを表し、以下のいずれかの形式である。
1 x
2
3
出力
3 番目の形式のクエリの個数を q として、q 行出力せよ。i (1 \leq i \leq q) 行目には i 番目の 3 番目の形式のクエリに対する出力をせよ。
入力例 1
3 3 1 2 3
出力例 1
1 12
1 番目のクエリにおいて、S は 1
なので ( 1 を 998244353 で割った余りに等しい) 1 を出力します。
2 番目のクエリにおいて、S は 12
になります。
3 番目のクエリにおいて、S は 12
なので ( 12 を 998244353 で割った余りに等しい) 12 を出力します。
入力例 2
3 1 5 2 3
出力例 2
5
入力例 3
11 1 9 1 9 1 8 1 2 1 4 1 4 1 3 1 5 1 3 2 3
出力例 3
0
出力されるべき値は 998244353 で割った余りであることに注意してください。
Score : 400 points
Problem Statement
We have a string S. Initially, S= 1
.
Process Q queries in the following formats in order.
1 x
: Append a digit x at the end of S.2
: Delete the digit at the beginning of S.3
: Print the number represented by S in decimal, modulo 998244353.
Constraints
- 1 \leq Q \leq 6 \times 10^5
- For each query in the first format, x \in \{1,2,3,4,5,6,7,8,9\}.
- A query in the second format is given only if S has a length of 2 or greater.
- There is at least one query in the third format.
Input
The input is given from Standard Input in the following format:
Q \mathrm{query}_1 \vdots \mathrm{query}_Q
Here, \mathrm{query}_i denotes the i-th query, which is in one of the following formats:
1 x
2
3
Output
Print q lines, where q is the number of queries in the third format. The i-th line (1 \leq i \leq q) should correspond to the i-th query in the third format.
Sample Input 1
3 3 1 2 3
Sample Output 1
1 12
In the first query, S is 1
, so you should print 1 modulo 998244353, that is, 1.
In the second query, S becomes 12
.
In the third query, S is 12
, so you should print 12 modulo 998244353, that is, 12.
Sample Input 2
3 1 5 2 3
Sample Output 2
5
Sample Input 3
11 1 9 1 9 1 8 1 2 1 4 1 4 1 3 1 5 1 3 2 3
Sample Output 3
0
Be sure to print numbers modulo 998244353.