Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
N 両編成の電車があります。 この電車の前から K 両目の車両は後ろから何両目ですか?
制約
- 1 \leq K \leq N \leq 100
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K
出力
答えを 1 行で出力せよ。
入力例 1
5 2
出力例 1
4
5 両編成の電車で前から 2 両目の車両は後ろから 4 両目です。
入力例 2
1 1
出力例 2
1
入力例 3
99 50
出力例 3
50
Score : 100 points
Problem Statement
There is a train consisting of N cars. What is the position, counted from the back, of the car that is K-th from the front of this train?
Constraints
- 1 \leq K \leq N \leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K
Output
If the car is X-th from the back, output the integer X in one line.
Sample Input 1
5 2
Sample Output 1
4
For a train consisting of five cars, the car that is second from the front is fourth from the back.
Sample Input 2
1 1
Sample Output 2
1
Sample Input 3
99 50
Sample Output 3
50
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
o と x からなる長さ N の文字列 S が与えられます。
N 個の椅子が左右一列に並んでおり、左から i 番目の椅子には、S の i 文字目が o ならば人が座っており、x ならば人は座っていません。
以下の条件をすべて満たす椅子は何個あるか求めてください。
- 人が座っていない
- 左に椅子が無い、または左の椅子に人が座っていない
- 右に椅子が無い、または右の椅子に人が座っていない
制約
- 1 \leq N \leq 100
- N は整数
- S は
oとxからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを 1 行で出力せよ。
入力例 1
8 xxoxxxox
出力例 1
2
左から 1,5 番目の椅子のみが条件を満たします。
入力例 2
5 ooooo
出力例 2
0
入力例 3
1 x
出力例 3
1
Score : 200 points
Problem Statement
You are given a string S of length N consisting of o and x.
N chairs are arranged in a row from left to right. If the i-th character of S is o, a person is sitting in the i-th chair from the left; if it is x, no person is sitting there.
Find the number of chairs that satisfy all of the following conditions.
- No person is sitting in it.
- There is no chair to its left, or no person is sitting in the chair to its left.
- There is no chair to its right, or no person is sitting in the chair to its right.
Constraints
- 1 \leq N \leq 100
- N is an integer.
- S is a string of length N consisting of
oandx.
Input
The input is given from Standard Input in the following format:
N S
Output
Output the answer in one line.
Sample Input 1
8 xxoxxxox
Sample Output 1
2
Only the first and fifth chairs from the left satisfy the conditions.
Sample Input 2
5 ooooo
Sample Output 2
0
Sample Input 3
1 x
Sample Output 3
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
o と x からなる長さ N の文字列 S が与えられます。
N 個の袋が一列に並んでいて、各袋にはお菓子が 1 個ずつ入っています。
i 番目の袋には、S の i 文字目が o ならば「当たり」と書かれており、x ならば「はずれ」と書かれています。
k=1,2,\dots,N について以下の問題を解いてください。
高橋君は列の先頭から k 個の袋を受け取り、入っているお菓子を食べ、袋は持っておきます。
その後、以下の行動を可能な限り繰り返します。
高橋君が食べることのできるお菓子の個数を求めてください。
- 高橋君が持っている「当たり」と書かれた袋を 1 個捨て、列の先頭の袋を受け取り、中のお菓子を食べてその袋を持っておく。ただし、列にまだ袋が残っていて、かつ「当たり」と書かれた袋を持っている場合にしか行動はできない。
袋を受け取った場合、その袋は列から取り除かれることに注意してください。
制約
- 1 \leq N \leq 8 \times 10^5
- N は整数
- S は
oとxからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを合計 N 行で出力せよ。
l 行目には、k=l のときの答えを出力せよ。
入力例 1
5 oxoxo
出力例 1
2 4 5 5 5
k=1 のとき、受け取った袋には「当たり」と書かれています。 それを捨てて受け取った袋には「はずれ」と書かれており、それ以上の行動はできません。
入力例 2
3 ooo
出力例 2
3 3 3
入力例 3
1 x
出力例 3
1
Score : 300 points
Problem Statement
You are given a string S of length N consisting of o and x.
N bags are arranged in a row, and each bag contains one sweet.
On the i-th bag, "hit" is written if the i-th character of S is o, and "miss" is written if it is x.
For each k=1,2,\dots,N, solve the following problem.
Takahashi receives the first k bags from the front of the row, eats the sweets inside them, and keeps the bags.
Then, he repeats the following action as many times as possible.Find the number of sweets he can eat.
- He discards one bag marked "hit" that he is holding, receives the bag at the front of the row, eats the sweet inside it, and keeps that bag. This action can only be performed when there is still a bag remaining in the row and he is holding a bag marked "hit".
Note that when he receives a bag, that bag is removed from the row.
Constraints
- 1 \leq N \leq 8 \times 10^5
- N is an integer.
- S is a string of length N consisting of
oandx.
Input
The input is given from Standard Input in the following format:
N S
Output
Output a total of N lines.
The l-th line should contain the answer for k=l.
Sample Input 1
5 oxoxo
Sample Output 1
2 4 5 5 5
For k=1, the bag he receives is marked "hit". He discards it, and the bag he then receives is marked "miss", so he cannot perform any further actions.
Sample Input 2
3 ooo
Sample Output 2
3 3 3
Sample Input 3
1 x
Sample Output 3
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
あるゲームには N 人のプレイヤー 1,2,\dots,N がいます。
このゲームは 2 人のプレイヤーが 1 対 1 で戦う形式です。
N 人のプレイヤーによるトーナメント戦が M 回行われ、m 回目のトーナメント戦の決勝にはプレイヤー A_m,B_m の 2 人が勝ち上がりました。
以下の条件を満たすような 2 整数 x,y の組がいくつあるかを求めてください。
- 1 \leq x \lt y \leq N
- どのトーナメント戦においても、プレイヤー x とプレイヤー y の少なくとも一方が決勝に勝ち上がっている
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq A_i \lt B_i \leq N
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
出力
答えを 1 行で出力せよ。
入力例 1
5 5 1 2 3 4 1 3 2 3 2 5
出力例 1
1
(x,y)=(2,3) のとき条件は満たされ、それ以外のとき条件は満たされません。
たとえば 4 回目のトーナメントではプレイヤー 1,4 はいずれも決勝に勝ち上がっていないので、(x,y)=(1,4) は条件を満たしません。
入力例 2
7 8 2 4 1 3 1 7 1 3 1 2 1 6 1 5 1 3
出力例 2
2
条件を満たす (x,y) の組は、(1,2),(1,4) の 2 つです。
入力例 3
5 8 1 2 2 4 1 3 1 3 1 2 1 2 1 5 1 2
出力例 3
2
Score : 400 points
Problem Statement
There is a game with N players 1,2,\dots,N.
This game is played in a format where two players face each other one-on-one.
A tournament among the N players was held M times, and the two players A_m and B_m advanced to the final of the m-th tournament.
Find the number of pairs of integers x and y satisfying the following conditions.
- 1 \leq x \lt y \leq N
- In every tournament, at least one of players x and y advanced to the final.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq A_i \lt B_i \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
Output
Output the answer in one line.
Sample Input 1
5 5 1 2 3 4 1 3 2 3 2 5
Sample Output 1
1
The conditions are satisfied when (x,y)=(2,3), and not satisfied otherwise.
For example, in the fourth tournament, neither player 1 nor player 4 advanced to the final, so (x,y)=(1,4) does not satisfy the conditions.
Sample Input 2
7 8 2 4 1 3 1 7 1 3 1 2 1 6 1 5 1 3
Sample Output 2
2
There are two pairs (x,y) satisfying the conditions: (1,2) and (1,4).
Sample Input 3
5 8 1 2 2 4 1 3 1 3 1 2 1 2 1 5 1 2
Sample Output 3
2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
o と x からなる長さ N の文字列 S が与えられます。
ただし、S には o が K 個以上含まれることが保証されます。
高橋君はあるゲームを N 回行いました。
i 回目のゲームでは、S の i 文字目が o ならば高橋君は勝利し、x ならば高橋君は敗北しました。
高橋君は以下の条件を満たすような 2 整数 l,r を一つ選びます。
- 1 \leq l \leq r \leq N
- l 回目から r 回目までのゲームで K 勝以上している
このとき、l 回目から r 回目までのゲームでの勝率としてあり得る値の最大値を求めてください。
制約
- 1 \leq K \leq N \leq 10^6
- N と K は整数
- S は
oとxからなる長さ N の文字列 - S は
oを K 個以上含む
入力
入力は以下の形式で標準入力から与えられる。
N K S
出力
答えを 1 行で出力せよ。 真の答えとの絶対誤差または相対誤差が 10^{-6} 以下であれば正解として扱われる。
入力例 1
10 4 oxooxoxxox
出力例 1
0.6666666666
(l,r) として (1,6) を選ぶと勝率は \frac{2}{3} です。
条件を満たす範囲で勝率をこれより大きくすることはできません。
入力例 2
5 1 xxoxx
出力例 2
1
入力例 3
16 10 xxxoxooooxoxoooo
出力例 3
0.769230769230769
Score : 475 points
Problem Statement
You are given a string S of length N consisting of o and x.
It is guaranteed that S contains at least K occurrences of o.
Takahashi played a certain game N times.
In the i-th game, he won if the i-th character of S is o, and lost if it is x.
Takahashi chooses a pair of integers l and r satisfying the following conditions.
- 1 \leq l \leq r \leq N
- He won at least K times in the games from the l-th through the r-th.
Find the maximum possible value of the win rate in the games from the l-th through the r-th.
Constraints
- 1 \leq K \leq N \leq 10^6
- N and K are integers.
- S is a string of length N consisting of
oandx. - S contains at least K occurrences of
o.
Input
The input is given from Standard Input in the following format:
N K S
Output
Output the answer in one line. Answers with an absolute or relative error of at most 10^{-6} from the true answer will be accepted.
Sample Input 1
10 4 oxooxoxxox
Sample Output 1
0.6666666666
Choosing (1,6) as (l,r) gives a win rate of \frac{2}{3}.
It is impossible to make the win rate larger than this while satisfying the conditions.
Sample Input 2
5 1 xxoxx
Sample Output 2
1
Sample Input 3
16 10 xxxoxooooxoxoooo
Sample Output 3
0.769230769230769
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
長さ N の正整数列 A が与えられます。
N 頂点の重み付き完全無向グラフがあります。
各頂点には 1,2,\dots,N の番号が付いています。
1 \leq i \lt j \leq N を満たす i,j について、頂点 i と頂点 j を結ぶ辺の重みは A_i と A_j の最大公約数です。
このグラフの全域木に含まれる辺の重みの総和としてあり得る値の最大値を求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_1 \lt A_2 \lt \dots \lt A_N \leq 10^6
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを 1 行で出力せよ。
入力例 1
3 4 6 12
出力例 1
10
完全グラフには以下の 3 本の辺が張られています。
- 頂点 1 と頂点 2 を結ぶ重み 2 の辺
- 頂点 1 と頂点 3 を結ぶ重み 4 の辺
- 頂点 2 と頂点 3 を結ぶ重み 6 の辺
2 本目の辺と 3 本目の辺を選ぶと重みの総和は 10 になり、これがあり得る最大値です。
入力例 2
5 5 14 15 21 42
出力例 2
43
入力例 3
2 1 1000000
出力例 3
1
Score : 500 points
Problem Statement
You are given a sequence of positive integers A of length N.
There is a weighted complete undirected graph with N vertices.
The vertices are numbered 1,2,\dots,N.
For i and j satisfying 1 \leq i \lt j \leq N, the weight of the edge connecting vertices i and j is the greatest common divisor of A_i and A_j.
Find the maximum possible value of the sum of the weights of the edges included in a spanning tree of this graph.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_1 \lt A_2 \lt \dots \lt A_N \leq 10^6
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Output the answer in one line.
Sample Input 1
3 4 6 12
Sample Output 1
10
The complete graph has the following three edges.
- The edge connecting vertices 1 and 2 with weight 2
- The edge connecting vertices 1 and 3 with weight 4
- The edge connecting vertices 2 and 3 with weight 6
Choosing the second and third edges gives a sum of weights of 10, which is the maximum possible value.
Sample Input 2
5 5 14 15 21 42
Sample Output 2
43
Sample Input 3
2 1 1000000
Sample Output 3
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 575 点
問題文
a, b からなる K 個の文字列 S_1, S_2, \ldots, S_K が与えられます。また、i > K なる整数 i に対し、文字列 S_i を S_{i-1}, S_{i-2}, \ldots, S_{i-K} をこの順に連結した文字列として定めます。
あなたは、文字列 T に対して以下の操作を行います。
- |S_N| を S_N の長さとして、j = 1, 2, \ldots, |S_N| に対し以下の操作を順に行う。
- S_N の j 文字目が
aのとき、T の先頭の文字を末尾に移動させる。 - S_N の j 文字目が
bのとき、T 全体を反転させる。すなわち、T の文字の並びを逆にする。
- S_N の j 文字目が
一連の操作を終えた後の文字列 T を求めてください。
制約
- 2 \leq K \leq 100
- K は整数
- S_i は
a,bからなる空でない文字列 (1 \leq i \leq K) - S_1, S_2, \ldots, S_K の長さの和は 2 \times 10^5 以下
- 1 \leq N \leq 10^{18}
- N は整数
- T は英小文字からなる長さ 1 以上 2 \times 10^5 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
K S_1 S_2 \vdots S_K N T
出力
答えを出力せよ。
入力例 1
3 a aa b 5 abc
出力例 1
cab
S_1 = a、S_2 = aa、S_3 = b、S_4 = baaa、S_5 = baaabaa です。
操作によって文字列 T は abc → cba → bac → acb → cba → abc → bca → cab と変化するため、一連の操作を終えた後の文字列 T は cab となります。
入力例 2
2 a ba 6 fibonacci
出力例 2
canobific
入力例 3
5 aba a bb ba aab 1000000000000000000 abcba
出力例 3
aabcb
Score : 575 points
Problem Statement
You are given K strings S_1, S_2, \ldots, S_K consisting of a and b. Also, for an integer i > K, define the string S_i as the concatenation of S_{i-1}, S_{i-2}, \ldots, S_{i-K} in this order.
You perform the following operation on a string T.
- Perform the following operations for j = 1, 2, \ldots, |S_N| in order, where |S_N| is the length of S_N.
- If the j-th character of S_N is
a, move the first character of T to the end. - If the j-th character of S_N is
b, reverse T entirely. That is, reverse the order of the characters of T.
- If the j-th character of S_N is
Find the string T after the series of operations.
Constraints
- 2 \leq K \leq 100
- K is an integer.
- S_i is a non-empty string consisting of
aandb. (1 \leq i \leq K) - The sum of the lengths of S_1, S_2, \ldots, S_K is at most 2 \times 10^5.
- 1 \leq N \leq 10^{18}
- N is an integer.
- T is a string consisting of lowercase English letters with length between 1 and 2 \times 10^5, inclusive.
Input
The input is given from Standard Input in the following format:
K S_1 S_2 \vdots S_K N T
Output
Output the answer.
Sample Input 1
3 a aa b 5 abc
Sample Output 1
cab
S_1 = a, S_2 = aa, S_3 = b, S_4 = baaa, and S_5 = baaabaa.
Through the operations, the string T changes as abc → cba → bac → acb → cba → abc → bca → cab, so the string T after the series of operations is cab.
Sample Input 2
2 a ba 6 fibonacci
Sample Output 2
canobific
Sample Input 3
5 aba a bb ba aab 1000000000000000000 abcba
Sample Output 3
aabcb