Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
H 行 W 列のマス目の上に 0 個以上のセンサが配置されています。上から i 行目、左から j 列目のマス目を (i, j) と表記します。
センサが配置されているマス目の情報は長さ W の文字列 S_1, S_2, \ldots, S_H によって与えられ、S_i の j 文字目が # のとき、またそのときに限り (i, j) にセンサが配置されています。
このセンサは上下左右斜めに隣接しているマス目に存在する他のセンサと連動し、一つのセンサとして動作します。
ただし、マス目 (x, y) と (x', y') が上下左右斜めに隣接しているとは、\max(|x-x'|,|y-y'|) = 1 であることを指します。
また、センサ A とセンサ B が連動し、センサ A とセンサ C が連動しているとき、センサ B とセンサ C も連動することに注意してください。
連動するセンサを一つのセンサと見なしたとき、このマス目の上にあるセンサの個数を求めてください。
制約
- 1 \leq H, W \leq 1000
- H, W は整数
- S_i は各文字が
#または.である長さ W の文字列
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H
出力
答えを出力せよ。
入力例 1
5 6 .##... ...#.. ....## #.#... ..#...
出力例 1
3
連動しているセンサを一つのセンサと見なしたとき、
- (1,2),(1,3),(2,4),(3,5),(3,6) にあるセンサが連動したもの
- (4,1) にあるセンサ
- (4,3),(5,3) にあるセンサが連動したもの
の 3 つのセンサが存在します。
入力例 2
3 3 #.# .#. #.#
出力例 2
1
入力例 3
4 2 .. .. .. ..
出力例 3
0
入力例 4
5 47 .#..#..#####..#...#..#####..#...#...###...##### .#.#...#.......#.#...#......##..#..#...#..#.... .##....#####....#....#####..#.#.#..#......##### .#.#...#........#....#......#..##..#...#..#.... .#..#..#####....#....#####..#...#...###...#####
出力例 4
7
Score : 300 points
Problem Statement
There are zero or more sensors placed on a grid of H rows and W columns. Let (i, j) denote the square in the i-th row from the top and the j-th column from the left.
Whether each square contains a sensor is given by the strings S_1, S_2, \ldots, S_H, each of length W. (i, j) contains a sensor if and only if the j-th character of S_i is #.
These sensors interact with other sensors in the squares horizontally, vertically, or diagonally adjacent to them and operate as one sensor.
Here, a cell (x, y) and a cell (x', y') are said to be horizontally, vertically, or diagonally adjacent if and only if \max(|x-x'|,|y-y'|) = 1.
Note that if sensor A interacts with sensor B and sensor A interacts with sensor C, then sensor B and sensor C also interact.
Considering the interacting sensors as one sensor, find the number of sensors on this grid.
Constraints
- 1 \leq H, W \leq 1000
- H and W are integers.
- S_i is a string of length W where each character is
#or..
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H
Output
Print the answer.
Sample Input 1
5 6 .##... ...#.. ....## #.#... ..#...
Sample Output 1
3
When considering the interacting sensors as one sensor, the following three sensors exist:
- The interacting sensors at (1,2),(1,3),(2,4),(3,5),(3,6)
- The sensor at (4,1)
- The interacting sensors at (4,3),(5,3)
Sample Input 2
3 3 #.# .#. #.#
Sample Output 2
1
Sample Input 3
4 2 .. .. .. ..
Sample Output 3
0
Sample Input 4
5 47 .#..#..#####..#...#..#####..#...#...###...##### .#.#...#.......#.#...#......##..#..#...#..#.... .##....#####....#....#####..#.#.#..#......##### .#.#...#........#....#......#..##..#...#..#.... .#..#..#####....#....#####..#...#...###...#####
Sample Output 4
7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
この問題は F 問題 (Operate K) の部分問題であり、 K=1 です。
F 問題に正解するコードをこの問題に提出することで、この問題に正解できます。
文字列 S に対して以下の操作を 0 回以上 K 回以下行って、文字列 T と一致させられるか判定してください。
- 次の 3 種類の操作のうちひとつを選択し、実行する。
- S 中の (先頭や末尾を含む) 任意の位置に、任意の文字を 1 つ挿入する。
- S 中の文字を 1 つ選び、削除する。
- S 中の文字を 1 つ選び、別の 1 つの文字に変更する。
制約
- S,T は英小文字からなる長さ 1 以上 500000 以下の文字列
- \color{red}{K=1}
入力
入力は以下の形式で標準入力から与えられる。
K S T
出力
K 回以下の操作で S を T に一致させられる時 Yes 、そうでない時 No と出力せよ。
入力例 1
1 abc agc
出力例 1
Yes
abc の 2 文字目の b を g に置き換えることで、 abc を 1 回の操作で agc に変換できます。
入力例 2
1 abc awtf
出力例 2
No
1 回の操作では abc を awtf に変換できません。
入力例 3
1 abc ac
出力例 3
Yes
abc の 2 文字目の b を削除することで、 abc を 1 回の操作で ac に変換できます。
入力例 4
1 back black
出力例 4
Yes
back の 1 文字目と 2 文字目の間に l を挿入することで、 back を 1 回の操作で black に変換できます。
入力例 5
1 same same
出力例 5
Yes
初めから S=T である場合もあります。
入力例 6
1 leap read
出力例 6
No
Score : 350 points
Problem Statement
This problem is a sub-problem of Problem F (Operate K), with K=1.
You can solve this problem by submitting a correct solution for Problem F to this problem.
Determine whether it is possible to perform the following operation on string S between 0 and K times, inclusive, to make it identical to string T.
- Choose one of the following three operations and execute it.
- Insert any one character at any position in S (possibly the beginning or end).
- Delete one character from S.
- Choose one character in S and replace it with another character.
Constraints
- Each of S and T is a string of length between 1 and 500000, inclusive, consisting of lowercase English letters.
- \color{red}{K=1}
Input
The input is given from Standard Input in the following format:
K S T
Output
If S can be made identical to T with at most K operations, print Yes; otherwise, print No.
Sample Input 1
1 abc agc
Sample Output 1
Yes
Replacing the second character b of abc with g converts abc to agc in one operation.
Sample Input 2
1 abc awtf
Sample Output 2
No
abc cannot be converted to awtf in one operation.
Sample Input 3
1 abc ac
Sample Output 3
Yes
Deleting the second character b of abc converts abc to ac in one operation.
Sample Input 4
1 back black
Sample Output 4
Yes
Inserting l between the first and second characters of back converts back to black in one operation.
Sample Input 5
1 same same
Sample Output 5
Yes
It is also possible that S = T from the beginning.
Sample Input 6
1 leap read
Sample Output 6
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
1 から N までの番号がついた N 枚のカードが一列に並んでいて、各 i\ (1\leq i < N) に対してカード i とカード i+1 が隣り合っています。 カード i の表には A_i が、裏には B_i が書かれており、最初全てのカードは表を向いています。
今から、N 枚のカードのうち好きな枚数 (0 枚でも良い) を選んで裏返すことを考えます。 裏返すカードの選び方は 2^N 通りありますが、そのうち以下の条件を満たすものの数を 998244353 で割った余りを求めてください。
- 選んだカードを裏返した後、どの隣り合う 2 枚のカードについても、向いている面に書かれた数が相異なる。
制約
- 1\leq N \leq 2\times 10^5
- 1\leq A_i,B_i \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
答えを整数として出力せよ。
入力例 1
3 1 2 4 2 3 4
出力例 1
4
裏返すカードの番号の集合を S とします。
例えば S=\{2,3\} を選ぶと、向いている面に書かれた数はカード 1 から順に 1,2,4 となるため条件を満たします。
一方 S=\{3\} を選ぶと、向いている面に書かれた数はカード 1 から順に 1,4,4 となり、カード 2 とカード 3 の数が一致するため条件を満たしません。
条件を満たす S は \{\},\{1\},\{2\},\{2,3\} の 4 通りです。
入力例 2
4 1 5 2 6 3 7 4 8
出力例 2
16
入力例 3
8 877914575 602436426 861648772 623690081 476190629 262703497 971407775 628894325 822804784 450968417 161735902 822804784 161735902 822804784 822804784 161735902
出力例 3
48
Score : 400 points
Problem Statement
N cards, numbered 1 through N, are arranged in a line. For each i\ (1\leq i < N), card i and card (i+1) are adjacent to each other. Card i has A_i written on its front, and B_i written on its back. Initially, all cards are face up.
Consider flipping zero or more cards chosen from the N cards. Among the 2^N ways to choose the cards to flip, find the number, modulo 998244353, of such ways that:
- when the chosen cards are flipped, for every pair of adjacent cards, the integers written on their face-up sides are different.
Constraints
- 1\leq N \leq 2\times 10^5
- 1\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 the answer as an integer.
Sample Input 1
3 1 2 4 2 3 4
Sample Output 1
4
Let S be the set of card numbers to flip.
For example, when S=\{2,3\} is chosen, the integers written on their visible sides are 1,2, and 4, from card 1 to card 3, so it satisfies the condition.
On the other hand, when S=\{3\} is chosen, the integers written on their visible sides are 1,4, and 4, from card 1 to card 3, where the integers on card 2 and card 3 are the same, violating the condition.
Four S satisfy the conditions: \{\},\{1\},\{2\}, and \{2,3\}.
Sample Input 2
4 1 5 2 6 3 7 4 8
Sample Output 2
16
Sample Input 3
8 877914575 602436426 861648772 623690081 476190629 262703497 971407775 628894325 822804784 450968417 161735902 822804784 161735902 822804784 822804784 161735902
Sample Output 3
48
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
高橋君が住む世界の一週間は N 日からなります。
一週間は曜日 1,2,\dots,N と進んでいき、曜日 N が終わると次の週の曜日 1 が始まります。
ABC 国の国王である高橋君は、各曜日に「平日」「休日」のどちらかを割り当てます。この割り当ては毎週同じでなければなりません。また、少なくとも 1 つの曜日を「休日」に割り当てなければなりません。
この条件の下で、曜日 i の生産量は長さ N の数列 A を用いて以下のように定義されます。
- 曜日 i が「休日」である場合は 0
- 曜日 i が「平日」のとき、直前の休日が x 日前、直後の休日が y 日後である場合は A_{\min(x,y)}
- 割り当ては毎週繰り返されるため、 直前 / 直後 の「休日」が当日とは別の週に属する可能性があることに注意してください。詳しくはサンプルを参照してください。
上手く割り当てを決めたときの一週間当たりの生産量の最大値を答えてください。
但し、一週間当たりの生産量とは曜日 1,2,\dots,N の生産量の総和を指します。
制約
- 入力はすべて整数
- 1 \le N \le 5000
- 1 \le A_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
7 10 10 1 1 1 1 1
出力例 1
50
例えば曜日 2,4 を「休日」、残りを「平日」に割り当てることで、以下のように一週間当たりの生産量 50 を達成できます。
- 曜日 1 ... x=4,y=1 なので、この曜日の生産量は A_1 = 10 である。
- 曜日 2 ... 「休日」であるので、この曜日の生産量は 0 である。
- 曜日 3 ... x=1,y=1 なので、この曜日の生産量は A_1 = 10 である。
- 曜日 4 ... 「休日」であるので、この曜日の生産量は 0 である。
- 曜日 5 ... x=1,y=4 なので、この曜日の生産量は A_1 = 10 である。
- 曜日 6 ... x=2,y=3 なので、この曜日の生産量は A_2 = 10 である。
- 曜日 7 ... x=3,y=2 なので、この曜日の生産量は A_2 = 10 である。
一週間当たりの生産量を 51 以上にすることはできません。
入力例 2
10 200000000 500000000 1000000000 800000000 100000000 80000000 600000 900000000 1 20
出力例 2
5100000000
入力例 3
20 38 7719 21238 2437 8855 11797 8365 32285 10450 30612 5853 28100 1142 281 20537 15921 8945 26285 2997 14680
出力例 3
236980
Score : 500 points
Problem Statement
In the world where Takahashi lives, a week has N days.
Takahashi, the king of the Kingdom of AtCoder, assigns "weekday" or "holiday" to each day of week. The assignments should be the same for all weeks. At least one day of week should be assigned "holiday".
Under such conditions, the productivity of the i-th day of week is defined by a sequence A of length N as follows:
- if the i-th day of week is "holiday", its productivity is 0;
- if the i-th day of week is "weekday", its productivity is A_{\min(x,y)}, if the last holiday is x days before and the next one is y days after.
- Note that the last/next holiday may belong to a different week due to the periodic assignments. For details, see the Samples.
Find the maximum productivity per week when the assignments are chosen optimally.
Here, the productivity per week refers to the sum of the productivities of the 1-st, 2-nd, \dots, and N-th day of week.
Constraints
- All values in the input are integers.
- 1 \le N \le 5000
- 1 \le A_i \le 10^9
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the answer as an integer.
Sample Input 1
7 10 10 1 1 1 1 1
Sample Output 1
50
For example, we can assign "holiday" to the 2-nd and 4-th day of week and "weekday" to the rest to achieve a productivity of 50 per week:
- the 1-st day of week ... x=4 and y=1, so its productivity is A_1 = 10.
- the 2-nd day of week ... it is holiday, so its productivity is 0.
- the 3-st day of week ... x=1 and y=1, so its productivity is A_1 = 10.
- the 4-th day of week ... it is holiday, so its productivity is 0.
- the 5-th day of week ... x=1 and y=4, so its productivity is A_1 = 10.
- the 6-th day of week ... x=2 and y=3, so its productivity is A_2 = 10.
- the 7-th day of week ... x=3 and y=2, so its productivity is A_2 = 10.
It is impossible to make the productivity per week 51 or greater.
Sample Input 2
10 200000000 500000000 1000000000 800000000 100000000 80000000 600000 900000000 1 20
Sample Output 2
5100000000
Sample Input 3
20 38 7719 21238 2437 8855 11797 8365 32285 10450 30612 5853 28100 1142 281 20537 15921 8945 26285 2997 14680
Sample Output 3
236980
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
1 から N までの番号のついた N 個の区間が与えられます。 区間 i は [L_i,R_i] です。
区間 [l_a,r_a] と区間 [l_b,r_b] は (l_a < l_b < r_a < r_b) または (l_b < l_a < r_b < r_a) を満たすとき、交差するといいます。
f(l,r) を 1 \leq i \leq N を満たし、区間 [l,r] と区間 i が交差する i の個数と定義します。
0 \leq l < r \leq 10^{9} を満たす整数の組 (l,r) において、 f(l,r) の最大値を達成する (l,r) の組のうち l が最小のものを答えてください。そのような組が複数存在する場合はさらにそのうちで r が最小のものを答えてください (0 \leq l < r より、 答えるべき (l,r) の組は一意に定まります)。
制約
- 1 \leq N \leq 10^{5}
- 0 \leq L_i < R_i \leq 10^{9} (1 \leq i \leq N)
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N L_1 R_1 L_2 R_2 \vdots L_N R_N
出力
答えとなる組 (l,r) を次の形式で出力せよ。
l r
入力例 1
5 1 7 3 9 7 18 10 14 15 20
出力例 1
4 11
f(l,r) の最大値は 4 であり、f(l,r)=4 となる (l,r) のうち l の最小値は 4 です。 f(l,r)=4 かつ l=4 を満たす (l,r) は以下の 5 通りです。
- (l,r)=(4,11)
- (l,r)=(4,12)
- (l,r)=(4,13)
- (l,r)=(4,16)
- (l,r)=(4,17)
このうち、r の最小値は 11 であるため、4 と 11 を出力します。
入力例 2
11 856977192 996441446 298251737 935869360 396653206 658841528 710569907 929136831 325371222 425309117 379628374 697340458 835681913 939343451 140179224 887672320 375607390 611397526 93530028 581033295 249611310 775998537
出力例 2
396653207 887672321
Score : 550 points
Problem Statement
You are given N intervals numbered 1 to N. Interval i is [L_i, R_i].
Two intervals [l_a, r_a] and [l_b, r_b] are said to intersect if and only if they satisfy either (l_a < l_b < r_a < r_b) or (l_b < l_a < r_b < r_a).
Define f(l, r) as the number of intervals i (1 \leq i \leq N) that intersect with the interval [l, r].
Among all pairs of integers (l, r) satisfying 0 \leq l < r \leq 10^{9}, find the pair (l, r) that maximizes f(l, r). If there are multiple such pairs, choose the one with the smallest l. If there are still multiple pairs, choose the one with the smallest r among them. (Since 0 \leq l < r, the pair (l, r) to be answered is uniquely determined.)
Constraints
- 1 \leq N \leq 10^{5}
- 0 \leq L_i < R_i \leq 10^{9} (1 \leq i \leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N L_1 R_1 L_2 R_2 \vdots L_N R_N
Output
Print the sought pair (l, r) in the following format:
l r
Sample Input 1
5 1 7 3 9 7 18 10 14 15 20
Sample Output 1
4 11
The maximum value of f(l, r) is 4, and among the pairs (l, r) that achieve f(l, r) = 4, the smallest l is 4. The pairs (l, r) that satisfy f(l, r) = 4 and l = 4 are the following five:
- (l, r) = (4, 11)
- (l, r) = (4, 12)
- (l, r) = (4, 13)
- (l, r) = (4, 16)
- (l, r) = (4, 17)
Among these, the smallest r is 11, so print 4 and 11.
Sample Input 2
11 856977192 996441446 298251737 935869360 396653206 658841528 710569907 929136831 325371222 425309117 379628374 697340458 835681913 939343451 140179224 887672320 375607390 611397526 93530028 581033295 249611310 775998537
Sample Output 2
396653207 887672321