実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
長さ N の整数列 A=(A_1,A_2,\dots,A_N) と整数 X が与えられます。
i=1,2,\dots,N の順に以下を行ってください。
- もし A_i<X なら、 X=A_i に更新した上で
1を出力する。 - そうでないなら
0を出力する。
制約
- 入力は全て整数
- 1 \le N,X,A_i \le 100
入力
入力は以下の形式で標準入力から与えられる。
N X A_1 A_2 \dots A_N
出力
N 行出力せよ。
そのうち k 行目には、 i=k についての出力をせよ。
入力例 1
5 10 6 4 7 1 3
出力例 1
1 1 0 1 0
- 最初、 X=10 です。
- i=1 について、 A_1=6<X=10 なので、 X=6 に更新した上で
1を出力します。 - i=2 について、 A_2=4<X=6 なので、 X=4 に更新した上で
1を出力します。 - i=3 について、 A_3=7 \ge X=4 なので、
0を出力します。 - i=4 について、 A_4=1<X=4 なので、 X=1 に更新した上で
1を出力します。 - i=5 について、 A_5=3 \ge X=1 なので、
0を出力します。
入力例 2
1 1 1
出力例 2
0
入力例 3
8 20 9 19 14 17 17 4 18 4
出力例 3
1 0 0 0 0 1 0 0
Score : 100 points
Problem Statement
You are given a length-N integer sequence A=(A_1,A_2,\dots,A_N) and an integer X.
For i=1,2,\dots,N in this order, do the following.
- If A_i<X, update X=A_i and output
1. - Otherwise, output
0.
Constraints
- All input values are integers.
- 1 \le N,X,A_i \le 100
Input
The input is given from Standard Input in the following format:
N X A_1 A_2 \dots A_N
Output
Output N lines.
The k-th line should contain the output for i=k.
Sample Input 1
5 10 6 4 7 1 3
Sample Output 1
1 1 0 1 0
- Initially, X=10.
- For i=1: since A_1=6<X=10, update X=6 and output
1. - For i=2: since A_2=4<X=6, update X=4 and output
1. - For i=3: since A_3=7 \ge X=4, output
0. - For i=4: since A_4=1<X=4, update X=1 and output
1. - For i=5: since A_5=3 \ge X=1, output
0.
Sample Input 2
1 1 1
Sample Output 2
0
Sample Input 3
8 20 9 19 14 17 17 4 18 4
Sample Output 3
1 0 0 0 0 1 0 0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
高橋君はコショウが大好きです。
M 種類のコショウがレストランに置いてあり、それぞれを種類 1,2,\dots,M と呼びます。 このレストランに種類 j ( 1 \le j \le M ) のコショウは C_j g だけあります。
高橋君はこの店で N 個の料理を注文しました。
相性の都合で i ( 1 \le i \le N ) 番目の料理には種類 A_i のコショウしかかけることができず、 i 番目の料理にかけられるコショウの量の上限は B_i g です。
また、高橋君がこれらの料理にかけられるのはレストランに置いてあるコショウのみです。つまり、種類 j のコショウを合計 C_j g を超えてかけることはできません。
高橋君は、料理にかけたコショウの量の合計を最大にするようにどの料理にどれだけのコショウをかけるかを決めます。
このとき、高橋君は合計何 g のコショウを料理にかけることができますか?
制約
- 入力は全て整数
- 1 \le N,M \le 1000
- 1 \le A_i \le M
- 1 \le B_i,C_i \le 10^6
入力
入力は以下の形式で標準入力から与えられる。
N M C_1 C_2 \dots C_M A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
答えを整数として出力せよ。
入力例 1
7 5 4 4 8 3 7 1 2 2 3 5 2 4 10 2 3 5 4 2 3
出力例 1
15
この入力では 5 種類のコショウがあり、種類 1,2,3,4,5 のコショウがそれぞれ 4,4,8,3,7 g あります。
以下のようにすると料理にかけたコショウの量の合計が 15 g となり、これが達成可能な最大です。
- 1 番目の料理に種類 1 のコショウを 2 g かける。
- 2 番目の料理にはコショウをかけない。
- 3 番目の料理に種類 5 のコショウを 2 g かける。
- 4 番目の料理に種類 4 のコショウを 3 g かける。
- 5 番目の料理に種類 2 のコショウを 1 g かける。
- 6 番目の料理に種類 5 のコショウを 4 g かける。
- 7 番目の料理に種類 2 のコショウを 3 g かける。
入力例 2
1 1 1 1 1
出力例 2
1
入力例 3
15 10 7 94 100 82 63 81 75 2 76 73 10 44 5 77 10 47 7 32 2 82 5 90 3 37 6 70 6 28 3 25 2 26 10 56 1 69 5 46 7 26
出力例 3
438
Score : 200 points
Problem Statement
Takahashi loves pepper.
A restaurant has M types of pepper, called type 1,2,\dots,M. There are C_j grams of type j (1 \le j \le M) pepper at this restaurant.
He ordered N dishes at this restaurant.
Due to compatibility, only type A_i pepper can be sprinkled on dish i (1 \le i \le N), and the upper limit on the amount of pepper that can be sprinkled on dish i is B_i grams.
Also, he can only use the pepper available at the restaurant. That is, the total amount of type j pepper sprinkled cannot exceed C_j grams.
He decides how much pepper to sprinkle on each dish to maximize the total amount of pepper sprinkled on the dishes.
How many grams of pepper in total can he sprinkle on the dishes?
Constraints
- All input values are integers.
- 1 \le N,M \le 1000
- 1 \le A_i \le M
- 1 \le B_i,C_i \le 10^6
Input
The input is given from Standard Input in the following format:
N M C_1 C_2 \dots C_M A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
Output the answer as an integer.
Sample Input 1
7 5 4 4 8 3 7 1 2 2 3 5 2 4 10 2 3 5 4 2 3
Sample Output 1
15
In this input, there are five types of pepper, with 4,4,8,3,7 grams of type 1,2,3,4,5 pepper respectively.
The following gives a total of 15 grams of pepper sprinkled on the dishes, which is the maximum achievable.
- Sprinkle 2 grams of type 1 pepper on dish 1.
- Sprinkle no pepper on dish 2.
- Sprinkle 2 grams of type 5 pepper on dish 3.
- Sprinkle 3 grams of type 4 pepper on dish 4.
- Sprinkle 1 gram of type 2 pepper on dish 5.
- Sprinkle 4 grams of type 5 pepper on dish 6.
- Sprinkle 3 grams of type 2 pepper on dish 7.
Sample Input 2
1 1 1 1 1
Sample Output 2
1
Sample Input 3
15 10 7 94 100 82 63 81 75 2 76 73 10 44 5 77 10 47 7 32 2 82 5 90 3 37 6 70 6 28 3 25 2 26 10 56 1 69 5 46 7 26
Sample Output 3
438
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
1 から N の番号がついた N 個のボールが袋に入っています。ボール i には整数 A_i が書かれています。
Q 個のクエリを処理してください。
クエリでは長さ K の数列 B_1, B_2, \dots, B_{K} が与えられるので以下の一連の操作を行ってください。ここで、B_i は全て 1 以上 N 以下で、かつ相異なります。
- まず、ボール B_1, ボール B_2, \dots, ボール B_K を全て袋から取り出す。
- そして、現在の袋に入っているボールに書かれた整数の最小値を出力する。(この時、袋は空でないことが制約から保証されている。)
- その後、取り出した K 個のボールを全て袋に戻す。
制約
- 6 \leq N \leq 3 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq K \leq 5
- 1 \leq B_1 \lt B_2 \lt \dots \lt B_K \leq N
- 全てのクエリに対する K の総和は 4 \times 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで \mathrm{query}_i は i 番目のクエリを意味する。
N Q
A_1 A_2 \dots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
クエリは以下の形式で与えられる。
K B_1 B_2 \dots B_K
出力
Q 行出力せよ。i 行目には i 番目のクエリの答えを出力せよ。
入力例 1
6 6 3 2 5 9 1 2 2 4 5 5 1 2 3 4 6 3 2 5 6 4 1 2 5 6 1 5 3 1 2 3
出力例 1
2 1 3 5 2 1
例えば 1 番目のクエリでは、ボール 4 とボール 5 を袋から取り出します。この時、袋には以下の 4 個のボールが残っています:
- 3 が書き込まれたボール 1
- 2 が書き込まれたボール 2
- 5 が書き込まれたボール 3
- 2 が書き込まれたボール 6
よってこの時の袋に入っているボールに書かれた整数の最小値は 2 です。
Score : 300 points
Problem Statement
A bag contains N balls numbered 1 to N. Ball i has the integer A_i written on it.
Process Q queries.
For each query, a sequence B_1, B_2, \dots, B_{K} of length K is given, and you should perform the following sequence of operations. Here, all B_i are between 1 and N inclusive and are distinct.
- First, remove balls B_1, B_2, \dots, B_K from the bag.
- Then, output the minimum value among the integers written on the balls currently in the bag. (It is guaranteed by the constraints that the bag is not empty at this point.)
- Afterwards, return all K removed balls to the bag.
Constraints
- 6 \leq N \leq 3 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq K \leq 5
- 1 \leq B_1 \lt B_2 \lt \dots \lt B_K \leq N
- The sum of K over all queries is at most 4 \times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format, where \mathrm{query}_i denotes the i-th query.
N Q
A_1 A_2 \dots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is given in the following format.
K B_1 B_2 \dots B_K
Output
Output Q lines. The i-th line should contain the answer to the i-th query.
Sample Input 1
6 6 3 2 5 9 1 2 2 4 5 5 1 2 3 4 6 3 2 5 6 4 1 2 5 6 1 5 3 1 2 3
Sample Output 1
2 1 3 5 2 1
For example, in the first query, balls 4 and 5 are removed from the bag. At this point, the following four balls remain in the bag:
- Ball 1 with 3 written on it
- Ball 2 with 2 written on it
- Ball 3 with 5 written on it
- Ball 6 with 2 written on it
Thus, the minimum value among the integers written on the balls in the bag is 2.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
頂点 1,2,\dots,N の N 頂点からなる木が与えられます。この木の辺のうち i 本目は頂点 U_i と頂点 V_i を結びます。
頂点 i には整数 A_i が書かれています。
全ての k=1,2,\dots,N について以下の問題に答えてください。
- 問題: 頂点 1 から頂点 k への単純なパス (同じ頂点を複数回通らないパス) に含まれる頂点について、同じ整数の書かれた異なる 2 頂点の組が存在すれば
Yes、そうでないならNoと答えよ。- なお、木上の 2 つの頂点を結ぶ単純なパスが一意に定まることは証明できる。
制約
- 入力は全て整数
- 2 \le N \le 2 \times 10^5
- 1 \le A_i \le 10^9
- 1 \le U_i, V_i \le N
- 与えられるグラフは木
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 A_2 \dots A_N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
出力
N 行出力せよ。
そのうち i 行目には、 k=i である場合の問題の答えを出力せよ。
入力例 1
5 1 3 2 1 2 1 2 1 3 3 4 3 5
出力例 1
No No No Yes Yes
- k=1 について、頂点 1 から頂点 1 へのパスに含まれるのは頂点 1 です。パスが 1 頂点のみで構成されるので、答えは
Noとなります。 - k=2 について、頂点 1 から頂点 2 へのパスに含まれるのは頂点 1,2 で、それぞれに書かれた整数は 1,3 です。よって、答えは
Noです。 - k=3 について、頂点 1 から頂点 3 へのパスに含まれるのは頂点 1,3 で、それぞれに書かれた整数は 1,2 です。よって、答えは
Noです。 - k=4 について、頂点 1 から頂点 4 へのパスに含まれるのは頂点 1,3,4 で、それぞれに書かれた整数は 1,2,1 です。パス内の頂点 1,4 に同じ整数 1 が書かれているので、答えは
Yesです。 - k=5 について、頂点 1 から頂点 5 へのパスに含まれるのは頂点 1,3,5 で、それぞれに書かれた整数は 1,2,2 です。パス内の頂点 3,5 に同じ整数 2 が書かれているので、答えは
Yesです。
入力例 2
2 1000000000 1000000000 2 1
出力例 2
No Yes
入力例 3
10 10 7 3 9 1 3 8 5 7 10 3 6 8 6 6 1 9 7 7 10 5 4 4 2 10 2 1 9
出力例 3
No Yes Yes Yes Yes No No No No Yes
Score : 400 points
Problem Statement
You are given a tree with N vertices numbered 1,2,\dots,N. The i-th edge connects vertices U_i and V_i.
Each vertex i has an integer A_i written on it.
Answer the following problem for all k=1,2,\dots,N.
- Problem: Among the vertices on the simple path (a path that does not visit the same vertex more than once) from vertex 1 to vertex k, if there exist two distinct vertices with the same integer written on them, output
Yes; otherwise, outputNo.- It can be proved that the simple path connecting two vertices on a tree is unique.
Constraints
- All input values are integers.
- 2 \le N \le 2 \times 10^5
- 1 \le A_i \le 10^9
- 1 \le U_i, V_i \le N
- The given graph is a tree.
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 \dots A_N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
Output
Output N lines.
The i-th line should contain the answer to the problem for k=i.
Sample Input 1
5 1 3 2 1 2 1 2 1 3 3 4 3 5
Sample Output 1
No No No Yes Yes
- For k=1: the path from vertex 1 to vertex 1 contains only vertex 1. Since the path consists of only one vertex, the answer is
No. - For k=2: the path from vertex 1 to vertex 2 contains vertices 1,2, with integers 1,3 written on them respectively. Thus, the answer is
No. - For k=3: the path from vertex 1 to vertex 3 contains vertices 1,3, with integers 1,2 written on them respectively. Thus, the answer is
No. - For k=4: the path from vertex 1 to vertex 4 contains vertices 1,3,4, with integers 1,2,1 written on them respectively. Since vertices 1 and 4 on the path have the same integer 1 written on them, the answer is
Yes. - For k=5: the path from vertex 1 to vertex 5 contains vertices 1,3,5, with integers 1,2,2 written on them respectively. Since vertices 3 and 5 on the path have the same integer 2 written on them, the answer is
Yes.
Sample Input 2
2 1000000000 1000000000 2 1
Sample Output 2
No Yes
Sample Input 3
10 10 7 3 9 1 3 8 5 7 10 3 6 8 6 6 1 9 7 7 10 5 4 4 2 10 2 1 9
Sample Output 3
No Yes Yes Yes Yes No No No No Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
整数 N,M が与えられるので、 \lfloor N/M \rfloor を \color{red}{10007} で割った余りを求めてください。
ここで、 \lfloor x \rfloor は x 以下で最大の整数を表します。 例えば、 \lfloor 3.14 \rfloor=3, \lfloor 10 \rfloor = 10 です。
但し、この問題では N は直接与えられず、ランレングス圧縮された形で与えられます。
具体的には、 N は K 個の「数字 c_i と整数 l_i の組」からなる列で表現されます。
元の N を復元するには、以下の手順を用います。
- 最初、文字列 S を空文字列とする。
- i=1,2,\dots,K について、以下を繰り返す。
- S の末尾に数字 c_i を l_i 個付け加える。
- 最終的な S をひとつの整数として解釈したとき、その整数が N である。
制約
- 1 \le M \le 10^4
- 1 \le K \le 10^5
- c_i は 0,1,2,3,4,5,6,7,8,9 いずれかの数字
- 1 \le l_i \le 10^9
- c_1 \neq 0
- M,K,l_i は整数
入力
入力は以下の形式で標準入力から与えられる。
K M c_1 l_1 c_2 l_2 \vdots c_K l_K
出力
答えを出力せよ。
入力例 1
6 7 3 1 1 1 6 1 2 2 7 2 6 2
出力例 1
3797
この入力では N=316227766,M=7 です。
\lfloor 316227766/7 \rfloor = 45175395 であり、これを 10007 で割った余りである 3797 が最終的な答えとなります。
入力例 2
1 1 1 1
出力例 2
1
入力例 3
10 9999 9 419921892 9 923650333 6 476449815 1 8837775 2 141135534 5 462618481 3 202652735 0 771538044 4 321458589 0 570032864
出力例 3
8437
Score : 450 points
Problem Statement
You are given integers N and M. Find the remainder when \lfloor N/M \rfloor is divided by \color{red}{10007}.
Here, \lfloor x \rfloor denotes the largest integer not exceeding x. For example, \lfloor 3.14 \rfloor=3 and \lfloor 10 \rfloor = 10.
In this problem, N is not given directly; instead, it is given in run-length encoded form.
Specifically, N is represented by a sequence of K pairs of a digit c_i and an integer l_i.
To recover the original N, use the following procedure.
- Initially, let string S be the empty string.
- For i=1,2,\dots,K, repeat the following.
- Append l_i copies of digit c_i to the end of S.
- Interpret the final S as a single integer; that integer is N.
Constraints
- 1 \le M \le 10^4
- 1 \le K \le 10^5
- c_i is one of the digits 0,1,2,3,4,5,6,7,8,9.
- 1 \le l_i \le 10^9
- c_1 \neq 0
- M,K,l_i are integers.
Input
The input is given from Standard Input in the following format:
K M c_1 l_1 c_2 l_2 \vdots c_K l_K
Output
Output the answer.
Sample Input 1
6 7 3 1 1 1 6 1 2 2 7 2 6 2
Sample Output 1
3797
In this input, N=316227766 and M=7.
\lfloor 316227766/7 \rfloor = 45175395, and the remainder when this is divided by 10007 is 3797, which is the final answer.
Sample Input 2
1 1 1 1
Sample Output 2
1
Sample Input 3
10 9999 9 419921892 9 923650333 6 476449815 1 8837775 2 141135534 5 462618481 3 202652735 0 771538044 4 321458589 0 570032864
Sample Output 3
8437
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
2 次元平面上に 1 から N の番号がついた N カ所の地点があります。地点 i は座標 (X_i, Y_i) にあります。
あなたは地点 1 を出発して全ての地点をちょうど 1 回ずつ巡回して再び地点 1 に戻ることにしました。
ここで、地点 i から地点 j へ移動するには \vert X_i - X_j \vert + \vert Y_i - Y_j \vert 秒かかります。
地点 1 を出発してから 10^{10} 秒以内に全ての地点を巡回して再び地点 1 に戻ることができる経路を出力してください。なお、制約下においてこの条件を満たす経路が少なくとも 1 つ存在することが保証されます。
制約
- 1 \leq N \leq 6 \times 10^4
- 0 \leq X_i \leq 2 \times 10^7
- 0 \leq Y_i \leq 2 \times 10^7
- i \neq j ならば (X_i, Y_i) \neq (X_j, Y_j)
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
出力
i (1 \leq i \leq N) 番目に訪問する地点を p_i として以下の形式で答えを出力せよ。
p_1 p_2 \dots p_N
出力された答えは、以下の条件を全て満たす時に正答とみなされる。
- (p_1, p_2, \dots, p_N) は (1, 2, \dots, N) の順列
- p_1 = 1
- d(i, j) を地点 i から地点 j へ移動する時にかかる秒数としたとき、\displaystyle \sum_{i=1}^N d(p_i, p_{(i \bmod N) + 1} ) \leq 10^{10}
答えが複数ある場合はどれを出力しても正解とみなされる。
入力例 1
3 0 6 3 5 2 4
出力例 1
1 3 2
\displaystyle \sum_{i=1}^N d(p_i, p_{(i \bmod N) + 1}) = 4 + 2 + 4 = 10 であるため出力は条件を満たします。
入力例 2
10 9706344 19786176 19341349 15565412 5711023 19068083 12521132 14054301 14767612 17088029 14961700 18526945 13801766 5740101 6581153 8643675 13176196 16586661 4086263 5172719
出力例 2
1 5 2 6 4 7 9 8 3 10
Score : 525 points
Problem Statement
There are N locations numbered 1 to N on a two-dimensional plane. Location i is at coordinates (X_i, Y_i).
You will start from location 1, visit every location exactly once, and return to location 1.
Moving from location i to location j takes \vert X_i - X_j \vert + \vert Y_i - Y_j \vert seconds.
Output a route that visits all locations and returns to location 1 within 10^{10} seconds of starting from location 1. It is guaranteed that at least one such route exists under the given constraints.
Constraints
- 1 \leq N \leq 6 \times 10^4
- 0 \leq X_i \leq 2 \times 10^7
- 0 \leq Y_i \leq 2 \times 10^7
- (X_i, Y_i) \neq (X_j, Y_j) if i \neq j
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
Output
Let p_i be the i-th location visited (1 \leq i \leq N), and output your answer in the following format.
p_1 p_2 \dots p_N
Your answer is considered correct if all of the following conditions are satisfied.
- (p_1, p_2, \dots, p_N) is a permutation of (1, 2, \dots, N).
- p_1 = 1
- Letting d(i, j) be the number of seconds it takes to move from location i to location j, \displaystyle \sum_{i=1}^N d(p_i, p_{(i \bmod N) + 1} ) \leq 10^{10}.
If there are multiple valid answers, any of them will be accepted.
Sample Input 1
3 0 6 3 5 2 4
Sample Output 1
1 3 2
\displaystyle \sum_{i=1}^N d(p_i, p_{(i \bmod N) + 1}) = 4 + 2 + 4 = 10, so the output satisfies the condition.
Sample Input 2
10 9706344 19786176 19341349 15565412 5711023 19068083 12521132 14054301 14767612 17088029 14961700 18526945 13801766 5740101 6581153 8643675 13176196 16586661 4086263 5172719
Sample Output 2
1 5 2 6 4 7 9 8 3 10
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 675 点
問題文
高橋君と青木君はカードゲームをします。ゲームは以下のルールで進行します。
- 高橋君がデッキ A_1, A_2, \dots, A_N を提示する。ここで N \geq 3 である。
- 青木君がデッキ B_1, B_2, B_3 を提示する。
- 高橋君は青木君のデッキのうちいずれか 1 個を選び BAN する。青木君もまた高橋君のデッキのうちいずれか 1 個を選び BAN する。この 2 つの BAN 操作は 同時に 行われる。(つまり、一方が他方の選択を知って行動することはできない。)
- その後、高橋君と青木君は自身が提示したデッキの中から使用するデッキを 同時に 宣言する。ただし BAN されたデッキは宣言できない。
- その後、2 人は宣言したデッキを用いて 1 回だけ戦う。戦いの結果、どちらか一方が勝者となる。
整数 X_{i,j} (1 \leq i \leq N, 1 \leq j \leq 3) が与えられます。p_{i,j} = \dfrac{X_{i,j}}{10^6} とします。p_{i,j} は高橋君がデッキ A_i を、青木君がデッキ B_j を用いて戦った時の高橋君の勝率です。p_{i,j} の値は高橋君も青木君もあらかじめ知っています。
双方が自身の勝率を最大化するように行動した時の高橋君の勝率を求めてください。
T 個のテストケースが与えられるので、それぞれに対して答えを求めてください。
双方が自身の勝率を最大化するように行動した時の高橋君の勝率とは?
各段階でプレイヤーはその時点までに公開されている情報に基づいて可能な選択肢それぞれに合計が 1 となるように確率を割り当て、その確率に従ってランダムに 1 つ選ぶ戦略を取る。プレイヤーは、「勝率としてありうる最小値」、すなわち「相手が自身の勝率を最小化するような戦略を選んだ時の勝率」を最大化する戦略を選ぶ。条件を満たす戦略の選び方が複数ある場合はどれを選んでも良い。
両者がこの方針に従って行動した時、戦略の選び方によらず高橋君の勝率は一意に定まる。この値を求めよ。
制約
- 1 \leq T \leq 500
- 3 \leq N \leq 5 \times 10^4
- 0 \leq X_{i,j} \leq 10^6
- 全てのテストケースに対する N の総和は 5 \times 10^4 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_i は i 番目のテストケースを意味する。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースでは、入力は以下の形式で与えられる。
N
X_{1,1} X_{1,2} X_{1,3}
X_{2,1} X_{2,2} X_{2,3}
\vdots
X_{N,1} X_{N,2} X_{N,3}
出力
T 行出力せよ。i 行目には i 番目のテストケースの答えを出力せよ。
各テストケースでは双方が自身の勝率を最大化するように行動した時の高橋君の勝率を出力せよ。出力された答えは、真の値との絶対誤差または相対誤差が 10^{-6} 以下である時に正答とみなされる。
入力例 1
1 3 600000 500000 100000 400000 700000 300000 800000 200000 900000
出力例 1
0.5272727272727272
双方が自身の勝率を最大化するように行動した時の高橋君の勝率は 0.5272727 \cdots です。
入力例 2
3 6 402522 269943 705465 529104 292257 262398 598965 708517 393767 10680 760196 459175 163269 555933 100458 252724 762224 143581 4 720086 398924 165990 446155 678065 592893 674921 176800 484411 222632 81476 633357 5 212176 543920 484352 23874 760886 95861 291354 504948 790821 523486 327166 289413 621589 178581 628701
出力例 2
0.5136657634439492 0.5120053615937050 0.5396248345565982
入力例 3
6 3 600000 300000 100000 0 400000 600000 700000 400000 100000 3 700000 700000 200000 0 0 900000 500000 600000 900000 5 1000000 658525 1000000 867635 1000000 259903 727305 342912 1000000 995128 432824 1000000 69492 105991 1000000 6 900000 700000 300000 600000 300000 500000 900000 100000 900000 900000 1000000 0 800000 200000 900000 700000 800000 900000 7 300000 900000 800000 600000 1000000 700000 700000 200000 1000000 1000000 200000 200000 200000 400000 1000000 500000 800000 900000 900000 500000 800000 5 500000 500000 41274 270488 520436 605863 247128 522516 149943 502751 491747 516407 503752 488744 31814
出力例 3
0.3520000000000000 0.5571428571428572 0.9951279999999999 0.8478260869565216 0.8336956521739129 0.4988562153245037
Score : 675 points
Problem Statement
Takahashi and Aoki play a card game. The game proceeds according to the following rules.
- Takahashi presents decks A_1, A_2, \dots, A_N. Here N \geq 3.
- Aoki presents decks B_1, B_2, B_3.
- Takahashi chooses one deck from Aoki's decks and bans it. Aoki also chooses one deck from Takahashi's decks and bans it. These two ban operations are performed simultaneously. (That is, neither player can act based on the other's choice.)
- Then, Takahashi and Aoki each simultaneously declare which deck from their own presented decks they will use. However, the banned deck cannot be declared.
- Then, the two players battle exactly once using their declared decks. As a result, one of them wins.
You are given integers X_{i,j} (1 \leq i \leq N, 1 \leq j \leq 3). Let p_{i,j} = \dfrac{X_{i,j}}{10^6}. p_{i,j} is Takahashi's win rate when Takahashi uses deck A_i and Aoki uses deck B_j. Both Takahashi and Aoki know the values of p_{i,j} in advance.
Find Takahashi's win rate when both players act to maximize their own win rate.
T test cases are given; solve each of them.
What is Takahashi's win rate when both players act to maximize their own win rate?
At each stage, each player assigns probabilities summing to 1 to the available choices based on the information revealed up to that point, and randomly selects one choice according to those probabilities.Each player chooses a strategy that maximizes "the minimum possible win rate," that is, "the win rate when the opponent chooses a strategy that minimizes the player's win rate." If there are multiple strategies satisfying this condition, any of them may be chosen.
When both players act according to this policy, Takahashi's win rate is uniquely determined regardless of the specific strategy chosen. Find this value.
Constraints
- 1 \leq T \leq 500
- 3 \leq N \leq 5 \times 10^4
- 0 \leq X_{i,j} \leq 10^6
- The sum of N over all test cases is at most 5 \times 10^4.
- 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
For each test case, the input is given in the following format.
N
X_{1,1} X_{1,2} X_{1,3}
X_{2,1} X_{2,2} X_{2,3}
\vdots
X_{N,1} X_{N,2} X_{N,3}
Output
Output T lines. The i-th line should contain the answer to the i-th test case.
For each test case, output Takahashi's win rate when both players act to maximize their own win rate. The output is considered correct if the absolute or relative error from the true value is at most 10^{-6}.
Sample Input 1
1 3 600000 500000 100000 400000 700000 300000 800000 200000 900000
Sample Output 1
0.5272727272727272
Takahashi's win rate when both players act to maximize their own win rate is 0.5272727 \cdots.
Sample Input 2
3 6 402522 269943 705465 529104 292257 262398 598965 708517 393767 10680 760196 459175 163269 555933 100458 252724 762224 143581 4 720086 398924 165990 446155 678065 592893 674921 176800 484411 222632 81476 633357 5 212176 543920 484352 23874 760886 95861 291354 504948 790821 523486 327166 289413 621589 178581 628701
Sample Output 2
0.5136657634439492 0.5120053615937050 0.5396248345565982
Sample Input 3
6 3 600000 300000 100000 0 400000 600000 700000 400000 100000 3 700000 700000 200000 0 0 900000 500000 600000 900000 5 1000000 658525 1000000 867635 1000000 259903 727305 342912 1000000 995128 432824 1000000 69492 105991 1000000 6 900000 700000 300000 600000 300000 500000 900000 100000 900000 900000 1000000 0 800000 200000 900000 700000 800000 900000 7 300000 900000 800000 600000 1000000 700000 700000 200000 1000000 1000000 200000 200000 200000 400000 1000000 500000 800000 900000 900000 500000 800000 5 500000 500000 41274 270488 520436 605863 247128 522516 149943 502751 491747 516407 503752 488744 31814
Sample Output 3
0.3520000000000000 0.5571428571428572 0.9951279999999999 0.8478260869565216 0.8336956521739129 0.4988562153245037