Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は全 10^9 巻の漫画『すぬけ君』を読むことにしました。
初め、高橋君は『すぬけ君』の単行本を N 冊持っています。i 番目の単行本は a_i 巻です。
高橋君は『すぬけ君』を読み始める前に限り次の操作を 0 回以上何度でも繰り返せます。
- 現在持っている単行本が 1 冊以下の場合、何もしない。そうでない場合、現在持っている単行本から 2 冊を選んで売り、代わりに好きな巻を選んで 1 冊買う
その後、高橋君は『すぬけ君』を 1 巻、2 巻、3 巻、\ldots と順番に読みます。ただし、次に読むべき巻を持っていない状態になった場合、(他の巻を持っているかどうかに関わらず)その時点で『すぬけ君』を読むのをやめます。
高橋君が『すぬけ君』を最大で何巻まで読めるかを求めてください。ただし、1 巻を読めない場合の答えは 0 とします。
制約
- 1 \leq N \leq 3 \times 10^5
- 1 \leq a_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N a_1 \ldots a_N
出力
答えを出力せよ。
入力例 1
6 1 2 4 6 7 271
出力例 1
4
『すぬけ君』を読み始める前に「7 巻と 271 巻を選んで売り、代わりに 3 巻を選んで 1 冊買う」という内容で操作をすると、高橋君は 1 巻、2 巻、3 巻、4 巻、6 巻を 1 冊ずつ持っている状態になります。
この状態から『すぬけ君』を読み始めると、高橋君は 1 巻、2 巻、3 巻、4 巻を読み、続いて 5 巻を読もうとしますが持っていないためこの時点で『すぬけ君』を読むのをやめます。
操作の方法を変えても 5 巻を読むことは出来ないため、4 が答えです。
入力例 2
10 1 1 1 1 1 1 1 1 1 1
出力例 2
5
高橋君は同じ巻を 2 冊以上持っているかもしれません。
この入力に対しては以下のように 4 回操作をしてから『すぬけ君』を読み始めることで 5 巻まで読むことが出来、これが最大です。
- 1 巻を 2 冊選んで売り、代わりに 2 巻を選んで 1 冊買う
- 1 巻を 2 冊選んで売り、代わりに 3 巻を選んで 1 冊買う
- 1 巻を 2 冊選んで売り、代わりに 4 巻を選んで 1 冊買う
- 1 巻を 2 冊選んで売り、代わりに 5 巻を選んで 1 冊買う
入力例 3
1 5
出力例 3
0
高橋君は 1 巻を読めません。
Score : 300 points
Problem Statement
Takahashi is going to read a manga series "Snuke-kun" in 10^9 volumes.
Initially, Takahashi has N books of this series. The i-th book is Volume a_i.
Takahashi may repeat the following operation any number of (possibly zero) times only before he begins to read:
- Do nothing if he has 1 or less books; otherwise, sell two of the books he has and buy one book of any volume instead.
Then, Takahashi reads Volume 1, Volume 2, Volume 3, \ldots, in order. However, when he does not have a book of the next volume to read, he stops reading the series (regardless of the other volumes he has).
Find the latest volume of the series that he can read up to. If he cannot read any, let the answer be 0.
Constraints
- 1 \leq N \leq 3 \times 10^5
- 1 \leq a_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N a_1 \ldots a_N
Output
Print the answer.
Sample Input 1
6 1 2 4 6 7 271
Sample Output 1
4
Before he begins to read the series, he may do the following operation: "sell books of Volumes 7 and 271 and buy one book of Volume 3 instead." Then, he has one book each of Volumes 1, 2, 3, 4, and 6.
If he then begins to read the series, he reads Volumes 1, 2, 3, and 4, then tries to read Volume 5. However, he does not have one, so he stops reading at this point.
Regardless of the choices in the operation, he cannot read Volume 5, so the answer is 4.
Sample Input 2
10 1 1 1 1 1 1 1 1 1 1
Sample Output 2
5
Takahashi may have multiple books of the same volume.
For this input, if he performs the following 4 operations before he begins to read the series, he can read up to Volume 5, which is the maximum:
- Sell two books of Volume 1 and buy a book of Volume 2 instead.
- Sell two books of Volume 1 and buy a book of Volume 3 instead.
- Sell two books of Volume 1 and buy a book of Volume 4 instead.
- Sell two books of Volume 1 and buy a book of Volume 5 instead.
Sample Input 3
1 5
Sample Output 3
0
Takahashi cannot read Volume 1.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は武術家です。 武術家の覚えられる技は N 個あり、技 1, 2, \ldots, N と名前がついています。 1 \leq i \leq N について、技 i を習得するには時間 T_i の修練が必要で、 さらに、修練の開始時点で技 A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} をすでに習得している必要があります。 ここで、1 \leq j \leq K_i について、A_{i,j} < i であることが保証されます。
高橋君は時刻 0 の時点で技を 1 つも覚えていません。 高橋君は同時に 1 つの技に対する修練しかできず、また、一度始めた修練を途中でやめることもできません。 高橋君が技 N を習得するのに必要な時間の最小値を求めてください。
制約
- 1 \leq N \leq 2\times 10^5
- 1 \leq T_i \leq 10^9
- 0 \leq K_i < i
- 1 \leq A_{i,j} < i
- \sum_{i=1}^N K_i \leq 2\times 10^5
- A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} はすべて異なる。
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N
T_1 K_1 A_{1,1} A_{1,2} \ldots A_{1,K_1}
T_2 K_2 A_{2,1} A_{2,2} \ldots A_{2,K_2}
\vdots
T_N K_N A_{N,1} A_{N,2} \ldots A_{N,K_N}
出力
技 N を習得するのに必要な時間の最小値を出力せよ。
入力例 1
3 3 0 5 1 1 7 1 1
出力例 1
10
例えば高橋君は次のように行動することができます。
- 高橋君は時刻 0 に技 1 の修練を開始し、時刻 3 に技 1 を習得します。
- その後、時刻 3 に技 3 の修練を開始し、時刻 10 に技 3 を習得します。
このときが最短で、高橋君が技 3 を習得するのに必要な時間は 3+7=10 となります。 技 3 の習得のためには、技 2 を習得する必要はありません。
入力例 2
5 1000000000 0 1000000000 0 1000000000 0 1000000000 0 1000000000 4 1 2 3 4
出力例 2
5000000000
答えが 32 bit 整数に収まらないことがある事に注意してください。
Score : 300 points
Problem Statement
Takahashi is a martial artist. There are N moves that a martial artist can learn, called Move 1, 2, \ldots, N. For each 1 \leq i \leq N, it takes T_i minutes of practice to learn Move i. Additionally, at the beginning of that practice, all of the Moves A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} must be already learned. Here, it is guaranteed that A_{i,j} < i for each 1 \leq j \leq K_i.
Takahashi has not learned any move at time 0. He cannot practice for more than one move simultaneously, nor can he stop a practice he has already started. Find the minimum number of minutes needed for Takahashi to learn Move N.
Constraints
- 1 \leq N \leq 2\times 10^5
- 1 \leq T_i \leq 10^9
- 0 \leq K_i < i
- 1 \leq A_{i,j} < i
- \sum_{i=1}^N K_i \leq 2\times 10^5
- A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} are all distinct.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
T_1 K_1 A_{1,1} A_{1,2} \ldots A_{1,K_1}
T_2 K_2 A_{2,1} A_{2,2} \ldots A_{2,K_2}
\vdots
T_N K_N A_{N,1} A_{N,2} \ldots A_{N,K_N}
Output
Print the minimum number of minutes needed for Takahashi to learn Move N.
Sample Input 1
3 3 0 5 1 1 7 1 1
Sample Output 1
10
Here is one possible plan for Takahashi.
- At time 0, start practicing for Move 1 to learn Move 1 at time 3.
- Then, at time 3, start practicing for Move 3 to learn Move 3 at time 10.
Here, Takahashi spends 3+7=10 minutes to learn Move 3, which is the fastest possible. Note that he does not need to learn Move 2 to learn Move 3.
Sample Input 2
5 1000000000 0 1000000000 0 1000000000 0 1000000000 0 1000000000 4 1 2 3 4
Sample Output 2
5000000000
Note that the answer may not fit into a 32-bit integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
1 台のサーバーと N 台の PC があります。 サーバーおよび各 PC はそれぞれ 1 つずつ文字列を保持しており、最初は全て空文字列です。
Q 個のクエリが与えられます。各クエリは以下のいずれかの形式です。
1 p:PC p の文字列をサーバーの文字列で置き換える。2 p s:PC p の文字列の末尾に文字列 s を追加する。3 p:サーバーの文字列をPC p の文字列で置き換える。
全てのクエリを与えられた順に処理したときの最終的なサーバーの文字列を求めてください。
制約
- N,Q は整数
- 1\leq N,Q \leq 2\times 10^5
- 全てのクエリについて、p は整数であり、1 \leq p\leq N
- 全ての 2 種類目のクエリについて、s は英小文字からなる長さ 1 以上の文字列
- 全ての 2 種類目のクエリに対する s の長さの総和は 10^6 以下
入力
入力は以下の形式で標準入力から与えられる。
N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
ここで \mathrm{query}_i は i 番目のクエリを表し、以下のいずれかの形式で与えられる。
1 p
2 p s
3 p
出力
答えを出力せよ。
入力例 1
2 6 2 1 at 3 1 2 2 on 1 2 2 2 coder 3 2
出力例 1
atcoder
- 最初、サーバーおよび PC 1,2 の文字列は全て空である。
- 1 番目のクエリ: PC 1 の文字列の末尾に
atを追加する。このとき、サーバー、PC 1,2 の文字列はそれぞれ空、at、空である。 - 2 番目のクエリ: サーバーの文字列を PC 1 の文字列で置き換える。このとき、サーバー、PC 1,2 の文字列はそれぞれ
at、at、空である。 - 3 番目のクエリ: PC 2 の文字列の末尾に
onを追加する。このとき、サーバー、PC 1,2 の文字列はそれぞれat、at、onである。 - 4 番目のクエリ: PC 2 の文字列をサーバーの文字列で置き換える。このとき、サーバー、PC 1,2 の文字列はそれぞれ
at、at、atである。 - 5 番目のクエリ: PC 2 の文字列の末尾に
coderを追加する。このとき、サーバー、PC 1,2 の文字列はそれぞれat、at、atcoderである。 - 6 番目のクエリ: サーバーの文字列を PC 2 の文字列で置き換える。このとき、サーバー、PC 1,2 の文字列はそれぞれ
atcoder、at、atcoderである。
よって、最終的なサーバーの文字列は atcoder です。
入力例 2
100000 3 1 100 2 300 abc 3 200
出力例 2
最終的なサーバーの文字列は空です。
入力例 3
10 10 2 7 ladxf 2 7 zz 2 7 kfm 3 7 1 5 2 5 irur 3 5 1 6 2 6 ptilun 3 6
出力例 3
ladxfzzkfmirurptilun
Score : 425 points
Problem Statement
There is one server and N PCs. The server and each PC each hold one string, and initially all strings are empty.
Q queries are given. Each query is in one of the following formats:
1 p: Replace the string of PC p with the string of the server.2 p s: Append string s to the end of the string of PC p.3 p: Replace the string of the server with the string of PC p.
Find the final string of the server after processing all queries in the given order.
Constraints
- N,Q are integers
- 1\leq N,Q \leq 2\times 10^5
- For every query, p is an integer and 1 \leq p\leq N.
- For every query of type 2, s is a string of length at least 1 consisting of lowercase English letters.
- The sum of the lengths of s over all queries of type 2 is at most 10^6.
Input
The input is given from Standard Input in the following format:
N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Here, \mathrm{query}_i represents the i-th query and is given in one of the following formats:
1 p
2 p s
3 p
Output
Output the answer.
Sample Input 1
2 6 2 1 at 3 1 2 2 on 1 2 2 2 coder 3 2
Sample Output 1
atcoder
- Initially, the strings of the server and PCs 1,2 are all empty.
- 1st query: Append
atto the end of the string of PC 1. At this time, the strings of the server, PC 1,2 are empty,at, empty, respectively. - 2nd query: Replace the string of the server with the string of PC 1. At this time, the strings of the server, PC 1,2 are
at,at, empty, respectively. - 3rd query: Append
onto the end of the string of PC 2. At this time, the strings of the server, PC 1,2 areat,at,on, respectively. - 4th query: Replace the string of PC 2 with the string of the server. At this time, the strings of the server, PC 1,2 are
at,at,at, respectively. - 5th query: Append
coderto the end of the string of PC 2. At this time, the strings of the server, PC 1,2 areat,at,atcoder, respectively. - 6th query: Replace the string of the server with the string of PC 2. At this time, the strings of the server, PC 1,2 are
atcoder,at,atcoder, respectively.
Thus, the final string of the server is atcoder.
Sample Input 2
100000 3 1 100 2 300 abc 3 200
Sample Output 2
The final string of the server is empty.
Sample Input 3
10 10 2 7 ladxf 2 7 zz 2 7 kfm 3 7 1 5 2 5 irur 3 5 1 6 2 6 ptilun 3 6
Sample Output 3
ladxfzzkfmirurptilun
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
配点 : 550 点
問題文
xy 平面として表される町があり、サンタさんと、1 から N までの番号が付けられた N 人の子供が住んでいます。 サンタさんの家の座標は (S_X,S_Y) であり、子供 i\ (1\leq i\leq N) の家の座標は (X_i,Y_i) です。
サンタさんは、N 人の子供たちに番号順にプレゼントを 1 個ずつ配りたいです。 子供 i にプレゼントを配るためには、プレゼントを 1 個以上持った状態で子供 i の家に行く必要があります。 しかし、サンタさんは同時に K 個までしかプレゼントを持つことができず、プレゼントを補充するためには一旦自分の家に戻る必要があります(サンタさんの家には十分な数のプレゼントがあります)。
サンタさんが自分の家を出発し、N 人の子供たち全員にプレゼントを配って、自分の家まで戻ってくるために移動する距離の最小値を求めてください。
制約
- 1\leq K\leq N \leq 2\times 10^5
- -10^9\leq S_X,S_Y,X_i,Y_i \leq 10^9
- (S_X,S_Y)\neq (X_i,Y_i)
- (X_i,Y_i)\neq (X_j,Y_j)\ (i\neq j)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K S_X S_Y X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
出力
サンタさんが移動する距離の最小値を出力せよ。 出力は、真の値との絶対誤差または相対誤差が 10^{−6} 以下のとき正解と判定される。
入力例 1
3 2 1 1 3 1 1 2 3 2
出力例 1
9.236067977499790

上図において、赤い丸はサンタさんの家、中に数字が書かれた丸はその番号の子供の家を表しています。
サンタさんが以下のように行動することを考えます。
- プレゼントを 2 個持ってサンタさんの家を出る。
- 子供 1 の家に行ってプレゼントを 1 個配る。
- サンタさんの家に戻ってプレゼントを 1 個補充する。
- 子供 2 の家に行ってプレゼントを 1 個配る。
- 子供 3 の家に行ってプレゼントを 1 個配る。
- サンタさんの家に戻る。
このとき、サンタさんが移動する距離は 2+2+1+2+\sqrt{5}=7+\sqrt{5}=9.236\dots となり、これが最小です。
入力例 2
2 1 0 1 -1 1 1 1
出力例 2
4.000000000000000
入力例 3
8 3 735867677 193944314 586260100 -192321079 95834122 802780784 418379342 -790013317 -445130206 189801569 -354684803 -49687658 -204491568 -840249197 853829789 470958158 -751917965 762048217
出力例 3
11347715738.116592407226562
Score : 550 points
Problem Statement
There is a town represented as an xy-plane, where Santa lives, along with N children numbered 1 to N. Santa's house is at coordinates (S_X,S_Y), and the house of child i\ (1\leq i\leq N) is at (X_i,Y_i).
Santa wants to deliver one present to each of the N children in numerical order. To deliver a present to child i, Santa must visit the house of child i with at least one present in hand. However, Santa can only carry up to K presents at a time, and he must return to his own house to replenish presents (there are enough presents at Santa's house).
Find the minimum distance Santa must travel to leave his house, deliver presents to all N children, and return to his house.
Constraints
- 1\leq K\leq N \leq 2\times 10^5
- -10^9\leq S_X,S_Y,X_i,Y_i \leq 10^9
- (S_X,S_Y)\neq (X_i,Y_i)
- (X_i,Y_i)\neq (X_j,Y_j)\ (i\neq j)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K S_X S_Y X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
Output
Print the minimum distance Santa must travel. The output will be considered correct if the absolute or relative error from the true value is at most 10^{−6}.
Sample Input 1
3 2 1 1 3 1 1 2 3 2
Sample Output 1
9.236067977499790

In the figure above, the red circle represents Santa's house, and the circles with numbers represent the houses of the children with those numbers.
Consider Santa acting as follows:
- Leave his house with two presents.
- Go to child 1's house and deliver a present.
- Return to his house and replenish one present.
- Go to child 2's house and deliver a present.
- Go to child 3's house and deliver a present.
- Return to his house.
In this case, Santa travels the distance of 2+2+1+2+\sqrt{5}=7+\sqrt{5}=9.236\dots, which is the minimum.
Sample Input 2
2 1 0 1 -1 1 1 1
Sample Output 2
4.000000000000000
Sample Input 3
8 3 735867677 193944314 586260100 -192321079 95834122 802780784 418379342 -790013317 -445130206 189801569 -354684803 -49687658 -204491568 -840249197 853829789 470958158 -751917965 762048217
Sample Output 3
11347715738.116592407226562