Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
1,2,\dots,N の番号が付いた N 個の箱があります。最初は全ての箱が空です。
これから Q 個のボールが順番にやってきます。
高橋君は、数列 X=(X_1,X_2,\dots,X_Q) に従ってボールを箱に入れます。
具体的には、 i 番目にやってきたボールに次の処理を行います。
- X_i \ge 1 である場合 : このボールを、箱 X_i に入れる。
- X_i = 0 である場合 : このボールを、現在入っているボールが最も少ない箱のうち番号が最小である箱に入れる。
それぞれのボールをどの箱に入れたかを求めてください。
制約
- 入力は全て整数
- 1 \le N \le 100
- 1 \le Q \le 100
- 0 \le X_i \le N
入力
入力は以下の形式で標準入力から与えられる。
N Q X_1 X_2 \dots X_Q
出力
i 番目にやってきたボールを箱 B_i に入れたとき、次の形式に従って出力せよ。
B_1 B_2 \dots B_Q
入力例 1
4 5 2 0 3 0 0
出力例 1
2 1 3 4 1
箱が 4 つあり、ボールは 5 個やってきます。
- 最初、全ての箱は空です。
- 各箱に入っているボールの数は箱 1 から順に 0,0,0,0 個です。
- X_1=2 なので、 1 番目にやってきたボールを箱 2 に入れます。
- 各箱に入っているボールの数は箱 1 から順に 0,1,0,0 個となります。
- X_2=0 なので、 2 番目にやってきたボールを現在入っているボールが最も少ない箱のうち番号が最小である箱である箱 1 に入れます。
- 各箱に入っているボールの数は箱 1 から順に 1,1,0,0 個となります。
- X_3=3 なので、 3 番目にやってきたボールを箱 3 に入れます。
- 各箱に入っているボールの数は箱 1 から順に 1,1,1,0 個となります。
- X_4=0 なので、 4 番目にやってきたボールを現在入っているボールが最も少ない箱のうち番号が最小である箱である箱 4 に入れます。
- 各箱に入っているボールの数は箱 1 から順に 1,1,1,1 個となります。
- X_5=0 なので、 5 番目にやってきたボールを現在入っているボールが最も少ない箱のうち番号が最小である箱である箱 1 に入れます。
- 各箱に入っているボールの数は箱 1 から順に 2,1,1,1 個となります。
各ボールを、やってきた順に箱 2,1,3,4,1 に入れました。よって、 2 1 3 4 1 と出力します。
入力例 2
3 7 1 1 0 0 0 0 0
出力例 2
1 1 2 3 2 3 1
入力例 3
6 20 4 6 0 3 4 2 6 5 2 3 0 3 2 5 0 3 5 0 2 0
出力例 3
4 6 1 3 4 2 6 5 2 3 1 3 2 5 1 3 5 4 2 6
Score : 200 points
Problem Statement
There are N boxes numbered 1,2,\dots,N. Initially, all boxes are empty.
Q balls will come in order.
Takahashi will put the balls into the boxes according to the sequence X=(X_1,X_2,\dots,X_Q).
Specifically, he performs the following process for the i-th ball:
- If X_i \ge 1: Put this ball into box X_i.
- If X_i = 0: Put this ball into the box with the smallest number among those containing the fewest balls.
Find which box each ball was put into.
Constraints
- All input values are integers.
- 1 \le N \le 100
- 1 \le Q \le 100
- 0 \le X_i \le N
Input
The input is given from Standard Input in the following format:
N Q X_1 X_2 \dots X_Q
Output
If the i-th ball was put into box B_i, output in the following format:
B_1 B_2 \dots B_Q
Sample Input 1
4 5 2 0 3 0 0
Sample Output 1
2 1 3 4 1
There are 4 boxes, and 5 balls come.
- Initially, all boxes are empty.
- The numbers of balls in box 1,2,3,4 are 0,0,0,0, respectively.
- Since X_1=2, put the 1st ball into box 2.
- The numbers of balls in box 1,2,3,4 are 0,1,0,0, respectively.
- Since X_2=0, put the 2nd ball into box 1, which has the smallest number among those containing the fewest balls.
- The numbers of balls in box 1,2,3,4 are 1,1,0,0, respectively.
- Since X_3=3, put the 3rd ball into box 3.
- The numbers of balls in box 1,2,3,4 are 1,1,1,0, respectively.
- Since X_4=0, put the 4th ball into box 4, which has the smallest number among those containing the fewest balls.
- The numbers of balls in box 1,2,3,4 are 1,1,1,1, respectively.
- Since X_5=0, put the 5th ball into box 1, which has the smallest number among those containing the fewest balls.
- The numbers of balls in box 1,2,3,4 are 2,1,1,1, respectively.
The balls were put into boxes 2,1,3,4,1 in order. Thus, output 2 1 3 4 1.
Sample Input 2
3 7 1 1 0 0 0 0 0
Sample Output 2
1 1 2 3 2 3 1
Sample Input 3
6 20 4 6 0 3 4 2 6 5 2 3 0 3 2 5 0 3 5 0 2 0
Sample Output 3
4 6 1 3 4 2 6 5 2 3 1 3 2 5 1 3 5 4 2 6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は歯が左右一列に N 本生えています。現在の高橋君の歯の状態はある文字列 S によって表されます。
S の i 文字目が O のとき、左から i 番目の歯が丈夫であることを表します。S の i 文字目が X のとき、左から i 番目の歯が虫歯にかかっていることを表します。丈夫である歯は虫歯にかかっていません。
高橋君はある連続する K 本の歯が丈夫であるとき、その K 本の歯を使ってイチゴを 1 個食べることができます。イチゴを食べると、その K 本の歯が虫歯にかかり丈夫でなくなります。
このとき、高橋君は最大で何個のイチゴを食べることができるか求めてください。
制約
- 1 \leq K \leq N \leq 100
- N,K は整数
- S は
OとXからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N K S
出力
答えを出力せよ。
入力例 1
7 3 OOXOOOO
出力例 1
1
左から 4 本目の歯から左から 6 本目の歯までの連続する 3 本の丈夫な歯を使ってイチゴを 1 個食べることができます。これ以降、イチゴを食べることができません。また、他にどのような方法でイチゴを食べても 1 個以下しか食べることができません。よって、1 を出力します。
入力例 2
12 2 OXXOOOXOOOOX
出力例 2
3
入力例 3
22 5 XXOOOOOOOOXXOOOOOXXXXX
出力例 3
2
Score : 200 points
Problem Statement
Takahashi has N teeth arranged in a single row from left to right. The current condition of his teeth is represented by a string S.
If the i-th character of S is O, it means that the i-th tooth from the left is healthy. If it is X, it means that the i-th tooth has a cavity. Healthy teeth do not have cavities.
When he has K consecutive healthy teeth, he can eat one strawberry using those K teeth. After eating a strawberry, those K teeth develop cavities and become unhealthy.
Find the maximum number of strawberries he can eat.
Constraints
- 1 \leq K \leq N \leq 100
- N and K are integers.
- S is a string of length N consisting of
OandX.
Input
The input is given from Standard Input in the following format:
N K S
Output
Print the answer.
Sample Input 1
7 3 OOXOOOO
Sample Output 1
1
He can eat one strawberry by using the three consecutive healthy teeth from the 4th to 6th tooth from the left. After this, he cannot eat any more strawberries. Besides, there is no way for him to eat more than one strawberry. Therefore, print 1.
Sample Input 2
12 2 OXXOOOXOOOOX
Sample Output 2
3
Sample Input 3
22 5 XXOOOOOOOOXXOOOOOXXXXX
Sample Output 3
2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君はレベル N の赤い宝石を 1 個持っています。(他に宝石は持っていません。)
高橋君は次の操作を好きなだけ行うことができます。
- レベル n の赤い宝石 (n は 2 以上) を「レベル n-1 の赤い宝石 1 個と、レベル n の青い宝石 X 個」に変換する。
- レベル n の青い宝石 (n は 2 以上) を「レベル n-1 の赤い宝石 1 個と、レベル n-1 の青い宝石 Y 個」に変換する。
高橋君はレベル 1 の青い宝石ができるだけたくさんほしいです。操作によって高橋君はレベル 1 の青い宝石を最大何個入手できますか?
制約
- 1 \leq N \leq 10
- 1 \leq X \leq 5
- 1 \leq Y \leq 5
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X Y
出力
答えを出力せよ。
入力例 1
2 3 4
出力例 1
12
次のような変換を行うことで、高橋君はレベル 1 の青い宝石を 12 個手に入れることができます。
- まず、レベル 2 の赤い宝石 1 個を、レベル 1 の赤い宝石 1 個とレベル 2 の青い宝石 3 個に変換します。
- 操作後の高橋君は、レベル 1 の赤い宝石 1 個とレベル 2 の青い宝石 3 個を持っています。
- 次に、レベル 2 の青い宝石 1 個を、レベル 1 の赤い宝石 1 個とレベル 1 の青い宝石 4 個に変換します。この変換を 3 回繰り返します。
- 操作後の高橋君は、レベル 1 の赤い宝石 4 個とレベル 1 の青い宝石 12 個を持っています。
- これ以上変換を行うことはできません。
12 個より多くレベル 1 の青い宝石を手に入れることはできないので、答えは 12 になります。
入力例 2
1 5 5
出力例 2
0
高橋君がレベル 1 の青い宝石を 1 個も手に入れられない場合もあります。
入力例 3
10 5 5
出力例 3
3942349900
答えが 32 bit 整数に収まらない場合があることに注意してください。
Score : 300 points
Problem Statement
Takahashi has a red jewel of level N. (He has no other jewels.)
Takahashi can do the following operations any number of times.
- Convert a red jewel of level n (n is at least 2) into "a red jewel of level (n-1) and X blue jewels of level n".
- Convert a blue jewel of level n (n is at least 2) into "a red jewel of level (n-1) and Y blue jewels of level (n-1)".
Takahashi wants as many blue jewels of level 1 as possible. At most, how many blue jewels of level 1 can he obtain by the operations?
Constraints
- 1 \leq N \leq 10
- 1 \leq X \leq 5
- 1 \leq Y \leq 5
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X Y
Output
Print the answer.
Sample Input 1
2 3 4
Sample Output 1
12
Takahashi can obtain 12 blue jewels of level 1 by the following conversions.
- First, he converts a red jewel of level 2 into a red jewel of level 1 and 3 blue jewels of level 2.
- After this operation, Takahashi has 1 red jewel of level 1 and 3 blue jewels of level 2.
- Next, he repeats the following conversion 3 times: converting a blue jewel of level 2 into a red jewel of level 1 and 4 blue jewels of level 1.
- After these operations, Takahashi has 4 red jewels of level 1 and 12 blue jewels of level 1.
- He cannot perform a conversion anymore.
He cannot obtain more than 12 blue jewels of level 1, so the answer is 12.
Sample Input 2
1 5 5
Sample Output 2
0
Takahashi may not be able to obtain a blue jewel of level 1.
Sample Input 3
10 5 5
Sample Output 3
3942349900
Note that the answer may not fit into a 32-bit integer type.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
この問題における 11/22 文字列の定義は A 問題および E 問題と同じです。
文字列 T が以下の条件を全て満たすとき、T を 11/22 文字列 と呼びます。
- |T| は奇数である。ここで、|T| は T の長さを表す。
- 1 文字目から \frac{|T|+1}{2} - 1 文字目までが
1である。 - \frac{|T|+1}{2} 文字目が
/である。 - \frac{|T|+1}{2} + 1 文字目から |T| 文字目までが
2である。
例えば 11/22, 111/222, / は 11/22 文字列ですが、1122, 1/22, 11/2222, 22/11, //2/2/211 はそうではありません。
1, 2, / からなる長さ N の文字列 S が与えられます。S は / を 1 個以上含みます。
11/22 文字列であるような S の(連続な)部分文字列の長さの最大値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- S は
1,2,/からなる長さ N の文字列 - S は
/を 1 個以上含む
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
11/22 文字列であるような S の(連続な)部分文字列の長さの最大値を出力せよ。
入力例 1
8 211/2212
出力例 1
5
S の 2 文字目から 6 文字目からなる部分文字列は 11/22 で、これは 11/22 文字列です。S の部分文字列のうち 11/22 文字列であるものはこれが最長です。よって 5 が答えです。
入力例 2
5 22/11
出力例 2
1
入力例 3
22 /1211/2///2111/2222/11
出力例 3
7
Score : 300 points
Problem Statement
The definition of an 11/22 string in this problem is the same as in Problems A and E.
A string T is called an 11/22 string when it satisfies all of the following conditions:
- |T| is odd. Here, |T| denotes the length of T.
- The 1-st through (\frac{|T|+1}{2} - 1)-th characters are all
1. - The (\frac{|T|+1}{2})-th character is
/. - The (\frac{|T|+1}{2} + 1)-th through |T|-th characters are all
2.
For example, 11/22, 111/222, and / are 11/22 strings, but 1122, 1/22, 11/2222, 22/11, and //2/2/211 are not.
You are given a string S of length N consisting of 1, 2, and /, where S contains at least one /.
Find the maximum length of a (contiguous) substring of S that is an 11/22 string.
Constraints
- 1 \leq N \leq 2 \times 10^5
- S is a string of length N consisting of
1,2, and/. - S contains at least one
/.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the maximum length of a (contiguous) substring of S that is an 11/22 string.
Sample Input 1
8 211/2212
Sample Output 1
5
The substring from the 2-nd to 6-th character of S is 11/22, which is an 11/22 string. Among all substrings of S that are 11/22 strings, this is the longest. Therefore, the answer is 5.
Sample Input 2
5 22/11
Sample Output 2
1
Sample Input 3
22 /1211/2///2111/2222/11
Sample Output 3
7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
1 から N の番号がついた N 人を横一列に並べる方法のうち、以下の形式の M 個の条件全てを満たすものが存在するか判定してください。
- 条件:人 A_i と人 B_i は隣り合っている
制約
- 2 \leq N \leq 10^5
- 0 \leq M \leq 10^5
- 1\leq A_i < B_i \leq N
- (A_i,B_i) は相異なる
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 \vdots A_M B_M
出力
条件を満たす並べ方が存在するなら Yes、存在しないなら No と出力せよ。
入力例 1
4 2 1 3 2 3
出力例 1
Yes
例えば 4,1,3,2 の順に並べることで全ての条件を満たすことができます。
入力例 2
4 3 1 4 2 4 3 4
出力例 2
No
どのように並べても全ての条件を満たすことはできません。
Score : 400 points
Problem Statement
Determine whether there is a way to line up N people, numbered 1 to N, in a row side by side to satisfy all of the M conditions in the following format.
- Condition: Person A_i and Person B_i are adjacent.
Constraints
- 2 \leq N \leq 10^5
- 0 \leq M \leq 10^5
- 1\leq A_i < B_i \leq N
- All pairs (A_i,B_i) are distinct.
Input
Input is given from Standard Input in the following format:
N M A_1 B_1 \vdots A_M B_M
Output
If there is a way to line up people to satisfy the conditions, print Yes; if not, print No.
Sample Input 1
4 2 1 3 2 3
Sample Output 1
Yes
One way to satisfy all the conditions is to line them up in the order 4,1,3,2.
Sample Input 2
4 3 1 4 2 4 3 4
Sample Output 2
No
There is no way to line them up to satisfy all the conditions.