Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
ボールがいくつか刺さった棒が N 本あり、各ボールには英小文字が 1 個書かれています。
i = 1, 2, \ldots, N について、i 番目の棒に刺さった各ボールの英小文字は、文字列 S_i によって表されます。 具体的には、i 番目の棒には文字列 S_i の長さ |S_i| に等しい個数のボールが刺さっており、 刺さっているボールの英小文字を、棒のある端から順に並べたものは文字列 S_i と等しいです。
2 つの棒は、一方の棒に刺さっているボールの英小文字をどちらかの端から並べた列と、もう一方の棒に刺さっているボールの英小文字をどちらかの端から並べた列が一致するとき、同じ棒とみなされます。 より形式的には、1 以上 N 以下の整数 i, j について、i 本目の棒と j 本目の棒は、S_i が S_j と一致するか、S_i が S_j を前後反転したものと一致するとき、かつそのときに限り、同じとみなされます。
N 本の棒の中に、何種類の異なる棒があるかを出力してください。
制約
- N は整数
- 2 \leq N \leq 2 \times 10^5
- S_i は英小文字のみからなる文字列
- |S_i| \geq 1
- \sum_{i = 1}^N |S_i| \leq 2 \times 10^5
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
6 a abc de cba de abc
出力例 1
3
- S_2 =
abcが S_4 =cbaを前後反転したものと一致するため、2 番目の棒と 4 番目の棒は同じとみなされます。 - S_2 =
abcが S_6 =abcと一致するため、2 番目の棒と 6 番目の棒は同じとみなされます。 - S_3 =
deが S_5 =deと一致するため、3 番目の棒と 5 番目の棒は同じとみなされます。
よって、6 本の棒の中に、1 本目の棒、2 本目の棒( 4, 6 本目の棒と同じ)、3 本目の棒( 5 本目の棒と同じ)の 3 種類の異なる棒があります。
Score : 300 points
Problem Statement
There are N sticks with several balls stuck onto them. Each ball has a lowercase English letter written on it.
For each i = 1, 2, \ldots, N, the letters written on the balls stuck onto the i-th stick are represented by a string S_i. Specifically, the number of balls stuck onto the i-th stick is the length |S_i| of the string S_i, and S_i is the sequence of letters on the balls starting from one end of the stick.
Two sticks are considered the same when the sequence of letters on the balls starting from one end of one stick is equal to the sequence of letters starting from one end of the other stick. More formally, for integers i and j between 1 and N, inclusive, the i-th and j-th sticks are considered the same if and only if S_i equals S_j or its reversal.
Print the number of different sticks among the N sticks.
Constraints
- N is an integer.
- 2 \leq N \leq 2 \times 10^5
- S_i is a string consisting of lowercase English letters.
- |S_i| \geq 1
- \sum_{i = 1}^N |S_i| \leq 2 \times 10^5
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print the answer.
Sample Input 1
6 a abc de cba de abc
Sample Output 1
3
- S_2 =
abcequals the reversal of S_4 =cba, so the second and fourth sticks are considered the same. - S_2 =
abcequals S_6 =abc, so the second and sixth sticks are considered the same. - S_3 =
deequals S_5 =de, so the third and fifth sticks are considered the same.
Therefore, there are three different sticks among the six: the first, second (same as the fourth and sixth), and third (same as the fifth).
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
英小文字、,、" からなる長さ N の文字列 S が与えられます。S に含まれる " の個数は偶数であることが保証されています。
S に含まれる " の個数を 2K 個とすると、各 i=1,2,\ldots,K について 2i-1 番目の " から 2i 番目の " までの文字のことを 括られた文字 と呼びます。
あなたの仕事は、 S に含まれる , のうち、括られた文字 でないもの を . で置き換えて得られる文字列を答えることです。
制約
- N は 1 以上 2\times 10^5 以下の整数
- S は英小文字、
,、"からなる長さ N の文字列 - S に含まれる
"の個数は偶数
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを出力せよ。
入力例 1
8 "a,b"c,d
出力例 1
"a,b"c.d
S のうち "a,b" が括られた文字であり、c,d は括られた文字ではありません。
S に含まれる , のうち、括られた文字でないのは S の左から 7 番目の文字なので、7 番目の文字を . で置き換えたものが答えとなります。
入力例 2
5 ,,,,,
出力例 2
.....
入力例 3
20 a,"t,"c,"o,"d,"e,"r,
出力例 3
a."t,"c."o,"d."e,"r.
Score : 300 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters, ,, and ". It is guaranteed that S contains an even number of ".
Let 2K be the number of " in S. For each i=1,2,\ldots,K, the characters from the (2i-1)-th " through the (2i)-th " are said to be enclosed.
Your task is to replace each , in S that is not an enclosed character with . and print the resulting string.
Constraints
- N is an integer between 1 and 2\times 10^5, inclusive.
- S is a string of length N consisting of lowercase English letters,
,, and". - S contains an even number of
".
Input
The input is given from Standard Input in the following format:
N S
Output
Print the answer.
Sample Input 1
8 "a,b"c,d
Sample Output 1
"a,b"c.d
In S, "a,b" are enclosed characters, and c,d are not.
The , in S that is not an enclosed character is the seventh character from the left in S, so replace that character with . to get the answer.
Sample Input 2
5 ,,,,,
Sample Output 2
.....
Sample Input 3
20 a,"t,"c,"o,"d,"e,"r,
Sample Output 3
a."t,"c."o,"d."e,"r.
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
xy -平面上にいくつかのイチゴが載った長方形のケーキがあります。 ケーキは、長方形領域 \lbrace (x, y) : 0 \leq x \leq W, 0 \leq y \leq H \rbrace をちょうど占めます。
ケーキには N 個のイチゴが載っており、i = 1, 2, \ldots, N について、i 番目のイチゴの座標は (p_i, q_i) です。 2 個以上のイチゴが同一の座標にあることはありません。
高橋君は、このケーキを包丁で下記の通りにいくつかのピースに切り分けます。
- まず、ケーキを通る y 軸に並行な A 本の異なる直線、直線 x = a_1 、直線 x = a_2 、\ldots 、直線 x = a_A のそれぞれにそってケーキを切る。
- 次に、ケーキを通る x 軸に並行な B 本の異なる直線、直線 y = b_1 、直線 y = b_2 、\ldots 、直線 y = b_B のそれぞれにそってケーキを切る。
その結果、ケーキは (A+1)(B+1) 個の長方形のピースに分割されます。 高橋君はそれらのうちのいずれか 1 個だけを選んで食べます。 高橋君が選んだピースに載っているイチゴの個数としてあり得る最小値と最大値をそれぞれ出力してください。
ここで、最終的にピースの縁となる位置にはイチゴが存在しないことが保証されます。 より形式的な説明は下記の制約を参照してください。
制約
- 3 \leq W, H \leq 10^9
- 1 \leq N \leq 2 \times 10^5
- 0 \lt p_i \lt W
- 0 \lt q_i \lt H
- i \neq j \implies (p_i, q_i) \neq (p_j, q_j)
- 1 \leq A, B \leq 2 \times 10^5
- 0 \lt a_1 \lt a_2 \lt \cdots \lt a_A \lt W
- 0 \lt b_1 \lt b_2 \lt \cdots \lt b_B \lt H
- p_i \not \in \lbrace a_1, a_2, \ldots, a_A \rbrace
- q_i \not \in \lbrace b_1, b_2, \ldots, b_B \rbrace
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
W H N p_1 q_1 p_2 q_2 \vdots p_N q_N A a_1 a_2 \ldots a_A B b_1 b_2 \ldots b_B
出力
高橋君が選んだピースに載っているイチゴの個数としてあり得る最小値 m と最大値 M をそれぞれ、下記の形式の通り空白区切りで出力せよ。
m M
入力例 1
7 6 5 6 1 3 1 4 2 1 5 6 2 2 2 5 2 3 4
出力例 1
0 2
全 9 個のピースの内訳は、イチゴが 0 個載ったものが 6 個、イチゴが 1 個載ったものが 1 個、イチゴが 2 個載ったものが 2 個です。 よって、それらのうちのいずれか 1 個だけを選んで食べるとき、選んだピースに載っているイチゴの個数としてあり得る最小値は 0 、最大値は 2 です。
入力例 2
4 4 4 1 1 3 1 3 3 1 3 1 2 1 2
出力例 2
1 1
どのピースにもイチゴが 1 個載っています。
Score : 400 points
Problem Statement
There is a rectangular cake with some strawberries on the xy-plane. The cake occupies the rectangular area \lbrace (x, y) : 0 \leq x \leq W, 0 \leq y \leq H \rbrace.
There are N strawberries on the cake, and the coordinates of the i-th strawberry are (p_i, q_i) for i = 1, 2, \ldots, N. No two strawberries have the same coordinates.
Takahashi will cut the cake into several pieces with a knife, as follows.
- First, cut the cake along A different lines parallel to the y-axis: lines x = a_1, x = a_2, \ldots, x = a_A.
- Next, cut the cake along B different lines parallel to the x-axis: lines y = b_1, y = b_2, \ldots, y = b_B.
As a result, the cake will be divided into (A+1)(B+1) rectangular pieces. Takahashi will choose just one of these pieces to eat. Print the minimum and maximum possible numbers of strawberries on the chosen piece.
Here, it is guaranteed that there are no strawberries along the edges of the final pieces. For a more formal description, refer to the constraints below.
Constraints
- 3 \leq W, H \leq 10^9
- 1 \leq N \leq 2 \times 10^5
- 0 \lt p_i \lt W
- 0 \lt q_i \lt H
- i \neq j \implies (p_i, q_i) \neq (p_j, q_j)
- 1 \leq A, B \leq 2 \times 10^5
- 0 \lt a_1 \lt a_2 \lt \cdots \lt a_A \lt W
- 0 \lt b_1 \lt b_2 \lt \cdots \lt b_B \lt H
- p_i \not \in \lbrace a_1, a_2, \ldots, a_A \rbrace
- q_i \not \in \lbrace b_1, b_2, \ldots, b_B \rbrace
- All input values are integers.
Input
The input is given from Standard Input in the following format:
W H N p_1 q_1 p_2 q_2 \vdots p_N q_N A a_1 a_2 \ldots a_A B b_1 b_2 \ldots b_B
Output
Print the minimum possible number of strawberries m and the maximum possible number M on the chosen piece in the following format, separated by a space.
m M
Sample Input 1
7 6 5 6 1 3 1 4 2 1 5 6 2 2 2 5 2 3 4
Sample Output 1
0 2
There are nine pieces in total: six with zero strawberries, one with one strawberry, and two with two strawberries. Therefore, when choosing just one of these pieces to eat, the minimum possible number of strawberries on the chosen piece is 0, and the maximum possible number is 2.
Sample Input 2
4 4 4 1 1 3 1 3 3 1 3 1 2 1 2
Sample Output 2
1 1
Each piece has one strawberry on it.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : {500} 点
問題文
高橋くん一行は、AtCoder Land に行くためにランド内にあるホテルに泊まることにしました。
N 人の人と M 個のランクが付いた部屋があります。
i 番目の人はランクが A_i 以上の部屋に泊まりたいと考えています。
また、i 番目の部屋のランクは R_i、定員は B_i 人、客室料金は C_i 円です。 C_i 円払うことで、B_i 人以下であれば何人でも i 番目の部屋に泊まることができます。
N 人全員が部屋に泊まることが可能か判定し、可能な場合必要な金額の総和の最小値を求めてください。
制約
- 1\leq N,M \leq 5000
- 1\leq A_i,R_i,B_i,C_i \leq 5000
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 \ldots A_N R_1 B_1 C_1 \vdots R_M B_M C_M
出力
N 人全員が部屋に泊まることが可能な場合、必要な金額の総和の最小値を X 円とする。このとき X を出力せよ。
不可能な場合 -1 を出力せよ。
入力例 1
4 3 3 5 1 4 5 3 3 3 1 1 2 3 2
出力例 1
4
1 番目の人が 2 番目の部屋に泊まり、それ以外の人が 1 番目の部屋に泊まることを考えます。
このとき必要な金額の総和は 4 となり、これが最小であることが示せます。
入力例 2
8 5 2 5 1 5 2 1 2 4 4 2 5 7 2 4 7 4 2 1 4 7 3 3 8
出力例 2
11
入力例 3
10 1 1 1 1 1 1 1 1 1 1 1 10 4 1
出力例 3
-1
N 人全員が部屋に泊まることはできません。
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