実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
ナオヒロ君はモンスターを飼っています。モンスターの現在の体力は H です。
また、ナオヒロ君は N 種類の傷薬を持っています。傷薬は効き目の弱い順に 1 から N までの番号がついています。
傷薬 n をモンスターに与えると、モンスターの体力が P_n 増加します。ここで、P_1 \lt P_2 \lt \dots \lt P_N が成り立ちます。
ナオヒロ君は傷薬を 1 つモンスターに与えることで、モンスターの体力を X 以上にしたいです。
目標を達成できる傷薬のうち最も効き目の弱いものの番号を出力してください。(制約下においてそのような傷薬が存在することが保証されています。)
制約
- 2 \leq N \leq 100
- 1 \leq H \lt X \leq 999
- 1 \leq P_1 \lt P_2 \lt \dots \lt P_N = 999
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N H X P_1 P_2 \dots P_N
出力
目標を達成できる傷薬のうち最も効き目の弱いものの番号を出力せよ。
入力例 1
3 100 200 50 200 999
出力例 1
2
それぞれの傷薬をモンスターに 1 つ与えたときのモンスターの体力の変化は以下の通りです。
- 傷薬 1 をモンスターに与えるとモンスターの体力は 100 + 50 = 150 になります。
- 傷薬 2 をモンスターに与えるとモンスターの体力は 100 + 200 = 300 になります。
- 傷薬 3 をモンスターに与えるとモンスターの体力は 100 + 999 = 1099 になります。
与えた後に体力が X = 200 以上になっている傷薬は、傷薬 2 と傷薬 3 です。このうち最も効き目の弱い傷薬である傷薬 2 が答えになります。
入力例 2
2 10 21 10 999
出力例 2
2
入力例 3
10 500 999 38 420 490 585 613 614 760 926 945 999
出力例 3
4
Score : 100 points
Problem Statement
Naohiro has a monster. The monster's current health is H.
He also has N kinds of potions, numbered from 1 to N in ascending order of effectiveness.
If you give the monster potion n, its health will increase by P_n. Here, P_1 \lt P_2 \lt \dots \lt P_N.
He wants to increase the monster's health to X or above by giving it one of the potions.
Print the number of the least effective potion that can achieve the purpose. (The constraints guarantee that such a potion exists.)
Constraints
- 2 \leq N \leq 100
- 1 \leq H \lt X \leq 999
- 1 \leq P_1 \lt P_2 \lt \dots \lt P_N = 999
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N H X P_1 P_2 \dots P_N
Output
Print the number of the least effective potion that can achieve the purpose.
Sample Input 1
3 100 200 50 200 999
Sample Output 1
2
Below is the change in the monster's health when one of the potions is given to the monster.
- If potion 1 is given, the monster's health becomes 100 + 50 = 150.
- If potion 2 is given, the monster's health becomes 100 + 200 = 300.
- If potion 3 is given, the monster's health becomes 100 + 999 = 1099.
The potions that increase the monster's health to at least X = 200 are potions 2 and 3. The answer is the least effective of them, which is potion 2.
Sample Input 2
2 10 21 10 999
Sample Output 2
2
Sample Input 3
10 500 999 38 420 490 585 613 614 760 926 945 999
Sample Output 3
4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
N 人が一列に並んでいます。列の状態は長さ N の文字列 S で与えられ、前から i 番目の人は S の i 文字目が M
のとき男性、F
のとき女性です。
男女が交互に並んでいるかどうか判定してください。
男性同士が隣り合う箇所も女性同士が隣り合う箇所も存在しないとき、かつ、そのときに限り、男女が交互に並んでいるといいます。
制約
- 1 \leq N \leq 100
- N は整数である
- S は
M
およびF
のみからなる長さ N の文字列である
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
男女が交互に並んでいるとき Yes
、そうでないとき No
と出力せよ。
入力例 1
6 MFMFMF
出力例 1
Yes
男性同士が隣り合う箇所も女性同士が隣り合う箇所も存在せず、男女が交互に並んでいます。
入力例 2
9 FMFMMFMFM
出力例 2
No
入力例 3
1 F
出力例 3
Yes
Score : 100 points
Problem Statement
There is a row of N people. They are described by a string S of length N. The i-th person from the front is male if the i-th character of S is M
, and female if it is F
.
Determine whether the men and women are alternating.
It is said that the men and women are alternating if and only if there is no position where two men or two women are adjacent.
Constraints
- 1 \leq N \leq 100
- N is an integer.
- S is a string of length N consisting of
M
andF
.
Input
The input is given from Standard Input in the following format:
N S
Output
Print Yes
if the men and women are alternating, and No
otherwise.
Sample Input 1
6 MFMFMF
Sample Output 1
Yes
There is no position where two men or two women are adjacent, so the men and women are alternating.
Sample Input 2
9 FMFMMFMFM
Sample Output 2
No
Sample Input 3
1 F
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
数直線の原点に高橋君がいます。高橋君は座標 X にあるゴールに移動しようとしています。
座標 Y には壁があり、最初、高橋君は壁を超えて移動することができません。
座標 Z にあるハンマーを拾った後でなら、壁を破壊して通過できるようになります。
高橋君がゴールに到達することが可能か判定し、可能であれば移動距離の最小値を求めてください。
制約
- -1000 \leq X,Y,Z \leq 1000
- X,Y,Z は相異なり、いずれも 0 でない
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
X Y Z
出力
高橋君がゴールに到達することが可能であれば、移動距離の最小値を出力せよ。不可能であれば、かわりに -1
と出力せよ。
入力例 1
10 -10 1
出力例 1
10
高橋君はまっすぐゴールに向かうことができます。
入力例 2
20 10 -10
出力例 2
40
ゴールは壁の向こう側にあります。まずハンマーを拾い、壁を壊すことでゴールに到達することができます。
入力例 3
100 1 1000
出力例 3
-1
Score : 200 points
Problem Statement
Takahashi is at the origin of a number line. He wants to reach a goal at coordinate X.
There is a wall at coordinate Y, which Takahashi cannot go beyond at first.
However, after picking up a hammer at coordinate Z, he can destroy that wall and pass through.
Determine whether Takahashi can reach the goal. If he can, find the minimum total distance he needs to travel to do so.
Constraints
- -1000 \leq X,Y,Z \leq 1000
- X, Y, and Z are distinct, and none of them is 0.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
X Y Z
Output
If Takahashi can reach the goal, print the minimum total distance he needs to travel to do so. If he cannot, print -1
instead.
Sample Input 1
10 -10 1
Sample Output 1
10
Takahashi can go straight to the goal.
Sample Input 2
20 10 -10
Sample Output 2
40
The goal is beyond the wall. He can get there by first picking up the hammer and then destroying the wall.
Sample Input 3
100 1 1000
Sample Output 3
-1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
高橋君は野球をモチーフにしたゲームを作ろうとしましたが、うまくコードが書けなくて困っています。
高橋君の代わりに次の問題を解くプログラムを作ってください。
マス 0, マス 1, マス 2, マス 3 の 4 つのマス目があります。はじめマスの上には何もありません。
また、整数 P があり、はじめ P = 0 です。
正の整数からなる数列 A = (A_1, A_2, \dots, A_N) が与えられるので、i = 1, 2, \dots, N について順番に次の操作を行います。
- マス 0 に駒を 1 個置く。
- マス上のすべての駒を番号が A_i 大きいマスに進める。言い換えると、駒がマス x にあればその駒をマス x + A_i に移動する。
ただし移動先のマスが存在しない (すなわち x + A_i が 4 以上になる) 駒たちに関しては、それらを取り除いて P に取り除いた個数を加算する。
すべての操作を行った後の P の値を出力してください。
制約
- 1 \leq N \leq 100
- 1 \leq A_i \leq 4
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
操作終了時点での P の値を出力せよ。
入力例 1
4 1 1 3 2
出力例 1
3
操作を説明すると次のようになり、操作終了時点での P の値は 3 になります。
- i=1 での操作
- マス 0 に駒を置く。この時点でマス 0 にコマが乗っている。
- すべての駒を 1 大きいマスに進める。移動を終えた時点でマス 1 に駒が乗っている。
- i=2 での操作
- マス 0 に駒を置く。この時点でマス 0, 1 にコマが乗っている。
- すべての駒を 1 大きいマスに進める。移動を終えた時点でマス 1, 2 に駒が乗っている。
- i=3 での操作
- マス 0 に駒を置く。この時点でマス 0, 1, 2 にコマが乗っている。
- すべての駒を 3 大きいマスに進める。
この時、マス 1,2 にある駒は移動先のマスが存在しないため (それぞれ 1+3=4,2+3=5 なので) 、盤上から取り除いて P に 2 を加算する。P の値は 2 になる。
移動を終えた時点でマス 3 に駒が乗っている。
- i=4 での操作
- マス 0 に駒を置く。この時点でマス 0, 3 にコマが乗っている。
- すべての駒を 2 大きいマスに進める。
この時、マス 3 にある駒は移動先のマスが存在しないため (3+2=5 なので) 、盤上から取り除いて P に 1 を加算する。P の値は 3 になる。
移動を終えた時点でマス 2 に駒が乗っている。
入力例 2
3 1 1 1
出力例 2
0
P の値が操作中に変化しない場合もあります。
入力例 3
10 2 2 4 1 1 1 4 2 2 1
出力例 3
8
Score : 200 points
Problem Statement
Takahashi is trying to create a game inspired by baseball, but he is having difficulty writing the code.
Write a program for Takahashi that solves the following problem.
There are 4 squares called Square 0, Square 1, Square 2, and Square 3. Initially, all squares are empty.
There is also an integer P; initially, P = 0.
Given a sequence of positive integers A = (A_1, A_2, \dots, A_N), perform the following operations for i = 1, 2, \dots, N in this order:
- Put a piece on Square 0.
- Advance every piece on the squares A_i squares ahead. In other words, if Square x has a piece, move the piece to Square (x + A_i).
If, however, the destination square does not exist (i.e. x + A_i is greater than or equal to 4) for a piece, remove it. Add to P the number of pieces that have been removed.
Print the value of P after all the operations have been performed.
Constraints
- 1 \leq N \leq 100
- 1 \leq A_i \leq 4
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the value of P after all the operations have been performed.
Sample Input 1
4 1 1 3 2
Sample Output 1
3
The operations are described below. After all the operations have been performed, P equals 3.
- The operations for i=1:
- Put a piece on Square 0. Now, Square 0 has a piece.
- Advance every piece on the squares 1 square ahead. After these moves, Square 1 has a piece.
- The operations for i=2:
- Put a piece on Square 0. Now, Squares 0 and 1 have a piece.
- Advance every piece on the squares 1 square ahead. After these moves, Squares 1 and 2 have a piece.
- The operations for i=3:
- Put a piece on Square 0. Now, Squares 0, 1, and 2 have a piece.
- Advance every piece on the squares 3 squares ahead.
Here, for the pieces on Squares 1 and 2, the destination squares do not exist (since 1+3=4 and 2+3=5), so remove these pieces and add 2 to P. P now equals 2. After these moves, Square 3 has a piece.
- The operations for i=4:
- Put a piece on Square 0. Now, Squares 0 and 3 have a piece.
- Advance every piece on the squares 2 squares ahead.
Here, for the piece on Square 3, the destination square does not exist (since 3+2=5), so remove this piece and add 1 to P. P now equals 3.
After these moves, Square 2 has a piece.
Sample Input 2
3 1 1 1
Sample Output 2
0
The value of P may not be updated by the operations.
Sample Input 3
10 2 2 4 1 1 1 4 2 2 1
Sample Output 3
8
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君は英小文字からなる文字列 T を青木君に向けて送りました。その結果、青木君は英小文字からなる文字列 T' を受信しました。
T' は T から一部が変更されてしまっている可能性があり、具体的には、下記の 4 つのうちのちょうど 1 つが成り立つことがわかっています。
- T' は、T と等しい。
- T' は、T のいずれか 1 つの位置(先頭と末尾も含む)に英小文字を 1 つ挿入して得られる文字列である。
- T' は、T からある 1 文字を削除して得られる文字列である。
- T' は、T のある 1 文字を別の英小文字に変更して得られる文字列である。
青木君が受信した文字列 T' と、英小文字からなる N 個の文字列 S_1, S_2, \ldots, S_N が入力として与えられるので、 S_1, S_2, \ldots, S_N のうち、高橋君が送った文字列 T と等しい可能性があるものをすべて求めてください。
制約
- N は整数
- 1 \leq N \leq 5 \times 10^5
- S_i と T' は英小文字からなる長さ 1 以上 5 \times 10^5 以下の文字列
- S_1, S_2, \ldots, S_N の長さの総和は 5 \times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
N T' S_1 S_2 \vdots S_N
出力
S_1, S_2, \ldots, S_N のうち T と等しい可能性があるものすべての添字を昇順に並べた列を (i_1, i_2, \ldots, i_K) とする。 この列の長さ K および列自体を、下記の形式にしたがって出力せよ。
K i_1 i_2 \ldots i_K
入力例 1
5 ababc ababc babc abacbc abdbc abbac
出力例 1
4 1 2 3 4
S_1, S_2, \ldots, S_5 のうち、T と等しい可能性があるものは S_1, S_2, S_3, S_4 の 4 つであることが下記の通りわかります。
- S_1 は T と等しい可能性があります。なぜなら、T' =
ababc
は S_1 =ababc
と等しいからです。 - S_2 は T と等しい可能性があります。なぜなら、T' =
ababc
は S_2 =babc
の先頭に文字a
を挿入して得られる文字列だからです。 - S_3 は T と等しい可能性があります。なぜなら、T' =
ababc
は S_3 =abacbc
から 4 文字目のc
を削除して得られる文字列だからです。 - S_4 は T と等しい可能性があります。なぜなら、T' =
ababc
は S_4 =abdbc
の 3 文字目のd
をb
に変更して得られる文字列だからです。 - S_5 は T と等しい可能性はありません。なぜなら、S_5 =
abbac
を T としたとき、T' =ababc
は問題文中の 4 つの条件をいずれも満たさないからです。
入力例 2
1 aoki takahashi
出力例 2
0
入力例 3
9 atcoder atoder atcode athqcoder atcoder tacoder jttcoder atoder atceoder atcoer
出力例 3
6 1 2 4 7 8 9
Score : 300 points
Problem Statement
Takahashi sent a string T consisting of lowercase English letters to Aoki. As a result, Aoki received a string T' consisting of lowercase English letters.
T' may have been altered from T. Specifically, exactly one of the following four conditions is known to hold.
- T' is equal to T.
- T' is a string obtained by inserting one lowercase English letter at one position (possibly the beginning and end) in T.
- T' is a string obtained by deleting one character from T.
- T' is a string obtained by changing one character in T to another lowercase English letter.
You are given the string T' received by Aoki and N strings S_1, S_2, \ldots, S_N consisting of lowercase English letters. Find all the strings among S_1, S_2, \ldots, S_N that could equal the string T sent by Takahashi.
Constraints
- N is an integer.
- 1 \leq N \leq 5 \times 10^5
- S_i and T' are strings of length between 1 and 5 \times 10^5, inclusive, consisting of lowercase English letters.
- The total length of S_1, S_2, \ldots, S_N is at most 5 \times 10^5.
Input
The input is given from Standard Input in the following format:
N T' S_1 S_2 \vdots S_N
Output
Let (i_1, i_2, \ldots, i_K) be the sequence of indices of all the strings among S_1, S_2, \ldots, S_N that could be equal to T, in ascending order. Print the length K of this sequence, and the sequence itself, in the following format:
K i_1 i_2 \ldots i_K
Sample Input 1
5 ababc ababc babc abacbc abdbc abbac
Sample Output 1
4 1 2 3 4
Among S_1, S_2, \ldots, S_5, the strings that could be equal to T are S_1, S_2, S_3, S_4, as explained below.
- S_1 could be equal to T, because T' =
ababc
is equal to S_1 =ababc
. - S_2 could be equal to T, because T' =
ababc
is obtained by inserting the lettera
at the beginning of S_2 =babc
. - S_3 could be equal to T, because T' =
ababc
is obtained by deleting the fourth characterc
from S_3 =abacbc
. - S_4 could be equal to T, because T' =
ababc
is obtained by changing the third characterd
in S_4 =abdbc
tob
. - S_5 could not be equal to T, because if we take S_5 =
abbac
as T, then T' =ababc
does not satisfy any of the four conditions in the problem statement.
Sample Input 2
1 aoki takahashi
Sample Output 2
0
Sample Input 3
9 atcoder atoder atcode athqcoder atcoder tacoder jttcoder atoder atceoder atcoer
Sample Output 3
6 1 2 4 7 8 9
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
3\times3 のマス目に 1 から 9 までの数字が書き込まれており、上から i 行目、左から j 列目 (1\leq i\leq3,1\leq j\leq3) に書き込まれている数字は c _ {i,j} です。
異なるマスに同じ数字が書き込まれている場合もありますが、同じ数字が縦・横・斜めに 3 つ連続して書き込まれていることはありません。 より厳密には、c _ {i,j} について次の条件のすべてが成り立っていることが保証されます。
- どの 1\leq i\leq3 についても、c _ {i,1}=c _ {i,2}=c _ {i,3} ではない
- どの 1\leq j\leq3 についても、c _ {1,j}=c _ {2,j}=c _ {3,j} ではない
- c _ {1,1}=c _ {2,2}=c _ {3,3} ではない
- c _ {3,1}=c _ {2,2}=c _ {1,3} ではない
高橋くんは、それぞれのマスに書かれている数字をランダムな順番で知ります。 高橋くんは、縦・横・斜めの列のうちの 1 つでも次の条件を満たしたときがっかりします。
- はじめに知ったほうの 2 マスに書かれた数字が同じであり、最後に知ったマスに書かれた数字がそれと異なる。
高橋くんががっかりせずにすべてのマスに書かれた数字を知る確率を求めてください。
制約
- c _ {i,j}\in\lbrace1,2,3,4,5,6,7,8,9\rbrace\ (1\leq i\leq3,1\leq j\leq3)
- c _ {i,1}=c _ {i,2}=c _ {i,3} ではない (1\leq i\leq3)
- c _ {1,j}=c _ {2,j}=c _ {3,j} ではない (1\leq j\leq3)
- c _ {1,1}=c _ {2,2}=c _ {3,3} ではない
- c _ {1,3}=c _ {2,2}=c _ {3,1} ではない
入力
入力は以下の形式で標準入力から与えられる。
c _ {1,1} c _ {1,2} c _ {1,3} c _ {2,1} c _ {2,2} c _ {2,3} c _ {3,1} c _ {3,2} c _ {3,3}
出力
高橋くんががっかりせずにすべてのマスに書かれた数字を知る確率を 1 行で出力せよ。 真の値からの絶対誤差が 10 ^ {-8} 以下であるとき、正答と判定される。
入力例 1
3 1 9 2 5 6 2 7 1
出力例 1
0.666666666666666666666666666667
例えば、高橋くんが c _ {3,1}=2,c _ {2,1}=2,c _ {1,1}=3 の順に知った場合、高橋くんはがっかりしてしまいます。
対して、高橋くんが c _ {1,1},c _ {1,2},c _ {1,3},c _ {2,1},c _ {2,2},c _ {2,3},c _ {3,1},c _ {3,2},c _ {3,3} の順に数字を知った場合、がっかりすることなくすべての数字を知ることができます。
高橋くんががっかりすることなくすべての数字を知ることができる確率は \dfrac 23 です。 絶対誤差が 10 ^ {-8} 以下であれば正答と判定されるため、0.666666657 や 0.666666676 のように出力しても正解になります。
入力例 2
7 7 6 8 6 8 7 7 6
出力例 2
0.004982363315696649029982363316
入力例 3
3 6 7 1 9 7 5 7 5
出力例 3
0.4
Score : 300 points
Problem Statement
There is a 3\times3 grid with numbers between 1 and 9, inclusive, written in each square. The square at the i-th row from the top and j-th column from the left (1\leq i\leq3,1\leq j\leq3) contains the number c _ {i,j}.
The same number may be written in different squares, but not in three consecutive cells vertically, horizontally, or diagonally. More precisely, it is guaranteed that c _ {i,j} satisfies all of the following conditions.
- c _ {i,1}=c _ {i,2}=c _ {i,3} does not hold for any 1\leq i\leq3.
- c _ {1,j}=c _ {2,j}=c _ {3,j} does not hold for any 1\leq j\leq3.
- c _ {1,1}=c _ {2,2}=c _ {3,3} does not hold.
- c _ {3,1}=c _ {2,2}=c _ {1,3} does not hold.
Takahashi will see the numbers written in each cell in random order. He will get disappointed when there is a line (vertical, horizontal, or diagonal) that satisfies the following condition.
- The first two squares he sees contain the same number, but the last square contains a different number.
Find the probability that Takahashi sees the numbers in all the squares without getting disappointed.
Constraints
- c _ {i,j}\in\lbrace1,2,3,4,5,6,7,8,9\rbrace\ (1\leq i\leq3,1\leq j\leq3)
- c _ {i,1}=c _ {i,2}=c _ {i,3} does not hold for any 1\leq i\leq3.
- c _ {1,j}=c _ {2,j}=c _ {3,j} does not hold for any 1\leq j\leq3.
- c _ {1,1}=c _ {2,2}=c _ {3,3} does not hold.
- c _ {3,1}=c _ {2,2}=c _ {1,3} does not hold.
Input
The input is given from Standard Input in the following format:
c _ {1,1} c _ {1,2} c _ {1,3} c _ {2,1} c _ {2,2} c _ {2,3} c _ {3,1} c _ {3,2} c _ {3,3}
Output
Print one line containing the probability that Takahashi sees the numbers in all the squares without getting disappointed. Your answer will be considered correct if the absolute error from the true value is at most 10 ^ {-8}.
Sample Input 1
3 1 9 2 5 6 2 7 1
Sample Output 1
0.666666666666666666666666666667
For example, if Takahashi sees c _ {3,1}=2,c _ {2,1}=2,c _ {1,1}=3 in this order, he will get disappointed.
On the other hand, if Takahashi sees c _ {1,1},c _ {1,2},c _ {1,3},c _ {2,1},c _ {2,2},c _ {2,3},c _ {3,1},c _ {3,2},c _ {3,3} in this order, he will see all numbers without getting disappointed.
The probability that Takahashi sees all the numbers without getting disappointed is \dfrac 23. Your answer will be considered correct if the absolute error from the true value is at most 10 ^ {-8}, so outputs such as 0.666666657 and 0.666666676 would also be accepted.
Sample Input 2
7 7 6 8 6 8 7 7 6
Sample Output 2
0.004982363315696649029982363316
Sample Input 3
3 6 7 1 9 7 5 7 5
Sample Output 3
0.4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君は電車のおもちゃを連結させたり分離させたりして遊んでいます。
電車は N 個あり、電車 1 、電車 2 、\ldots 、電車 N と名前がついています。
はじめ電車どうしは連結しておらず全てバラバラです。
クエリが Q 個与えられるので、与えられた順番に処理してください。
クエリは次の 3 種類のいずれかです。
-
1 x y
:電車 x の後部と、電車 y の前部を連結させる。
以下のことが保証されます。- x \neq y
- クエリを処理する直前に、電車 x の後部と連結している電車は存在しない
- クエリを処理する直前に、電車 y の前部と連結している電車は存在しない
- クエリを処理する直前に、電車 x と電車 y は異なる連結成分に属する
-
2 x y
:電車 x の後部と、電車 y の前部を分離させる。
以下のことが保証されます。- x \neq y
- クエリを処理する直前に、電車 x の後部と電車 y の前部は直接連結している
-
3 x
:電車 x が含まれる連結成分に属する電車の番号を、先頭から順番に全て出力する。
制約
- 2 \leq N \leq 10^5
- 1 \leq Q \leq 10^5
- 1 \leq x \leq N
- 1 \leq y \leq N
- 入力は全て整数
- クエリは全て問題文の条件を満たす
3 x
の形式のクエリで出力する電車の番号の個数の合計は 10^6 以下
入力
入力は以下の形式で標準入力から与えられる。
N Q \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
i 番目のクエリ \mathrm{query}_i では、まずクエリの種類 c_i( 1, 2, 3 のいずれか)が与えられる。
c_i = 1,2 の場合は x,y が追加で与えられ、c_i =3 の場合は x が追加で与えられる。
すなわち、各クエリは以下に示す 3 つの形式のいずれかである。
1 x y
2 x y
3 x
出力
ある c_i = 3 のタイプのクエリにおいて、出力すべき値が j_1, j_2, \ldots , j_M であるとする。
このとき以下の形式で 1 行に出力せよ。
M j_1 j_2 \ldots j_M
c_i = 3 のタイプのクエリの数を q として、q 行出力せよ。
k (1 \leq k \leq q) 行目では k 番目のそのようなクエリに対する答えを出力せよ。
入力例 1
7 14 1 6 3 1 4 1 1 5 2 1 2 7 1 3 5 3 2 3 4 3 6 2 3 5 2 4 1 1 1 5 3 2 3 4 3 6
出力例 1
5 6 3 5 2 7 2 4 1 5 6 3 5 2 7 4 1 5 2 7 1 4 2 6 3
\mathrm{query}_5 まで処理した時、電車は以下のようになっています。
この時、たとえば電車 2 は、電車 3,5,6,7 と同じ連結成分に属していますが、電車 1,4 とは同じ連結成分に属していません。
\mathrm{query}_{11} まで処理した時、電車は以下のようになっています。
Score : 400 points
Problem Statement
Takahashi is playing with toy trains, connecting and disconnecting them.
There are N toy train cars, with car numbers: Car 1, Car 2, \ldots, Car N.
Initially, all cars are separated.
You will be given Q queries. Process them in the order they are given. There are three kinds of queries, as follows.
-
1 x y
: Connect the front of Car y to the rear of Car x.
It is guaranteed that:- x \neq y
- just before this query, no train is connected to the rear of Car x;
- just before this query, no train is connected to the front of Car y;
- just before this query, Car x and Car y belong to different connected components.
-
2 x y
: Disconnect the front of Car y from the rear of Car x.
It is guaranteed that:- x \neq y;
- just before this query, the front of Car y is directly connected to the rear of Car x.
-
3 x
: Print the car numbers of the cars belonging to the connected component containing Car x, from front to back.
Constraints
- 2 \leq N \leq 10^5
- 1 \leq Q \leq 10^5
- 1 \leq x \leq N
- 1 \leq y \leq N
- All values in input are integers.
- All queries satisfy the conditions in the Problem Statement.
- The queries of the format
3 x
ask to print at most 10^6 car numbers in total.
Input
Input is given from Standard Input in the following format:
N Q \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
The i-th query \mathrm{query}_i begins with an integer c_i (1, 2, or 3) representing the kind of the query, followed by x and y if c_i = 1 or 2, and followed by x if c_i = 3.
In short, each query is in one of the following three formats:
1 x y
2 x y
3 x
Output
If a query with c_i = 3 asks to print the values j_1, j_2, \ldots, j_M, output the following line:
M j_1 j_2 \ldots j_M
Your output should consist of q lines, where q is the number of queries with c_i = 3.
The k-th line (1 \leq k \leq q) should contain the response to the k-th such query.
Sample Input 1
7 14 1 6 3 1 4 1 1 5 2 1 2 7 1 3 5 3 2 3 4 3 6 2 3 5 2 4 1 1 1 5 3 2 3 4 3 6
Sample Output 1
5 6 3 5 2 7 2 4 1 5 6 3 5 2 7 4 1 5 2 7 1 4 2 6 3
The figure below shows the cars when the first 5 queries are processed.
For example, Car 2 belongs to the same connected component as Cars 3, 5, 6, 7, which is different from the connected component containing Cars 1, 4.
The figure below shows the cars when the first 11 queries are processed.
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
0
と 1
のみからなる文字列であって、文字列中のどの連続する 2 文字も異なるようなものを 良い文字列 とよびます。
0
と 1
のみからなる長さ N の文字列 S が与えられます。
Q 個のクエリが与えられるので、順に処理してください。
クエリは次の 2 種類です。
1 L R
: S の L 文字目から R 文字目までの0
と1
を反転させる。すなわち、L\leq i\leq R をみたす整数 i について、S の i 文字目が0
ならば1
に、1
ならば0
に変更する。2 L R
: S の L 文字目から R 文字目までを(順番を変えずに)抜き出した長さ (R-L+1) の文字列を S' とする。S' が良い文字列ならばYes
を、そうでないならばNo
を出力する。
制約
- 1\leq N, Q\leq 5\times 10^5
- S は
0
と1
のみからなる長さ N の文字列 - 1,2 種類目のクエリについて、1\leq L\leq R\leq N
- 2 種類目のクエリが少なくとも 1 つ存在する。
- N, Q, L, R は整数
入力
入力は以下の形式で標準入力から与えられる。
N Q S query_1 query_2 \vdots query_Q
各クエリ query_i (1\leq i\leq Q) は、
1 L R
または、
2 L R
の形で与えられる。
出力
2 種類目のクエリの数を K 個として、K 行出力せよ。
i 行目には i 個目の 2 種類目のクエリに対する出力を出力せよ。
入力例 1
5 6 10100 2 1 3 2 1 5 1 1 4 2 1 5 1 3 3 2 2 4
出力例 1
Yes No Yes No
最初、S=10100
です。このとき、クエリを与えられた順に処理すると以下のようになります。
- 1 番目のクエリについて、S の 1 文字目から 3 文字目までを抜き出した文字列は S'=
101
です。これは良い文字列なのでYes
を出力します。 - 2 番目のクエリについて、S の 1 文字目から 5 文字目までを抜き出した文字列は S'=
10100
です。これは良い文字列でないのでNo
を出力します。 - 3 番目のクエリについて、S の 1 文字目から 4 文字目までの
0
と1
を反転させます。文字列 S は S=01010
となります。 - 4 番目のクエリについて、S の 1 文字目から 5 文字目までを抜き出した文字列は S'=
01010
です。これは良い文字列なのでYes
を出力します。 - 5 番目のクエリについて、S の 3 文字目の
0
と1
を反転させます。文字列 S は S=01110
となります。 - 6 番目のクエリについて、S の 2 文字目から 4 文字目までを抜き出した文字列は S'=
111
です。これは良い文字列でないのでNo
を出力します。
入力例 2
1 2 1 1 1 1 2 1 1
出力例 2
Yes
0
または 1
の 1 文字からなる文字列は良い文字列の条件をみたすことに注意してください。
Score: 450 points
Problem Statement
A string consisting of 0
and 1
is called a good string if two consecutive characters in the string are always different.
You are given a string S of length N consisting of 0
and 1
.
Q queries will be given and must be processed in order.
There are two types of queries:
1 L R
: Flip each of the L-th to R-th characters of S. That is, for each integer i satisfying L\leq i\leq R, change the i-th character of S to0
if it is1
, and vice versa.2 L R
: Let S' be the string of length (R-L+1) obtained by extracting the L-th to R-th characters of S (without changing the order). PrintYes
if S' is a good string andNo
otherwise.
Constraints
- 1\leq N, Q\leq 5\times 10^5
- S is a string of length N consisting of
0
and1
. - 1\leq L\leq R\leq N for queries of types 1 and 2.
- There is at least one query of type 2.
- N, Q, L, and R are integers.
Input
The input is given from Standard Input in the following format:
N Q S query_1 query_2 \vdots query_Q
Each query query_i (1\leq i\leq Q) is given in the form:
1 L R
or:
2 L R
Output
Let K be the number of queries of type 2. Print K lines.
The i-th line should contain the response to the i-th query of type 2.
Sample Input 1
5 6 10100 2 1 3 2 1 5 1 1 4 2 1 5 1 3 3 2 2 4
Sample Output 1
Yes No Yes No
Initially, S=10100
. When processing the queries in the order they are given, the following occurs:
- For the first query, the string obtained by extracting the 1-st to 3-rd characters of S is S'=
101
. This is a good string, so printYes
. - For the second query, the string obtained by extracting the 1-st to 5-th characters of S is S'=
10100
. This is not a good string, so printNo
. - For the third query, flip each of the 1-st to 4-th characters of S. The string S becomes S=
01010
. - For the fourth query, the string obtained by extracting the 1-st to 5-th character of S is S'=
01010
. This is a good string, so printYes
. - For the fifth query, flip the 3-rd character of S. The string S becomes S=
01110
. - For the sixth query, the string obtained by extracting the 2-nd to 4-th character of S is S'=
111
. This is not a good string, so printNo
.
Sample Input 2
1 2 1 1 1 1 2 1 1
Sample Output 2
Yes
Note that a string of a single character 0
or 1
satisfies the condition of being a good string.
実行時間制限: 3.5 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
あなたの前に体力 H のモンスターが現れ、ターン制の戦闘が開始しました。
あなたは、ターン 1,2,… のそれぞれに呪文 1,…,N の N 種類の呪文のうち一つを唱えます。
ターン i に呪文 j を唱えると、その呪文の効果としてターン i,i+1,…,i+t_j -1 のそれぞれでモンスターの体力が d_j 減ります。
モンスターの体力を 0 以下にすることが可能な最も早いターンを求めてください。
制約
- 1 \leq N \leq 3 \times 10^5
- 1 \leq H \leq 10^{18}
- 1 \leq t_i,d_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N H t_1 d_1 \vdots t_N d_N
出力
答えを出力せよ。
入力例 1
2 20 2 2 5 1
出力例 1
6
以下のようにするとターン 6 にモンスターの体力を 0 以下に出来ます。これが最も早いターンです。
- ターン 1 に魔法 1 を使う。ターン 1 に使った魔法の効果でモンスターの体力が 2 減り、18 になる。
- ターン 2 に魔法 2 を使う。ターン 1,2 に使った魔法の効果でモンスターの体力が 2+1=3 減り、15 になる。
- ターン 3 に魔法 1 を使う。ターン 2,3 に使った魔法の効果でモンスターの体力が 1+2=3 減り、12 になる。
- ターン 4 に魔法 2 を使う。ターン 2,3,4 に使った魔法の効果でモンスターの体力が 1+2+1=4 減り、8 になる。
- ターン 5 に魔法 1 を使う。ターン 2,4,5 に使った魔法の効果でモンスターの体力が 1+1+2=4 減り、4 になる。
- ターン 6 に魔法 2 を使う。ターン 2,4,5,6 に使った魔法の効果でモンスターの体力が 1+1+2+1=5 減り、-1 になる。
入力例 2
10 200 1 21 1 1 1 1 8 4 30 1 3 1 10 2 8 1 9 1 4 4
出力例 2
9
Score : 525 points
Problem Statement
A monster with health H has appeared in front of you, and a turn-based battle has started.
In each turn 1,2,…, you cast one of N spells, spells 1,…,N.
If you cast spell i in turn j, the monster's health reduces by d_i in each turn j,j+1,…,j+t_i -1.
Find the earliest turn that you can make the monster's health 0 or less.
Constraints
- 1 \leq N \leq 3 \times 10^5
- 1 \leq H \leq 10^{18}
- 1 \leq t_i,d_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N H t_1 d_1 \vdots t_N d_N
Output
Print the answer.
Sample Input 1
2 20 2 2 5 1
Sample Output 1
6
The following procedure makes the monster's health 0 or less in turn 6, which is the earliest.
- Cast spell 1 in turn 1. Due to the spell cast in turn 1, the monster's health reduces by 2 and becomes 18.
- Cast spell 2 in turn 2. Due to the spells cast in turns 1 and 2, the monster's health reduces by 2+1=3 and becomes 15.
- Cast spell 1 in turn 3. Due to the spells cast in turns 2 and 3, the monster's health reduces by 1+2=3 and becomes 12.
- Cast spell 2 in turn 4. Due to the spells cast in turns 2,3 and 4, the monster's health reduces by 1+2+1=4 and becomes 8.
- Cast spell 1 in turn 5. Due to the spells cast in turns 2,4 and 5, the monster's health reduces by 1+1+2=4 and becomes 4.
- Cast spell 2 in turn 6. Due to the spells cast in turns 2,4,5 and 6, the monster's health reduces by 1+1+2+1=5 and becomes -1.
Sample Input 2
10 200 1 21 1 1 1 1 8 4 30 1 3 1 10 2 8 1 9 1 4 4
Sample Output 2
9