実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 266 点
問題文
高橋君は、フィギュアスケートの地区予選大会の運営スタッフとして働いています。
この大会では N 人の選手が演技を披露し、それぞれの選手は M 人の審査員から採点を受けます。選手 i(1 \leq i \leq N)が審査員 j(1 \leq j \leq M)から受け取る得点は整数 S_{i,j} です。
フィギュアスケートの採点では、極端な採点の影響を抑えるため、各選手の最終得点は以下のように計算されます。
- M \geq 3 の場合:選手が受け取った M 個の得点の中から、最も高い得点 1 つと最も低い得点 1 つを除きます。同じ値の得点が複数あっても、除くのはそれぞれ 1 つずつだけです。残りの M - 2 個の得点の平均値(合計を M - 2 で割った値)を求め、小数点以下を切り捨てた整数を最終得点とします。
- M = 1 または M = 2 の場合:得点を除外すると残りが 0 個以下になってしまうため、除外は行いません。M 個すべての得点の平均値(合計を M で割った値)を求め、小数点以下を切り捨てた整数を最終得点とします。
ここで「小数点以下を切り捨てる」とは、その値を超えない最大の整数をとる操作(床関数 \lfloor \cdot \rfloor)を意味します。なお、得点はすべて 0 以上であるため、最終得点も 0 以上の整数となります。
本戦への出場資格を得るためには、最終得点が基準点 K 点以上である必要があります。最終得点が K 点未満の選手は予選落ちとなります。
高橋君に代わって、予選落ちとなる選手の人数を求めてください。
制約
- 1 \leq N \leq 10^5
- 1 \leq M \leq 100
- 0 \leq K \leq 100
- 0 \leq S_{i,j} \leq 100 (1 \leq i \leq N, 1 \leq j \leq M)
- 入力はすべて整数
入力
N M K
S_{1,1} S_{1,2} \ldots S_{1,M}
S_{2,1} S_{2,2} \ldots S_{2,M}
\vdots
S_{N,1} S_{N,2} \ldots S_{N,M}
- 1 行目には、参加選手数を表す整数 N、審査員数を表す整数 M、基準点を表す整数 K が、スペース区切りで与えられる。
- 2 行目から N + 1 行目には、各選手が各審査員から受け取る得点が与えられる。
- 1 + i 行目(1 \leq i \leq N)には、選手 i が審査員 1, 2, \ldots, M から受け取る得点 S_{i,1}, S_{i,2}, \ldots, S_{i,M} がスペース区切りで与えられる。
出力
予選落ちとなる選手の人数を 1 行で出力してください。
入力例 1
3 5 70 80 75 60 90 85 50 55 60 65 70 100 100 100 100 100
出力例 1
1
入力例 2
4 2 50 60 40 100 0 49 50 55 55
出力例 2
1
入力例 3
5 7 75 80 85 90 70 75 88 92 60 65 70 55 58 62 68 100 99 98 97 96 95 94 74 74 74 74 74 74 74 75 75 75 75 75 75 75
出力例 3
2
Score : 266 pts
Problem Statement
Takahashi is working as an operations staff member for a figure skating regional qualifying competition.
In this competition, N athletes perform, and each athlete is scored by M judges. The score that athlete i (1 \leq i \leq N) receives from judge j (1 \leq j \leq M) is an integer S_{i,j}.
In figure skating scoring, to reduce the influence of extreme scores, each athlete's final score is calculated as follows:
- When M \geq 3: From the M scores the athlete received, the single highest score and the single lowest score are removed. Even if there are multiple scores with the same value, only one of each is removed. The average of the remaining M - 2 scores (the sum divided by M - 2) is computed, and the integer obtained by truncating the decimal part is the final score.
- When M = 1 or M = 2: Since removing scores would leave 0 or fewer remaining, no scores are removed. The average of all M scores (the sum divided by M) is computed, and the integer obtained by truncating the decimal part is the final score.
Here, "truncating the decimal part" means taking the largest integer that does not exceed the value (the floor function \lfloor \cdot \rfloor). Note that since all scores are 0 or greater, the final score is also a non-negative integer.
To qualify for the main competition, an athlete's final score must be at least the threshold score of K points. Athletes whose final score is less than K points are eliminated in the qualifying round.
On behalf of Takahashi, determine the number of athletes who are eliminated in the qualifying round.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq M \leq 100
- 0 \leq K \leq 100
- 0 \leq S_{i,j} \leq 100 (1 \leq i \leq N, 1 \leq j \leq M)
- All input values are integers
Input
N M K
S_{1,1} S_{1,2} \ldots S_{1,M}
S_{2,1} S_{2,2} \ldots S_{2,M}
\vdots
S_{N,1} S_{N,2} \ldots S_{N,M}
- The first line contains three space-separated integers: N representing the number of athletes, M representing the number of judges, and K representing the threshold score.
- From the 2nd line to the (N + 1)-th line, the scores each athlete receives from each judge are given.
- The (1 + i)-th line (1 \leq i \leq N) contains the scores S_{i,1}, S_{i,2}, \ldots, S_{i,M} that athlete i receives from judges 1, 2, \ldots, M, separated by spaces.
Output
Output the number of athletes who are eliminated in the qualifying round, in a single line.
Sample Input 1
3 5 70 80 75 60 90 85 50 55 60 65 70 100 100 100 100 100
Sample Output 1
1
Sample Input 2
4 2 50 60 40 100 0 49 50 55 55
Sample Output 2
1
Sample Input 3
5 7 75 80 85 90 70 75 88 92 60 65 70 55 58 62 68 100 99 98 97 96 95 94 74 74 74 74 74 74 74 75 75 75 75 75 75 75
Sample Output 3
2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 333 点
問題文
高橋君は、円形の公園の管理人をしています。この公園の外周には等間隔に N 本の柱が立っており、柱には 1 から N までの番号が時計回りに付けられています。
公園の円の中心に街灯を 1 基設置する計画があります。この街灯は特殊な指向性を持ち、いずれかの柱の方向を正面として照らします。
街灯の正面方向を柱 i に設定したとき、柱 j に対するずれ角を次のように定義します。柱 i と柱 j を円の中心から見たとき、時計回り・反時計回りのうち小さい方の角度(度数法)をずれ角とします。柱は N 本が等間隔に並んでいるため、隣り合う柱の間の中心角は \frac{360}{N} 度です。柱 i から柱 j まで、時計回りに数えた柱の間隔と反時計回りに数えた柱の間隔のうち小さい方を d(i, j) とすると、
d(i, j) = \min\bigl(|i - j|,\; N - |i - j|\bigr)
であり、ずれ角は d(i, j) \times \frac{360}{N} 度です。
公園の外周にある N 本の柱のうち M 本にはベンチが設置されています。k 番目のベンチは柱 S_k の位置にあります(S_1, S_2, \ldots, S_M はすべて互いに異なります)。
街灯の正面方向を柱 1, 2, \ldots, N のいずれかに設定するとき、その設定のコストを「M 個のベンチが設置された柱それぞれに対するずれ角のうち、最大のもの」と定義します。すなわち、正面方向を柱 i に設定したときのコストは
\max_{1 \leq k \leq M} \; d(i, S_k) \times \frac{360}{N}
です。
高橋君は、コストが最小となるように街灯の正面方向を選びたいです。コストの最小値を求めてください。
ただし、答えは既約分数 \frac{P}{Q}(P \geq 0, Q \geq 1, \gcd(P, Q) = 1)の形で P/Q と出力してください。答えが 0 の場合は 0/1 と、答えが整数の場合は分母を 1 として P/1 の形で出力してください。
制約
- 2 \leq N \leq 10^9
- 1 \leq M \leq \min(N, 2 \times 10^5)
- 1 \leq S_k \leq N (1 \leq k \leq M)
- S_k はすべて互いに異なる
- 入力はすべて整数
入力
N M S_1 S_2 \ldots S_M
- 1 行目には、柱の総数を表す整数 N と、ベンチの数を表す整数 M が、スペース区切りで与えられる。
- 2 行目には、ベンチが設置されている柱の番号 S_1, S_2, \ldots, S_M が、スペース区切りで与えられる。
出力
コストの最小値を既約分数の形で P/Q と 1 行で出力せよ。ここで P は 0 以上の整数、Q は正の整数であり、\gcd(P, Q) = 1 を満たす。
入力例 1
8 3 1 3 6
出力例 1
135/1
入力例 2
12 2 4 10
出力例 2
90/1
入力例 3
25 7 2 5 9 14 18 21 24
出力例 3
144/1
入力例 4
1000000000 12 1 12345678 98765432 200000000 333333333 400000001 555555555 666666666 777777777 888888888 999999999 1000000000
出力例 4
3800000007/25000000
入力例 5
2 1 2
出力例 5
0/1
Score : 333 pts
Problem Statement
Takahashi is the caretaker of a circular park. Along the perimeter of this park, N pillars are placed at equal intervals, numbered from 1 to N in clockwise order.
There is a plan to install one street light at the center of the park's circle. This street light has a special directional property and illuminates facing the direction of one of the pillars.
When the front direction of the street light is set to pillar i, the deviation angle with respect to pillar j is defined as follows. When viewing pillars i and j from the center of the circle, the deviation angle is the smaller of the clockwise and counterclockwise angles (in degrees). Since the N pillars are equally spaced, the central angle between adjacent pillars is \frac{360}{N} degrees. Let d(i, j) be the smaller of the number of pillar intervals counted clockwise from pillar i to pillar j and the number counted counterclockwise. Then,
d(i, j) = \min\bigl(|i - j|,\; N - |i - j|\bigr)
and the deviation angle is d(i, j) \times \frac{360}{N} degrees.
Among the N pillars on the park's perimeter, M of them have benches installed. The k-th bench is located at pillar S_k (S_1, S_2, \ldots, S_M are all distinct).
When the front direction of the street light is set to one of the pillars 1, 2, \ldots, N, the cost of that setting is defined as "the maximum deviation angle among those for each of the M pillars with benches." That is, the cost when the front direction is set to pillar i is
\max_{1 \leq k \leq M} \; d(i, S_k) \times \frac{360}{N}
Takahashi wants to choose the front direction of the street light so that the cost is minimized. Find the minimum value of the cost.
The answer should be output in the form P/Q as an irreducible fraction \frac{P}{Q} (P \geq 0, Q \geq 1, \gcd(P, Q) = 1). If the answer is 0, output 0/1. If the answer is an integer, output it in the form P/1 with denominator 1.
Constraints
- 2 \leq N \leq 10^9
- 1 \leq M \leq \min(N, 2 \times 10^5)
- 1 \leq S_k \leq N (1 \leq k \leq M)
- All S_k are distinct
- All inputs are integers
Input
N M S_1 S_2 \ldots S_M
- The first line contains the integer N representing the total number of pillars and the integer M representing the number of benches, separated by a space.
- The second line contains the pillar numbers S_1, S_2, \ldots, S_M where benches are installed, separated by spaces.
Output
Output the minimum value of the cost as an irreducible fraction in the form P/Q on a single line. Here, P is a non-negative integer, Q is a positive integer, and \gcd(P, Q) = 1.
Sample Input 1
8 3 1 3 6
Sample Output 1
135/1
Sample Input 2
12 2 4 10
Sample Output 2
90/1
Sample Input 3
25 7 2 5 9 14 18 21 24
Sample Output 3
144/1
Sample Input 4
1000000000 12 1 12345678 98765432 200000000 333333333 400000001 555555555 666666666 777777777 888888888 999999999 1000000000
Sample Output 4
3800000007/25000000
Sample Input 5
2 1 2
Sample Output 5
0/1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 366 点
問題文
高橋君は、庭にある花壇の管理を任されています。花壇には N 本の花が一列に植えられており、左から順に花 1, 花 2, \ldots, 花 N と番号が付けられています。水やりを始める前の時点で、花 i の高さは H_i です。
高橋君は M 日間にわたって水やりを行います。j 日目(j = 1, 2, \ldots, M)には、花 L_j, L_j + 1, \ldots, R_j に水をやります。水をやられた花は、1 本あたり高さが W_j だけ増加します。
M 日間の水やりがすべて終わった後の花 i の高さを、花 i の最終的な高さと呼びます。花 i の最終的な高さが目標の高さ T_i 以上であるとき、花 i は「十分に成長した」とみなされます。
十分に成長した花の本数を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq H_i \leq 10^9(1 \leq i \leq N)
- 1 \leq T_i \leq 10^{18}(1 \leq i \leq N)
- 1 \leq L_j \leq R_j \leq N(1 \leq j \leq M)
- 1 \leq W_j \leq 10^9(1 \leq j \leq M)
- 入力はすべて整数である。
入力
N M H_1 H_2 \ldots H_N T_1 T_2 \ldots T_N L_1 R_1 W_1 L_2 R_2 W_2 \vdots L_M R_M W_M
- 1 行目には、花の本数を表す整数 N と、水やりの日数を表す整数 M が、スペース区切りで与えられる。
- 2 行目には、各花の水やり前の高さを表す整数 H_1, H_2, \ldots, H_N が、スペース区切りで与えられる。
- 3 行目には、各花の目標の高さを表す整数 T_1, T_2, \ldots, T_N が、スペース区切りで与えられる。
- 続く M 行のうち j 行目には、j 日目に水をやる区間の左端 L_j、右端 R_j、および水やりによって 1 本あたり増加する高さ W_j が、スペース区切りで与えられる。
出力
十分に成長した花の本数を 1 行で出力せよ。
入力例 1
5 3 10 20 30 40 50 25 35 50 40 80 1 3 5 2 4 10 1 5 3
出力例 1
2
入力例 2
8 4 1 1 1 1 1 1 1 1 10 15 20 25 20 15 10 5 1 8 5 2 7 5 3 6 5 4 5 5
出力例 2
4
入力例 3
10 5 1000000000 500000000 1 999999999 100 200 300 400 500 600 1000000000000000000 1000000000 100 2000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1 10 1000000000 1 5 1000000000 3 8 1000000000 1 10 1000000000 5 10 1000000000
出力例 3
9
Score : 366 pts
Problem Statement
Takahashi is in charge of managing a flower bed in his garden. The flower bed has N flowers planted in a row, numbered flower 1, flower 2, \ldots, flower N from left to right. Before watering begins, the height of flower i is H_i.
Takahashi waters the flowers over M days. On day j (j = 1, 2, \ldots, M), he waters flowers L_j, L_j + 1, \ldots, R_j. Each watered flower increases in height by W_j per flower.
The height of flower i after all M days of watering are completed is called the final height of flower i. Flower i is considered "sufficiently grown" if its final height is at least the target height T_i.
Find the number of sufficiently grown flowers.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq H_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq T_i \leq 10^{18} (1 \leq i \leq N)
- 1 \leq L_j \leq R_j \leq N (1 \leq j \leq M)
- 1 \leq W_j \leq 10^9 (1 \leq j \leq M)
- All input values are integers.
Input
N M H_1 H_2 \ldots H_N T_1 T_2 \ldots T_N L_1 R_1 W_1 L_2 R_2 W_2 \vdots L_M R_M W_M
- The first line contains an integer N representing the number of flowers and an integer M representing the number of watering days, separated by a space.
- The second line contains integers H_1, H_2, \ldots, H_N representing the heights of the flowers before watering, separated by spaces.
- The third line contains integers T_1, T_2, \ldots, T_N representing the target heights of the flowers, separated by spaces.
- The j-th of the following M lines contains the left endpoint L_j, the right endpoint R_j of the interval to be watered on day j, and the height increase per flower W_j from watering, separated by spaces.
Output
Output the number of sufficiently grown flowers in a single line.
Sample Input 1
5 3 10 20 30 40 50 25 35 50 40 80 1 3 5 2 4 10 1 5 3
Sample Output 1
2
Sample Input 2
8 4 1 1 1 1 1 1 1 1 10 15 20 25 20 15 10 5 1 8 5 2 7 5 3 6 5 4 5 5
Sample Output 2
4
Sample Input 3
10 5 1000000000 500000000 1 999999999 100 200 300 400 500 600 1000000000000000000 1000000000 100 2000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1 10 1000000000 1 5 1000000000 3 8 1000000000 1 10 1000000000 5 10 1000000000
Sample Output 3
9
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君と青木君は、それぞれ N 個の部屋からなる施設を管理しています。各施設の部屋は木構造の通路で結ばれており、どちらも部屋 1 が入口(根)です。通路は双方向に通行可能であり、部屋 1 を根として各部屋の親子関係が定まります。
N 個の部屋からなる根付き木に対して、巡回記録を以下の手順で定義します。
- 各部屋について、その部屋の子の部屋(直接通路で繋がる、根からより遠い側の部屋)を訪れる順番を決める。この順番は部屋ごとに任意に決めてよい。
- 空文字列から始めて、入口(部屋 1)に対して以下の再帰的手順を実行し、得られた文字列を巡回記録とする。
部屋 v に対する再帰的手順:
- まず、文字
aを末尾に追加する。 - 次に、手順1で決めた順番に従い、部屋 v の子の部屋それぞれに対して順に以下を行う:その子の部屋に対してこの再帰的手順を実行する(部屋 v に戻る際に文字の追加は行わない)。
- 最後に、文字
bを末尾に追加する。
部屋 v が子を持たない場合、2番目の処理で何もせず、a の直後に b が追加されます。
このようにして得られる文字列の長さは常に 2N です。
子の訪問順序の選び方によって、異なる巡回記録が得られることがあります。高橋君の施設から得られるすべての巡回記録の集合と、青木君の施設から得られるすべての巡回記録の集合の共通部分に含まれる文字列を、共通巡回記録と呼びます。
すべての共通巡回記録を辞書順に列挙してください。ただし、異なる子の訪問順序から同一の文字列が得られる場合は重複なく 1 つだけ出力します。文字の大小は a < b とします。
制約
- 1 \leq N \leq 10
- 1 \leq UA_i, VA_i \leq N、UA_i \neq VA_i (1 \leq i \leq N-1)
- 1 \leq UB_i, VB_i \leq N、UB_i \neq VB_i (1 \leq i \leq N-1)
- 高橋君の施設の通路は木構造をなす(N 個の部屋が N-1 本の無向辺で連結)
- 青木君の施設の通路は木構造をなす(N 個の部屋が N-1 本の無向辺で連結)
- 入力はすべて整数
入力
N
UA_1 VA_1
UA_2 VA_2
:
UA_{N-1} VA_{N-1}
UB_1 VB_1
UB_2 VB_2
:
UB_{N-1} VB_{N-1}
- 1 行目には、施設の部屋数を表す整数 N が与えられる。
- 続く N - 1 行では、高橋君の施設の通路が与えられる。
- そのうち i 行目 (1 \leq i \leq N-1) には、高橋君の施設の i 番目の通路が結ぶ 2 つの部屋の番号を表す整数 UA_i, VA_i がスペース区切りで与えられる。
- さらに続く N - 1 行では、青木君の施設の通路が与えられる(高橋君の施設の辺の直後に空行を挟まず続く)。
- そのうち i 行目 (1 \leq i \leq N-1) には、青木君の施設の i 番目の通路が結ぶ 2 つの部屋の番号を表す整数 UB_i, VB_i がスペース区切りで与えられる。
N = 1 のときは辺の行は存在しない。
出力
共通巡回記録の個数を K とする。
まず 1 行目に K を出力せよ。共通巡回記録が存在しない場合は 0 を出力し、それ以降は何も出力しない。
K \geq 1 の場合、続く K 行に、すべての共通巡回記録を辞書順に 1 行に 1 つずつ出力せよ。
入力例 1
3 1 2 1 3 1 2 1 3
出力例 1
1 aababb
入力例 2
3 1 2 2 3 1 2 1 3
出力例 2
0
入力例 3
6 1 2 1 3 2 4 2 5 3 6 1 4 1 2 4 5 4 6 2 3
出力例 3
2 aaababbaabbb aaabbaababbb
入力例 4
10 1 2 1 3 1 4 2 5 2 6 3 7 4 8 5 9 7 10 1 6 1 2 1 3 6 4 6 5 4 8 2 7 7 9 3 10
出力例 4
12 aaaabbabbaaabbbaabbb aaaabbabbaabbaaabbbb aaaabbbaaabbabbaabbb aaaabbbaabaabbbaabbb aaaabbbaabbaaabbabbb aaaabbbaabbaabaabbbb aaabaabbbaaabbbaabbb aaabaabbbaabbaaabbbb aaabbaaabbabbaaabbbb aaabbaaabbbaaabbabbb aaabbaaabbbaabaabbbb aaabbaabaabbbaaabbbb
入力例 5
1
出力例 5
1 ab
Score : 400 pts
Problem Statement
Takahashi and Aoki each manage a facility consisting of N rooms. The rooms in each facility are connected by corridors forming a tree structure, and in both facilities, room 1 is the entrance (root). Corridors can be traversed in both directions, and the parent-child relationships of the rooms are determined by taking room 1 as the root.
For a rooted tree consisting of N rooms, a traversal record is defined by the following procedure:
- For each room, determine the order in which to visit its child rooms (rooms directly connected by a corridor that are farther from the root). This order may be chosen arbitrarily for each room.
- Starting from an empty string, execute the following recursive procedure on the entrance (room 1), and the resulting string is the traversal record.
Recursive procedure for room v:
- First, append the character
ato the end. - Next, following the order determined in step 1, for each child room of room v, do the following in order: execute this recursive procedure on that child room (no characters are appended when returning to room v).
- Finally, append the character
bto the end.
If room v has no children, nothing is done in the second step, and b is appended immediately after a.
The length of the string obtained in this way is always 2N.
Depending on the choice of visiting order of children, different traversal records may be obtained. A string that belongs to the intersection of the set of all traversal records obtainable from Takahashi's facility and the set of all traversal records obtainable from Aoki's facility is called a common traversal record.
Enumerate all common traversal records in lexicographic order. If the same string is obtained from different child visiting orders, output it only once without duplication. The character ordering is a < b.
Constraints
- 1 \leq N \leq 10
- 1 \leq UA_i, VA_i \leq N, UA_i \neq VA_i (1 \leq i \leq N-1)
- 1 \leq UB_i, VB_i \leq N, UB_i \neq VB_i (1 \leq i \leq N-1)
- The corridors of Takahashi's facility form a tree structure (N rooms connected by N-1 undirected edges)
- The corridors of Aoki's facility form a tree structure (N rooms connected by N-1 undirected edges)
- All input values are integers
Input
N
UA_1 VA_1
UA_2 VA_2
:
UA_{N-1} VA_{N-1}
UB_1 VB_1
UB_2 VB_2
:
UB_{N-1} VB_{N-1}
- The first line contains an integer N representing the number of rooms in each facility.
- The following N - 1 lines give the corridors of Takahashi's facility.
- The i-th of these lines (1 \leq i \leq N-1) contains integers UA_i and VB_i, separated by a space, representing the two rooms connected by the i-th corridor of Takahashi's facility.
- The next N - 1 lines give the corridors of Aoki's facility (immediately following the edges of Takahashi's facility with no blank line in between).
- The i-th of these lines (1 \leq i \leq N-1) contains integers UB_i and VB_i, separated by a space, representing the two rooms connected by the i-th corridor of Aoki's facility.
When N = 1, no edge lines exist.
Output
Let K be the number of common traversal records.
First, output K on the first line. If no common traversal records exist, output 0 and output nothing further.
If K \geq 1, output all common traversal records in lexicographic order on the following K lines, one per line.
Sample Input 1
3 1 2 1 3 1 2 1 3
Sample Output 1
1 aababb
Sample Input 2
3 1 2 2 3 1 2 1 3
Sample Output 2
0
Sample Input 3
6 1 2 1 3 2 4 2 5 3 6 1 4 1 2 4 5 4 6 2 3
Sample Output 3
2 aaababbaabbb aaabbaababbb
Sample Input 4
10 1 2 1 3 1 4 2 5 2 6 3 7 4 8 5 9 7 10 1 6 1 2 1 3 6 4 6 5 4 8 2 7 7 9 3 10
Sample Output 4
12 aaaabbabbaaabbbaabbb aaaabbabbaabbaaabbbb aaaabbbaaabbabbaabbb aaaabbbaabaabbbaabbb aaaabbbaabbaaabbabbb aaaabbbaabbaabaabbbb aaabaabbbaaabbbaabbb aaabaabbbaabbaaabbbb aaabbaaabbabbaaabbbb aaabbaaabbbaaabbabbb aaabbaaabbbaabaabbbb aaabbaabaabbbaaabbbb
Sample Input 5
1
Sample Output 5
1 ab
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 433 点
問題文
高橋君は、文化祭の装飾として複数の看板を横一列につなげて長い横断幕を作ろうとしています。手元には N 枚の看板があり、i 番目の看板には文字列 S_i が書かれています。ここで、文字列 S_i の長さ(文字数)を |S_i| と表します。
高橋君はこれらの看板を 1 番目から N 番目まで順番に、左から右へつなげていきます。つなげるとき、スペースを節約するために、隣り合う 2 枚の看板同士をできるだけ多く重ねて配置します。
具体的には、i 番目の看板 (i \geq 2) をつなげるとき、直前の看板の文字列 S_{i-1} と今回の看板の文字列 S_i について、次の条件をすべて満たす正の整数 l を考えます。
- 1 \leq l < |S_{i-1}| かつ l < |S_i| である。(すなわち、どちらの文字列も完全には重なりに含まれず、それぞれ少なくとも 1 文字は重なりの外に残る。)
- S_{i-1} の末尾 l 文字と S_i の先頭 l 文字が一致する。
このような正の整数 l が存在する場合、その最大値を L_i とします。存在しない場合は L_i = 0 とします。
i 番目の看板をつなげる際、S_i の先頭 L_i 文字を直前の看板の末尾と重ねて配置します。L_i = 0 のときは重ねずにそのまま末尾に連結します。いずれの場合も、S_i のうち横断幕に新たに加わる文字数は |S_i| - L_i です。
N 枚すべての看板を順につなげたとき、横断幕全体の総文字数(重なった部分は一度だけ数える)を求めてください。
総文字数は次の式で計算できます。
|S_1| + \sum_{i=2}^{N} (|S_i| - L_i)
例
S_1 = abcabc、S_2 = abcxyz の場合を考えます。
S_1 の末尾と S_2 の先頭の一致を調べます。l の条件より 1 \leq l < 6 です。l = 3 のとき、S_1 の末尾 3 文字 abc と S_2 の先頭 3 文字 abc が一致します。l = 4, 5 では一致しません。よって条件を満たす最大の l は 3 であり、L_2 = 3 となります。
つなげた結果は abcabcxyz で、総文字数は 6 + (6 - 3) = 9 です。
制約
- 1 \leq N \leq 10^5
- 1 \leq |S_i| \leq 10^6
- \displaystyle \sum_{i=1}^{N} |S_i| \leq 10^6
- S_i は英小文字のみからなる。
入力
N S_1 S_2 \vdots S_N
1 行目には、看板の枚数を表す整数 N が与えられる。続く N 行のうち i 行目 (1 \leq i \leq N) には、i 番目の看板に書かれた文字列 S_i が与えられる。
出力
N 枚の看板を順につなげたとき、横断幕全体の総文字数を整数として 1 行で出力せよ。
入力例 1
3 abcabc abcxyz xyzabc
出力例 1
12
入力例 2
4 cat dog bird fish
出力例 2
14
入力例 3
8 abracadabra abraxyz xyzxyz yzalpha alphabet betamax maximum umami
出力例 3
36
入力例 4
20 aaaaab abaaaa aaaacaaa caaad aadbbb bbbccc cccdde ddefgh ghijklm klmnop opqrstu stuvwx vwxyzab zabzab abzabc abcabcabc bcabcx xcdef defdefg fghello
出力例 4
71
入力例 5
1 a
出力例 5
1
Score : 433 pts
Problem Statement
Takahashi is trying to create a long banner by connecting multiple signs in a horizontal row as decoration for a cultural festival. He has N signs, and the i-th sign has the string S_i written on it. Here, the length (number of characters) of string S_i is denoted as |S_i|.
Takahashi connects these signs in order from the 1-st to the N-th, from left to right. When connecting them, to save space, he overlaps adjacent signs as much as possible.
Specifically, when connecting the i-th sign (i \geq 2), he considers positive integers l that satisfy all of the following conditions regarding the string S_{i-1} of the previous sign and the string S_i of the current sign:
- 1 \leq l < |S_{i-1}| and l < |S_i|. (That is, neither string is completely contained in the overlap, and each has at least 1 character remaining outside the overlap.)
- The last l characters of S_{i-1} match the first l characters of S_i.
If such a positive integer l exists, let L_i be its maximum value. If none exists, let L_i = 0.
When connecting the i-th sign, the first L_i characters of S_i are overlapped with the end of the previous sign. When L_i = 0, it is simply concatenated to the end without any overlap. In either case, the number of characters newly added to the banner from S_i is |S_i| - L_i.
Find the total number of characters of the entire banner when all N signs are connected in order (where overlapping portions are counted only once).
The total number of characters can be calculated by the following formula:
|S_1| + \sum_{i=2}^{N} (|S_i| - L_i)
Example
Consider the case where S_1 = abcabc and S_2 = abcxyz.
We check for matches between the suffix of S_1 and the prefix of S_2. By the condition on l, we have 1 \leq l < 6. When l = 3, the last 3 characters of S_1, abc, match the first 3 characters of S_2, abc. For l = 4, 5, there is no match. Therefore, the maximum l satisfying the conditions is 3, and L_2 = 3.
The connected result is abcabcxyz, and the total number of characters is 6 + (6 - 3) = 9.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq |S_i| \leq 10^6
- \displaystyle \sum_{i=1}^{N} |S_i| \leq 10^6
- S_i consists only of lowercase English letters.
Input
N S_1 S_2 \vdots S_N
The first line contains an integer N representing the number of signs. In the following N lines, the i-th line (1 \leq i \leq N) contains the string S_i written on the i-th sign.
Output
Output in a single line the total number of characters of the entire banner when the N signs are connected in order, as an integer.
Sample Input 1
3 abcabc abcxyz xyzabc
Sample Output 1
12
Sample Input 2
4 cat dog bird fish
Sample Output 2
14
Sample Input 3
8 abracadabra abraxyz xyzxyz yzalpha alphabet betamax maximum umami
Sample Output 3
36
Sample Input 4
20 aaaaab abaaaa aaaacaaa caaad aadbbb bbbccc cccdde ddefgh ghijklm klmnop opqrstu stuvwx vwxyzab zabzab abzabc abcabcabc bcabcx xcdef defdefg fghello
Sample Output 4
71
Sample Input 5
1 a
Sample Output 5
1