実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
英小文字からなる文字列 S が与えられます。S の空でない部分文字列は何種類ありますか?
ただし、部分文字列とは連続する部分列のことを指します。例えば、xxx は yxxxy の部分文字列ですが、xxyxx の部分文字列ではありません。
制約
- S は英小文字からなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
yay
出力例 1
5
S の空でない部分文字列は以下の 5 種類です。
ayayyayay
入力例 2
aababc
出力例 2
17
入力例 3
abracadabra
出力例 3
54
Score: 200 points
Problem Statement
You are given a string S consisting of lowercase English letters. How many different non-empty substrings does S have?
A substring is a contiguous subsequence. For example, xxx is a substring of yxxxy but not of xxyxx.
Constraints
- S is a string of length between 1 and 100, inclusive, consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
yay
Sample Output 1
5
S has the following five different non-empty substrings:
ayayyayay
Sample Input 2
aababc
Sample Output 2
17
Sample Input 3
abracadabra
Sample Output 3
54
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
座標平面上に N 枚の長方形のシートが張られています。
各シートが覆う長方形領域の各辺はそれぞれ x 軸または y 軸と平行であり、
具体的には、i 枚目のシートはちょうど A_i \leq x\leq B_i かつ C_i \leq y\leq D_i をみたす領域全体を覆っています。
1 枚以上のシートによって覆われている領域 の面積を S とすると、
S は制約の条件下で整数となる事が証明できます。
S を整数の形で出力してください。
制約
- 2\leq N\leq 100
- 0\leq A_i<B_i\leq 100
- 0\leq C_i<D_i\leq 100
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 C_1 D_1 A_2 B_2 C_2 D_2 \vdots A_N B_N C_N D_N
出力
1 枚以上のシートによって覆われている領域の面積 S を整数で出力せよ。
入力例 1
3 0 5 1 3 1 4 0 5 2 5 2 4
出力例 1
20
3 枚のシートによって覆われている領域は次のようになります。
ここで、赤色・黄色・青色はそれぞれ 1 枚目・ 2 枚目・ 3 枚目のシートによって覆われている領域を表しています。

よって、1 枚以上のシートによって覆われている領域の面積は S=20 となります。
入力例 2
2 0 100 0 100 0 100 0 100
出力例 2
10000
異なるシートが同じ領域を覆っている事があることに注意してください。
入力例 3
3 0 1 0 1 0 3 0 5 5 10 0 10
出力例 3
65
Score : 200 points
Problem Statement
There are N rectangular sheets spread out on a coordinate plane.
Each side of the rectangular region covered by each sheet is parallel to the x- or y-axis.
Specifically, the i-th sheet covers exactly the region satisfying A_i \leq x\leq B_i and C_i \leq y\leq D_i.
Let S be the area of the region covered by one or more sheets. It can be proved that S is an integer under the constraints.
Print S as an integer.
Constraints
- 2\leq N\leq 100
- 0\leq A_i<B_i\leq 100
- 0\leq C_i<D_i\leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 B_1 C_1 D_1 A_2 B_2 C_2 D_2 \vdots A_N B_N C_N D_N
Output
Print the area S of the region covered by one or more sheets as an integer.
Sample Input 1
3 0 5 1 3 1 4 0 5 2 5 2 4
Sample Output 1
20
The three sheets cover the following regions.
Here, red, yellow, and blue represent the regions covered by the first, second, and third sheets, respectively.

Therefore, the area of the region covered by one or more sheets is S=20.
Sample Input 2
2 0 100 0 100 0 100 0 100
Sample Output 2
10000
Note that different sheets may cover the same region.
Sample Input 3
3 0 1 0 1 0 3 0 5 5 10 0 10
Sample Output 3
65
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
(1, \dots, N) の順列 P = (P_1, \dots, P_N) が与えられます。ただし、(P_1, \dots, P_N) \neq (1, \dots, N) です。
(1 \dots, N) の順列を全て辞書順で小さい順に並べたとき、P が K 番目であるとします。辞書順で小さい方から K-1 番目の順列を求めてください。
順列とは?
(1, \dots, N) の順列とは、(1, \dots, N) を並べ替えて得られる数列のことをいいます。
辞書順とは?
長さ N の数列 A = (A_1, \dots, A_N), B = (B_1, \dots, B_N) に対し、A が B より辞書順で真に小さいとは、ある整数 1 \leq i \leq N が存在して、下記の 2 つがともに成り立つことをいいます。
- (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1})
- A_i < B_i
制約
- 2 \leq N \leq 100
- 1 \leq P_i \leq N \, (1 \leq i \leq N)
- P_i \neq P_j \, (i \neq j)
- (P_1, \dots, P_N) \neq (1, \dots, N)
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N P_1 \ldots P_N
出力
求める順列を Q = (Q_1, \dots, Q_N) として、Q_1, \dots, Q_N をこの順に空白区切りで一行に出力せよ。
入力例 1
3 3 1 2
出力例 1
2 3 1
(1, 2, 3) の順列を辞書順で小さい順に並べると次のようになります。
- (1, 2, 3)
- (1, 3, 2)
- (2, 1, 3)
- (2, 3, 1)
- (3, 1, 2)
- (3, 2, 1)
よって P = (3, 1, 2) は小さい方から 5 番目であり、求める順列、すなわち小さい方から 5 - 1 = 4 番目の順列は (2, 3, 1) です。
入力例 2
10 9 8 6 5 10 3 1 2 4 7
出力例 2
9 8 6 5 10 2 7 4 3 1
Score : 300 points
Problem Statement
You are given a permutation P = (P_1, \dots, P_N) of (1, \dots, N), where (P_1, \dots, P_N) \neq (1, \dots, N).
Assume that P is the K-th lexicographically smallest among all permutations of (1 \dots, N). Find the (K-1)-th lexicographically smallest permutation.
What are permutations?
A permutation of (1, \dots, N) is an arrangement of (1, \dots, N) into a sequence.
What is lexicographical order?
For sequences of length N, A = (A_1, \dots, A_N) and B = (B_1, \dots, B_N), A is said to be strictly lexicographically smaller than B if and only if there is an integer 1 \leq i \leq N that satisfies both of the following.
- (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1}).
- A_i < B_i.
Constraints
- 2 \leq N \leq 100
- 1 \leq P_i \leq N \, (1 \leq i \leq N)
- P_i \neq P_j \, (i \neq j)
- (P_1, \dots, P_N) \neq (1, \dots, N)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N P_1 \ldots P_N
Output
Let Q = (Q_1, \dots, Q_N) be the sought permutation. Print Q_1, \dots, Q_N in a single line in this order, separated by spaces.
Sample Input 1
3 3 1 2
Sample Output 1
2 3 1
Here are the permutations of (1, 2, 3) in ascending lexicographical order.
- (1, 2, 3)
- (1, 3, 2)
- (2, 1, 3)
- (2, 3, 1)
- (3, 1, 2)
- (3, 2, 1)
Therefore, P = (3, 1, 2) is the fifth smallest, so the sought permutation, which is the fourth smallest (5 - 1 = 4), is (2, 3, 1).
Sample Input 2
10 9 8 6 5 10 3 1 2 4 7
Sample Output 2
9 8 6 5 10 2 7 4 3 1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
H_1 行 W_1 列の行列 A と、H_2 行 W_2 列の行列 B が与えられます。
- 1 \leq i \leq H_1 かつ 1 \leq j \leq W_1 を満たす整数の組 (i, j) について、行列 A の i 行目 j 列目の要素は A_{i, j} です。
- 1 \leq i \leq H_2 かつ 1 \leq j \leq W_2 を満たす整数の組 (i, j) について、行列 B の i 行目 j 列目の要素は B_{i, j} です。
行列 A に対して、下記の 2 つの操作のうちどちらかを行うことを、好きなだけ( 0 回でも良い)繰り返すことができます。
- A の行を任意に 1 つ選んで削除する。
- A の列を任意に 1 つ選んで削除する。
行列 A を行列 B に一致させることができるかどうかを判定して下さい。
制約
- 1 \leq H_2 \leq H_1 \leq 10
- 1 \leq W_2 \leq W_1 \leq 10
- 1 \leq A_{i, j} \leq 10^9
- 1 \leq B_{i, j} \leq 10^9
- 入力中の値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H_1 W_1
A_{1, 1} A_{1, 2} \ldots A_{1, W_1}
A_{2, 1} A_{2, 2} \ldots A_{2, W_1}
\vdots
A_{H_1, 1} A_{H_1, 2} \ldots A_{H_1, W_1}
H_2 W_2
B_{1, 1} B_{1, 2} \ldots B_{1, W_2}
B_{2, 1} B_{2, 2} \ldots B_{2, W_2}
\vdots
B_{H_2, 1} B_{H_2, 2} \ldots B_{H_2, W_2}
出力
行列 A を行列 B に一致させることができる場合は Yes を、
一致させることができない場合は No を出力せよ。
ジャッジは英小文字と英大文字を厳密に区別することに注意せよ。
入力例 1
4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 3 6 8 9 16 18 19
出力例 1
Yes
初期状態の行列 A から 2 列目を削除すると、行列 A は
1 3 4 5 6 8 9 10 11 13 14 15 16 18 19 20
となります。そこからさらに 3 行目を削除すると、行列 A は
1 3 4 5 6 8 9 10 16 18 19 20
となります。そこからさらに 1 行目を削除すると、行列 A は
6 8 9 10 16 18 19 20
となります。そこからさらに 4 列目を削除すると、行列 A は
6 8 9 16 18 19
となります。これは行列 B と一致します。
操作の繰り返しによって行列 A を行列 B に一致させることができるので Yes を出力します。
入力例 2
3 3 1 1 1 1 1 1 1 1 1 1 1 2
出力例 2
No
どのように操作を行っても、 行列 A を行列 B に一致させることはできません。
よって、No を出力します。
Score : 300 points
Problem Statement
You are given a matrix A with H_1 rows and W_1 columns, and a matrix B with H_2 rows and W_2 columns.
- For all integer pairs (i, j) such that 1 \leq i \leq H_1 and 1 \leq j \leq W_1, the element at the i-th row and j-th column of matrix A is A_{i, j}.
- For all integer pairs (i, j) such that 1 \leq i \leq H_2 and 1 \leq j \leq W_2, the element at the i-th row and j-th column of matrix B is B_{i, j}.
You may perform the following operations on the matrix A any number of (possibly 0) times in any order:
- Choose an arbitrary row of A and remove it.
- Choose an arbitrary column of A and remove it.
Determine if it is possible to make the matrix A equal the matrix B.
Constraints
- 1 \leq H_2 \leq H_1 \leq 10
- 1 \leq W_2 \leq W_1 \leq 10
- 1 \leq A_{i, j} \leq 10^9
- 1 \leq B_{i, j} \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
H_1 W_1
A_{1, 1} A_{1, 2} \ldots A_{1, W_1}
A_{2, 1} A_{2, 2} \ldots A_{2, W_1}
\vdots
A_{H_1, 1} A_{H_1, 2} \ldots A_{H_1, W_1}
H_2 W_2
B_{1, 1} B_{1, 2} \ldots B_{1, W_2}
B_{2, 1} B_{2, 2} \ldots B_{2, W_2}
\vdots
B_{H_2, 1} B_{H_2, 2} \ldots B_{H_2, W_2}
Output
Print Yes if it is possible to make the matrix A equal the matrix B;
print No otherwise.
Note that the judge is case-sensitive.
Sample Input 1
4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 3 6 8 9 16 18 19
Sample Output 1
Yes
Removing the 2-nd column from the initial A results in:
1 3 4 5 6 8 9 10 11 13 14 15 16 18 19 20
Then, removing the 3-rd row from A results in:
1 3 4 5 6 8 9 10 16 18 19 20
Then, removing the 1-st row from A results in:
6 8 9 10 16 18 19 20
Then, removing the 4-th column from A results in:
6 8 9 16 18 19
Now the matrix equals the matrix B.
Thus, we can make the matrix A equal the matrix B by repeating the operations, so Yes should be printed.
Sample Input 2
3 3 1 1 1 1 1 1 1 1 1 1 1 2
Sample Output 2
No
Regardless of how we perform the operations, we cannot make the matrix A equal the matrix B,
so No should be printed.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
N+2 個のマスが横一列に並んでいます。左から i 番目のマスをマス i と表します。
マス 1 からマス N には石が 1 個ずつ置かれています。
各 1\leq i \leq N について、S_i が W のときマス i に置かれている石の色は白であり、S_i が B のときマス i に置かれている石の色は黒です。
マス N+1,N+2 には何も置かれていません。
あなたは以下の操作を好きな回数(0 回でもよい)行うことができます。
- 石が 2 個並んでいる箇所を選び、その 2 個の石を順序を保って空きマスに移す。
より正確には次の通り。1 以上 N+1 以下の整数 x であって、マス x,x+1 の両方に石が置かれているものを選ぶ。石の置かれていないマスを k,k+1 とする。マス x,x+1 にある石をそれぞれマス k,k+1 に移動する。
以下の状態にすることが可能か判定し、可能なら操作回数の最小値を求めてください。
- マス 1 からマス N には石が 1 個ずつ置かれており、各 1\leq i \leq N について、T_i が
Wのときマス i に置かれている石の色は白、T_i がBのときマス i に置かれている石の色は黒である。
制約
- 2 \leq N \leq 14
- N は整数である
- S,T は
BおよびWのみからなる長さ N の文字列である
入力
入力は以下の形式で標準入力から与えられる。
N S T
出力
目的の状態にすることが可能なら操作回数の最小値を出力せよ。不可能ならかわりに -1 を出力せよ。
入力例 1
6 BWBWBW WWWBBB
出力例 1
4
石が置かれていないマスを . と表します。以下のようにして 4 回の操作で目的の状態にすることができ、これが最小回数です。
BWBWBW..BW..BWBWBWWBB..W..WBBBWWWWWBBB..
入力例 2
6 BBBBBB WWWWWW
出力例 2
-1
入力例 3
14 BBBWBWWWBBWWBW WBWWBBWWWBWBBB
出力例 3
7
Score : 425 points
Problem Statement
There are N+2 cells arranged in a row. Let cell i denote the i-th cell from the left.
There is one stone placed in each of the cells from cell 1 to cell N.
For each 1 \leq i \leq N, the stone in cell i is white if S_i is W, and black if S_i is B.
Cells N+1 and N+2 are empty.
You can perform the following operation any number of times (possibly zero):
- Choose a pair of adjacent cells that both contain stones, and move these two stones to the empty two cells while preserving their order.
More precisely, choose an integer x such that 1 \leq x \leq N+1 and both cells x and x+1 contain stones. Let k and k+1 be the empty two cells. Move the stones from cells x and x+1 to cells k and k+1, respectively.
Determine if it is possible to achieve the following state, and if so, find the minimum number of operations required:
- Each of the cells from cell 1 to cell N contains one stone, and for each 1 \leq i \leq N, the stone in cell i is white if T_i is
W, and black if T_i isB.
Constraints
- 2 \leq N \leq 14
- N is an integer.
- Each of S and T is a string of length N consisting of
BandW.
Input
The input is given from Standard Input in the following format:
N S T
Output
If it is possible to achieve the desired state, print the minimum number of operations required. If it is impossible, print -1.
Sample Input 1
6 BWBWBW WWWBBB
Sample Output 1
4
Using . to represent an empty cell, the desired state can be achieved in four operations as follows, which is the minimum:
BWBWBW..BW..BWBWBWWBB..W..WBBBWWWWWBBB..
Sample Input 2
6 BBBBBB WWWWWW
Sample Output 2
-1
Sample Input 3
14 BBBWBWWWBBWWBW WBWWBBWWWBWBBB
Sample Output 3
7