Time Limit: 2 sec / Memory Limit: 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
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
横一列に 4 つのマスが並んでいます。
各文字が 0
または 1
である長さ 4 の文字列 S が与えられます。
S の i 文字目が 1
であるとき、左から i 番目のマスには 1 人の人がおり、
S の i 文字目が 0
であるとき、左から i 番目のマスには人がいません。
全ての人が一斉に、1 つ右隣のマスへ移動します。この移動により、もともと右端のマスにいた人は消えます。
移動後の各マスに人がいるかどうかを、S と同様のルールで文字列として出力してください。
制約
- S は
0
,1
のみからなる長さ 4 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
長さ 4 の文字列であって、移動後、左から i 番目のマスに人がいるならば i 文字目が 1
、いないならば 0
であるようなものを出力せよ。
入力例 1
1011
出力例 1
0101
移動により、左から 1 番目のマスにいた人は左から 2 番目のマスに、
左から 3 番目のマスにいた人は左から 4 番目のマスに移動し、
左から 4 番目のマス(右端のマス)にいた人は消えます。
入力例 2
0000
出力例 2
0000
入力例 3
1111
出力例 3
0111
Score : 100 points
Problem Statement
There are 4 squares lined up horizontally.
You are given a string S of length 4 consisting of 0
and 1
.
If the i-th character of S is 1
, there is a person in the i-th square from the left;
if the i-th character of S is 0
, there is no person in the i-th square from the left.
Now, everyone will move to the next square to the right simultaneously. By this move, the person who was originally in the rightmost square will disappear.
Determine if there will be a person in each square after the move. Print the result as a string in the same format as S. (See also Sample Input / Output for clarity.)
Constraints
- S is a string of length 4 consisting of
0
and1
.
Input
Input is given from Standard Input in the following format:
S
Output
Print a string of length 4 such that the i-th character is 1
if there will be a person in the i-th square from the left after the move, and 0
otherwise.
Sample Input 1
1011
Sample Output 1
0101
After the move, the person who was originally in the 1-st square will move to the 2-nd square,
the person in the 3-rd square to the 4-th square,
and the person in the 4-th square will disappear.
Sample Input 2
0000
Sample Output 2
0000
Sample Input 3
1111
Sample Output 3
0111
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
正整数 M が与えられます。 以下の条件を全て満たす正整数 N と非負整数列 A=(A_1,A_2,\ldots,A_N) を一つ求めてください。
- 1\le N\le 20
- 0\le A_i\le 10 (1\le i\le N)
- \displaystyle \sum_{i=1}^N 3^{A_i}=M
ただし、制約下では条件を満たす N と A の組が必ず存在することが証明できます。
制約
- 1\le M\le 10^5
入力
入力は以下の形式で標準入力から与えられる。
M
出力
以下の形式で条件を満たす N と A を出力せよ。
N A_1 A_2 \ldots A_N
なお、条件を満たす N と A の組が複数存在する場合は、どれを出力しても正答となる。
入力例 1
6
出力例 1
2 1 1
N=2 、A=(1,1) とすると \displaystyle \sum_{i=1}^N 3^{A_i}=3+3=6 より全ての条件を満たします。
他に N=4 、 A=(0,0,1,0) なども条件を満たします。
入力例 2
100
出力例 2
4 2 0 2 4
入力例 3
59048
出力例 3
20 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
1\le N\le 20 という制約に注意してください。
Score : 200 points
Problem Statement
You are given a positive integer M. Find a positive integer N and a sequence of non-negative integers A = (A_1, A_2, \ldots, A_N) that satisfy all of the following conditions:
- 1 \le N \le 20
- 0 \le A_i \le 10 (1 \le i \le N)
- \displaystyle \sum_{i=1}^N 3^{A_i} = M
It can be proved that under the constraints, there always exists at least one such pair of N and A satisfying the conditions.
Constraints
- 1 \le M \le 10^5
Input
The input is given from Standard Input in the following format:
M
Output
Print N and A satisfying the conditions in the following format:
N A_1 A_2 \ldots A_N
If there are multiple valid pairs of N and A, any of them is acceptable.
Sample Input 1
6
Sample Output 1
2 1 1
For example, with N=2 and A=(1,1), we have \displaystyle \sum_{i=1}^N 3^{A_i} = 3+3=6, satisfying all conditions.
Another example is N=4 and A=(0,0,1,0), which also satisfies the conditions.
Sample Input 2
100
Sample Output 2
4 2 0 2 4
Sample Input 3
59048
Sample Output 3
20 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
Note the condition 1 \le N \le 20.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は英小文字からなる文字列 S をキーボードで入力しようとしました。
高橋君は画面を見ずにキーボードだけを見てタイピングをしていました。
誤って別の英小文字を入力してしまったときにはその直後にバックスペースキーを押しましたが、バックスペースキーが壊れていたため誤って入力された文字は消去されず、実際に入力された文字列は文字列 T となりました。
また、英小文字以外のキーを誤って押してしまうことはありませんでした。
T のうち高橋君が誤って入力した文字でないものを正しく入力された文字であると定めます。
正しく入力された文字が T の何文字目であるか答えてください。
制約
- S, T は長さ 1 以上 2 \times 10^5 以下の英小文字からなる文字列
- T は問題文中の手続きにより得られる文字列
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
S の長さを |S| として、正しく入力された文字が A_1, A_2, \ldots, A_{|S|} 文字目であるとき A_1, A_2, \ldots, A_{|S|} の値をこの順に空白区切りで出力せよ。
ただし、出力は昇順になるようにせよ。すなわち、各 1 \leq i \leq |S| - 1 に対して A_i < A_{i + 1} を満たすようにせよ。
入力例 1
abc axbxyc
出力例 1
1 3 6
高橋君のタイピングの一連の流れは以下のようになります。
a
を入力する。b
を入力しようとするが、誤ってx
を入力してしまう。- バックスペースキーを押すが、文字の削除は行われない。
b
を入力する。c
を入力しようとするが、誤ってx
を入力してしまう。- バックスペースキーを押すが、文字の削除は行われない。
c
を入力しようとするが、誤ってy
を入力してしまう。- バックスペースキーを押すが、文字の削除は行われない。
c
を入力する。
正しく入力された文字は 1, 3, 6 文字目です。
入力例 2
aaaa bbbbaaaa
出力例 2
5 6 7 8
入力例 3
atcoder atcoder
出力例 3
1 2 3 4 5 6 7
高橋君が誤った文字を入力することはありませんでした。
Score: 200 points
Problem Statement
Takahashi tried to type a string S consisting of lowercase English letters using a keyboard.
He was typing while looking only at the keyboard, not the screen.
Whenever he mistakenly typed a different lowercase English letter, he immediately pressed the backspace key. However, the backspace key was broken, so the mistakenly typed letter was not deleted, and the actual string typed was T.
He did not mistakenly press any keys other than those for lowercase English letters.
The characters in T that were not mistakenly typed are called correctly typed characters.
Determine the positions in T of the correctly typed characters.
Constraints
- S and T are strings of lowercase English letters with lengths between 1 and 2 \times 10^5, inclusive.
- T is a string obtained by the procedure described in the problem statement.
Input
The input is given from Standard Input in the following format:
S T
Output
Let |S| be the length of S. If the correctly typed characters are the A_1-th, A_2-th, \ldots, A_{|S|}-th characters of T, print the values of A_1, A_2, \ldots, A_{|S|} in this order, separated by spaces.
Ensure that the output is in ascending order. That is, A_i < A_{i + 1} should hold for each 1 \leq i \leq |S| - 1.
Sample Input 1
abc axbxyc
Sample Output 1
1 3 6
The sequence of Takahashi's typing is as follows:
- Type
a
. - Try to type
b
but mistakenly typex
. - Press the backspace key, but the character is not deleted.
- Type
b
. - Try to type
c
but mistakenly typex
. - Press the backspace key, but the character is not deleted.
- Try to type
c
but mistakenly typey
. - Press the backspace key, but the character is not deleted.
- Type
c
.
The correctly typed characters are the first, third, and sixth characters.
Sample Input 2
aaaa bbbbaaaa
Sample Output 2
5 6 7 8
Sample Input 3
atcoder atcoder
Sample Output 3
1 2 3 4 5 6 7
Takahashi did not mistakenly type any characters.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
整数 N が与えられるので、以下の問題を解いてください。
f(x)= ( x 以下の正整数で、 x と桁数が同じものの数) とします。
f(1)+f(2)+\dots+f(N) を 998244353 で割った余りを求めてください。
制約
- N は整数
- 1 \le N < 10^{18}
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを整数として出力せよ。
入力例 1
16
出力例 1
73
- 1 以上 9 以下の正整数 x について、 x 以下の整数で、 x と桁数が同じものは 1,2,\dots,x です。
- これより、 f(1)=1,f(2)=2,...,f(9)=9 となります。
- 10 以上 16 以下の正整数 x について、 x 以下の整数で、 x と桁数が同じものは 10,11,\dots,x です。
- これより、 f(10)=1,f(11)=2,...,f(16)=7 となります。
結局、求める答えは 73 です。
入力例 2
238
出力例 2
13870
入力例 3
999999999999999999
出力例 3
762062362
998244353 で割った余りを求めることに注意してください。
Score : 300 points
Problem Statement
Given an integer N, solve the following problem.
Let f(x)= (The number of positive integers at most x with the same number of digits as x).
Find f(1)+f(2)+\dots+f(N) modulo 998244353.
Constraints
- N is an integer.
- 1 \le N < 10^{18}
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer as an integer.
Sample Input 1
16
Sample Output 1
73
- For a positive integer x between 1 and 9, the positive integers at most x with the same number of digits as x are 1,2,\dots,x.
- Thus, we have f(1)=1,f(2)=2,...,f(9)=9.
- For a positive integer x between 10 and 16, the positive integers at most x with the same number of digits as x are 10,11,\dots,x.
- Thus, we have f(10)=1,f(11)=2,...,f(16)=7.
The final answer is 73.
Sample Input 2
238
Sample Output 2
13870
Sample Input 3
999999999999999999
Sample Output 3
762062362
Be sure to find the sum modulo 998244353.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 人のすぬけ君が円周上に並んでおり、反時計回りに 1,2,...,N の番号がついています。
i\, (1 \leq i \leq N) 番目のすぬけ君は時刻 t に宝石をもらうと S_i 単位時間後、すなわち時刻 t+S_i にその宝石を (i+1) 番目のすぬけ君に渡します。ただし、(N+1) 番目のすぬけ君とは 1 番目のすぬけ君のことを指すとします。
また、高橋君は時刻 T_i に i 番目のすぬけ君に宝石を渡します。
全ての i\, (1 \leq i \leq N) について、i 番目のすぬけ君が初めて宝石をもらう時刻を求めてください。なお、宝石の受け渡しにかかる時間は無視できるものとします。
制約
- 1 \leq N \leq 200000
- 1 \leq S_i,T_i \leq 10^9
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \ldots S_N T_1 T_2 \ldots T_N
出力
N 行出力せよ。i\, (1 \leq i \leq N) 行目には、i 番目のすぬけ君が初めて宝石をもらう時刻を出力すること。
入力例 1
3 4 1 5 3 10 100
出力例 1
3 7 8
時刻 13 までのすぬけ君と高橋君の行動を時系列順に並べます。
時刻 3 : 高橋君が 1 番目のすぬけ君に宝石を渡します。
時刻 7 : 1 番目のすぬけ君が 2 番目のすぬけ君に宝石を渡します。
時刻 8 : 2 番目のすぬけ君が 3 番目のすぬけ君に宝石を渡します。
時刻 10 : 高橋君が 2 番目のすぬけ君に宝石を渡します。
時刻 11 : 2 番目のすぬけ君が 3 番目のすぬけ君に宝石を渡します。
時刻 13 : 3 番目のすぬけ君が 1 番目のすぬけ君に宝石を渡します。
時刻 14 以降も彼らは宝石の受け渡しを行いますが、答えには影響しません。
入力例 2
4 100 100 100 100 1 1 1 1
出力例 2
1 1 1 1
S_i や T_i が相異なるとは限らないことに注意してください。
入力例 3
4 1 2 3 4 1 2 4 7
出力例 3
1 2 4 7
あるすぬけくんが同時刻に複数の宝石の受け渡しをする可能性があること、特に高橋くんとすぬけくんの両方から同時に宝石を貰う可能性があることに注意してください。
入力例 4
8 84 87 78 16 94 36 87 93 50 22 63 28 91 60 64 27
出力例 4
50 22 63 28 44 60 64 27
Score : 300 points
Problem Statement
There are N creatures standing in a circle, called Snuke 1, 2, ..., N in counter-clockwise order.
When Snuke i (1 \leq i \leq N) receives a gem at time t, S_i units of time later, it will hand that gem to Snuke i+1 at time t+S_i. Here, Snuke N+1 is Snuke 1.
Additionally, Takahashi will hand a gem to Snuke i at time T_i.
For each i (1 \leq i \leq N), find the time when Snuke i receives a gem for the first time. Assume that it takes a negligible time to hand a gem.
Constraints
- 1 \leq N \leq 200000
- 1 \leq S_i,T_i \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N S_1 S_2 \ldots S_N T_1 T_2 \ldots T_N
Output
Print N lines. The i-th line (1 \leq i \leq N) should contain the time when Snuke i receives a gem for the first time.
Sample Input 1
3 4 1 5 3 10 100
Sample Output 1
3 7 8
We will list the three Snuke's and Takahashi's actions up to time 13 in chronological order.
Time 3: Takahashi hands a gem to Snuke 1.
Time 7: Snuke 1 hands a gem to Snuke 2.
Time 8: Snuke 2 hands a gem to Snuke 3.
Time 10: Takahashi hands a gem to Snuke 2.
Time 11: Snuke 2 hands a gem to Snuke 3.
Time 13: Snuke 3 hands a gem to Snuke 1.
After that, they will continue handing gems, though it will be irrelevant to the answer.
Sample Input 2
4 100 100 100 100 1 1 1 1
Sample Output 2
1 1 1 1
Note that the values S_i and T_i may not be distinct.
Sample Input 3
4 1 2 3 4 1 2 4 7
Sample Output 3
1 2 4 7
Note that a Snuke may perform multiple transactions simultaneously. Particularly, a Snuke may receive gems simultaneously from Takahashi and another Snuke.
Sample Input 4
8 84 87 78 16 94 36 87 93 50 22 63 28 91 60 64 27
Sample Output 4
50 22 63 28 44 60 64 27
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋くんは、N 個の単語からなる文章をウィンドウに表示させようとしています。 すべての単語の縦幅は等しく、i 番目 (1\leq i\leq N) の単語の横幅は L _ i です。
文章は、横幅 1 の空白を単語の区切りとしてウィンドウに表示されます。 より厳密には、高橋くんが横幅 W のウィンドウに文章を表示しているとき、次の条件が成り立っています。
- 文章はいくつかの行に分かれている。
- 1 番目の単語は一番上の行の先頭に表示されている。
- i 番目 (2\leq i\leq N) の単語は、i-1 番目の単語の次に間隔を 1 だけ開けて表示されているか、i-1 番目の単語が含まれる行の下の行の先頭に表示されているかの一方である。それ以外の場所に表示されていることはない。
- それぞれの行の横幅は W を超えない。ここで、行の横幅とは最も左にある単語の左端から最も右にある単語の右端までの距離を指す。
高橋くんが文章をウィンドウに表示したとき、文章が M 行に収まりました。 ウィンドウの横幅としてありえる最小の値を求めてください。
制約
- 1\leq M\leq N\leq2\times10 ^ 5
- 1\leq L _ i\leq10^9\ (1\leq i\leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M L _ 1 L _ 2 \ldots L _ N
出力
答えを 1 行で出力せよ。
入力例 1
13 3 9 5 2 7 1 8 8 2 1 5 2 3 6
出力例 1
26
ウィンドウの横幅が 26 のとき、以下のようにして与えられた文章を 3 行に収めることができます。
ウィンドウの横幅が 25 以下のときは与えられた文章を 3 行に収めることができないため、26 を出力してください。
単語を複数の行にまたがって表示させたり、行の横幅がウィンドウの横幅を上回ったり、単語を並べ替えたりしてはいけないことに注意してください。
入力例 2
10 1 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
出力例 2
10000000009
答えが 32\operatorname{bit} 整数に収まらない場合があることに注意してください。
入力例 3
30 8 8 55 26 97 48 37 47 35 55 5 17 62 2 60 23 99 73 34 75 7 46 82 84 29 41 32 31 52 32 60
出力例 3
189
Score : 400 points
Problem Statement
Takahashi is displaying a sentence with N words in a window. All words have the same height, and the width of the i-th word (1\leq i\leq N) is L _ i.
The words are displayed in the window separated by a space of width 1. More precisely, when the sentence is displayed in a window of width W, the following conditions are satisfied.
- The sentence is divided into several lines.
- The first word is displayed at the beginning of the top line.
- The i-th word (2\leq i\leq N) is displayed either with a gap of 1 after the (i-1)-th word, or at the beginning of the line below the line containing the (i-1)-th word. It will not be displayed anywhere else.
- The width of each line does not exceed W. Here, the width of a line refers to the distance from the left end of the leftmost word to the right end of the rightmost word.
When Takahashi displayed the sentence in the window, the sentence fit into M or fewer lines. Find the minimum possible width of the window.
Constraints
- 1\leq M\leq N\leq2\times10 ^ 5
- 1\leq L _ i\leq10^9\ (1\leq i\leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M L _ 1 L _ 2 \ldots L _ N
Output
Print the answer in one line.
Sample Input 1
13 3 9 5 2 7 1 8 8 2 1 5 2 3 6
Sample Output 1
26
When the width of the window is 26, you can fit the given sentence into three lines as follows.
You cannot fit the given sentence into three lines when the width of the window is 25 or less, so print 26.
Note that you should not display a word across multiple lines, let the width of a line exceed the width of the window, or rearrange the words.
Sample Input 2
10 1 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 2
10000000009
Note that the answer may not fit into a 32\operatorname{bit} integer.
Sample Input 3
30 8 8 55 26 97 48 37 47 35 55 5 17 62 2 60 23 99 73 34 75 7 46 82 84 29 41 32 31 52 32 60
Sample Output 3
189
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
縦 H 行横 W 列のマス目があります。 上から i 行目 (1\leq i\leq H)、左から j 列目 (1\leq j\leq W) のマスをマス (i,j) と呼ぶことにします。
はじめ、マス (i,j) には強さ S _ {i,j} のスライムがおり、マス (P,Q) にいるスライムが高橋くんです。
高橋くんが以下の行動を好きな回数(0 回でもよい)行ったあとの、高橋くんの強さとしてありえる最大値を求めてください。
- 高橋くんに隣接するスライムのうち、強さが高橋くんの強さの \dfrac1X 倍未満のものを選んで吸収する。 その結果、吸収されたスライムは消滅し、高橋君の強さは吸収したスライムの強さだけ増加する。
上記の行動の際、スライムが吸収され消滅したことで生じた隙間は直ちに高橋くんによって埋められ、消滅したスライムに隣接していたスライム(それらが存在すれば)は新たに高橋くんと隣接します(入出力例1の説明も参照してください)。
制約
- 1\leq H,W\leq500
- 1\leq P\leq H
- 1\leq Q\leq W
- 1\leq X\leq10^9
- 1\leq S _ {i,j}\leq10^{12}
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W X P Q S _ {1,1} S _ {1,2} \ldots S _ {1,W} S _ {2,1} S _ {2,2} \ldots S _ {2,W} \vdots S _ {H,1} S _ {H,2} \ldots S _ {H,W}
出力
高橋くんが行動を行ったあとの高橋くんの強さとしてありえる最大値を出力せよ。
入力例 1
3 3 2 2 2 14 6 9 4 9 20 17 15 7
出力例 1
28
はじめ、それぞれのマスにいるスライムの強さは以下の図のようになっています。
例えば、高橋くんは次のように行動を行うことができます。
- マス (2,1) にいるスライムを吸収する。高橋くんの強さは 9+4=13 となり、新たにマス (1,1) のスライムとマス (3,1) のスライムが高橋くんと隣接する。
- マス (1,2) にいるスライムを吸収する。高橋くんの強さは 13+6=19 となり、新たにマス (1,3) のスライムが高橋くんと隣接する。
- マス (1,3) にいるスライムを吸収する。高橋くんの強さは 19+9=28 となる。
以上の行動を行ったあと、高橋くんの強さは 28 となります。
高橋くんがどのように行動を行っても、高橋くんの強さを 28 より大きくすることはできないため、28
を出力してください。
高橋くんの強さの \dfrac12 倍未満のスライムしか吸収できないことに注意してください。 例えば、上図の右側の状態からマス (1,1) にいるスライムを吸収することはできません。
入力例 2
3 4 1 1 1 5 10 1 1 10 1 1 1 1 1 1 1
出力例 2
5
高橋くんはどのスライムも吸収できません。
入力例 3
8 10 2 1 5 388 130 971 202 487 924 247 286 237 316 117 166 918 106 336 928 493 391 235 398 124 280 425 955 212 988 227 222 307 226 336 302 478 246 950 368 291 236 170 101 370 200 204 141 287 410 388 314 205 460 291 104 348 337 404 399 416 263 415 339 105 420 302 334 231 481 466 366 401 452 119 432 292 403 371 417 351 231 482 184
出力例 3
1343
Score : 450 points
Problem Statement
There is a grid with H horizontal rows and W vertical columns. Let (i, j) denote the cell at the i-th row (1\leq i\leq H) from the top and j-th column (1\leq j\leq W) from the left.
Initially, there is a slime with strength S _ {i,j} in cell (i,j), and Takahashi is the slime in the cell (P,Q).
Find the maximum possible strength of Takahashi after performing the following action any number of times (possibly zero):
- Among the slimes adjacent to him, choose one whose strength is strictly less than \dfrac{1}{X} times his strength and absorb it. As a result, the absorbed slime disappears, and Takahashi's strength increases by the strength of the absorbed slime.
When performing the above action, the gap left by the disappeared slime is immediately filled by Takahashi, and the slimes that were adjacent to the disappeared one (if any) become newly adjacent to Takahashi (refer to the explanation in sample 1).
Constraints
- 1\leq H,W\leq500
- 1\leq P\leq H
- 1\leq Q\leq W
- 1\leq X\leq10^9
- 1\leq S _ {i,j}\leq10^{12}
- All input values are integers.
Input
The input is given in the following format from Standard Input:
H W X P Q S _ {1,1} S _ {1,2} \ldots S _ {1,W} S _ {2,1} S _ {2,2} \ldots S _ {2,W} \vdots S _ {H,1} S _ {H,2} \ldots S _ {H,W}
Output
Print the maximum possible strength of Takahashi after performing the action.
Sample Input 1
3 3 2 2 2 14 6 9 4 9 20 17 15 7
Sample Output 1
28
Initially, the strength of the slime in each cell is as follows:
For example, Takahashi can act as follows:
- Absorb the slime in cell (2,1). His strength becomes 9+4=13, and the slimes in cells (1,1) and (3,1) become newly adjacent to him.
- Absorb the slime in cell (1,2). His strength becomes 13+6=19, and the slime in cell (1,3) becomes newly adjacent to him.
- Absorb the slime in cell (1,3). His strength becomes 19+9=28.
After these actions, his strength is 28.
No matter how he acts, it is impossible to get a strength greater than 28, so print 28
.
Note that Takahashi can only absorb slimes whose strength is strictly less than half of his strength. For example, in the figure on the right above, he cannot absorb the slime in cell (1,1).
Sample Input 2
3 4 1 1 1 5 10 1 1 10 1 1 1 1 1 1 1
Sample Output 2
5
He cannot absorb any slimes.
Sample Input 3
8 10 2 1 5 388 130 971 202 487 924 247 286 237 316 117 166 918 106 336 928 493 391 235 398 124 280 425 955 212 988 227 222 307 226 336 302 478 246 950 368 291 236 170 101 370 200 204 141 287 410 388 314 205 460 291 104 348 337 404 399 416 263 415 339 105 420 302 334 231 481 466 366 401 452 119 432 292 403 371 417 351 231 482 184
Sample Output 3
1343
Time Limit: 5 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 個の頂点と M 本の辺からなる有向グラフがあります。各辺には美しさとコストの 2 つの正整数値が定められています。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i から頂点 v_i への有向辺であり、その美しさは b_i 、コストは c_i です。 ここで、u_i \lt v_i が制約として保証されます。
頂点 1 から頂点 N へのパス P を 1 つ選んだときの、下記の値としてあり得る最大値を求めてください。
- P 上のすべての辺の美しさの総和を、P 上のすべての辺のコストの総和で割った値
ここで、与えられるグラフにおいて頂点 1 から頂点 N へのパスが 1 個以上存在することが制約として保証されます。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq u_i \lt v_i \leq N
- 1 \leq b_i, c_i \leq 10^4
- 頂点 1 から頂点 N へのパスが存在する
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 b_1 c_1 u_2 v_2 b_2 c_2 \vdots u_M v_M b_M c_M
出力
答えを出力せよ。出力された値と真の値の相対誤差もしくは絶対誤差が 10^{-9} 以下のとき、正答と判定される。
入力例 1
5 7 1 2 3 6 1 3 9 5 2 3 1 5 2 4 5 3 2 5 1 9 3 4 4 8 4 5 2 7
出力例 1
0.7500000000000000
パス P として、 2, 6, 7 番目の辺をこの順に通り頂点 1 \rightarrow 3 \rightarrow 4 \rightarrow 5 とたどるパスを選んだとき、「 P 上のすべての辺の美しさの総和を P 上のすべての辺のコストの総和で割った値」 は、 (b_2 + b_6 + b_7) / (c_2 + c_6 + c_7) = (9 + 4 + 2) / (5 + 8 + 7) = 15 / 20 = 0.75 であり、これがあり得る最大値です。
入力例 2
3 3 1 3 1 1 1 3 2 1 1 3 3 1
出力例 2
3.0000000000000000
入力例 3
10 20 3 4 1 2 7 9 4 5 2 4 4 5 4 5 1 4 6 9 4 1 9 10 3 2 6 10 5 5 5 6 1 2 5 6 5 2 2 3 2 3 6 10 4 4 4 6 3 4 4 8 4 1 3 5 3 2 2 4 3 2 3 5 4 2 1 5 3 4 1 2 4 2 3 7 2 2 7 8 1 3
出力例 3
1.8333333333333333
Score : 500 points
Problem Statement
There is a directed graph with N vertices and M edges. Each edge has two positive integer values: beauty and cost.
For i = 1, 2, \ldots, M, the i-th edge is directed from vertex u_i to vertex v_i, with beauty b_i and cost c_i. Here, the constraints guarantee that u_i \lt v_i.
Find the maximum value of the following for a path P from vertex 1 to vertex N.
- The total beauty of all edges on P divided by the total cost of all edges on P.
Here, the constraints guarantee that the given graph has at least one path from vertex 1 to vertex N.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq u_i \lt v_i \leq N
- 1 \leq b_i, c_i \leq 10^4
- There is a path from vertex 1 to vertex N.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M u_1 v_1 b_1 c_1 u_2 v_2 b_2 c_2 \vdots u_M v_M b_M c_M
Output
Print the answer. Your output will be judged as correct if the relative or absolute error from the true answer is at most 10^{-9}.
Sample Input 1
5 7 1 2 3 6 1 3 9 5 2 3 1 5 2 4 5 3 2 5 1 9 3 4 4 8 4 5 2 7
Sample Output 1
0.7500000000000000
For the path P that passes through the 2-nd, 6-th, and 7-th edges in this order and visits vertices 1 \rightarrow 3 \rightarrow 4 \rightarrow 5, the total beauty of all edges on P divided by the total cost of all edges on P is (b_2 + b_6 + b_7) / (c_2 + c_6 + c_7) = (9 + 4 + 2) / (5 + 8 + 7) = 15 / 20 = 0.75, and this is the maximum possible value.
Sample Input 2
3 3 1 3 1 1 1 3 2 1 1 3 3 1
Sample Output 2
3.0000000000000000
Sample Input 3
10 20 3 4 1 2 7 9 4 5 2 4 4 5 4 5 1 4 6 9 4 1 9 10 3 2 6 10 5 5 5 6 1 2 5 6 5 2 2 3 2 3 6 10 4 4 4 6 3 4 4 8 4 1 3 5 3 2 2 4 3 2 3 5 4 2 1 5 3 4 1 2 4 2 3 7 2 2 7 8 1 3
Sample Output 3
1.8333333333333333