実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
長さ N の数列 A=(A_1,A_2,\ldots,A_N) および正整数 P,Q,R,S が与えられます。
ここで、P,Q,R,S は、1\leq P\leq Q<R\leq S \leq N および Q-P=S-R をみたしています。
数列 A の P 番目から Q 番目の項までと R 番目から S 番目の項までを入れ替えた数列を B=(B_1, B_2,\ldots, B_N) とします。
数列 B を出力してください。
制約
- 1\leq N \leq 100
- 1\leq A_i\leq 100
- 1\leq P\leq Q<R\leq S \leq N
- Q-P=S-R
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N P Q R S A_1 A_2 \ldots A_N
出力
B_1, B_2,\ldots, B_N を空白区切りで出力せよ。
入力例 1
8 1 3 5 7 1 2 3 4 5 6 7 8
出力例 1
5 6 7 4 1 2 3 8
数列 A=(1,2,3,4,5,6,7,8) の 1 番目から 3 番目の項 (1,2,3) と 5 番目から 7 番目までの項 (5,6,7) を 入れ替えると, B=(5,6,7,4,1,2,3,8) となります。 よってこれを空白区切りで出力します。
入力例 2
5 2 3 4 5 2 2 1 1 1
出力例 2
2 1 1 2 1
数列には同じ整数が複数回現れる事もあります。
入力例 3
2 1 1 2 2 50 100
出力例 3
100 50
入力例 4
10 2 4 7 9 22 75 26 45 72 81 47 29 97 2
出力例 4
22 47 29 97 72 81 75 26 45 2
Score : 100 points
Problem Statement
You are given a sequence A=(A_1,A_2,\ldots,A_N) of length N and positive integers P,Q,R, and S.
Here, P,Q,R, and S satisfy 1\leq P\leq Q<R\leq S \leq N and Q-P=S-R.
Let B=(B_1, B_2,\ldots, B_N) be the sequence obtained by swapping the P-th through Q-th terms and the R-th through S-th terms of A.
Print the sequence B.
Constraints
- 1\leq N \leq 100
- 1\leq A_i\leq 100
- 1\leq P\leq Q<R\leq S \leq N
- Q-P=S-R
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N P Q R S A_1 A_2 \ldots A_N
Output
Print B_1, B_2,\ldots, B_N, with spaces in between.
Sample Input 1
8 1 3 5 7 1 2 3 4 5 6 7 8
Sample Output 1
5 6 7 4 1 2 3 8
Swapping the 1-st through 3-rd terms (1,2,3) and the 5-th through 7-th terms (5,6,7) of the sequence A=(1,2,3,4,5,6,7,8) results in B=(5,6,7,4,1,2,3,8), which should be printed with spaces in between.
Sample Input 2
5 2 3 4 5 2 2 1 1 1
Sample Output 2
2 1 1 2 1
The same integer may occur multiple times in the sequence.
Sample Input 3
2 1 1 2 2 50 100
Sample Output 3
100 50
Sample Input 4
10 2 4 7 9 22 75 26 45 72 81 47 29 97 2
Sample Output 4
22 47 29 97 72 81 75 26 45 2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
英大文字と英小文字からなる文字列 S が与えられます。S の英大文字のみを元の順序で連結して得られる文字列を出力してください。
制約
- S は英大文字と英小文字からなる文字列である
- S の長さは 1 以上 100 以下である
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S の英大文字のみを元の順序で連結して得られる文字列を出力せよ。
入力例 1
AtCoderBeginnerContest
出力例 1
ACBC
S の英大文字のみを元の順序で連結して得られる文字列は ACBC です。よって、ACBC を出力します。
入力例 2
PaymentRequired
出力例 2
PR
入力例 3
program
出力例 3
S に英大文字が含まれないことがあることに注意してください。
Score : 100 points
Problem Statement
You are given a string S consisting of uppercase and lowercase English letters. Print the string obtained by concatenating only the uppercase letters of S in their original order.
Constraints
- S is a string of uppercase and lowercase English letters.
- The length of S is between 1 and 100, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print the string obtained by concatenating only the uppercase letters of S in their original order.
Sample Input 1
AtCoderBeginnerContest
Sample Output 1
ACBC
The string obtained by concatenating only the uppercase letters of S in their original order is ACBC. Hence, print ACBC.
Sample Input 2
PaymentRequired
Sample Output 2
PR
Sample Input 3
program
Sample Output 3
Note that S may contain no uppercase letters.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
1 から N までの番号がついた N 人の参加者が、1 から M までの番号がついた M 問からなるコンテストに参加します。
1 以上 N 以下の整数 i 、1 以上 M 以下の整数 j について、S_i の j 番目の文字が o のとき参加者 i は問題 j を解くことが可能で、S_i の j 番目の文字が x のとき参加者 i は問題 j を解くことが不可能です。
このコンテストは、二人の参加者でペアを組んで参加します。二人が協力することで M 問全てを解くことが可能であるようなペアの個数を答えてください。
より厳密には、1\leq x < y\leq N を満たす整数の組 (x,y) であって、 1 以上 M 以下の任意の整数 j について、参加者 x か参加者 y の少なくとも一方は問題 j を解くことが可能であるという条件を満たすものの個数を答えてください。
制約
- N は 2 以上 30 以下の整数
- M は 1 以上 30 以下の整数
- S_i は
o,xからなる長さ M の文字列
入力
入力は以下の形式で標準入力から与えられる。
N M S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
5 5 ooooo oooxx xxooo oxoxo xxxxx
出力例 1
5
参加者 1 と 2 のペア、参加者 1 と 3 のペア、参加者 1 と 4 のペア、参加者 1 と 5 のペア、参加者 2 と 3 のペアの 5 個のペアが条件を満たします。
例えば参加者 2 と 4 のペアは、問題 4 が解けないので条件を満たしません。
入力例 2
3 2 ox xo xx
出力例 2
1
入力例 3
2 4 xxxx oxox
出力例 3
0
Score : 200 points
Problem Statement
N participants, numbered 1 to N, will participate in a contest with M problems, numbered 1 to M.
For an integer i between 1 and N and an integer j between 1 and M, participant i can solve problem j if the j-th character of S_i is o, and cannot solve it if that character is x.
The participants must be in pairs. Print the number of ways to form a pair of participants who can collectively solve all the M problems.
More formally, print the number of pairs (x,y) of integers satisfying 1\leq x < y\leq N such that for any integer j between 1 and M, at least one of participant x and participant y can solve problem j.
Constraints
- N is an integer between 2 and 30, inclusive.
- M is an integer between 1 and 30, inclusive.
- S_i is a string of length M consisting of
oandx.
Input
The input is given from Standard Input in the following format:
N M S_1 S_2 \vdots S_N
Output
Print the answer.
Sample Input 1
5 5 ooooo oooxx xxooo oxoxo xxxxx
Sample Output 1
5
The following five pairs satisfy the condition: participants 1 and 2, participants 1 and 3, participants 1 and 4, participants 1 and 5, and participants 2 and 3.
On the other hand, the pair of participants 2 and 4, for instance, does not satisfy the condition because they cannot solve problem 4.
Sample Input 2
3 2 ox xo xx
Sample Output 2
1
Sample Input 3
2 4 xxxx oxox
Sample Output 3
0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
ボウリングのピンは 1 から 10 の番号が付けられており、上から見ると下図のように配置されます。

この図の二つの点線に挟まれた部分を列と呼ぶことにします。
例えば、ピン 1, 5 とピン 3, 9 はそれぞれ同じ列に存在します。
いくつかのピンが倒れた状態のうち、特殊なものはスプリットと呼ばれます。
ピンの配置がスプリットであるとは、以下の条件が全て成り立つことを言います。
- ピン 1 が倒れている。
- ある二つの異なる列であって、次の条件を満たすものが存在する。
- それぞれの列には、立っているピンが 1 本以上存在する。
- それらの列の間に、ピンが全て倒れている列が存在する。
具体例は入出力例を参考にしてください。
さて、あるピンの配置が長さ 10 の文字列 S として与えられます。
i = 1, \dots, 10 について、ピン i が倒れているとき S の i 文字目は 0 であり、ピン i が立っているとき S の i 文字目は 1 です。
S で表されるピンの配置がスプリットかどうか判定してください。
制約
- S は
0と1からなる長さ 10 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S で表されるピンの配置がスプリットなら Yes を、そうでないなら No を出力せよ。
入力例 1
0101110101
出力例 1
Yes
倒れているピンを灰色で、立っているピンを白色で示すと下図のようになります。

ピン 5 が立っている列とピン 6 が立っている列の間にはピン 3, 9 が置かれている列が存在しますが、ピン 3, 9 はいずれも倒れているので、この配置はスプリットです。
入力例 2
0100101001
出力例 2
Yes

入力例 3
0000100110
出力例 3
No

この配置はスプリットではありません。
入力例 4
1101110101
出力例 4
No

ピン 1 が倒れていないので、スプリットではありません。
Score : 200 points
Problem Statement
Bowling pins are numbered 1 through 10. The following figure is a top view of the arrangement of the pins:

Let us call each part between two dotted lines in the figure a column.
For example, Pins 1 and 5 belong to the same column, and so do Pin 3 and 9.
When some of the pins are knocked down, a special situation called split may occur.
A placement of the pins is a split if both of the following conditions are satisfied:
- Pin 1 is knocked down.
- There are two different columns that satisfy both of the following conditions:
- Each of the columns has one or more standing pins.
- There exists a column between these columns such that all pins in the column are knocked down.
See also Sample Inputs and Outputs for examples.
Now, you are given a placement of the pins as a string S of length 10.
For i = 1, \dots, 10, the i-th character of S is 0 if Pin i is knocked down, and is 1 if it is standing.
Determine if the placement of the pins represented by S is a split.
Constraints
- S is a string of length 10 consisting of
0and1.
Input
Input is given from Standard Input in the following format:
S
Output
If the placement of the pins represented by S is a split, print Yes; otherwise, print No.
Sample Input 1
0101110101
Sample Output 1
Yes
In the figure below, the knocked-down pins are painted gray, and the standing pins are painted white:

Between the column containing a standing pin 5 and the column containing a standing pin 6 is a column containing Pins 3 and 9. Since Pins 3 and 9 are both knocked down, the placement is a split.
Sample Input 2
0100101001
Sample Output 2
Yes

Sample Input 3
0000100110
Sample Output 3
No

This placement is not a split.
Sample Input 4
1101110101
Sample Output 4
No

This is not a split because Pin 1 is not knocked down.
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N 行 N 列のグリッドが与えられます。ここで、N は偶数です。グリッドの上から i 行目、左から j 列目のマスをマス (i, j) と表記します。
グリッドの各マスは黒か白のいずれかで塗られており、A_{i, j} = # のときマス (i, j) は黒、A_{i, j} = . のときマス (i, j) は白で塗られています。
i = 1, 2, \ldots, \frac{N}{2} の順に以下の操作を行った後のグリッドの各マスの色を求めてください。
- i 以上 N + 1 - i 以下の整数 x, y について、マス (y, N + 1 - x) の色をマス (x, y) の色で置き換える。この置き換えは条件を満たすすべての整数 x, y について同時に行う。
制約
- N は 2 以上 3000 以下の偶数
- A_{i, j} は
#または.
入力
入力は以下の形式で標準入力から与えられる。
N
A_{1, 1}A_{1, 2}\ldotsA_{1, N}
A_{2, 1}A_{2, 2}\ldotsA_{2, N}
\vdots
A_{N, 1}A_{N, 2}\ldotsA_{N, N}
出力
すべての操作を終えた後、マス (i, j) の色が黒であるとき B_{i, j} = #、マス (i, j) の色が白であるとき B_{i, j} = . として以下の形式で出力せよ。
B_{1, 1}B_{1, 2}\ldotsB_{1, N}
B_{2, 1}B_{2, 2}\ldotsB_{2, N}
\vdots
B_{N, 1}B_{N, 2}\ldotsB_{N, N}
入力例 1
8 .......# .......# .####..# .####..# .##....# .##....# .####### .#######
出力例 1
........ #######. #.....#. #.###.#. #.#...#. #.#####. #....... ########
操作によってグリッドの各マスの色は以下のように変化します。
.......# ........ ........ ........ ........ .......# ######.. #######. #######. #######. .####..# ######.. #....##. #.....#. #.....#. .####..# → ##..##.. → #....##. → #.##..#. → #.###.#. .##....# ##..##.. #..####. #.##..#. #.#...#. .##....# ##...... #..####. #.#####. #.#####. .####### ##...... #....... #....... #....... .####### ######## ######## ######## ########
入力例 2
6 .#.#.# ##.#.. ...### ###... ..#.## #.#.#.
出力例 2
#.#.#. .#.#.# #.#.#. .#.#.# #.#.#. .#.#.#
入力例 3
12 .......#.### #...#...#..# ###.#..##### ..#.#.#.#... .#.....#.### .......#.#.. #...#..#.... #####....... ...#...#.#.# ..###..#..## #..#.#.#.#.# .####.......
出力例 3
.#..##...##. #.#.#.#.#... ###.##..#... #.#.#.#.#... #.#.##...##. ............ ............ .###.###.### ...#...#.#.. .###...#.### ...#...#...# .###...#.###
Score : 400 points
Problem Statement
You are given a grid with N rows and N columns, where N is an even number. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left.
Each cell is painted black or white. If A_{i, j} = #, cell (i, j) is black; if A_{i, j} = ., it is white.
Find the color of each cell after performing the following operation for i = 1, 2, \ldots, \frac{N}{2} in this order.
- For all pairs of integers x, y between i and N + 1 - i, inclusive, replace the color of cell (y, N + 1 - x) with the color of cell (x, y). Perform these replacements simultaneously for all such pairs x, y.
Constraints
- N is an even number between 2 and 3000, inclusive.
- Each A_{i, j} is
#or..
Input
The input is given from Standard Input in the following format:
N
A_{1,1}A_{1,2}\ldots A_{1,N}
A_{2,1}A_{2,2}\ldots A_{2,N}
\vdots
A_{N,1}A_{N,2}\ldots A_{N,N}
Output
After all operations, let B_{i, j} = # if cell (i, j) is black, and B_{i, j} = . if it is white. Print the grid in the following format:
B_{1,1}B_{1,2}\ldots B_{1,N}
B_{2,1}B_{2,2}\ldots B_{2,N}
\vdots
B_{N,1}B_{N,2}\ldots B_{N,N}
Sample Input 1
8 .......# .......# .####..# .####..# .##....# .##....# .####### .#######
Sample Output 1
........ #######. #.....#. #.###.#. #.#...#. #.#####. #....... ########
The operations change the colors of the grid cells as follows:
.......# ........ ........ ........ ........ .......# ######.. #######. #######. #######. .####..# ######.. #....##. #.....#. #.....#. .####..# → ##..##.. → #....##. → #.##..#. → #.###.#. .##....# ##..##.. #..####. #.##..#. #.#...#. .##....# ##...... #..####. #.#####. #.#####. .####### ##...... #....... #....... #....... .####### ######## ######## ######## ########
Sample Input 2
6 .#.#.# ##.#.. ...### ###... ..#.## #.#.#.
Sample Output 2
#.#.#. .#.#.# #.#.#. .#.#.# #.#.#. .#.#.#
Sample Input 3
12 .......#.### #...#...#..# ###.#..##### ..#.#.#.#... .#.....#.### .......#.#.. #...#..#.... #####....... ...#...#.#.# ..###..#..## #..#.#.#.#.# .####.......
Sample Output 3
.#..##...##. #.#.#.#.#... ###.##..#... #.#.#.#.#... #.#.##...##. ............ ............ .###.###.### ...#...#.#.. .###...#.### ...#...#...# .###...#.###
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
英小文字からなる文字列 S, T が与えられます。ここで、S と T の長さは等しいです。
X を空の配列とし、以下の操作を S と T が等しくなるまで繰り返します。
- S の文字を 1 文字書き換え、X の末尾に S を追加する。
こうして得られる文字列の配列 X のうち要素数最小のものを求めてください。要素数最小のものが複数考えられる場合は、そのうち辞書順最小のものを求めてください。
文字列の配列の辞書順とは
長さ N の文字列 S = S_1 S_2 \ldots S_N が長さ N の文字列 T = T_1 T_2 \ldots T_N より辞書順で小さいとは、ある整数 1 \leq i \leq N が存在して下記の 2 つがともに成り立つことをいいます。
- S_1 S_2 \ldots S_{i-1} = T_1 T_2 \ldots T_{i-1}
- S_i が T_i よりアルファベット順で早い。
要素数 M の文字列の配列 X = (X_1,X_2,\ldots,X_M) が要素数 M の文字列の配列 Y = (Y_1,Y_2,\ldots,Y_M) より辞書順で小さいとは、ある整数 1 \leq j \leq M が存在して下記の 2 つがともに成り立つことをいいます。
- (X_1,X_2,\ldots,X_{j-1}) = (Y_1,Y_2,\ldots,Y_{j-1})
- X_j が Y_j より辞書順で小さい。
制約
- S, T は英小文字からなる長さ 1 以上 100 以下の文字列
- S と T の長さは等しい
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
答えの要素数を M として M + 1 行出力せよ。
1 行目には M の値を出力せよ。
i + 1 (1 \leq i \leq M) 行目には答えの i 番目の要素を出力せよ。
入力例 1
adbe bcbc
出力例 1
3 acbe acbc bcbc
はじめ、S = adbe です。
以下のように操作することで、X = ( acbe , acbc , bcbc ) とすることができます。
-
S を
acbeに書き換え、X の末尾にacbeを追加する。 -
S を
acbcに書き換え、X の末尾にacbcを追加する。 -
S を
bcbcに書き換え、X の末尾にbcbcを追加する。
入力例 2
abcde abcde
出力例 2
0
入力例 3
afwgebrw oarbrenq
出力例 3
8 aawgebrw aargebrw aarbebrw aarbebnw aarbebnq aarbeenq aarbrenq oarbrenq
Score : 300 points
Problem Statement
You are given two strings S and T consisting of lowercase English letters. Here, S and T have equal lengths.
Let X be an empty array, and repeat the following operation until S equals T:
- Change one character in S, and append S to the end of X.
Find the array of strings X with the minimum number of elements obtained in this way. If there are multiple such arrays with the minimum number of elements, find the lexicographically smallest one among them.
What is lexicographical order on arrays of strings?
A string S = S_1 S_2 \ldots S_N of length N is lexicographically smaller than a string T = T_1 T_2 \ldots T_N of length N if there exists an integer 1 \leq i \leq N such that both of the following are satisfied:
- S_1 S_2 \ldots S_{i-1} = T_1 T_2 \ldots T_{i-1}
- S_i comes earlier than T_i in alphabetical order.
An array of strings X = (X_1,X_2,\ldots,X_M) with M elements is lexicographically smaller than an array of strings Y = (Y_1,Y_2,\ldots,Y_M) with M elements if there exists an integer 1 \leq j \leq M such that both of the following are satisfied:
- (X_1,X_2,\ldots,X_{j-1}) = (Y_1,Y_2,\ldots,Y_{j-1})
- X_j is lexicographically smaller than Y_j.
Constraints
- S and T are strings consisting of lowercase English letters with length between 1 and 100, inclusive.
- The lengths of S and T are equal.
Input
The input is given from Standard Input in the following format:
S T
Output
Let M be the number of elements in the desired array. Print M + 1 lines.
The first line should contain the value of M.
The i + 1-th line (1 \leq i \leq M) should contain the i-th element of the array.
Sample Input 1
adbe bcbc
Sample Output 1
3 acbe acbc bcbc
Initially, S = adbe.
We can obtain X = ( acbe , acbc , bcbc ) by performing the following operations:
-
Change S to
acbeand appendacbeto the end of X. -
Change S to
acbcand appendacbcto the end of X. -
Change S to
bcbcand appendbcbcto the end of X.
Sample Input 2
abcde abcde
Sample Output 2
0
Sample Input 3
afwgebrw oarbrenq
Sample Output 3
8 aawgebrw aargebrw aarbebrw aarbebnw aarbebnq aarbeenq aarbrenq oarbrenq
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N \times M のグリッドがあり、この上にプレイヤーがいます。
このグリッドの上から i 行目、左から j 列目をマス (i,j) と書きます。
このグリッドの各マスは 氷 か 岩 であり、その情報は N 個の長さ M の文字列 S_1,S_2,\dots,S_N として与えられます。
- もし S_i の j 文字目が
.なら、マス (i,j) は 氷 である。 - もし S_i の j 文字目が
#なら、マス (i,j) は 岩 である。
なお、このグリッドの外周 ( 1 行目、 N 行目、 1 列目、 M 列目の全てのマス ) は 岩 です。
最初、プレイヤーはマス (2,2) の上で停止しています。このマスは 氷 です。
プレイヤーは以下の行動を 0 度以上何度でも行うことができます。
- まず、プレイヤーは上下左右の移動方向を指定する。
- その後、プレイヤーは岩のマスにぶつかるまでその方向に移動する。厳密には以下の通りである。
- もし移動方向に隣接するマスが 氷 なら、そのマスに移動し、同じ方向に移動を継続する。
- もし移動方向に隣接するマスが 岩 なら、今いるマスに留まり、移動を終了する。
プレイヤーが触れる (通過または上で停止する) ことができる 氷 の数を求めてください。
制約
- 3 \le N,M \le 200
- S_i は
#と.からなる長さ M の文字列 - i=1 または i=N または j=1 または j=M であるとき、マス (i,j) は 岩
- マス (2,2) は 氷
入力
入力は以下の形式で標準入力から与えられる。
N M S_1 S_2 \vdots S_N
出力
答えを整数として出力せよ。
入力例 1
6 6 ###### #....# #.#..# #..#.# #....# ######
出力例 1
12
例えばマス (5,5) には以下のように移動することで上で停止することができます。
- (2,2) \rightarrow (5,2) \rightarrow (5,5)
例えばマス (2,4) には以下のように移動することで通過することができます。
- (2,2) \rightarrow (2,5) の移動中に (2,4) を通過する。
例えばマス (3,4) は通過することも上で停止することもできません。
入力例 2
21 25 ######################### #..............###...#### #..............#..#...### #........###...#...#...## #........#..#..#........# #...##...#..#..#...#....# #..#..#..###...#..#.....# #..#..#..#..#..###......# #..####..#..#...........# #..#..#..###............# #..#..#.................# #........##.............# #.......#..#............# #..........#....#.......# #........###...##....#..# #..........#..#.#...##..# #.......#..#....#..#.#..# ##.......##.....#....#..# ###.............#....#..# ####.................#..# #########################
出力例 2
215
Score : 400 points
Problem Statement
There is an N \times M grid and a player standing on it.
Let (i,j) denote the square at the i-th row from the top and j-th column from the left of this grid.
Each square of this grid is ice or rock, which is represented by N strings S_1,S_2,\dots,S_N of length M as follows:
- if the j-th character of S_i is
., square (i,j) is ice; - if the j-th character of S_i is
#, square (i,j) is rock.
The outer periphery of this grid (all squares in the 1-st row, N-th row, 1-st column, M-th column) is rock.
Initially, the player rests on the square (2,2), which is ice.
The player can make the following move zero or more times.
- First, specify the direction of movement: up, down, left, or right.
- Then, keep moving in that direction until the player bumps against a rock. Formally, keep doing the following:
- if the next square in the direction of movement is ice, go to that square and keep moving;
- if the next square in the direction of movement is rock, stay in the current square and stop moving.
Find the number of ice squares the player can touch (pass or rest on).
Constraints
- 3 \le N,M \le 200
- S_i is a string of length M consisting of
#and.. - Square (i, j) is rock if i=1, i=N, j=1, or j=M.
- Square (2,2) is ice.
Input
The input is given from Standard Input in the following format:
N M S_1 S_2 \vdots S_N
Output
Print the answer as an integer.
Sample Input 1
6 6 ###### #....# #.#..# #..#.# #....# ######
Sample Output 1
12
For instance, the player can rest on (5,5) by moving as follows:
- (2,2) \rightarrow (5,2) \rightarrow (5,5).
The player can pass (2,4) by moving as follows:
- (2,2) \rightarrow (2,5), passing (2,4) in the process.
The player cannot pass or rest on (3,4).
Sample Input 2
21 25 ######################### #..............###...#### #..............#..#...### #........###...#...#...## #........#..#..#........# #...##...#..#..#...#....# #..#..#..###...#..#.....# #..#..#..#..#..###......# #..####..#..#...........# #..#..#..###............# #..#..#.................# #........##.............# #.......#..#............# #..........#....#.......# #........###...##....#..# #..........#..#.#...##..# #.......#..#....#..#.#..# ##.......##.....#....#..# ###.............#....#..# ####.................#..# #########################
Sample Output 2
215
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
N 頂点 M 辺の連結な無向グラフがあり、 i 番目の辺は頂点 U_i と頂点 V_i を双方向に結びます。
また、全ての頂点に整数が書いてあり、頂点 v には整数 A_v が書かれています。
頂点 1 から頂点 N への単純なパス ( 同じ頂点を複数回通らないパス ) に対して、以下のように得点を定めます。
- パス上の頂点に書かれた整数を通った順に並べた数列 を S とする。
- S が広義単調増加になっていない場合、そのパスの得点は 0 である。
- そうでない場合、 S に含まれる整数の種類数が得点となる。
頂点 1 から頂点 N への全ての単純なパスのうち、最も得点が高いものを求めてその得点を出力してください。
S が広義単調増加であるとは?
長さ l の数列 S=(S_1,S_2,\dots,S_l) が広義単調増加であるとは、 全ての整数 1 \le i < l について S_i \le S_{i+1} を満たすことを言います。制約
- 入力は全て整数
- 2 \le N \le 2 \times 10^5
- N-1 \le M \le 2 \times 10^5
- 1 \le A_i \le 2 \times 10^5
- グラフは連結である
- 1 \le U_i < V_i \le N
- i \neq j なら (U_i,V_i) \neq (U_j,V_j)
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N U_1 V_1 U_2 V_2 \vdots U_M V_M
出力
答えを整数として出力せよ。
入力例 1
5 6 10 20 30 40 50 1 2 1 3 2 5 3 4 3 5 4 5
出力例 1
4
1 \rightarrow 3 \rightarrow 4 \rightarrow 5 というパスについて S=(10,30,40,50) となり、このパスの得点は 4 で、これが最大です。
入力例 2
4 5 1 10 11 4 1 2 1 3 2 3 2 4 3 4
出力例 2
0
頂点 1 から頂点 N への単純パスであって、 S が広義単調増加となるものはありません。この場合、最大の得点は 0 です。
入力例 3
10 12 1 2 3 3 4 4 4 6 5 7 1 3 2 9 3 4 5 6 1 2 8 9 4 5 8 10 7 10 4 6 2 8 6 7
出力例 3
5
Score : 525 points
Problem Statement
There is a connected undirected graph with N vertices and M edges, where the i-th edge connects vertex U_i and vertex V_i bidirectionally.
Each vertex has an integer written on it, with integer A_v written on vertex v.
For a simple path from vertex 1 to vertex N (a path that does not pass through the same vertex multiple times), the score is determined as follows:
- Let S be the sequence of integers written on the vertices along the path, listed in the order they are visited.
- If S is not non-decreasing, the score of that path is 0.
- Otherwise, the score is the number of distinct integers in S.
Find the path from vertex 1 to vertex N with the highest score among all simple paths and print that score.
What does it mean for S to be non-decreasing?
A sequence S=(S_1,S_2,\dots,S_l) of length l is said to be non-decreasing if and only if S_i \le S_{i+1} for all integers 1 \le i < l.Constraints
- All input values are integers.
- 2 \le N \le 2 \times 10^5
- N-1 \le M \le 2 \times 10^5
- 1 \le A_i \le 2 \times 10^5
- The graph is connected.
- 1 \le U_i < V_i \le N
- (U_i,V_i) \neq (U_j,V_j) if i \neq j.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N U_1 V_1 U_2 V_2 \vdots U_M V_M
Output
Print the answer as an integer.
Sample Input 1
5 6 10 20 30 40 50 1 2 1 3 2 5 3 4 3 5 4 5
Sample Output 1
4
The path 1 \rightarrow 3 \rightarrow 4 \rightarrow 5 has S=(10,30,40,50) for a score of 4, which is the maximum.
Sample Input 2
4 5 1 10 11 4 1 2 1 3 2 3 2 4 3 4
Sample Output 2
0
There is no simple path from vertex 1 to vertex N such that S is non-decreasing. In this case, the maximum score is 0.
Sample Input 3
10 12 1 2 3 3 4 4 4 6 5 7 1 3 2 9 3 4 5 6 1 2 8 9 4 5 8 10 7 10 4 6 2 8 6 7
Sample Output 3
5
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
長さ N の A, B からなる文字列 S が与えられます。
以下のクエリを合計 Q 個処理してください。
1 i c
タイプ 1 : S の i 文字目を c に変更する。
2 l r
タイプ 2 :
文字列 T を、現在の文字列 S の l 文字目から r 文字目までを抜き出した文字列とする。
文字列 T が以下の操作で得られる文字列であれば Yes 、そうでなければ No と出力する。
- 空文字列から始め、以下の 2 つの操作を好きな順序で 0 回以上何回でも行う。
- 文字列の(先頭・末尾を含めた)任意の箇所を選択し、そこに
Aを挿入する。- 文字列の(先頭・末尾を含めた)任意の箇所を選択し、そこに
ABを挿入する。
制約
- N は 1 \le N \le 5 \times 10^5 を満たす整数
- S は
A,Bからなる長さ N の文字列 - Q は 1 \le Q \le 2 \times 10^5 を満たす整数
- 与えられるクエリはタイプ 1,2 のいずれかである
- タイプ 1 のクエリは以下の制約を満たす
- i は 1 \le i \le N を満たす整数
- c は
A,Bのいずれか
- タイプ 2 のクエリは以下の制約を満たす
- l,r は 1 \le l \le r \le N を満たす整数
入力
入力は以下の形式で標準入力から与えられる。
N
S
Q
{\rm Query}_1
{\rm Query}_2
\vdots
{\rm Query}_Q
但し、 {\rm Query}_i は i 個目のクエリを表し、クエリの入力は問題文中の形式に従う。
出力
タイプ 2 のクエリが入力されるごとに、答えを 1 行に出力せよ。
入力例 1
10 AABBAABABB 6 2 1 10 1 5 B 2 1 10 2 6 8 1 3 A 2 1 10
出力例 1
Yes No Yes Yes
この入力には 6 個のクエリが含まれます。
- 最初、 S=
AABBAABABBです。 - 1 つ目のクエリについて、 S の 1 文字目から 10 文字目までを抜き出した T=
AABBAABABBは以下の手順を踏むことで得ることができるため、Yesと出力します。- 空文字列から始める。
- 空文字列の先頭に
ABを挿入し、文字列がABとなる。 ABの 1 文字目の直後にABを挿入し、文字列がAABBとなる。AABBの末尾にABを挿入し、文字列がAABBABとなる。AABBABの 5 文字目の直後にABを挿入し、文字列がAABBAABBとなる。AABBAABBの 7 文字目の直後にABを挿入し、文字列がAABBAABABBとなる。
- 2 つ目のクエリについて、 S の 5 文字目を
Bに変更します。その結果、 S=AABBBABABBとなります。 - 3 つ目のクエリについて、 S の 1 文字目から 10 文字目までを抜き出した T=
AABBBABABBは問題文中の操作では得られないため、Noと出力します。 - 4 つ目のクエリについて、 S の 6 文字目から 8 文字目までを抜き出した T=
ABAは問題文中の操作で得ることができるため、Yesと出力します。 - 5 つ目のクエリについて、 S の 3 文字目を
Aに変更します。その結果、 S=AAABBABABBとなります。 - 6 つ目のクエリについて、 S の 1 文字目から 10 文字目までを抜き出した T=
AAABBABABBは問題文中の操作で得ることができるため、Yesと出力します。
Score : 525 points
Problem Statement
You are given a string S of length N consisting of A and B.
Process a total of Q queries of the following types.
1 i c
Type 1: Change the i-th character of S to c.
2 l r
Type 2:
Let string T be the string obtained by extracting the l-th through r-th characters of the current string S.
If it is possible to obtain string T by the following operation, output Yes; otherwise, output No.
- Starting from the empty string, perform the following two operations any number of times, possibly zero, in any order.
- Choose any position in the string (possibly the beginning or the end) and insert
Athere.- Choose any position in the string (possibly the beginning or the end) and insert
ABthere.
Constraints
- N is an integer satisfying 1 \le N \le 5 \times 10^5.
- S is a string of length N consisting of
AandB. - Q is an integer satisfying 1 \le Q \le 2 \times 10^5.
- Each given query is of type 1 or 2.
- Queries of type 1 satisfy the following constraints:
- i is an integer satisfying 1 \le i \le N, and
- c is
AorB.
- Queries of type 2 satisfy the following constraints:
- l and r are integers satisfying 1 \le l \le r \le N.
Input
The input is given from Standard Input in the following format:
N
S
Q
{\rm Query}_1
{\rm Query}_2
\vdots
{\rm Query}_Q
Here, {\rm Query}_i represents the i-th query, and the input format for a query follows the format described in the problem statement.
Output
Each time a query of type 2 is given, output the answer on one line.
Sample Input 1
10 AABBAABABB 6 2 1 10 1 5 B 2 1 10 2 6 8 1 3 A 2 1 10
Sample Output 1
Yes No Yes Yes
This input contains six queries.
- Initially, S=
AABBAABABB. - For the 1-st query, T=
AABBAABABB, obtained by extracting the 1-st through 10-th characters of S, can be obtained by the following steps, so outputYes.- Start from the empty string.
- Insert
ABat the beginning of the empty string, making the stringAB. - Insert
ABright after the 1-st character ofAB, making the stringAABB. - Insert
ABat the end ofAABB, making the stringAABBAB. - Insert
ABright after the 5-th character ofAABBAB, making the stringAABBAABB. - Insert
ABright after the 7-th character ofAABBAABB, making the stringAABBAABABB.
- For the 2-nd query, change the 5-th character of S to
B. As a result, S=AABBBABABB. - For the 3-rd query, T=
AABBBABABB, obtained by extracting the 1-st through 10-th characters of S, cannot be obtained by the operation described in the problem statement, so outputNo. - For the 4-th query, T=
ABA, obtained by extracting the 6-th through 8-th characters of S, can be obtained by the operation described in the problem statement, so outputYes. - For the 5-th query, change the 3-rd character of S to
A. As a result, S=AAABBABABB. - For the 6-th query, T=
AAABBABABB, obtained by extracting the 1-st through 10-th characters of S, can be obtained by the operation described in the problem statement, so outputYes.