実行時間制限: 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
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
長さ N の整数列 A = (A_1, A_2, \dots, A_N) が与えられます。
A の連続するとは限らない、長さが 2 以上である部分列 A'=(A'_1,A'_2,\ldots,A'_k) のうち以下の条件を満たすものの個数を求めてください。
- A'_1 \leq A'_k
なお、この値は非常に大きくなることがあるため、998244353 で割ったあまりを出力してください。
ただし、2 つの部分列は、列として同じであっても、取り出す添字が異なる場合は区別されます。
制約
- 2 \leq N \leq 3 \times 10^5
- 1 \leq A_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
A の連続するとは限らない、長さが 2 以上である部分列のうち問題文中の条件を満たすものの個数を、998244353 で割ったあまりを出力せよ。
入力例 1
3 1 2 1
出力例 1
3
A=(1,2,1) の連続するとは限らない、長さが 2 以上である部分列は (1,2), (1,1), (2,1), (1,2,1) の 4 通りあります。
そのうち問題文中の条件を満たすものは、(1,2), (1,1), (1,2,1) の 3 通りです。
入力例 2
3 1 2 2
出力例 2
4
列として同じであっても、取り出す添字が異なる場合 2 つの部分列は区別されることに注意してください。
この入出力例において、問題文中の条件を満たすような部分列は (1,2), (1,2), (2,2), (1,2,2) の 4 通りです。
入力例 3
3 3 2 1
出力例 3
0
問題文中の条件を満たすような部分列が存在しない場合もあります。
入力例 4
10 198495780 28463047 859606611 212983738 946249513 789612890 782044670 700201033 367981604 302538501
出力例 4
830
Score : 500 points
Problem Statement
Given is a sequence of N integers: A = (A_1, A_2, \dots, A_N).
Find the number of (not necessarily contiguous) subsequences A'=(A'_1,A'_2,\ldots,A'_k) of length at least 2 that satisfy the following condition:
- A'_1 \leq A'_k.
Since the count can be enormous, print it modulo 998244353.
Here, two subsequences are distinguished when they originate from different sets of indices, even if they are the same as sequences.
Constraints
- 2 \leq N \leq 3 \times 10^5
- 1 \leq A_i \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
Print the number of (not necessarily contiguous) subsequences A'=(A'_1,A'_2,\ldots,A'_k) of length at least 2 that satisfy the condition in Problem Statement.
Sample Input 1
3 1 2 1
Sample Output 1
3
A=(1,2,1) has four (not necessarily contiguous) subsequences of length at least 2: (1,2), (1,1), (2,1), (1,2,1).
Three of them, (1,2), (1,1), (1,2,1), satisfy the condition in Problem Statement.
Sample Input 2
3 1 2 2
Sample Output 2
4
Note that two subsequences are distinguished when they originate from different sets of indices, even if they are the same as sequences.
In this Sample, there are four subsequences, (1,2), (1,2), (2,2), (1,2,2), that satisfy the condition.
Sample Input 3
3 3 2 1
Sample Output 3
0
There may be no subsequence that satisfies the condition.
Sample Input 4
10 198495780 28463047 859606611 212983738 946249513 789612890 782044670 700201033 367981604 302538501
Sample Output 4
830
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
箱を用意します。最初、箱は空です。
この箱に対して、以下の 2 種類の操作を合計 Q 個、入力で与えられた順に施します。
+ x
タイプ 1 : 箱の中に整数 x の書かれたボールを 1 つ追加する。
- x
タイプ 2 : 箱の中にある、整数 x の書かれたボールを 1 つ取り除く。
但し、取り除く前の時点で箱の中に整数 x が書かれたボールが含まれることが保証されます。
各操作後の箱に関して、以下の問題を解いてください。
箱からボールをいくつか取り出して、ボールに書かれた整数の総和を K とする方法の総数を 998244353 で割った余りを求めてください。
但し、箱の中に入っている全てのボールは区別可能です。
制約
- 入力は全て整数
- 1 \le Q \le 5000
- 1 \le K \le 5000
- タイプ 1 の操作に関して、 1 \le x \le 5000
- 全ての操作は問題文中の条件を満たす。
入力
入力は以下の形式で標準入力から与えられる。
但し、 \rm{Query}_{i} は i 個目の操作を表す。
Q K
\rm{Query}_{1}
\rm{Query}_{2}
\vdots
\rm{Query}_{Q}
出力
全体で Q 行出力せよ。
そのうち i 行目には、 i 個目までの操作を終えた時点での答えを出力せよ。
入力例 1
15 10 + 5 + 2 + 3 - 2 + 5 + 10 - 3 + 1 + 3 + 3 - 5 + 1 + 7 + 4 - 3
出力例 1
0 0 1 0 1 2 2 2 2 2 1 3 5 8 5
この入力には、操作が 15 個含まれます。
最後の操作の後、箱の中に入ったボールは (5,10,1,3,1,7,4) となります。
総和を 10 にする方法は以下の 5 通りです。
- 5+1+3+1 ( 1,3,4,5 番目のボールを取り出す )
- 5+1+4 ( 1,3,7 番目のボールを取り出す )
- 5+1+4 ( 1,5,7 番目のボールを取り出す )
- 10 ( 2 番目のボールを取り出す )
- 3+7 ( 4,6 番目のボールを取り出す )
Score : 525 points
Problem Statement
We have a box, which is initially empty.
Let us perform a total of Q operations of the following two types in the order they are given in the input.
+ x
Type 1: Put into the box a ball with the integer x written on it.
- x
Type 2: Remove from the box a ball with the integer x written on it.
It is guaranteed that the box contains a ball with the integer x written on it before the operation.
For the box after each operation, solve the following problem.
Find the number, modulo 998244353, to pick up some number of balls from the box so that the integers written on them sum to K.
All balls in the box are distinguishable.
Constraints
- All input values are integers.
- 1 \le Q \le 5000
- 1 \le K \le 5000
- For each type-1 operation, 1 \le x \le 5000.
- All the operations satisfy the condition in the problem statement.
Input
The input is given from Standard Input in the following format, where \mathrm{Query}_i represents the i-th operation:
Q K
\rm{Query}_{1}
\rm{Query}_{2}
\vdots
\rm{Query}_{Q}
Output
Print Q lines.
The i-th line should contain the answer when the first i operations have been done.
Sample Input 1
15 10 + 5 + 2 + 3 - 2 + 5 + 10 - 3 + 1 + 3 + 3 - 5 + 1 + 7 + 4 - 3
Sample Output 1
0 0 1 0 1 2 2 2 2 2 1 3 5 8 5
This input contains 15 operations.
After the last operation, the box contains the balls (5,10,1,3,1,7,4).
There are five ways to pick up balls for a sum of 10:
- 5+1+3+1 (the 1-st, 3-rd, 4-th, 5-th balls)
- 5+1+4 (the 1-st, 3-rd, 7-th balls)
- 5+1+4 (the 1-st, 5-th, 7-th balls)
- 10 (the 2-nd ball)
- 3+7 (the 4-th, 6-th balls)