実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
文字列が N 個与えられます。
i 番目 (1\leq i\leq N) に与えられる文字列 S _ i は Takahashi か Aoki のどちらかと等しいです。
S _ i が Takahashi と等しい i がいくつあるか求めてください。
制約
- 1\leq N\leq 100
- N は整数
- S _ i は
TakahashiかAokiのいずれか (1\leq i\leq N)
入力
入力は以下の形式で標準入力から与えられる。
N S _ 1 S _ 2 \vdots S _ N
出力
S _ i が Takahashi と等しい i の個数を整数として一行に出力せよ。
入力例 1
3 Aoki Takahashi Takahashi
出力例 1
2
S _ 2,S _ 3 の 2 つが Takahashi と等しく、S _ 1 はそうではありません。
よって、2 を出力してください。
入力例 2
2 Aoki Aoki
出力例 2
0
Takahashi と等しい S _ i が存在しないこともあります。
入力例 3
20 Aoki Takahashi Takahashi Aoki Aoki Aoki Aoki Takahashi Aoki Aoki Aoki Takahashi Takahashi Aoki Takahashi Aoki Aoki Aoki Aoki Takahashi
出力例 3
7
Score : 100 points
Problem Statement
You are given N strings.
The i-th string S_i (1 \leq i \leq N) is either Takahashi or Aoki.
How many i are there such that S_i is equal to Takahashi?
Constraints
- 1 \leq N \leq 100
- N is an integer.
- Each S_i is
TakahashiorAoki. (1 \leq i \leq N)
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print the count of i such that S_i is equal to Takahashi as an integer in a single line.
Sample Input 1
3 Aoki Takahashi Takahashi
Sample Output 1
2
S_2 and S_3 are equal to Takahashi, while S_1 is not.
Therefore, print 2.
Sample Input 2
2 Aoki Aoki
Sample Output 2
0
It is possible that no S_i is equal to Takahashi.
Sample Input 3
20 Aoki Takahashi Takahashi Aoki Aoki Aoki Aoki Takahashi Aoki Aoki Aoki Takahashi Takahashi Aoki Takahashi Aoki Aoki Aoki Aoki Takahashi
Sample Output 3
7
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
4 枚のカードがあり、それぞれのカードには整数 A,B,C,D が書かれています。
ここに 1 枚カードを加え、フルハウスとできるか判定してください。
ただし、 5 枚組のカードは以下の条件を満たすとき、またそのときに限って、フルハウスであると呼ばれます。
- 異なる整数 x,y について、 x が書かれたカード 3 枚と y が書かれたカード 2 枚からなる。
制約
- 入力は全て整数
- 1 \le A,B,C,D \le 13
入力
入力は以下の形式で標準入力から与えられる。
A B C D
出力
1 枚カードを加えてフルハウスとできる場合は Yes 、そうでないときは No と出力せよ。
入力例 1
7 7 7 1
出力例 1
Yes
7,7,7,1 に 1 を加えた時、フルハウスとなります。
入力例 2
13 12 11 10
出力例 2
No
13,12,11,10 に何を加えてもフルハウスにはなりません。
入力例 3
3 3 5 5
出力例 3
Yes
3,3,5,5 に 3 を加えた時、フルハウスとなります。
また、 5 を加えてもフルハウスとなります。
入力例 4
8 8 8 8
出力例 4
No
8,8,8,8 に何を加えてもフルハウスにはなりません。
同じ 5 枚のカードはフルハウスではないことに注意してください。
入力例 5
1 3 4 1
出力例 5
No
Score : 100 points
Problem Statement
There are four cards with integers A,B,C,D written on them.
Determine whether a Full House can be formed by adding one card.
A set of five cards is called a Full House if and only if the following condition is satisfied:
- For two distinct integers x and y, there are three cards with x written on them and two cards with y written on them.
Constraints
- All input values are integers.
- 1 \le A,B,C,D \le 13
Input
The input is given from Standard Input in the following format:
A B C D
Output
If adding one card can form a Full House, print Yes; otherwise, print No.
Sample Input 1
7 7 7 1
Sample Output 1
Yes
Adding 1 to 7,7,7,1 forms a Full House.
Sample Input 2
13 12 11 10
Sample Output 2
No
Adding anything to 13,12,11,10 does not form a Full House.
Sample Input 3
3 3 5 5
Sample Output 3
Yes
Adding 3,3,5,5 to 3 forms a Full House.
Also, adding 5 forms a Full House.
Sample Input 4
8 8 8 8
Sample Output 4
No
Adding anything to 8,8,8,8 does not form a Full House.
Note that five identical cards do not form a Full House.
Sample Input 5
1 3 4 1
Sample Output 5
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
英小文字からなる文字列 S が与えられます。S の空でない部分文字列は何種類ありますか?
ただし、部分文字列とは連続する部分列のことを指します。例えば、xxx は yxxxy の部分文字列ですが、xxyxx の部分文字列ではありません。
制約
- S は英小文字からなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
yay
出力例 1
5
S の空でない部分文字列は以下の 5 種類です。
ayayyayay
入力例 2
aababc
出力例 2
17
入力例 3
abracadabra
出力例 3
54
Score: 200 points
Problem Statement
You are given a string S consisting of lowercase English letters. How many different non-empty substrings does S have?
A substring is a contiguous subsequence. For example, xxx is a substring of yxxxy but not of xxyxx.
Constraints
- S is a string of length between 1 and 100, inclusive, consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
yay
Sample Output 1
5
S has the following five different non-empty substrings:
ayayyayay
Sample Input 2
aababc
Sample Output 2
17
Sample Input 3
abracadabra
Sample Output 3
54
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
非負整数 X に対し、 i=0,1,\dots,K-1 の順に次の操作を行ったとき、操作を全て終えた時点での X を求めてください。
- X の 10^i の位以下を四捨五入する。
- 厳密には、 X を「 |Y-X| が最小となる 10^{i+1} の倍数のうち最大のもの」である Y に置き換える。
- 具体例を挙げる。
- 273 の 10^1 の位以下を四捨五入すれば 300 となる。
- 999 の 10^2 の位以下を四捨五入すれば 1000 となる。
- 100 の 10^9 の位以下を四捨五入すれば 0 となる。
- 1015 の 10^0 の位以下を四捨五入すれば 1020 となる。
制約
- X,K は整数
- 0 \le X < 10^{15}
- 1 \le K \le 15
入力
入力は以下の形式で標準入力から与えられる。
X K
出力
答えを整数として出力せよ。
入力例 1
2048 2
出力例 1
2100
操作の過程で、 X は 2048 \rightarrow 2050 \rightarrow 2100 と変化します。
入力例 2
1 15
出力例 2
0
入力例 3
999 3
出力例 3
1000
入力例 4
314159265358979 12
出力例 4
314000000000000
X は 32bit 整数型に収まらない可能性があります。
Score : 200 points
Problem Statement
Given a non-negative integer X, perform the following operation for i=1,2,\dots,K in this order and find the resulting X.
- Round X off to the nearest 10^i.
- Formally, replace X with Y that is "the largest multiple of 10^i that minimizes |Y-X|."
- Here are some examples:
- Rounding 273 off to the nearest 10^2 yields 300.
- Rounding 999 off to the nearest 10^3 yields 1000.
- Rounding 100 off to the nearest 10^{10} yields 0.
- Rounding 1015 off to the nearest 10^1 yields 1020.
Constraints
- X and K are integers.
- 0 \le X < 10^{15}
- 1 \le K \le 15
Input
The input is given from Standard Input in the following format:
X K
Output
Print the answer as an integer.
Sample Input 1
2048 2
Sample Output 1
2100
X changes as 2048 \rightarrow 2050 \rightarrow 2100 by the operations.
Sample Input 2
1 15
Sample Output 2
0
Sample Input 3
999 3
Sample Output 3
1000
Sample Input 4
314159265358979 12
Sample Output 4
314000000000000
X may not fit into a 32-bit integer type.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
1 から N の番号が付いた N 人がコイントスを何回かしました。人 i は A_i 回表を出し、B_i 回裏を出したこと分かっています。
人 i のコイントスの 成功率 は \displaystyle\frac{A_i}{A_i+B_i} で定義されます。人 1,\ldots,N の番号を、成功率の高い順に並び替えてください。成功率が同じ人が複数いる場合、その中では人の番号が小さい順になるように並び替えてください。
制約
- 2\leq N \leq 2\times 10^5
- 0\leq A_i, B_i\leq 10^9
- A_i+B_i \geq 1
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 \vdots A_N B_N
出力
人 1,\ldots,N の番号を成功率の高い順に空白区切りで出力せよ。成功率が同じ人の番号は昇順に並び替えて出力せよ。
入力例 1
3 1 3 3 1 2 2
出力例 1
2 3 1
人 1 の成功率は 0.25、人 2 の成功率は 0.75、人 3 の成功率は 0.5 です。
成功率の高い順に並び替えると出力例の順番になります。
入力例 2
2 1 3 2 6
出力例 2
1 2
人 1,2 は成功率が同じなので、番号の昇順に出力することに注意してください。
入力例 3
4 999999999 1000000000 333333333 999999999 1000000000 999999997 999999998 1000000000
出力例 3
3 1 4 2
Score : 300 points
Problem Statement
N people numbered 1 through N tossed a coin several times. We know that person i's tosses resulted in A_i heads and B_i tails.
Person i's success rate of the tosses is defined by \displaystyle\frac{A_i}{A_i+B_i}. Sort people 1,\ldots,N in descending order of their success rates, with ties broken in ascending order of their assigned numbers.
Constraints
- 2\leq N \leq 2\times 10^5
- 0\leq A_i, B_i\leq 10^9
- A_i+B_i \geq 1
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 B_1 \vdots A_N B_N
Output
Print the numbers of people 1,\ldots,N in descending order of their success rates, with ties broken in ascending order of their assigned numbers.
Sample Input 1
3 1 3 3 1 2 2
Sample Output 1
2 3 1
Person 1's success rate is 0.25, person 2's is 0.75, and person 3's is 0.5.
Sort them in descending order of their success rates to obtain the order in Sample Output.
Sample Input 2
2 1 3 2 6
Sample Output 2
1 2
Note that person 1 and 2 should be printed in ascending order of their numbers, as they have the same success rates.
Sample Input 3
4 999999999 1000000000 333333333 999999999 1000000000 999999997 999999998 1000000000
Sample Output 3
3 1 4 2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
(1,2,\ldots,N) の並び替えである数列 A=(A_1,\ldots,A_N) が与えられます。
次の操作を 0 回以上 N-1 回以下行うことで、A を (1,2,\ldots,N) にしてください。
- 操作:1\leq i < j \leq N を満たす整数の組 (i,j) を自由に選ぶ。A の i 番目と j 番目の要素を入れ替える。
なお、制約の条件下で必ず A を (1,2,\ldots,N) にできることが証明できます。
制約
- 2 \leq N \leq 2\times 10^5
- (A_1,\ldots,A_N) は (1,2,\ldots,N) の並び替えである
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N A_1 \ldots A_N
出力
操作回数を K として、K+1 行出力せよ。
1 行目には K を出力せよ。
l+1 行目 (1\leq l \leq K) には l 回目の操作で選ぶ整数 i,j を空白区切りで出力せよ。
問題文中の条件を満たすどのような出力も正解とみなされる。
入力例 1
5 3 4 1 2 5
出力例 1
2 1 3 2 4
操作により数列は次のように変化します。
- 最初 A=(3,4,1,2,5) である。
- 1 回目の操作で 1 番目の要素と 3 番目の要素を入れ替える。A=(1,4,3,2,5) になる。
- 2 回目の操作で 2 番目の要素と 4 番目の要素を入れ替える。A=(1,2,3,4,5) になる。
この他、次のような出力でも正解とみなされます。
4 2 3 3 4 1 2 2 3
入力例 2
4 1 2 3 4
出力例 2
0
入力例 3
3 3 1 2
出力例 3
2 1 2 2 3
Score: 300 points
Problem Statement
You are given a permutation A=(A_1,\ldots,A_N) of (1,2,\ldots,N).
Transform A into (1,2,\ldots,N) by performing the following operation between 0 and N-1 times, inclusive:
- Operation: Choose any pair of integers (i,j) such that 1\leq i < j \leq N. Swap the elements at the i-th and j-th positions of A.
It can be proved that under the given constraints, it is always possible to transform A into (1,2,\ldots,N).
Constraints
- 2 \leq N \leq 2\times 10^5
- (A_1,\ldots,A_N) is a permutation of (1,2,\ldots,N).
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 \ldots A_N
Output
Let K be the number of operations. Print K+1 lines.
The first line should contain K.
The (l+1)-th line (1\leq l \leq K) should contain the integers i and j chosen for the l-th operation, separated by a space.
Any output that satisfies the conditions in the problem statement will be considered correct.
Sample Input 1
5 3 4 1 2 5
Sample Output 1
2 1 3 2 4
The operations change the sequence as follows:
- Initially, A=(3,4,1,2,5).
- The first operation swaps the first and third elements, making A=(1,4,3,2,5).
- The second operation swaps the second and fourth elements, making A=(1,2,3,4,5).
Other outputs such as the following are also considered correct:
4 2 3 3 4 1 2 2 3
Sample Input 2
4 1 2 3 4
Sample Output 2
0
Sample Input 3
3 3 1 2
Sample Output 3
2 1 2 2 3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
2次元平面上の N 点 (X_1,Y_1),\ldots,(X_N,Y_N) に家が建っています。
最初、点 (S_x,S_y) にサンタクロースがいます。サンタクロースは列 (D_1,C_1),\ldots,(D_M,C_M) に従って以下の行動を行います。
- i=1,2,\ldots,M の順に以下のように移動する。
- 現在サンタクロースがいる点を (x,y) とする。
- D_i が
Uなら、(x,y) から (x,y+C_i) に直線で移動する。 - D_i が
Dなら、(x,y) から (x,y-C_i) に直線で移動する。 - D_i が
Lなら、(x,y) から (x-C_i,y) に直線で移動する。 - D_i が
Rなら、(x,y) から (x+C_i,y) に直線で移動する。
- D_i が
- 現在サンタクロースがいる点を (x,y) とする。
行動を終えたあとにサンタクロースがいる点と、行動により通過または到達した家の数を求めてください。ただし、同じ家を複数回通過または到達してもそれらは重複して数えません。
制約
- 1 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- -10^9 \leq X_i,Y_i \leq 10^9
- (X_i,Y_i) は相異なる
- -10^9 \leq S_x,S_y \leq 10^9
- (S_x,S_y) に家は建っていない
- D_i は
U,D,L,Rのいずれかである - 1 \leq C_i \leq 10^9
- 与えられる数値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M S_x S_y X_1 Y_1 \vdots X_N Y_N D_1 C_1 \vdots D_M C_M
出力
行動を終えたあとサンタクロースがいる点を (X,Y)、行動により通過または到達した家の数を C とするとき、X,Y,C をこの順に空白区切りで出力せよ。
入力例 1
3 4 3 2 2 2 3 3 2 1 L 2 D 1 R 1 U 2
出力例 1
2 3 2
サンタクロースは以下のように行動します。

- D_1=
Lなので (3,2) から (3-2,2) に直線で移動する。このとき (2,2) に建っている家を通過する。 - D_2=
Dなので (1,2) から (1,2-1) に直線で移動する。 - D_3=
Rなので (1,1) から (1+1,1) に直線で移動する。このとき (2,1) に建っている家を通過する。 - D_4=
Uなので (2,1) から (2,1+2) に直線で移動する。このとき (2,2) に建っている家を通過するが、この家はすでに通過したことがある家である。
行動により通過または到達した家の数は 2 です。
入力例 2
1 3 0 0 1 1 R 1000000000 R 1000000000 R 1000000000
出力例 2
3000000000 0 0
オーバーフローに注意してください。
Score : 425 points
Problem Statement
There are N houses at points (X_1,Y_1),\ldots,(X_N,Y_N) on a two-dimensional plane.
Initially, Santa Claus is at point (S_x,S_y). He will act according to the sequence (D_1,C_1),\ldots,(D_M,C_M) as follows:
- For i=1,2,\ldots,M in order, he moves as follows:
- Let (x,y) be the point where he currently is.
- If D_i is
U, move in a straight line from (x,y) to (x,y+C_i). - If D_i is
D, move in a straight line from (x,y) to (x,y-C_i). - If D_i is
L, move in a straight line from (x,y) to (x-C_i,y). - If D_i is
R, move in a straight line from (x,y) to (x+C_i,y).
- If D_i is
- Let (x,y) be the point where he currently is.
Find the point where he is after completing all actions, and the number of distinct houses he passed through or arrived at during his actions. If the same house is passed multiple times, it is only counted once.
Constraints
- 1 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- -10^9 \leq X_i,Y_i \leq 10^9
- The pairs (X_i,Y_i) are distinct.
- -10^9 \leq S_x,S_y \leq 10^9
- There is no house at (S_x,S_y).
- Each D_i is one of
U,D,L,R. - 1 \leq C_i \leq 10^9
- All input numbers are integers.
Input
The input is given from Standard Input in the following format:
N M S_x S_y X_1 Y_1 \vdots X_N Y_N D_1 C_1 \vdots D_M C_M
Output
Let (X,Y) be the point where he is after completing all actions, and C be the number of distinct houses passed through or arrived at. Print X,Y,C in this order separated by spaces.
Sample Input 1
3 4 3 2 2 2 3 3 2 1 L 2 D 1 R 1 U 2
Sample Output 1
2 3 2
Santa Claus behaves as follows:

- D_1=
L, so he moves from (3,2) to (3-2,2) in a straight line. During this, he passes through the house at (2,2). - D_2=
D, so he moves from (1,2) to (1,2-1) in a straight line. - D_3=
R, so he moves from (1,1) to (1+1,1) in a straight line. During this, he passes through the house at (2,1). - D_4=
U, so he moves from (2,1) to (2,1+2) in a straight line. During this, he passes through the house at (2,2), but it has already been passed.
The number of houses he passed or arrived during his actions is 2.
Sample Input 2
1 3 0 0 1 1 R 1000000000 R 1000000000 R 1000000000
Sample Output 2
3000000000 0 0
Be careful with overflow.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
2 次元平面上に N 個の街と M 個の宝箱があります。街 i は座標 (X_i,Y_i) に、宝箱 i は座標 (P_i,Q_i) にあります。
高橋君は原点を出発し、N 個の街全てを訪れたのち原点に戻る旅行をしようと考えています。
宝箱を訪れる必要はありませんが、宝箱の中にはそれぞれブースターが 1 つあり、ブースターを拾うごとに移動速度が 2 倍になります。
高橋君の最初の移動速度が単位時間あたり 1 であるとき、旅行にかかる時間の最小値を求めてください。
制約
- 1 \leq N \leq 12
- 0 \leq M \leq 5
- -10^9 \leq X_i,Y_i,P_i,Q_i \leq 10^9
- (0,0),(X_i,Y_i),(P_i,Q_i) は相異なる
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M X_1 Y_1 \vdots X_N Y_N P_1 Q_1 \vdots P_M Q_M
出力
答えを出力せよ。なお、想定解答との絶対誤差または相対誤差が 10^{-6} 以下であれば正解として扱われる。
入力例 1
2 1 1 1 0 1 1 0
出力例 1
2.5000000000
以下のように移動するのが最適解の一つです。
- 原点から宝箱 1 までの距離 1 を速さ 1 で移動する。時間が 1 かかる
- 宝箱 1 から街 1 までの距離 1 を速さ 2 で移動する。時間が 0.5 かかる
- 街 1 から街 2までの距離 1 を速さ 2 で移動する。時間が 0.5 かかる
- 街 2から原点までの距離 1 を速さ 2 で移動する。時間が 0.5 かかる
入力例 2
2 1 1 1 0 1 100 0
出力例 2
3.4142135624
以下のように移動するのが最適解の一つです。
- 原点 から街 1 までの距離 1.41\ldots を速さ 1 で移動する。時間が 1.41\ldots かかる
- 街 1 から街 2 までの距離 1 を速さ 1 で移動する。時間が 1 かかる
- 街 2 から原点までの距離 1 を速さ 1 で移動する。時間が 1 かかる
入力例 3
1 2 4 4 1 0 0 1
出力例 3
4.3713203436
以下のように移動するのが最適解の一つです。
- 原点から宝箱 1 までの距離 1 を速さ 1 で移動する。時間が 1 かかる
- 宝箱 1 から宝箱 2 までの距離 1.41\ldots を速さ 2 で移動する。時間が 0.707\ldots かかる
- 宝箱 2 から街 1 までの距離 5 を速さ 4 で移動する。時間が 1.25 かかる
- 街 1 から原点までの距離 5.65\ldots を速さ 4 で移動する。時間が 1.41\ldots かかる
Score : 500 points
Problem Statement
In a two-dimensional plane, there are N towns and M chests. Town i is at the coordinates (X_i,Y_i), and chest i is at the coordinates (P_i,Q_i).
Takahashi will go on a trip where he starts at the origin, visits all N towns, and then returns to the origin.
It is not mandatory to visit chests, but each chest contains an accelerator. Each time he picks up an accelerator, his moving speed gets multiplied by 2.
Takahashi's initial moving speed is 1. Find the shortest time needed to complete the trip.
Constraints
- 1 \leq N \leq 12
- 0 \leq M \leq 5
- -10^9 \leq X_i,Y_i,P_i,Q_i \leq 10^9
- (0,0), (X_i,Y_i), and (P_i,Q_i) are distinct.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M X_1 Y_1 \vdots X_N Y_N P_1 Q_1 \vdots P_M Q_M
Output
Print the answer. Your output will be considered correct if the absolute or relative error from the judge's answer is at most 10^{-6}.
Sample Input 1
2 1 1 1 0 1 1 0
Sample Output 1
2.5000000000
Here is one optimal way to complete the trip.
- Go the distance 1 from the origin to chest 1 at a speed of 1, taking a time of 1.
- Go the distance 1 from chest 1 to town 1 at a speed of 2, taking a time of 0.5.
- Go the distance 1 from town 1 to town 2 at a speed of 2, taking a time of 0.5.
- Go the distance 1 from town 2 to the origin at a speed of 2, taking a time of 0.5.
Sample Input 2
2 1 1 1 0 1 100 0
Sample Output 2
3.4142135624
Here is one optimal way to complete the trip.
- Go the distance 1.41\ldots from the origin to town 1 at a speed of 1, taking a time of 1.41\ldots.
- Go the distance 1 from town 1 to town 2 at a speed of 1, taking a time of 1.
- Go the distance 1 from town 2 to the origin at a speed of 1, taking a time of 1.
Sample Input 3
1 2 4 4 1 0 0 1
Sample Output 3
4.3713203436
Here is one optimal way to complete the trip.
- Go the distance 1 from the origin to chest 1 at a speed of 1, taking a time of 1.
- Go the distance 1.41\ldots from chest 1 to chest 2 at a speed of 2, taking a time of 0.707\ldots.
- Go the distance 5 from chest 2 to town 1 at a speed of 4, taking a time of 1.25.
- Go the distance 5.65\ldots from town 1 to the origin at a speed of 4, taking a time of 1.41\ldots.
実行時間制限: 2 sec / メモリ制限: 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