実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
N 個の整数 A_1,A_2,\dots,A_N が与えられます。 また、B_i=A_i\times A_{i+1}\ (1\leq i\leq N-1) と定めます。
B_1,B_2,\dots,B_{N-1} をこの順に空白区切りで出力してください。
制約
- 2\leq N \leq 100
- 1\leq A_i \leq 100
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
B_1,B_2,\dots,B_{N-1} をこの順に空白区切りで出力せよ。
入力例 1
3 3 4 6
出力例 1
12 24
B_1=A_1\times A_2 = 12, B_2=A_2\times A_3 = 24 です。
入力例 2
5 22 75 26 45 72
出力例 2
1650 1950 1170 3240
Score: 100 points
Problem Statement
You are given N integers A_1, A_2, \dots, A_N. Also, define B_i = A_i \times A_{i+1}\ (1 \leq i \leq N-1).
Print B_1, B_2, \dots, B_{N-1} in this order, separated by spaces.
Constraints
- 2 \leq N \leq 100
- 1 \leq A_i \leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print B_1, B_2, \dots, B_{N-1} in this order, separated by spaces.
Sample Input 1
3 3 4 6
Sample Output 1
12 24
We have B_1 = A_1 \times A_2 = 12, B_2 = A_2 \times A_3 = 24.
Sample Input 2
5 22 75 26 45 72
Sample Output 2
1650 1950 1170 3240
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
高橋君は
2025 年 5 月 17 日 A 時 B 分締切のレポートを、
2025 年 5 月 17 日 C 時 D 分に提出しました。
ここで、「A 時 B 分」と「C 時 D 分」は異なる時刻であることが保証されます。
高橋君が締切前にレポートを提出しているならば Yes を、そうでないならば No を出力してください。
制約
- 0 \leq A,C \leq 23
- 0 \leq B,D \leq 59
- (A,B)\neq(C,D)
- A,B,C,D は整数
入力
入力は以下の形式で標準入力から与えられる。
A B C D
出力
高橋君が締切前にレポートを提出しているならば Yes を、そうでないならば No を出力せよ。
入力例 1
22 40 22 30
出力例 1
Yes
レポートの締切は 22 時 40 分であり、高橋君は 22 時 30 分に提出しているため、締切前にレポートを提出しています。
よって、Yes を出力します。
入力例 2
22 40 22 45
出力例 2
No
レポートの締切は 22 時 40 分であり、高橋君は 22 時 45 分に提出しているため、締切後にレポートを提出しています。
よって、No を出力します。
入力例 3
12 0 11 30
出力例 3
Yes
Score : 100 points
Problem Statement
Takahashi had a report whose deadline was B minutes past A o'clock on May 17, 2025. He submitted it at D minutes past C o'clock on May 17, 2025.
It is guaranteed that "B minutes past A o'clock" and "D minutes past C o'clock" are different times.
Output Yes if Takahashi submitted the report before the deadline, and No otherwise.
Constraints
- 0 \leq A, C \leq 23
- 0 \leq B, D \leq 59
- (A, B) \neq (C, D)
- A, B, C, and D are integers.
Input
The input is given from Standard Input in the following format:
A B C D
Output
If Takahashi submitted the report before the deadline, output Yes; otherwise, output No.
Sample Input 1
22 40 22 30
Sample Output 1
Yes
The deadline is 22:40, and he submitted at 22:30, so he submitted before the deadline.
Hence, output Yes.
Sample Input 2
22 40 22 45
Sample Output 2
No
The deadline is 22:40, and he submitted at 22:45, so he submitted after the deadline.
Hence, output No.
Sample Input 3
12 0 11 30
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
4 桁の暗証番号 X_1X_2X_3X_4 が与えられます。 番号は先頭の桁が 0 であることもあり得ます。 暗証番号は以下のいずれかの条件をみたすとき弱い暗証番号と呼ばれます。
- 4 桁とも同じ数字である。
- 1\leq i\leq 3 をみたす任意の整数 i について、 X_{i+1} が、 X_i の次の数字である。 ただし、 0\leq j\leq 8 について j の次の数字は j+1 であり、 9 の次の数字は 0 である。
与えられた暗証番号が弱い暗証番号ならば Weak を、そうでないならば Strong を出力してください。
制約
- 0 \leq X_1, X_2, X_3, X_4 \leq 9
- X_1, X_2, X_3, X_4 は整数である。
入力
入力は以下の形式で標準入力から与えられる。
X_1X_2X_3X_4
出力
与えられた暗証番号が弱い暗証番号ならば Weak を、そうでないならば Strong を出力せよ。
入力例 1
7777
出力例 1
Weak
4 桁ともすべて 7 であるため、 1 つめの条件をみたしており、弱い暗証番号です。
入力例 2
0112
出力例 2
Strong
1 桁目と 2 桁目が異なっており、 3 桁目は 2 桁目の次の数字ではないため、どちらの条件もみたしていません。
入力例 3
9012
出力例 3
Weak
9 の次の数字が 0 であることに注意してください。
Score : 200 points
Problem Statement
You are given a 4-digit PIN: X_1X_2X_3X_4, which may begin with a 0. The PIN is said to be weak when it satisfies one of the following conditions:
- All of the four digits are the same.
- For each integer i such that 1\leq i\leq 3, X_{i+1} follows X_i. Here, j+1 follows j for each 0\leq j\leq 8, and 0 follows 9.
If the given PIN is weak, print Weak; otherwise, print Strong.
Constraints
- 0 \leq X_1, X_2, X_3, X_4 \leq 9
- X_1, X_2, X_3, and X_4 are integers.
Input
Input is given from Standard Input in the following format:
X_1X_2X_3X_4
Output
If the given PIN is weak, print Weak; otherwise, print Strong.
Sample Input 1
7777
Sample Output 1
Weak
All four digits are 7, satisfying the first condition, so this PIN is weak.
Sample Input 2
0112
Sample Output 2
Strong
The first and second digits differ, and the third digit does not follow the second digit, so neither condition is satisfied.
Sample Input 3
9012
Sample Output 3
Weak
Note that 0 follows 9.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
6 つの面を持つサイコロが 3 個あります。
i 個目のサイコロの j 個目の面には A_{i,j} が書かれています。
どのサイコロも、各面が出る確率は \frac{1}{6} です。
これらのサイコロを同時に振ったとき、4,5,6 の書かれた目が 1 つずつ出る確率を求めてください。
制約
- A_{i,j} は 1 以上 6 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
A_{1,1} A_{1,2} A_{1,3} A_{1,4} A_{1,5} A_{1,6}
A_{2,1} A_{2,2} A_{2,3} A_{2,4} A_{2,5} A_{2,6}
A_{3,1} A_{3,2} A_{3,3} A_{3,4} A_{3,5} A_{3,6}
出力
答えを出力せよ。 真の解との絶対誤差または相対誤差が 10^{-6} 以下のとき正解とみなされる。
入力例 1
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
出力例 1
0.0277777778
求める確率は \frac{1}{36} です。 0.027778 などの出力でも正解となります。
入力例 2
4 5 6 4 5 6 4 4 5 5 6 6 6 5 4 4 5 6
出力例 2
0.2222222222
求める確率は \frac{2}{9} です。
Score : 200 points
Problem Statement
There are three dice, each with six faces.
The j-th face of the i-th die has A_{i,j} written on it.
For each die, each face comes up with probability \frac{1}{6}.
When these dice are rolled simultaneously, find the probability that the values 4,5,6 each appear exactly once.
Constraints
- A_{i,j} is an integer between 1 and 6, inclusive.
Input
The input is given from Standard Input in the following format:
A_{1,1} A_{1,2} A_{1,3} A_{1,4} A_{1,5} A_{1,6}
A_{2,1} A_{2,2} A_{2,3} A_{2,4} A_{2,5} A_{2,6}
A_{3,1} A_{3,2} A_{3,3} A_{3,4} A_{3,5} A_{3,6}
Output
Output the answer. The answer is considered correct if the absolute or relative error from the true answer is at most 10^{-6}.
Sample Input 1
1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
Sample Output 1
0.0277777778
The desired probability is \frac{1}{36}. An output such as 0.027778 is also accepted.
Sample Input 2
4 5 6 4 5 6 4 4 5 5 6 6 6 5 4 4 5 6
Sample Output 2
0.2222222222
The desired probability is \frac{2}{9}.
実行時間制限: 2 sec / メモリ制限: 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
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
二次元平面上に高橋君がいます。高橋君は原点から移動を N 回行いました。
N 回の移動は長さ N の文字列で表され、意味は次の通りです。
- i 回目の高橋君の移動後の座標は、移動前の座標を (x,y) として、
- S の i 文字目が
Rであるとき (x+1,y) - S の i 文字目が
Lであるとき (x-1,y) - S の i 文字目が
Uであるとき (x,y+1) - S の i 文字目が
Dであるとき (x,y-1)
- S の i 文字目が
N 回の移動 (始点と終点を含む) で、高橋君が同じ座標にいたことがあるかどうかを判定してください。
制約
- 1 \leq N \leq 2\times 10^5
- N は整数
- S は
R,L,U,Dのみからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
N 回の移動 (始点と終点を含む) で、高橋君が同じ座標にいたことがあれば Yes、なければ No と出力せよ。
入力例 1
5 RLURU
出力例 1
Yes
高橋君のいる座標は (0,0)\to (1,0)\to (0,0)\to (0,1)\to (1,1)\to (1,2) と変化します。
入力例 2
20 URDDLLUUURRRDDDDLLLL
出力例 2
No
Score : 300 points
Problem Statement
Takahashi is on a two-dimensional plane. Starting from the origin, he made N moves.
The N moves are represented by a string of length N as described below:
-
Takahashi's coordinates after the i-th move are:
- (x+1,y) if the i-th character of S is
R; - (x-1,y) if the i-th character of S is
L; - (x,y+1) if the i-th character of S is
U; and - (x,y-1) if the i-th character of S is
D,
where (x,y) is his coordinates before the move.
- (x+1,y) if the i-th character of S is
Determine if Takahashi visited the same coordinates multiple times in the course of the N moves (including the starting and ending points).
Constraints
- 1 \leq N \leq 2\times 10^5
- N is an integer.
- S is a string of length N consisting of
R,L,U, andD.
Input
The input is given from Standard Input in the following format:
N S
Output
Print Yes if Takahashi visited the same coordinates multiple times in the course of the N moves; print No otherwise.
Sample Input 1
5 RLURU
Sample Output 1
Yes
Takahashi's coordinates change as follows: (0,0)\to (1,0)\to (0,0)\to (0,1)\to (1,1)\to (1,2).
Sample Input 2
20 URDDLLUUURRRDDDDLLLL
Sample Output 2
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N 組のカップルが一列に座っています。
2 組のカップルであって、もともと両方のカップルは隣り合わせで座っておらず、かつ 4 人の間で席を交換することで両方のカップルが隣り合わせで座れるようになる組の個数を数えてください。
長さ 2N の数列 A=(A_1,A_2,\dots,A_{2N}) があります。A には 1, 2, \dots, N がそれぞれ 2 回ずつ登場します。
1 \leq a \lt b \leq N を満たす整数対 (a, b) であって以下の条件を全て満たすものの個数を求めてください。
- A 内において a 同士は隣接していない。
- A 内において b 同士は隣接していない。
- 次の操作を 1 回以上自由な回数行うことで、A 内において a 同士が隣接していて、かつ b 同士が隣接している状態にすることができる。
- A_i = a, A_j = b を満たす整数対 (i, j) (1 \leq i \leq 2N, 1 \leq j \leq 2N) を選び、A_i と A_j を入れ替える。
T 個のテストケースが与えられるので、それぞれに対して答えを求めてください。
制約
- 1 \leq T \leq 2 \times 10^5
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq N
- A には 1, 2, \dots, N がそれぞれ 2 回ずつ登場する
- 全てのテストケースに対する N の総和は 2 \times 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで、\mathrm{case}_i は i 番目のテストケースを意味する。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N
A_1 A_2 \dots A_{2N}
出力
T 行出力せよ。i 行目には i 番目のテストケースの答えを出力せよ。
入力例 1
3 3 1 2 3 3 1 2 4 1 1 2 2 3 3 4 4 5 1 2 3 4 5 1 2 3 4 5
出力例 1
1 0 4
1 番目のテストケースについて考えます。
(a, b)=(1, 2) は問題文の条件を満たします。理由は次の通りです。
- A 内において 1 同士は隣接していない。
- A 内において 2 同士は隣接していない。
- (i,j)=(1, 6) として A_1 と A_6 を入れ替える操作を行うことで、1 同士が隣接していて、かつ 2 同士が隣接している状態にすることができる。
問題文の条件を満たす (a, b) は (1, 2) のみです。
Score : 400 points
Problem Statement
N couples are seated in a line.
Count the number of pairs of couples such that neither couple was originally sitting next to each other, and both couples can end up sitting next to each other by swapping seats among those four people.
There is a sequence A = (A_1, A_2, \dots, A_{2N}) of length 2N. Each of the integers 1, 2, \dots, N appears exactly twice in A.
Find the number of integer pairs (a, b) satisfying 1 \leq a < b \leq N and all of the following conditions:
- The two occurrences of a in A are not adjacent.
- The two occurrences of b in A are not adjacent.
- By performing the following operation one or more times in any order, it is possible to reach a state where the two occurrences of a in A are adjacent and the two occurrences of b in A are also adjacent.
- Choose an integer pair (i, j) (1 \leq i \leq 2N, 1 \leq j \leq 2N) such that A_i = a and A_j = b, and swap A_i with A_j.
You are given T test cases; solve each of them.
Constraints
- 1 \leq T \leq 2 \times 10^5
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq N
- Each of 1, 2, \dots, N appears exactly twice in A.
- 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, where \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
A_1 A_2 \dots A_{2N}
Output
Print T lines. The i-th line should contain the answer for the i-th test case.
Sample Input 1
3 3 1 2 3 3 1 2 4 1 1 2 2 3 3 4 4 5 1 2 3 4 5 1 2 3 4 5
Sample Output 1
1 0 4
Consider the first test case.
(a, b) = (1, 2) satisfies the conditions in the problem statement, for the following reasons:
- The two occurrences of 1 in A are not adjacent.
- The two occurrences of 2 in A are not adjacent.
- By performing the operation where (i, j) = (1, 6) and swapping A_1 with A_6, you can reach a state where the two occurrences of 1 are adjacent and the two occurrences of 2 are also adjacent.
(1, 2) is the only pair (a, b) that satisfies the conditions.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
高橋君と N 匹の動物がいます。 N 匹の動物はそれぞれ動物 1 、動物 2 、\ldots 、動物 N と呼ばれます。
高橋君は下記の N 種類の行動をそれぞれ好きな回数だけ( 0 回でも良い)行います。
- A_1 円払い、動物 1 と動物 2 に餌をあげる。
- A_2 円払い、動物 2 と動物 3 に餌をあげる。
- A_3 円払い、動物 3 と動物 4 に餌をあげる。
- \cdots
- A_i 円払い、動物 i と動物 (i+1) に餌をあげる。
- \cdots
- A_{N-2} 円払い、動物 (N-2) と動物 (N-1) に餌をあげる。
- A_{N-1} 円払い、動物 (N-1) と動物 N に餌をあげる。
- A_N 円払い、動物 N と動物 1 に餌をあげる。
上記の N 種類目の行動では、「動物 N と動物 1 に」餌をあげることに注意してください。
すべての動物にそれぞれ 1 回以上餌をあげるまでにかかる費用の合計として考えられる最小値を出力してください。
制約
- 2 \leq N \leq 3 \times 10^5
- 1 \leq A_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
すべての動物にそれぞれ 1 回以上餌をあげるまでにかかる費用の合計として考えられる最小値を出力せよ。
入力例 1
5 2 5 3 2 5
出力例 1
7
高橋君が 1 種類目、3 種類目、4 種類目の行動をそれぞれ 1 回ずつ行うと、 動物 1 に 1 回、動物 2 に 1 回、動物 3 に 1 回、動物 4 に 2 回、動物 5 に 1 回餌をあげることになり、すべての動物にそれぞれ 1 回以上餌をあげることができます。 このときにかかる費用の合計は A_1 + A_3 + A_4 = 2 + 3 + 2 = 7 円であり、これが考えられる最小値です。
入力例 2
20 29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62
出力例 2
426
Score : 500 points
Problem Statement
Takahashi is with N animals. The N animals are called Animal 1, Animal 2, \ldots, Animal N.
Takahashi will perform the following N kinds of action. Each action can be performed any number of (possibly zero) times.
- Pay A_1 yen (the currency in Japan) to feed Animals 1 and 2.
- Pay A_2 yen to feed Animals 2 and 3.
- Pay A_3 yen to feed Animals 3 and 4.
- \cdots
- Pay A_i yen to feed Animals i and (i+1).
- \cdots
- Pay A_{N-2} yen to feed Animals (N-2) and (N-1).
- Pay A_{N-1} yen to feed Animals (N-1) and N.
- Pay A_N yen to feed Animals N and 1.
Note that the N-th action above feeds "Animals N and 1."
Print the minimum possible total cost to feed every animal at least once.
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 minimum possible total cost to feed every animal at least once.
Sample Input 1
5 2 5 3 2 5
Sample Output 1
7
If Takahashi performs the 1-st, 3-rd, and 4-th actions once each, Animals 1, 2, 3, 4, and 5 are fed once, once, once, twice, once, respectively, so every animal is fed at least once. The total cost to do so is A_1 + A_3 + A_4 = 2 + 3 + 2 = 7 yen, which is the minimum possible.
Sample Input 2
20 29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62
Sample Output 2
426
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
Q 個の整数の 3 つ組 (a_1, b_1, d_1), (a_2, b_2, d_2), \ldots, (a_Q, b_Q, d_Q) が与えられます。
集合 \lbrace 1, 2, \ldots, Q\rbrace の部分集合 S が良い集合であることを、 下記の条件を満たす長さ N の整数列 (X_1, X_2, \ldots, X_N) が存在することと定めます。
すべての i \in S について、X_{a_i} - X_{b_i} = d_i が成り立つ。
S が空集合である状態から始め、i = 1, 2, \ldots, Q の順に下記の操作を行います。
もし S \cup \lbrace i \rbrace が良い集合なら、S を S \cup \lbrace i \rbrace で置き換える。
最終的な S のすべての要素を昇順に出力してください。
制約
- 入力される値はすべて整数
- 1 \leq N, Q \leq 2 \times 10^5
- 1 \leq a_i, b_i \leq N
- -10^9 \leq d_i \leq 10^9
入力
入力は以下の形式で標準入力から与えられる。
N Q a_1 b_1 d_1 a_2 b_2 d_2 \vdots a_Q b_Q d_Q
出力
最終的な S のすべての要素を昇順に並べた列 (s_1, s_2, \ldots, s_k) を、下記の形式で空白区切りで出力せよ。
s_1 s_2 \ldots s_k
入力例 1
3 5 1 2 2 3 2 -3 2 1 -1 3 3 0 1 3 5
出力例 1
1 2 4 5
S が空集合である状態から始め、i = 1, 2, 3, 4, 5 の順に問題文中の操作を下記の通り行います。
- i = 1 について、集合 S \cup \lbrace i \rbrace = \lbrace 1 \rbrace は良い集合です。なぜなら、例えば (X_1, X_2, X_3) = (3, 1, 4) が問題文中の条件を満たすからです。よって、S を \lbrace 1\rbrace で置き換えます。
- i = 2 について、集合 S \cup \lbrace i \rbrace = \lbrace 1, 2 \rbrace は良い集合です。なぜなら、例えば (X_1, X_2, X_3) = (3, 1, -2) が問題文中の条件を満たすからです。よって、S を \lbrace 1, 2\rbrace で置き換えます。
- i = 3 について、集合 S \cup \lbrace i \rbrace = \lbrace 1, 2, 3 \rbrace は良い集合ではありません。
- i = 4 について、集合 S \cup \lbrace i \rbrace = \lbrace 1, 2, 4 \rbrace は良い集合です。なぜなら、例えば (X_1, X_2, X_3) = (3, 1, -2) が問題文中の条件を満たすからです。よって、S を \lbrace 1, 2, 4\rbrace で置き換えます。
- i = 5 について、集合 S \cup \lbrace i \rbrace = \lbrace 1, 2, 4, 5 \rbrace は良い集合です。なぜなら、例えば (X_1, X_2, X_3) = (3, 1, -2) が問題文中の条件を満たすからです。よって、S を \lbrace 1, 2, 4, 5\rbrace で置き換えます。
よって、最終的な S は \lbrace 1, 2, 4, 5\rbrace です。
入力例 2
200000 1 1 1 1
出力例 2
最終的な S は空集合です。
入力例 3
5 20 4 2 125421359 2 5 -191096267 3 4 -42422908 3 5 -180492387 3 3 174861038 2 3 -82998451 3 4 -134761089 3 1 -57159320 5 2 191096267 2 4 -120557647 4 2 125421359 2 3 142216401 4 5 -96172984 3 5 -108097816 1 5 -50938496 1 2 140157771 5 4 65674908 4 3 35196193 4 4 0 3 4 188711840
出力例 3
1 2 3 6 8 9 11 14 15 16 17 19
Score : 525 points
Problem Statement
You are given Q triples of integers (a_1, b_1, d_1), (a_2, b_2, d_2), \ldots, (a_Q, b_Q, d_Q).
A subset S of the set \lbrace 1, 2, \ldots, Q\rbrace is defined to be a good set if there exists an integer sequence (X_1, X_2, \ldots, X_N) of length N that satisfies:
X_{a_i} - X_{b_i} = d_i for all i \in S.
Starting with S as an empty set, perform the following operation for i = 1, 2, \ldots, Q in this order:
If S \cup \lbrace i \rbrace is a good set, then replace S with S \cup \lbrace i \rbrace.
Print all elements of the final set S in ascending order.
Constraints
- All input values are integers.
- 1 \leq N, Q \leq 2 \times 10^5
- 1 \leq a_i, b_i \leq N
- -10^9 \leq d_i \leq 10^9
Input
The input is given from Standard Input in the following format:
N Q a_1 b_1 d_1 a_2 b_2 d_2 \vdots a_Q b_Q d_Q
Output
Print the sequence (s_1, s_2, \ldots, s_k) of all elements of the final set S in ascending order, separated by spaces, in the following format:
s_1 s_2 \ldots s_k
Sample Input 1
3 5 1 2 2 3 2 -3 2 1 -1 3 3 0 1 3 5
Sample Output 1
1 2 4 5
Starting with S as an empty set, perform the operation described in the problem statement for i = 1, 2, 3, 4, 5 in this order, as follows.
- For i = 1, the set S \cup \lbrace i \rbrace = \lbrace 1 \rbrace is a good set, because (X_1, X_2, X_3) = (3, 1, 4) satisfies the condition in the problem statement, for example, so replace S with \lbrace 1\rbrace.
- For i = 2, the set S \cup \lbrace i \rbrace = \lbrace 1, 2 \rbrace is a good set, because (X_1, X_2, X_3) = (3, 1, -2) satisfies the condition in the problem statement, for example, so replace S with \lbrace 1, 2\rbrace.
- For i = 3, the set S \cup \lbrace i \rbrace = \lbrace 1, 2, 3 \rbrace is not a good set.
- For i = 4, the set S \cup \lbrace i \rbrace = \lbrace 1, 2, 4 \rbrace is a good set, because (X_1, X_2, X_3) = (3, 1, -2) satisfies the condition in the problem statement, for example, so replace S with \lbrace 1, 2, 4\rbrace.
- For i = 5, the set S \cup \lbrace i \rbrace = \lbrace 1, 2, 4, 5 \rbrace is a good set, because (X_1, X_2, X_3) = (3, 1, -2) satisfies the condition in the problem statement, for example, so replace S with \lbrace 1, 2, 4, 5\rbrace.
Therefore, the final set S is \lbrace 1, 2, 4, 5\rbrace.
Sample Input 2
200000 1 1 1 1
Sample Output 2
The final set S is empty.
Sample Input 3
5 20 4 2 125421359 2 5 -191096267 3 4 -42422908 3 5 -180492387 3 3 174861038 2 3 -82998451 3 4 -134761089 3 1 -57159320 5 2 191096267 2 4 -120557647 4 2 125421359 2 3 142216401 4 5 -96172984 3 5 -108097816 1 5 -50938496 1 2 140157771 5 4 65674908 4 3 35196193 4 4 0 3 4 188711840
Sample Output 3
1 2 3 6 8 9 11 14 15 16 17 19