Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英小文字からなる長さ N の文字列 S が与えられます。S の末尾の文字を出力してください。
制約
- N は整数
- 1 ≤ N ≤ 1000
- S は英小文字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
S の末尾の文字を出力せよ。
入力例 1
5 abcde
出力例 1
e
S = {}abcde です。S の末尾の文字は e なので、e を出力します。
入力例 2
1 a
出力例 2
a
Score : 100 points
Problem Statement
Given a string S of length N consisting of lowercase English alphabets, print the last character of S.
Constraints
- N is an integer.
- 1 ≤ N ≤ 1000
- S is a string of length N consisting of lowercase English alphabets.
Input
Input is given from Standard Input in the following format:
N S
Output
Print the last character of S.
Sample Input 1
5 abcde
Sample Output 1
e
The last character of S = {}abcde is e, so e should be printed.
Sample Input 2
1 a
Sample Output 2
a
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
N 個の整数の 2 つ組 (A_1, B_1), (A_2, B_2), \ldots, (A_N, B_N) が与えられます。 各 i = 1, 2, \ldots, N について、A_i + B_i を出力してください。
制約
- 1 \leq N \leq 1000
- -10^9 \leq A_i, B_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
N 行出力せよ。 i = 1, 2, \ldots, N について、i 行目には A_i+B_i を出力せよ。
入力例 1
4 3 5 2 -6 -5 0 314159265 123456789
出力例 1
8 -4 -5 437616054
- 1 行目には、A_1 + B_1 = 3 + 5 = 8 を、
- 2 行目には、A_2 + B_2 = 2 + (-6) = -4 を、
- 3 行目には、A_3 + B_3 = (-5) + 0 = -5 を、
- 4 行目には、A_4 + B_4 = 314159265 + 123456789 = 437616054 を出力します。
Score : 100 points
Problem Statement
You are given N pairs of integers: (A_1, B_1), (A_2, B_2), \ldots, (A_N, B_N). For each i = 1, 2, \ldots, N, print A_i + B_i.
Constraints
- 1 \leq N \leq 1000
- -10^9 \leq A_i, B_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 B_1 A_2 B_2 \vdots A_N B_N
Output
Print N lines. For i = 1, 2, \ldots, N, the i-th line should contain A_i+B_i.
Sample Input 1
4 3 5 2 -6 -5 0 314159265 123456789
Sample Output 1
8 -4 -5 437616054
- The first line should contain A_1 + B_1 = 3 + 5 = 8.
- The second line should contain A_2 + B_2 = 2 + (-6) = -4.
- The third line should contain A_3 + B_3 = (-5) + 0 = -5.
- The fourth line should contain A_4 + B_4 = 314159265 + 123456789 = 437616054.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
連長圧縮(ランレングス圧縮)を復元してください。ただし、長すぎる場合には
Too Longと出力してください。
N 個の文字と整数の組 (c_1,l_1),(c_2,l_2),\ldots,(c_N,l_N) が与えられます。
l_1 個の文字 c_1、l_2 個の文字 c_2、\ldots、l_N 個の文字 c_N をこの順に連結させた文字列を S とします。
S を出力してください。ただし、S の長さが 100 を超える場合には代わりに Too Long と出力してください。
制約
- 1\leq N\leq 100
- 1\leq l_i\leq 10^{18}
- N,l_i は整数
- c_i は英小文字
- c_i\neq c_{i+1}
入力
入力は以下の形式で標準入力から与えられる。
N c_1 l_1 c_2 l_2 \vdots c_N l_N
出力
S の長さが 100 以下なら S を、そうでないなら Too Long と出力せよ。
入力例 1
8 m 1 i 1 s 2 i 1 s 2 i 1 p 2 i 1
出力例 1
mississippi
S は mississippi です。S の長さは 100 以下であるため S を出力します。
入力例 2
7 a 1000000000000000000 t 1000000000000000000 c 1000000000000000000 o 1000000000000000000 d 1000000000000000000 e 1000000000000000000 r 1000000000000000000
出力例 2
Too Long
S の長さは 7\times 10^{18} であるため、Too Long を出力します。
入力例 3
1 a 100
出力例 3
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
入力例 4
6 g 4 j 1 m 4 e 4 d 3 i 4
出力例 4
ggggjmmmmeeeedddiiii
Score : 200 points
Problem Statement
Restore run-length encoding. If the result is too long, output
Too Long.
You are given N pairs of characters and integers (c_1,l_1),(c_2,l_2),\ldots,(c_N,l_N).
Let S be the string formed by concatenating l_1 characters c_1, l_2 characters c_2, \ldots, and l_N characters c_N in this order.
Output S. However, if the length of S exceeds 100, output Too Long instead.
Constraints
- 1\leq N\leq 100
- 1\leq l_i\leq 10^{18}
- N and l_i are integers.
- Each c_i is a lowercase English letter.
- c_i\neq c_{i+1}
Input
The input is given from Standard Input in the following format:
N c_1 l_1 c_2 l_2 \vdots c_N l_N
Output
If the length of S is at most 100, output S; otherwise, output Too Long.
Sample Input 1
8 m 1 i 1 s 2 i 1 s 2 i 1 p 2 i 1
Sample Output 1
mississippi
S is mississippi. Since the length of S is not greater than 100, output S.
Sample Input 2
7 a 1000000000000000000 t 1000000000000000000 c 1000000000000000000 o 1000000000000000000 d 1000000000000000000 e 1000000000000000000 r 1000000000000000000
Sample Output 2
Too Long
The length of S is 7\times 10^{18}, so output Too Long.
Sample Input 3
1 a 100
Sample Output 3
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Sample Input 4
6 g 4 j 1 m 4 e 4 d 3 i 4
Sample Output 4
ggggjmmmmeeeedddiiii
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
00, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 のボタンがある電卓があります。
この電卓で文字列 x が表示されている時に b のボタンを押すと、表示される文字列は x の末尾に b を連結したものとなります。
最初、電卓には空文字列 ( 0 文字の文字列 ) が表示されています。
この電卓に文字列 S を表示させるためにボタンを押す回数の最小値を求めてください。
制約
- S は
0,1,2,3,4,5,6,7,8,9からなる長さ 1 以上 1000 以下の列 - S の先頭は
0でない
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを整数として出力せよ。
入力例 1
1000000007
出力例 1
6
1000000007 を表示させるには、 1, 00, 00, 00, 00, 7 のボタンをこの順に押せばよく、ボタンを押した回数は 6 回で、これが達成可能な最小値です。
入力例 2
998244353
出力例 2
9
入力例 3
32000
出力例 3
4
Score : 200 points
Problem Statement
There is a calculator with the buttons 00, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
When a string x is displayed on this calculator and you press a button b, the resulting displayed string becomes the string x with b appended to its end.
Initially, the calculator displays the empty string (a string of length 0).
Find the minimum number of button presses required to display the string S on this calculator.
Constraints
- S is a string of length at least 1 and at most 1000, consisting of
0,1,2,3,4,5,6,7,8,9. - The first character of S is not
0.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer as an integer.
Sample Input 1
1000000007
Sample Output 1
6
To display 1000000007, you can press the buttons 1, 00, 00, 00, 00, 7 in this order. The total number of button presses is 6, and this is the minimum possible.
Sample Input 2
998244353
Sample Output 2
9
Sample Input 3
32000
Sample Output 3
4
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
英小文字からなる長さ M の文字列 N 個 S_1,S_2,\dots,S_N が与えられます。ここで、S_i は互いに異なります。
これらを並び替えた文字列の列 T_1,T_2,\dots,T_N であって、以下の条件を満たすものが存在するか判定してください。
- 1 \le i \le N-1 を満たす全ての整数 i に対して、T_i を 1 文字だけ別の英小文字に変えて T_{i+1} にすることが出来る。
制約
- 2 \le N \le 8
- 1 \le M \le 5
- S_i は英小文字からなる長さ M の文字列である。(1 \le i \le N)
- S_i は互いに異なる。
入力
入力は以下の形式で標準入力から与えられる。
N M S_1 S_2 \vdots S_N
出力
問題文の条件を満たす列が存在するならば Yes を、そうでないならば No を出力せよ。
入力例 1
4 4 bbed abcd abed fbed
出力例 1
Yes
abcd , abed , bbed , fbed の順に並び替えると条件を満たします。
入力例 2
2 5 abcde abced
出力例 2
No
どのように並び替えても条件を満たすことは出来ません。
入力例 3
8 4 fast face cast race fact rice nice case
出力例 3
Yes
Score : 250 points
Problem Statement
You are given N strings S_1,S_2,\dots,S_N, each of length M, consisting of lowercase English letter. Here, S_i are pairwise distinct.
Determine if one can rearrange these strings to obtain a new sequence of strings T_1,T_2,\dots,T_N such that:
- for all integers i such that 1 \le i \le N-1, one can alter exactly one character of T_i to another lowercase English letter to make it equal to T_{i+1}.
Constraints
- 2 \le N \le 8
- 1 \le M \le 5
- S_i is a string of length M consisting of lowercase English letters. (1 \le i \le N)
- S_i are pairwise distinct.
Input
The input is given from Standard Input in the following format:
N M S_1 S_2 \vdots S_N
Output
Print Yes if one can obtain a conforming sequence; print No otherwise.
Sample Input 1
4 4 bbed abcd abed fbed
Sample Output 1
Yes
One can rearrange them in this order: abcd, abed, bbed, fbed. This sequence satisfies the condition.
Sample Input 2
2 5 abcde abced
Sample Output 2
No
No matter how the strings are rearranged, the condition is never satisfied.
Sample Input 3
8 4 fast face cast race fact rice nice case
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
xy 平面上に 1 から N までの番号が付いた N 個の点があります。
点 i は座標 (X_i,Y_i) にあり、相異なる 2 点は異なる位置に存在します。
この N 点から 3 点を選ぶとき、選ばれた 3 点を線分で結んだ図形が正の面積を持つ三角形になるような点の選び方の総数を求めてください。
制約
- 入力は全て整数である
- 3 \le N \le 300
- -10^9 \le X_i,Y_i \le 10^9
- i \neq j ならば (X_i,Y_i) \neq (X_j,Y_j)
入力
入力は以下の形式で標準入力から与えられる。
N X_1 Y_1 X_2 Y_2 \dots X_N Y_N
出力
答えを整数として出力せよ。
入力例 1
4 0 1 1 3 1 1 -1 -1
出力例 1
3
点を図示すると、以下のようになります。

三角形をなすような点の選び方は、 \{1,2,3\},\{1,3,4\},\{2,3,4\} の 3 つです。
入力例 2
20 224 433 987654321 987654321 2 0 6 4 314159265 358979323 0 0 -123456789 123456789 -1000000000 1000000000 124 233 9 -6 -4 0 9 5 -7 3 333333333 -333333333 -9 -1 7 -10 -1 5 324 633 1000000000 -1000000000 20 0
出力例 2
1124
Score : 300 points
Problem Statement
In the xy-plane, we have N points numbered 1 through N.
Point i is at the coordinates (X_i,Y_i). Any two different points are at different positions.
Find the number of ways to choose three of these N points so that connecting the chosen points with segments results in a triangle with a positive area.
Constraints
- All values in input are integers.
- 3 \le N \le 300
- -10^9 \le X_i,Y_i \le 10^9
- (X_i,Y_i) \neq (X_j,Y_j) if i \neq j.
Input
Input is given from Standard Input in the following format:
N X_1 Y_1 X_2 Y_2 \dots X_N Y_N
Output
Print the answer as an integer.
Sample Input 1
4 0 1 1 3 1 1 -1 -1
Sample Output 1
3
The figure below illustrates the points.

There are three ways to choose points that form a triangle: \{1,2,3\},\{1,3,4\},\{2,3,4\}.
Sample Input 2
20 224 433 987654321 987654321 2 0 6 4 314159265 358979323 0 0 -123456789 123456789 -1000000000 1000000000 124 233 9 -6 -4 0 9 5 -7 3 333333333 -333333333 -9 -1 7 -10 -1 5 324 633 1000000000 -1000000000 20 0
Sample Output 2
1124
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
X と . からなる文字列 S が与えられます。
S に対して、次の操作を 0 回以上 K 回以下行うことができます。
.をXに置き換える
操作後に、X を最大で何個連続させることができますか?
制約
- 1 \leq |S| \leq 2 \times 10^5
- S の各文字は
Xまたは.である - 0 \leq K \leq 2 \times 10^5
- K は整数である
入力
入力は以下の形式で標準入力から与えられる。
S K
出力
答えを出力せよ。
入力例 1
XX...X.X.X. 2
出力例 1
5
S の 7 文字目と 9 文字目の . を X に置き換えて XX...XXXXX. とすると、6 文字目から 10 文字目で X が 5 個連続しています。
X を 6 個以上連続させることはできないので、答えは 5 です。
入力例 2
XXXX 200000
出力例 2
4
操作を行う回数は 0 回でも構いません。
Score : 400 points
Problem Statement
Given is a string S consisting of X and ..
You can do the following operation on S between 0 and K times (inclusive).
- Replace a
.with anX.
What is the maximum possible number of consecutive Xs in S after the operations?
Constraints
- 1 \leq |S| \leq 2 \times 10^5
- Each character of S is
Xor.. - 0 \leq K \leq 2 \times 10^5
- K is an integer.
Input
Input is given from Standard Input in the following format:
S K
Output
Print the answer.
Sample Input 1
XX...X.X.X. 2
Sample Output 1
5
After replacing the Xs at the 7-th and 9-th positions with X, we have XX...XXXXX., which has five consecutive Xs at 6-th through 10-th positions.
We cannot have six or more consecutive Xs, so the answer is 5.
Sample Input 2
XXXX 200000
Sample Output 2
4
It is allowed to do zero operations.
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
N 個の島と、2 つの島の間を双方向に結ぶ M 本の橋があり、
それぞれ島 1, 島 2, \ldots, 島 N および 橋 1, 橋 2, \ldots, 橋 M と番号づけられています。
橋 i は島 U_i と島 V_i を相互に結んでおり、どちらの方向に移動するにも T_i だけ時間がかかります。
ここで、橋の両端が同一の島であるような橋は存在しませんが、ある 2 つの島の間が 2 本以上の橋で直接繋がれている可能性はあります。
また、どの 2 つの島の間もいくつかの橋をわたって移動することができます。
Q 個の問題が与えられるので、各問題に対する答えを求めてください。i 番目の問題は次のようなものです。
相異なる K_i 本の橋、橋 B_{i,1}, 橋 B_{i,2}, \ldots, 橋 B_{i,K_i} が与えられます。
これらの橋をすべて 1 回以上わたり、島 1 から島 N まで移動するために必要な時間の最小値を求めてください。
ただし、島 1 から島 N までの移動において、橋をわたって島の間を移動するのにかかる時間以外は無視できるものとします。
また、与えられた橋はどの順で、またどの向きにわたってもかまいません。
制約
- 2\leq N \leq 400
- N-1\leq M \leq 2\times 10^5
- 1\leq U_i<V_i\leq N
- 1\leq T_i\leq 10^9
- 1\leq Q\leq 3000
- 1\leq K_i\leq 5
- 1\leq B_{i,1}<B_{i,2}<\cdots<B_{i,K_i}\leq M
- 入力はすべて整数
- どの 2 つの島の間もいくつかの橋をわたって移動することができる。
入力
入力は以下の形式で標準入力から与えられる。
N M
U_1 V_1 T_1
U_2 V_2 T_2
\vdots
U_M V_M T_M
Q
K_1
B_{1,1} B_{1,2} \cdots B_{1,{K_1}}
K_2
B_{2,1} B_{2,2} \cdots B_{2,{K_2}}
\vdots
K_Q
B_{Q,1} B_{Q,2} \cdots B_{Q,{K_Q}}
出力
Q 行出力せよ。i 行目(1\leq i\leq Q)には i 番目の問題に対する答えを整数で出力せよ。
入力例 1
3 5 1 2 10 1 3 20 1 3 30 2 3 15 2 3 25 2 1 1 2 3 5
出力例 1
25 70
1 番目の問題では橋 1 をわたった上で島 1 から島 3 へ移動するために必要な時間の最小値を求める必要があります。
橋 1 を使って島 1 から島 2 に移動した後に橋 4 を使って島 2 から島 3 に移動したとき時間は 10+15=25 だけかかり、このときが最小です。
よって、1 行目には 25 を出力します。
2 番目の問題では橋 3 および橋 5 をわたった上で島 1 から島 3 へ移動するために必要な時間の最小値を求める必要があります。
橋 3 を通って島 1 から島 3 に移動した後に橋 5 を使って島 2 へ移動し、橋 4 を使用して島 3 に移動したとき時間は 30+25+15=70 だけかかり、このときが最小です。よって、2 行目には 70 を出力します。
入力例 2
6 6 1 5 1 2 5 1 2 4 1 3 4 1 3 6 1 1 6 1 2 5 1 2 3 4 5 1 5
出力例 2
5 3
各問題において指定された橋をわたる際、わたる方向はどちらでも問題ありません。
入力例 3
5 5 1 2 1000000000 2 3 1000000000 3 4 1000000000 4 5 1000000000 1 5 1000000000 1 1 3
出力例 3
4000000000
答えが 32bit 整数型に収まらない可能性があることに注意してください。
Score : 450 points
Problem Statement
There are N islands and M bidirectional bridges connecting two islands. The islands and bridges are numbered 1, 2, \ldots, N and 1, 2, \ldots, M, respectively.
Bridge i connects islands U_i and V_i, and the time it takes to cross it in either direction is T_i.
No bridge connects an island to itself, but it is possible for two islands to be directly connected by more than one bridge.
One can travel between any two islands using some bridges.
You are given Q queries, so answer each of them. The i-th query is as follows:
You are given K_i distinct bridges: bridges B_{i,1}, B_{i,2}, \ldots, B_{i,K_i}.
Find the minimum time required to travel from island 1 to island N using each of these bridges at least once.
Only consider the time spent crossing bridges.
You can cross the given bridges in any order and in any direction.
Constraints
- 2 \leq N \leq 400
- N-1 \leq M \leq 2 \times 10^5
- 1 \leq U_i < V_i \leq N
- 1 \leq T_i \leq 10^9
- 1 \leq Q \leq 3000
- 1 \leq K_i \leq 5
- 1 \leq B_{i,1} < B_{i,2} < \cdots < B_{i,K_i} \leq M
- All input values are integers.
- It is possible to travel between any two islands using some bridges.
Input
The input is given from Standard Input in the following format:
N M
U_1 V_1 T_1
U_2 V_2 T_2
\vdots
U_M V_M T_M
Q
K_1
B_{1,1} B_{1,2} \cdots B_{1,{K_1}}
K_2
B_{2,1} B_{2,2} \cdots B_{2,{K_2}}
\vdots
K_Q
B_{Q,1} B_{Q,2} \cdots B_{Q,{K_Q}}
Output
Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer to the i-th query as an integer.
Sample Input 1
3 5 1 2 10 1 3 20 1 3 30 2 3 15 2 3 25 2 1 1 2 3 5
Sample Output 1
25 70
For the first query, we need to find the minimum time to travel from island 1 to island 3 while using bridge 1. The minimum time is achieved by using bridge 1 to move from island 1 to island 2, then using bridge 4 to move from island 2 to island 3. The time taken is 10 + 15 = 25. Hence, print 25 on the first line.
For the second query, we need to find the minimum time to travel from island 1 to island 3 while using both bridges 3 and 5. The minimum time is achieved by using bridge 3 to move from island 1 to island 3, then using bridge 5 to move to island 2, and finally using bridge 4 to return to island 3. The time taken is 30 + 25 + 15 = 70. Hence, print 70 on the second line.
Sample Input 2
6 6 1 5 1 2 5 1 2 4 1 3 4 1 3 6 1 1 6 1 2 5 1 2 3 4 5 1 5
Sample Output 2
5 3
For each query, you can cross the specified bridges in either direction.
Sample Input 3
5 5 1 2 1000000000 2 3 1000000000 3 4 1000000000 4 5 1000000000 1 5 1000000000 1 1 3
Sample Output 3
4000000000
Beware that the answer may not fit in a 32-bit integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
整数 N,M と長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
k=0,1,\ldots,M-1 に対して以下の問題を解いてください。
整数列 B=(B_1,B_2,\ldots,B_N) を B_i が A_i+k を M で割ったあまりとなるように定義したときの、 B の転倒数を求めてください。
転倒数とは
数列 (A_1,A_2,\dots,A_N) の転倒数とは、 1 \le i < j \le N かつ A_i > A_j を満たす整数組 (i,j) の個数を指します。制約
- 1\le N,M\le 2\times 10^5
- 0\le A_i< M
- 入力される値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_N
出力
M 行出力せよ。
i 行目 (1\le i\le M) には、 k=i-1 の場合の答えを出力せよ。
入力例 1
3 3 2 1 0
出力例 1
3 1 1
- k=0 のとき: B=(2 , 1 ,0) となります。このときの転倒数は 3 です。
- k=1 のとき: B=(0,2,1) となります。このときの転倒数は 1 です。
- k=2 のとき: B=(1,0,2) となります。このときの転倒数は 1 です。
入力例 2
5 6 5 3 5 0 1
出力例 2
7 3 3 1 1 5
入力例 3
7 7 0 1 2 3 4 5 6
出力例 3
0 6 10 12 12 10 6
Score : 500 points
Problem Statement
You are given integers N, M and a length-N sequence of non-negative integers A = (A_1, A_2, \ldots, A_N).
For k = 0, 1, \ldots, M-1, solve the following problem:
Define an integer sequence B = (B_1, B_2, \ldots, B_N) so that B_i is the remainder of A_i + k when divided by M. Find the inversion number in B.
What is the inversion number?
The inversion number of a sequence (A_1, A_2, \dots, A_N) is the number of integer pairs (i, j) satisfying 1 \le i < j \le N and A_i > A_j.Constraints
- 1 \le N,M \le 2\times 10^5
- 0 \le A_i < M
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_N
Output
Print M lines.
The i-th line (1 \le i \le M) should contain the answer for the case k = i-1.
Sample Input 1
3 3 2 1 0
Sample Output 1
3 1 1
- For k=0: B=(2, 1, 0). The inversion number is 3.
- For k=1: B=(0, 2, 1). The inversion number is 1.
- For k=2: B=(1, 0, 2). The inversion number is 1.
Sample Input 2
5 6 5 3 5 0 1
Sample Output 2
7 3 3 1 1 5
Sample Input 3
7 7 0 1 2 3 4 5 6
Sample Output 3
0 6 10 12 12 10 6