Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
人 1, 人 2,\ldots, 人 N の N 人が一列に並んでいます。
並び方の情報が長さ N の数列 A=(A _ 1,A _ 2,\ldots,A _ N) として与えられます。
A _ i\ (1\leq i\leq N) は次のような情報を表しています。
- A _ i=-1 のとき、人 i は先頭に並んでいる。
- A _ i\neq -1 のとき、人 i は人 A _ i のすぐ後ろに並んでいる。
列に並んでいる人の番号を先頭から順番に出力してください。
制約
- 1\leq N\leq3\times10 ^ 5
- A _ i=-1 もしくは 1\leq A _ i\leq N\ (1\leq i\leq N)
- 情報と矛盾しないような N 人の並び方がただ 1 通り存在する
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A _ 1 A _ 2 \ldots A _ N
出力
人 s _ 1, 人 s _ 2,\ldots, 人 s _ N がこの順に列に並んでいるとき、s _ 1,s _ 2,\ldots,s _ N をこの順に空白を区切りとして出力せよ。
入力例 1
6 4 1 -1 5 3 2
出力例 1
3 5 4 1 2 6
先頭から、人 3, 人 5, 人 4, 人 1, 人 2, 人 6 がこの順に列に並んでいるとき、与えられた情報と一致しています。
実際、
- 人 1 は人 4 のすぐ後ろに並んでいます。
- 人 2 は人 1 のすぐ後ろに並んでいます。
- 人 3 は先頭に並んでいます。
- 人 4 は人 5 のすぐ後ろに並んでいます。
- 人 5 は人 3 のすぐ後ろに並んでいます。
- 人 6 は人 2 のすぐ後ろに並んでいます。
となり、与えられた情報と一致していることが確認できます。
よって、3,5,4,1,2,6 をこの順に空白区切りで出力してください。
入力例 2
10 -1 1 2 3 4 5 6 7 8 9
出力例 2
1 2 3 4 5 6 7 8 9 10
入力例 3
30 3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7
出力例 3
10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14
Score: 300 points
Problem Statement
There are N people standing in a line: person 1, person 2, \ldots, person N.
You are given the arrangement of the people as a sequence A=(A _ 1,A _ 2,\ldots,A _ N) of length N.
A _ i\ (1\leq i\leq N) represents the following information:
- if A _ i=-1, person i is at the front of the line;
- if A _ i\neq -1, person i is right behind person A _ i.
Print the people's numbers in the line from front to back.
Constraints
- 1\leq N\leq3\times10 ^ 5
- A _ i=-1 or 1\leq A _ i\leq N\ (1\leq i\leq N)
- There is exactly one way to arrange the N people consistent with the information given.
- 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
Output
If person s _ 1, person s _ 2, \ldots, person s _ N are standing in the line in this order, print s _ 1, s _ 2, \ldots, and s _ N in this order, separated by spaces.
Sample Input 1
6 4 1 -1 5 3 2
Sample Output 1
3 5 4 1 2 6
If person 3, person 5, person 4, person 1, person 2, and person 6 stand in line in this order from front to back, the arrangement matches the given information.
Indeed, it can be seen that:
- person 1 is standing right behind person 4,
- person 2 is standing right behind person 1,
- person 3 is at the front of the line,
- person 4 is standing right behind person 5,
- person 5 is standing right behind person 3, and
- person 6 is standing right behind person 2.
Thus, print 3, 5, 4, 1, 2, and 6 in this order, separated by spaces.
Sample Input 2
10 -1 1 2 3 4 5 6 7 8 9
Sample Output 2
1 2 3 4 5 6 7 8 9 10
Sample Input 3
30 3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7
Sample Output 3
10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
あなたは N 本の鍵 1,2,\dots,N を持っています。
このうち何本かの鍵は正しい鍵で、それ以外はダミーの鍵です。
また、鍵を何本でも挿し込める ドアX があり、この ドアX は正しい鍵を K 本以上挿し込んだ時、またその時に限って開きます。
あなたはこれらの鍵に対して M 回のテストを行いました。このうち i 回目のテストの内容は次の通りです。
- C_i 本の鍵 A_{i,1},A_{i,2},\dots,A_{i,C_i} を ドアX に挿し込む。
- テスト結果はひとつの英文字 R_i で表現される。
- R_i =
oのとき i 回目のテストでドアが開いたことを表す。 - R_i =
xのとき i 回目のテストでドアが開かなかったことを表す。
- R_i =
各鍵が正しいかダミーかの組み合わせは 2^N 通り考えられますが、このうちどのテスト結果にも矛盾しない組み合わせの個数を求めてください。
ただし、与えられるテスト結果が誤っており上記の条件を満たす組み合わせが存在しない場合もあります。その場合は 0 通りと解答してください。
制約
- N,M,K,C_i,A_{i,j} は整数
- 1 \le K \le N \le 15
- 1 \le M \le 100
- 1 \le C_i \le N
- 1 \le A_{i,j} \le N
- j \neq k ならば A_{i,j} \neq A_{i,k}
- R_i は
oまたはx
入力
入力は以下の形式で標準入力から与えられる。
N M K
C_1 A_{1,1} A_{1,2} \dots A_{1,C_1} R_1
C_2 A_{2,1} A_{2,2} \dots A_{2,C_2} R_2
\vdots
C_M A_{M,1} A_{M,2} \dots A_{M,C_M} R_M
出力
答えを整数として出力せよ。
入力例 1
3 2 2 3 1 2 3 o 2 2 3 x
出力例 1
2
この入力では鍵が 3 本あり、テストは 2 回行われました。
また、 ドアX を開くのに必要な正しい鍵の本数は 2 本です。
- 1 回目のテストでは鍵 1,2,3 を使い、その結果 ドアX は開きました。
- 2 回目のテストでは鍵 2,3 を使い、その結果 ドアX は開きませんした。
各鍵が正しいかダミーかの組み合わせであって、どのテスト結果にも矛盾しないものは以下の 2 通りです。
- 鍵 1 は本物、鍵 2 はダミー、鍵 3 は本物である。
- 鍵 1 は本物、鍵 2 は本物、鍵 3 はダミーである。
入力例 2
4 5 3 3 1 2 3 o 3 2 3 4 o 3 3 4 1 o 3 4 1 2 o 4 1 2 3 4 x
出力例 2
0
問題文中でも述べた通り、答えが 0 通りである場合もあります。
入力例 3
11 4 9 10 1 2 3 4 5 6 7 8 9 10 o 11 1 2 3 4 5 6 7 8 9 10 11 o 10 11 10 9 8 7 6 5 4 3 2 x 10 11 9 1 4 3 7 5 6 2 10 x
出力例 3
8
Score : 300 points
Problem Statement
You have N keys numbered 1, 2, \dots, N.
Some of these are real keys, while the others are dummies.
There is a door, Door X, into which you can insert any number of keys. Door X will open if and only if at least K real keys are inserted.
You have conducted M tests on these keys. The i-th test went as follows:
- You inserted C_i keys A_{i,1}, A_{i,2}, \dots, A_{i,C_i} into Door X.
- The test result is represented by a single English letter R_i.
- R_i =
omeans that Door X opened in the i-th test. - R_i =
xmeans that Door X did not open in the i-th test.
- R_i =
There are 2^N possible combinations of which keys are real and which are dummies. Among these, find the number of combinations that do not contradict any of the test results.
It is possible that the given test results are incorrect and no combination satisfies the conditions. In such a case, report 0.
Constraints
- N, M, K, C_i, and A_{i,j} are integers.
- 1 \le K \le N \le 15
- 1 \le M \le 100
- 1 \le C_i \le N
- 1 \le A_{i,j} \le N
- A_{i,j} \neq A_{i,k} if j \neq k.
- R_i is
oorx.
Input
The input is given from Standard Input in the following format:
N M K
C_1 A_{1,1} A_{1,2} \dots A_{1,C_1} R_1
C_2 A_{2,1} A_{2,2} \dots A_{2,C_2} R_2
\vdots
C_M A_{M,1} A_{M,2} \dots A_{M,C_M} R_M
Output
Print the answer as an integer.
Sample Input 1
3 2 2 3 1 2 3 o 2 2 3 x
Sample Output 1
2
In this input, there are three keys and two tests were conducted.
Two correct keys are required to open Door X.
- In the first test, keys 1, 2, 3 were used, and Door X opened.
- In the second test, keys 2, 3 were used, and Door X did not open.
There are two combinations of which keys are real and which are dummies that do not contradict any of the test results:
- Key 1 is real, key 2 is a dummy, and key 3 is real.
- Key 1 is real, key 2 is real, and key 3 is a dummy.
Sample Input 2
4 5 3 3 1 2 3 o 3 2 3 4 o 3 3 4 1 o 3 4 1 2 o 4 1 2 3 4 x
Sample Output 2
0
As mentioned in the problem statement, the answer may be 0.
Sample Input 3
11 4 9 10 1 2 3 4 5 6 7 8 9 10 o 11 1 2 3 4 5 6 7 8 9 10 11 o 10 11 10 9 8 7 6 5 4 3 2 x 10 11 9 1 4 3 7 5 6 2 10 x
Sample Output 3
8
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
縦 H マス、横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i, j) と表します。
はじめ、全てのマスには壁が 1 個ずつ立てられています。
以下で説明されるクエリを Q 個与えられる順に処理した後に、残っている壁の個数を求めてください。
q 番目のクエリでは整数 R_q, C_q が与えられます。
あなたは (R_q, C_q) に爆弾を置いて壁を爆破します。その結果、以下の処理が行われます。
- (R_q, C_q) に壁が存在する場合は、その壁を破壊して処理を終了する。
- (R_q, C_q) に壁が存在しない場合は、(R_q, C_q) から上下左右に見て最初に現れる壁を破壊する。厳密に述べると、以下の 4 個の処理が同時に行われる。
- (i, C_q) に壁が存在して (k, C_q) (i \lt k \lt R_q) に壁が存在しないような i \lt R_q が存在する時、(i, C_q) に存在する壁を破壊する。
- (i, C_q) に壁が存在して (k, C_q) (R_q \lt k \lt i) に壁が存在しないような i \gt R_q が存在する時、(i, C_q) に存在する壁を破壊する。
- (R_q, j) に壁が存在して (R_q, k) (j \lt k \lt C_q) に壁が存在しないような j \lt C_q が存在する時、(R_q, j) に存在する壁を破壊する。
- (R_q, j) に壁が存在して (R_q, k) (C_q \lt k \lt j) に壁が存在しないような j \gt C_q が存在する時、(R_q, j) に存在する壁を破壊する。
制約
- 1 \leq H, W
- H \times W \leq 4 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq R_q \leq H
- 1 \leq C_q \leq W
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
H W Q R_1 C_1 R_2 C_2 \vdots R_Q C_Q
出力
クエリを全て処理した後に、残っている壁の個数を出力せよ。
入力例 1
2 4 3 1 2 1 2 1 3
出力例 1
2
クエリを処理する手順を説明すると次のようになります。
- 1 番目のクエリでは (R_1, C_1) = (1, 2) である。 (1, 2) に壁は存在するので (1, 2) の壁が爆破される。
- 2 番目のクエリでは (R_2, C_2) = (1, 2) である。 (1, 2) に壁は存在しないので、(1, 2) から上下左右に見て最初に現れる壁である (2,2),(1,1),(1,3) が爆破される。
- 3 番目のクエリでは (R_2, C_2) = (1, 3) である。 (1, 3) に壁は存在しないので、(1, 3) から上下左右に見て最初に現れる壁である (2,3),(1,4) が爆破される。
全てのクエリを処理した後に残っている壁は (2, 1), (2, 4) の 2 個です。
入力例 2
5 5 5 3 3 3 3 3 2 2 2 1 2
出力例 2
10
入力例 3
4 3 10 2 2 4 1 1 1 4 2 2 1 3 1 1 3 1 2 4 3 4 2
出力例 3
2
Score : 400 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.
Initially, there is one wall in each cell.
After processing Q queries explained below in the order they are given, find the number of remaining walls.
In the q-th query, you are given two integers R_q and C_q.
You place a bomb at (R_q, C_q) to destroy walls. As a result, the following process occurs.
- If there is a wall at (R_q, C_q), destroy that wall and end the process.
- If there is no wall at (R_q, C_q), destroy the first walls that appear when looking up, down, left, and right from (R_q, C_q). More precisely, the following four processes occur simultaneously:
- If there exists an i \lt R_q such that a wall exists at (i, C_q) and no wall exists at (k, C_q) for all i \lt k \lt R_q, destroy the wall at (i, C_q).
- If there exists an i \gt R_q such that a wall exists at (i, C_q) and no wall exists at (k, C_q) for all R_q \lt k \lt i, destroy the wall at (i, C_q).
- If there exists a j \lt C_q such that a wall exists at (R_q, j) and no wall exists at (R_q, k) for all j \lt k \lt C_q, destroy the wall at (R_q, j).
- If there exists a j \gt C_q such that a wall exists at (R_q, j) and no wall exists at (R_q, k) for all C_q \lt k \lt j, destroy the wall at (R_q, j).
Constraints
- 1 \leq H, W
- H \times W \leq 4 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq R_q \leq H
- 1 \leq C_q \leq W
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W Q R_1 C_1 R_2 C_2 \vdots R_Q C_Q
Output
Print the number of remaining walls after processing all queries.
Sample Input 1
2 4 3 1 2 1 2 1 3
Sample Output 1
2
The process of handling the queries can be explained as follows:
- In the 1st query, (R_1, C_1) = (1, 2). There is a wall at (1, 2), so the wall at (1, 2) is destroyed.
- In the 2nd query, (R_2, C_2) = (1, 2). There is no wall at (1, 2), so the walls at (2,2),(1,1),(1,3), which are the first walls that appear when looking up, down, left, and right from (1, 2), are destroyed.
- In the 3rd query, (R_3, C_3) = (1, 3). There is no wall at (1, 3), so the walls at (2,3),(1,4), which are the first walls that appear when looking up, down, left, and right from (1, 3), are destroyed.
After processing all queries, there are two remaining walls, at (2, 1) and (2, 4).
Sample Input 2
5 5 5 3 3 3 3 3 2 2 2 1 2
Sample Output 2
10
Sample Input 3
4 3 10 2 2 4 1 1 1 4 2 2 1 3 1 1 3 1 2 4 3 4 2
Sample Output 3
2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
長さ N の数列 A = (A_1, A_2, \dots, A_N), B = (B_1, B_2, \dots, B_N) が与えられます。
\lbrace 1, 2, \dots, N \rbrace の部分集合であって大きさが K のものを 1 つ選び S とします。この時、以下の式の値としてあり得る最小値を求めてください。
T 個のテストケースが与えられるので、それぞれに対して答えを求めてください。
制約
- 1 \leq T \leq 2 \times 10^5
- 1 \leq K \leq N \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq 10^6
- 全てのテストケースに対する N の総和は 2 \times 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_i は i 番目のテストケースを意味する。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N K A_1 A_2 \dots A_N B_1 B_2 \dots B_N
出力
T 行出力せよ。i 行目には i 番目のテストケースの答えを出力せよ。
入力例 1
3 3 2 3 7 6 9 2 4 5 3 6 4 1 5 9 8 6 5 1 7 10 6 61 95 61 57 69 49 46 47 14 43 39 79 48 92 90 76 30 16 30 94
出力例 1
42 60 14579
1 番目のテストケースでは、S = \lbrace 2, 3 \rbrace を選ぶと式の値が 7 \times (2 + 4) = 42 になり、これが最小です。
Score : 475 points
Problem Statement
You are given sequences of length N: A = (A_1, A_2, \dots, A_N) and B = (B_1, B_2, \dots, B_N).
Let S be a subset of \lbrace1, 2, \dots, N\rbrace of size K.
Here, find the minimum possible value of the following expression:
You are given T test cases; solve each of them.
Constraints
- 1 \leq T \leq 2 \times 10^5
- 1 \leq K \leq N \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq 10^6
- The sum of N over all test cases is at most 2 \times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format. Here, \mathrm{case}_i denotes the i-th test case.
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case is given in the following format:
N K A_1 A_2 \dots A_N B_1 B_2 \dots B_N
Output
Print T lines. The i-th line should contain the answer for the i-th test case.
Sample Input 1
3 3 2 3 7 6 9 2 4 5 3 6 4 1 5 9 8 6 5 1 7 10 6 61 95 61 57 69 49 46 47 14 43 39 79 48 92 90 76 30 16 30 94
Sample Output 1
42 60 14579
In the first test case, for S = \{2, 3\}, the value of the expression is 7 \times (2 + 4) = 42, which is the minimum.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 個のサイコロがあります。 i = 1, 2, \ldots, N について、i 番目のサイコロを振ると 1 以上 A_i 以下の整数の出目がそれぞれ等確率でランダムにでます。
N 個のサイコロすべてを同時に振るとき、その結果が下記の条件を満たす確率を \text{mod } 998244353 で求めてください。
N 個のサイコロの中からいくつか( N 個全部でも良い)を選ぶ方法であって、選んだサイコロの出目の和がちょうど 10 であるようなものが存在する。
確率 \text{mod } 998244353 の定義
この問題で求める確率は必ず有理数になることが証明できます。 また、この問題の制約下では、求める確率を既約分数 \frac{y}{x} で表したときに x が 998244353 で割り切れないことが保証されます。
このとき xz \equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まります。この z を答えてください。
制約
- 1 \leq N \leq 100
- 1 \leq A_i \leq 10^6
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
4 1 7 2 9
出力例 1
942786334
例えば、1, 2, 3, 4 個目のサイコロの出目が順に 1, 3, 2, 7 だった場合、この結果は問題文中の条件を満たします。 実際、2, 4 番目のサイコロを選択すると、選んだサイコロの出目の和が 3 + 7 = 10 になります。 また、1, 3, 4 番目のサイコロを選択しても、選んだサイコロの出目の和が 1 + 2 + 7 = 10 になります。
一方、1, 2, 3, 4 個目のサイコロの出目が順に 1, 6, 1, 5 だった場合、 どのようにサイコロを選んでも選んだサイコロの出目の和が 10 にならないため、この結果は問題文中の条件を満たしません。
この入力例では、N 個のサイコロを振った結果が問題文中の条件を満たす確率は \frac{11}{18} です。 よって、その \text{mod } 998244353 における値である 942786334 を出力します。
入力例 2
7 1 10 100 1000 10000 100000 1000000
出力例 2
996117877
Score : 500 points
Problem Statement
We have N dice. For each i = 1, 2, \ldots, N, when the i-th die is thrown, it shows a random integer between 1 and A_i, inclusive, with equal probability.
Find the probability, modulo 998244353, that the following condition is satisfied when the N dice are thrown simultaneously.
There is a way to choose some (possibly all) of the N dice so that the sum of their results is 10.
How to find a probability modulo 998244353
It can be proved that the sought probability is always a rational number. Additionally, the constraints of this problem guarantee that if the sought probability is represented as an irreducible fraction \frac{y}{x}, then x is not divisible by 998244353.
Here, there is a unique integer z such that xz \equiv y \pmod{998244353}. Report this z.
Constraints
- 1 \leq N \leq 100
- 1 \leq A_i \leq 10^6
- 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
Output
Print the answer.
Sample Input 1
4 1 7 2 9
Sample Output 1
942786334
For instance, if the first, second, third, and fourth dice show 1, 3, 2, and 7, respectively, these results satisfy the condition. In fact, if the second and fourth dice are chosen, the sum of their results is 3 + 7 = 10. Alternatively, if the first, third, and fourth dice are chosen, the sum of their results is 1 + 2 + 7 = 10.
On the other hand, if the first, second, third, and fourth dice show 1, 6, 1, and 5, respectively, there is no way to choose some of them so that the sum of their results is 10, so the condition is not satisfied.
In this sample input, the probability of the results of the N dice satisfying the condition is \frac{11}{18}. Thus, print this value modulo 998244353, that is, 942786334.
Sample Input 2
7 1 10 100 1000 10000 100000 1000000
Sample Output 2
996117877