Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
長さ N の整数列 A と整数 K,X が与えられます。
整数列 A の K 要素目の直後に整数 X を 1 つ挿入した整数列 B を出力してください。
制約
- 入力は全て整数
- 1 \le K \le N \le 100
- 1 \le A_i,X \le 100
入力
入力は以下の形式で標準入力から与えられる。
N K X A_1 A_2 \dots A_N
出力
整数列 A の K 要素目の直後に整数 X を 1 つ挿入した整数列 B を、以下の形式で出力せよ。
B_1 B_2 \dots B_{N+1}
入力例 1
4 3 7 2 3 5 11
出力例 1
2 3 5 7 11
K=3, X=7, A=(2,3,5,11) のとき、 B=(2,3,5,7,11) です。
入力例 2
1 1 100 100
出力例 2
100 100
入力例 3
8 8 3 9 9 8 2 4 4 3 5
出力例 3
9 9 8 2 4 4 3 5 3
Score : 100 points
Problem Statement
You are given an integer sequence A of length N and integers K and X.
Print the integer sequence B obtained by inserting the integer X immediately after the K-th element of the sequence A.
Constraints
- All input values are integers.
- 1 \le K \le N \le 100
- 1 \le A_i, X \le 100
Input
The input is given from Standard Input in the following format:
N K X A_1 A_2 \dots A_N
Output
Print the integer sequence B obtained by inserting the integer X immediately after the K-th element of the sequence A, in the following format:
B_1 B_2 \dots B_{N+1}
Sample Input 1
4 3 7 2 3 5 11
Sample Output 1
2 3 5 7 11
For K=3, X=7, and A=(2,3,5,11), we get B=(2,3,5,7,11).
Sample Input 2
1 1 100 100
Sample Output 2
100 100
Sample Input 3
8 8 3 9 9 8 2 4 4 3 5
Sample Output 3
9 9 8 2 4 4 3 5 3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
整数 A,B が与えられるので、 A+B の値を答えてください。
但し、この問題は N 択問題であり、 i 番の選択肢は C_i です。
正解となる 選択肢の番号 を出力してください。
制約
- 入力は全て整数
- 1 \le N \le 300
- 1 \le A,B \le 1000
- 1 \le C_i \le 2000
- C_i は相異なる。すなわち、同じ選択肢が複数存在することはない。
- A+B=C_i なる i が丁度 1 つ存在する。すなわち、正解となる選択肢が必ずただ 1 つ存在する。
入力
入力は以下の形式で標準入力から与えられる。
N A B C_1 C_2 \dots C_N
出力
答えを整数として出力せよ。
入力例 1
3 125 175 200 300 400
出力例 1
2
125+175 = 300 です。
1 番の選択肢は 200 、 2 番の選択肢は 300 、 3 番の選択肢は 400 です。
よって正解となる選択肢の番号は 2 番であり、これを出力します。
入力例 2
1 1 1 2
出力例 2
1
1 択問題である場合もあります。
入力例 3
5 123 456 135 246 357 468 579
出力例 3
5
Score : 100 points
Problem Statement
Given integers A and B, find A+B.
This is a N-choice problem; the i-th choice is C_i.
Print the index of the correct choice.
Constraints
- All values in the input are integers.
- 1 \le N \le 300
- 1 \le A,B \le 1000
- 1 \le C_i \le 2000
- C_i are pairwise distinct. In other words, no two choices have the same value.
- There is exactly one i such that A+B=C_i. In other words, there is always a unique correct choice.
Input
The input is given from Standard Input in the following format:
N A B C_1 C_2 \dots C_N
Output
Print the answer as an integer.
Sample Input 1
3 125 175 200 300 400
Sample Output 1
2
We have 125+175 = 300.
The first, second, and third choices are 200, 300, and 400, respectively.
Thus, the 2-nd choice is correct, so 2 should be printed.
Sample Input 2
1 1 1 2
Sample Output 2
1
The problem may be a one-choice problem.
Sample Input 3
5 123 456 135 246 357 468 579
Sample Output 3
5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
数直線の原点に高橋君がいます。高橋君は座標 X にあるゴールに移動しようとしています。
座標 Y には壁があり、最初、高橋君は壁を超えて移動することができません。
座標 Z にあるハンマーを拾った後でなら、壁を破壊して通過できるようになります。
高橋君がゴールに到達することが可能か判定し、可能であれば移動距離の最小値を求めてください。
制約
- -1000 \leq X,Y,Z \leq 1000
- X,Y,Z は相異なり、いずれも 0 でない
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
X Y Z
出力
高橋君がゴールに到達することが可能であれば、移動距離の最小値を出力せよ。不可能であれば、かわりに -1 と出力せよ。
入力例 1
10 -10 1
出力例 1
10
高橋君はまっすぐゴールに向かうことができます。
入力例 2
20 10 -10
出力例 2
40
ゴールは壁の向こう側にあります。まずハンマーを拾い、壁を壊すことでゴールに到達することができます。
入力例 3
100 1 1000
出力例 3
-1
Score : 200 points
Problem Statement
Takahashi is at the origin of a number line. He wants to reach a goal at coordinate X.
There is a wall at coordinate Y, which Takahashi cannot go beyond at first.
However, after picking up a hammer at coordinate Z, he can destroy that wall and pass through.
Determine whether Takahashi can reach the goal. If he can, find the minimum total distance he needs to travel to do so.
Constraints
- -1000 \leq X,Y,Z \leq 1000
- X, Y, and Z are distinct, and none of them is 0.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
X Y Z
Output
If Takahashi can reach the goal, print the minimum total distance he needs to travel to do so. If he cannot, print -1 instead.
Sample Input 1
10 -10 1
Sample Output 1
10
Takahashi can go straight to the goal.
Sample Input 2
20 10 -10
Sample Output 2
40
The goal is beyond the wall. He can get there by first picking up the hammer and then destroying the wall.
Sample Input 3
100 1 1000
Sample Output 3
-1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
英小文字からなる長さ N の文字列 S が与えられます。
長さ K の文字列 t の出現回数を、以下を満たす整数 i の個数として定義します。
- 1 \leq i \leq N-K+1
- S の i 文字目から i+K-1 文字目までからなる部分文字列が t に一致する。
長さ K の文字列に対する出現回数の最大値 x を求めてください。 また、出現回数が x となるような長さ K の文字列をすべて辞書順昇順に出力してください。
制約
- N, K は整数
- S は英小文字からなる長さ N の文字列
- 1 \leq K \leq N \leq 100
入力
入力は以下の形式で標準入力から与えられる。
N K S
出力
2 行出力せよ。
1 行目には、長さ K の文字列に対する出現回数の最大値 x を出力せよ。
2 行目には、出現回数が x となるような長さ K の文字列を辞書順昇順に、空白区切りで出力せよ。
入力例 1
9 3 ovowowovo
出力例 1
2 ovo owo
出現回数 2 の文字列として、以下が挙げられます。
- 文字列
ovoについて、i=1,7 が条件を満たすことから、ovoの出現回数は 2 です。 - 文字列
owoについて、i=3,5 が条件を満たすことから、owoの出現回数は 2 です。
出現回数が 2 よりも大きいような長さ 3 の文字列は存在しないので、求める最大値は 2 です。
一方で、出現回数が 2 よりも小さい文字列として、以下が挙げられます。
- 文字列
vowについて、i=2 が条件を満たすことから、vowの出現回数は 1 です。 - 文字列
oooについて、条件を満たす i が存在しないことから、oooの出現回数は 0 です。
入力例 2
9 1 ovowowovo
出力例 2
5 o
入力例 3
35 3 thequickbrownfoxjumpsoverthelazydog
出力例 3
2 the
Score : 200 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters.
Define the number of occurrences of a string t of length K as the number of integers i that satisfy the following condition:
- 1 \leq i \leq N-K+1
- The substring of S from the i-th character to the (i+K-1)-th character matches t.
Find the maximum value x of the number of occurrences of a string of length K. Also, output all strings of length K with x occurrences in ascending lexicographical order.
Constraints
- N, K are integers.
- S is a string of length N consisting of lowercase English letters.
- 1 \leq K \leq N \leq 100
Input
The input is given from Standard Input in the following format:
N K S
Output
Output two lines.
The first line should contain the maximum value x of the number of occurrences of a string of length K.
The second line should contain all strings of length K with x occurrences in ascending lexicographical order, separated by spaces.
Sample Input 1
9 3 ovowowovo
Sample Output 1
2 ovo owo
The following strings have two occurrences:
- For the string
ovo, i=1,7 satisfy the condition, so the number of occurrences ofovois 2. - For the string
owo, i=3,5 satisfy the condition, so the number of occurrences ofowois 2.
There is no string of length 3 with more than two occurrences, so the maximum value is 2.
On the other hand, the following are examples of strings with fewer than two occurrences:
- For the string
vow, i=2 satisfies the condition, so the number of occurrences ofvowis 1. - For the string
ooo, there is no i that satisfies the condition, so the number of occurrences ofooois 0.
Sample Input 2
9 1 ovowowovo
Sample Output 2
5 o
Sample Input 3
35 3 thequickbrownfoxjumpsoverthelazydog
Sample Output 3
2 the
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
縦 N マス、横 N マスの N ^ 2 マスからなるマス目があります。 上から i 行目 (1\leq i\leq N) 、左から j 列目 (1\leq j\leq N) のマスをマス (i,j) と呼ぶことにします。
それぞれのマスは、空マスであるかコマが置かれているかのどちらかです。 マス目には合計で M 個のコマが置かれており、k 番目 (1\leq k\leq M) のコマはマス (a _ k,b _ k) に置かれています。
あなたは、すでに置かれているどのコマにも取られないように、いずれかの空マスに自分のコマを置きたいです。
マス (i,j) に置かれているコマは、次のどれかの条件を満たすコマを取ることができます。
- マス (i+2,j+1) に置かれている
- マス (i+1,j+2) に置かれている
- マス (i-1,j+2) に置かれている
- マス (i-2,j+1) に置かれている
- マス (i-2,j-1) に置かれている
- マス (i-1,j-2) に置かれている
- マス (i+1,j-2) に置かれている
- マス (i+2,j-1) に置かれている
ただし、存在しないマスについての条件は常に満たされないものとします。
たとえば、マス (4,4) に置かれているコマは、以下の図で青く示されたマスに置かれているコマを取ることができます。

あなたがコマを置くことができるマスがいくつあるか求めてください。
制約
- 1\leq N\leq10 ^ 9
- 1\leq M\leq2\times10 ^ 5
- 1\leq a _ k\leq N,1\leq b _ k\leq N\ (1\leq k\leq M)
- (a _ k,b _ k)\neq(a _ l,b _ l)\ (1\leq k\lt l\leq M)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M a _ 1 b _ 1 a _ 2 b _ 2 \vdots a _ M b _ M
出力
すでに置かれているコマに取られずに自分のコマを置くことができる空マスの個数を出力せよ。
入力例 1
8 6 1 4 2 1 3 8 4 5 5 2 8 3
出力例 1
38
すでに置かれているコマは、以下の図で青く示されたマスに置かれたコマを取ることができます。

よって、あなたがすでに置かれているコマに取られないように自分のコマを置くことができるマスは残りの 38 マスです。
入力例 2
1000000000 1 1 1
出力例 2
999999999999999997
10 ^ {18} マスのうち、置くことができないマスはマス (1,1),(2,3),(3,2) の 3 マスのみです。
答えが 2 ^ {32} 以上になる場合があることに注意してください。
入力例 3
20 10 1 4 7 11 7 15 8 10 11 6 12 5 13 1 15 2 20 10 20 15
出力例 3
338
Score : 300 points
Problem Statement
There is a grid of N^2 squares with N rows and N columns. Let (i,j) denote the square at the i-th row from the top (1\leq i\leq N) and j-th column from the left (1\leq j\leq N).
Each square is either empty or has a piece placed on it. There are M pieces placed on the grid, and the k-th (1\leq k\leq M) piece is placed on square (a_k,b_k).
You want to place your piece on an empty square in such a way that it cannot be captured by any of the existing pieces.
A piece placed on square (i,j) can capture pieces that satisfy any of the following conditions:
- Placed on square (i+2,j+1)
- Placed on square (i+1,j+2)
- Placed on square (i-1,j+2)
- Placed on square (i-2,j+1)
- Placed on square (i-2,j-1)
- Placed on square (i-1,j-2)
- Placed on square (i+1,j-2)
- Placed on square (i+2,j-1)
Here, conditions involving non-existent squares are considered to never be satisfied.
For example, a piece placed on square (4,4) can capture pieces placed on the squares shown in blue in the following figure:

How many squares can you place your piece on?
Constraints
- 1\leq N\leq10^9
- 1\leq M\leq2\times10^5
- 1\leq a_k\leq N,1\leq b_k\leq N\ (1\leq k\leq M)
- (a_k,b_k)\neq(a_l,b_l)\ (1\leq k\lt l\leq M)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M a_1 b_1 a_2 b_2 \vdots a_M b_M
Output
Print the number of empty squares where you can place your piece without it being captured by any existing pieces.
Sample Input 1
8 6 1 4 2 1 3 8 4 5 5 2 8 3
Sample Output 1
38
The existing pieces can capture pieces placed on the squares shown in blue in the following figure:

Therefore, you can place your piece on the remaining 38 squares.
Sample Input 2
1000000000 1 1 1
Sample Output 2
999999999999999997
Out of 10^{18} squares, only 3 squares cannot be used: squares (1,1), (2,3), and (3,2).
Note that the answer may be 2^{32} or greater.
Sample Input 3
20 10 1 4 7 11 7 15 8 10 11 6 12 5 13 1 15 2 20 10 20 15
Sample Output 3
338
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 個のボールが左右一列に並んでいます。初め、左から i \, (1 \leq i \leq N) 番目のボールには整数 i が書かれています。
高橋君は Q 回の操作を行いました。 i \, (1 \leq i \leq Q) 回目に行われた操作は次のようなものです。
- 整数 x_i が書かれているボールをその右隣のボールと入れ替える。ただし、整数 x_i が書かれているボールが元々右端にあった場合、代わりに左隣のボールと入れ替える。
操作後において左から i \, (1 \leq i \leq N) 番目のボールに書かれている整数を a_i とします。 a_1,\ldots,a_N を求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq x_i \leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q x_1 \vdots x_Q
出力
a_1,\ldots,a_N を空白区切りで出力せよ。
入力例 1
5 5 1 2 3 4 5
出力例 1
1 2 3 5 4
操作は以下のように行われます。
- 1 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 2,1,3,4,5 となる。
- 2 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,4,5 となる。
- 3 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,4,3,5 となる。
- 4 と書かれたボールを右隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,4,5 となる。
- 5 と書かれたボールは右端にあるので左隣のボールと入れ替える。ボールに書かれた整数は左から 1,2,3,5,4 となる。
入力例 2
7 7 7 7 7 7 7 7 7
出力例 2
1 2 3 4 5 7 6
入力例 3
10 6 1 5 2 9 6 6
出力例 3
1 2 3 4 5 7 6 8 10 9
Score : 300 points
Problem Statement
N balls are lined up in a row from left to right. Initially, the i-th (1 \leq i \leq N) ball from the left has an integer i written on it.
Takahashi has performed Q operations. The i-th (1 \leq i \leq Q) operation was as follows.
- Swap the ball with the integer x_i written on it with the next ball to the right. If the ball with the integer x_i written on it was originally the rightmost ball, swap it with the next ball to the left instead.
Let a_i be the integer written on the i-th (1 \leq i \leq N) ball after the operations. Find a_1,\ldots,a_N.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq x_i \leq N
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q x_1 \vdots x_Q
Output
Print a_1,\ldots,a_N, with spaces in between.
Sample Input 1
5 5 1 2 3 4 5
Sample Output 1
1 2 3 5 4
The operations are performed as follows.
- Swap the ball with 1 written on it with the next ball to the right. Now, the balls have integers 2,1,3,4,5 written on them, from left to right.
- Swap the ball with 2 written on it with the next ball to the right. Now, the balls have integers 1,2,3,4,5 written on them, from left to right.
- Swap the ball with 3 written on it with the next ball to the right. Now, the balls have integers 1,2,4,3,5 written on them, from left to right.
- Swap the ball with 4 written on it with the next ball to the right. Now, the balls have integers 1,2,3,4,5 written on them, from left to right.
- Swap the ball with 5 written on it with the next ball to the left, since it is the rightmost ball. Now, the balls have integers 1,2,3,5,4 written on them, from left to right.
Sample Input 2
7 7 7 7 7 7 7 7 7
Sample Output 2
1 2 3 4 5 7 6
Sample Input 3
10 6 1 5 2 9 6 6
Sample Output 3
1 2 3 4 5 7 6 8 10 9
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
N 行 N 列のグリッドがあります。高橋君は以下の条件を満たすように各マスを黒または白のいずれかに塗り分けたいと考えています。
- すべての行について以下の条件が成り立つ。
- ある整数 i\ (0\leq i\leq N) が存在して、その行の左から i 個のマスは黒、それ以外は白で塗られている。
- すべての列について以下の条件が成り立つ。
- ある整数 i\ (0\leq i\leq N) が存在して、その列の上から i 個のマスは黒、それ以外は白で塗られている。
M 個のマスがすでに塗られています。そのうち i 個目は上から X_i 行目、左から Y_i 列目のマスで、C_i が B のとき黒で、 W のとき白で塗られています。
高橋君はまだ塗られていない残りの N^2-M 個のマスの色をうまく決めることで条件を満たすことができるか判定してください。
制約
- 1\leq N\leq 10^9
- 1\leq M\leq \min(N^2,2\times 10^5)
- 1\leq X_i,Y_i\leq N
- (X_i,Y_i)\neq (X_j,Y_j)\ (i\neq j)
- C_i は
BまたはW - 入力される数値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M X_1 Y_1 C_1 \vdots X_M Y_M C_M
出力
条件を満たすことができるとき Yes を、そうでないとき No を出力せよ。
入力例 1
4 3 4 1 B 3 2 W 1 3 B
出力例 1
Yes
例えば以下の図のように色を塗り分けると条件を満たすことができます。すでに塗られているマスを赤色の線で囲んでいます。

入力例 2
2 2 1 2 W 2 2 B
出力例 2
No
塗られていない残りの 2 つのマスをどのように塗っても,条件を満たすことはできません。
入力例 3
1 1 1 1 W
出力例 3
Yes
入力例 4
2289 10 1700 1083 W 528 967 B 1789 211 W 518 1708 W 1036 779 B 136 657 B 759 1497 B 902 1309 B 1814 712 B 936 763 B
出力例 4
No
Score : 425 points
Problem Statement
There is an N \times N grid. Takahashi wants to color each cell black or white so that all of the following conditions are satisfied:
- For every row, the following condition holds:
- There exists an integer i\ (0\leq i\leq N) such that the leftmost i cells are colored black, and the rest are colored white.
- For every column, the following condition holds:
- There exists an integer i\ (0\leq i\leq N) such that the topmost i cells are colored black, and the rest are colored white.
Out of these N^2 cells, M of them have already been colored. Among them, the i-th one is at the X_i-th row from the top and the Y_i-th column from the left, and it is colored black if C_i is B and white if C_i is W.
Determine whether he can color the remaining uncolored N^2 - M cells so that all the conditions are satisfied.
Constraints
- 1\leq N\leq 10^9
- 1\leq M\leq \min(N^2,2\times 10^5)
- 1\leq X_i,Y_i\leq N
- (X_i,Y_i)\neq (X_j,Y_j)\ (i\neq j)
- C_i is
BorW. - All input numbers are integers.
Input
The input is given from Standard Input in the following format:
N M X_1 Y_1 C_1 \vdots X_M Y_M C_M
Output
If it is possible to satisfy the conditions, print Yes; otherwise, print No.
Sample Input 1
4 3 4 1 B 3 2 W 1 3 B
Sample Output 1
Yes
For example, one can color the grid as in the following figure to satisfy the conditions. The cells already colored are surrounded by red borders.

Sample Input 2
2 2 1 2 W 2 2 B
Sample Output 2
No
No matter how the remaining two cells are colored, the conditions cannot be satisfied.
Sample Input 3
1 1 1 1 W
Sample Output 3
Yes
Sample Input 4
2289 10 1700 1083 W 528 967 B 1789 211 W 518 1708 W 1036 779 B 136 657 B 759 1497 B 902 1309 B 1814 712 B 936 763 B
Sample Output 4
No
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
N 個の島と、2 つの島の間を双方向に結ぶ M 本の橋があり、
それぞれ島 1, 島 2, \ldots, 島 N および 橋 1, 橋 2, \ldots, 橋 M と番号づけられています。
橋 i は島 U_i と島 V_i を相互に結んでおり、どちらの方向に移動するにも T_i だけ時間がかかります。
ここで、橋の両端が同一の島であるような橋は存在しませんが、ある 2 つの島の間が 2 本以上の橋で直接繋がれている可能性はあります。
また、どの 2 つの島の間もいくつかの橋をわたって移動することができます。
Q 個の問題が与えられるので、各問題に対する答えを求めてください。i 番目の問題は次のようなものです。
相異なる K_i 本の橋、橋 B_{i,1}, 橋 B_{i,2}, \ldots, 橋 B_{i,K_i} が与えられます。
これらの橋をすべて 1 回以上わたり、島 1 から島 N まで移動するために必要な時間の最小値を求めてください。
ただし、島 1 から島 N までの移動において、橋をわたって島の間を移動するのにかかる時間以外は無視できるものとします。
また、与えられた橋はどの順で、またどの向きにわたってもかまいません。
制約
- 2\leq N \leq 400
- N-1\leq M \leq 2\times 10^5
- 1\leq U_i<V_i\leq N
- 1\leq T_i\leq 10^9
- 1\leq Q\leq 3000
- 1\leq K_i\leq 5
- 1\leq B_{i,1}<B_{i,2}<\cdots<B_{i,K_i}\leq M
- 入力はすべて整数
- どの 2 つの島の間もいくつかの橋をわたって移動することができる。
入力
入力は以下の形式で標準入力から与えられる。
N M
U_1 V_1 T_1
U_2 V_2 T_2
\vdots
U_M V_M T_M
Q
K_1
B_{1,1} B_{1,2} \cdots B_{1,{K_1}}
K_2
B_{2,1} B_{2,2} \cdots B_{2,{K_2}}
\vdots
K_Q
B_{Q,1} B_{Q,2} \cdots B_{Q,{K_Q}}
出力
Q 行出力せよ。i 行目(1\leq i\leq Q)には i 番目の問題に対する答えを整数で出力せよ。
入力例 1
3 5 1 2 10 1 3 20 1 3 30 2 3 15 2 3 25 2 1 1 2 3 5
出力例 1
25 70
1 番目の問題では橋 1 をわたった上で島 1 から島 3 へ移動するために必要な時間の最小値を求める必要があります。
橋 1 を使って島 1 から島 2 に移動した後に橋 4 を使って島 2 から島 3 に移動したとき時間は 10+15=25 だけかかり、このときが最小です。
よって、1 行目には 25 を出力します。
2 番目の問題では橋 3 および橋 5 をわたった上で島 1 から島 3 へ移動するために必要な時間の最小値を求める必要があります。
橋 3 を通って島 1 から島 3 に移動した後に橋 5 を使って島 2 へ移動し、橋 4 を使用して島 3 に移動したとき時間は 30+25+15=70 だけかかり、このときが最小です。よって、2 行目には 70 を出力します。
入力例 2
6 6 1 5 1 2 5 1 2 4 1 3 4 1 3 6 1 1 6 1 2 5 1 2 3 4 5 1 5
出力例 2
5 3
各問題において指定された橋をわたる際、わたる方向はどちらでも問題ありません。
入力例 3
5 5 1 2 1000000000 2 3 1000000000 3 4 1000000000 4 5 1000000000 1 5 1000000000 1 1 3
出力例 3
4000000000
答えが 32bit 整数型に収まらない可能性があることに注意してください。
Score : 450 points
Problem Statement
There are N islands and M bidirectional bridges connecting two islands. The islands and bridges are numbered 1, 2, \ldots, N and 1, 2, \ldots, M, respectively.
Bridge i connects islands U_i and V_i, and the time it takes to cross it in either direction is T_i.
No bridge connects an island to itself, but it is possible for two islands to be directly connected by more than one bridge.
One can travel between any two islands using some bridges.
You are given Q queries, so answer each of them. The i-th query is as follows:
You are given K_i distinct bridges: bridges B_{i,1}, B_{i,2}, \ldots, B_{i,K_i}.
Find the minimum time required to travel from island 1 to island N using each of these bridges at least once.
Only consider the time spent crossing bridges.
You can cross the given bridges in any order and in any direction.
Constraints
- 2 \leq N \leq 400
- N-1 \leq M \leq 2 \times 10^5
- 1 \leq U_i < V_i \leq N
- 1 \leq T_i \leq 10^9
- 1 \leq Q \leq 3000
- 1 \leq K_i \leq 5
- 1 \leq B_{i,1} < B_{i,2} < \cdots < B_{i,K_i} \leq M
- All input values are integers.
- It is possible to travel between any two islands using some bridges.
Input
The input is given from Standard Input in the following format:
N M
U_1 V_1 T_1
U_2 V_2 T_2
\vdots
U_M V_M T_M
Q
K_1
B_{1,1} B_{1,2} \cdots B_{1,{K_1}}
K_2
B_{2,1} B_{2,2} \cdots B_{2,{K_2}}
\vdots
K_Q
B_{Q,1} B_{Q,2} \cdots B_{Q,{K_Q}}
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer to the i-th query as an integer.
Sample Input 1
3 5 1 2 10 1 3 20 1 3 30 2 3 15 2 3 25 2 1 1 2 3 5
Sample Output 1
25 70
For the first query, we need to find the minimum time to travel from island 1 to island 3 while using bridge 1. The minimum time is achieved by using bridge 1 to move from island 1 to island 2, then using bridge 4 to move from island 2 to island 3. The time taken is 10 + 15 = 25. Hence, print 25 on the first line.
For the second query, we need to find the minimum time to travel from island 1 to island 3 while using both bridges 3 and 5. The minimum time is achieved by using bridge 3 to move from island 1 to island 3, then using bridge 5 to move to island 2, and finally using bridge 4 to return to island 3. The time taken is 30 + 25 + 15 = 70. Hence, print 70 on the second line.
Sample Input 2
6 6 1 5 1 2 5 1 2 4 1 3 4 1 3 6 1 1 6 1 2 5 1 2 3 4 5 1 5
Sample Output 2
5 3
For each query, you can cross the specified bridges in either direction.
Sample Input 3
5 5 1 2 1000000000 2 3 1000000000 3 4 1000000000 4 5 1000000000 1 5 1000000000 1 1 3
Sample Output 3
4000000000
Beware that the answer may not fit in a 32-bit integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
あなたとディーラーでゲームをします。 ゲームは 1 以上 D 以下の整数が等確率で出る D 面サイコロ、0 で初期化された変数 x,y を用いて以下のように行われます。
-
あなたはサイコロを振り、出た目を x に加算する操作を好きな回数行える。ここで、あなたは操作を行うたびに次の操作を行うかを選択できる。
-
その後、ディーラーは y < L を満たす限り、サイコロを振り、出た目を y に加算する操作を繰り返す。
-
x > N の場合あなたの負けである。そうでない場合、y > N または x>y のいずれかを満たす場合あなたの勝ちで、どちらも満たさない場合あなたの負けである。
あなたが勝率を最大化するように適切に行動する際、勝率を求めてください。
制約
- 入力は全て整数
- 1\leq L\leq N\leq 2\times 10^5
- 1\leq D \leq N
入力
入力は以下の形式で標準入力から与えられる。
N L D
出力
答えを出力せよ。出力した値の真の値との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定される。
入力例 1
3 2 2
出力例 1
0.468750000000000
x が 2 以下の場合操作を続けるという戦略が最適であることが証明できます。
入力例 2
200000 200000 200000
出力例 2
0.999986408692793
Score: 550 points
Problem Statement
You will play a game against a dealer. The game uses a D-sided die (dice) that shows an integer from 1 to D with equal probability, and two variables x and y initialized to 0. The game proceeds as follows:
-
You may perform the following operation any number of times: roll the die and add the result to x. You can choose whether to continue rolling or not after each roll.
-
Then, the dealer will repeat the following operation as long as y < L: roll the die and add the result to y.
-
If x > N, you lose. Otherwise, you win if y > N or x > y and lose if neither is satisfied.
Determine the probability of your winning when you act in a way that maximizes the probability of winning.
Constraints
- All inputs are integers.
- 1 \leq L \leq N \leq 2 \times 10^5
- 1 \leq D \leq N
Input
The input is given from Standard Input in the following format:
N L D
Output
Print the answer. Your output will be considered correct if its absolute or relative error from the true value is at most 10^{-6}.
Sample Input 1
3 2 2
Sample Output 1
0.468750000000000
It can be proved that the optimal strategy is to continue rolling as long as x is not greater than 2.
Sample Input 2
200000 200000 200000
Sample Output 2
0.999986408692793