Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
v と w のみからなる文字列 S が与えられます。
S の中に、下に尖っている部分が何箇所あるかを出力してください(入出力例にある図もご参照ください)。
制約
- S は
vとwのみからなる文字列 - S の長さは 1 以上 100 以下
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを整数として出力せよ。
入力例 1
vvwvw
出力例 1
7

上の画像のように、vvwvw という文字列には下に尖った部分が 7 箇所あります。
入力例 2
v
出力例 2
1
入力例 3
wwwvvvvvv
出力例 3
12
Score : 100 points
Problem Statement
You are given a string S consisting of v and w.
Print the number of "bottoms" in the string S (see the figure at Sample Input/Output).
Constraints
- S is a string consisting of
vandw. - 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 answer as an integer.
Sample Input 1
vvwvw
Sample Output 1
7

The image above shows the seven "bottoms" in the string vvwvw.
Sample Input 2
v
Sample Output 2
1
Sample Input 3
wwwvvvvvv
Sample Output 3
12
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英小文字 a, b, \ldots, z の ASCII 文字コードはこの順に 97,98,\ldots,122 です。
97 以上 122 以下の整数 N が与えられるので、ASCII 文字コードが N であるような英小文字を出力してください。
制約
- N は 97 以上 122 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
97
出力例 1
a
ASCII 文字コードが 97 である英小文字は a です。
入力例 2
122
出力例 2
z
Score : 100 points
Problem Statement
The ASCII values of the lowercase English letters a, b, \ldots, z are 97,98,\ldots,122 in this order.
Given an integer N between 97 and 122, print the letter whose ASCII value is N.
Constraints
- N is an integer between 97 and 122 (inclusive).
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
97
Sample Output 1
a
97 is the ASCII value of a.
Sample Input 2
122
Sample Output 2
z
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
1,\ldots,N の番号のついた N 人の選手がゲームを行いました。選手 i のスコアは A_i であり、スコアが小さい方が上位になります。
ブービー賞に該当する選手、すなわち、下位から 2 番目の選手の番号を求めてください。
制約
- 2 \leq N \leq 2\times 10^5
- 1 \leq A_i \leq 10^9
- A_i は相異なる
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N A_1 \ldots A_N
出力
答えを出力せよ。
入力例 1
6 1 123 12345 12 1234 123456
出力例 1
3
6 人中 5 位になるのは、選手 3 です。
入力例 2
5 3 1 4 15 9
出力例 2
5
Score : 200 points
Problem Statement
N players, who are numbered 1, \ldots, N, have played a game. Player i has scored A_i, and a player with a smaller score ranks higher.
The player who ranks the second lowest will receive a booby prize. Who is this player? Answer with an integer representing the player.
Constraints
- 2 \leq N \leq 2\times 10^5
- 1 \leq A_i \leq 10^9
- A_i are distinct.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A_1 \ldots A_N
Output
Print the answer.
Sample Input 1
6 1 123 12345 12 1234 123456
Sample Output 1
3
It is Player 3 who ranks fifth among the six players.
Sample Input 2
5 3 1 4 15 9
Sample Output 2
5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
英大文字と英小文字からなる文字列のうち、以下の条件を全て満たすものを素晴らしい文字列ということとします。
- 英大文字が文字列の中に現れる。
- 英小文字が文字列の中に現れる。
- 全ての文字が相異なる。
例えば、AtCoder や Aa は素晴らしい文字列ですが、atcoder や Perfect は素晴らしい文字列ではありません。
文字列 S が与えられるので、S が素晴らしい文字列か判定してください。
制約
- 1 \le |S| \le 100
- S は英大文字と英小文字からなる文字列である。
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S が素晴らしい文字列ならば Yes を、そうでないならば No を出力せよ。
入力例 1
AtCoder
出力例 1
Yes
AtCoder は、英大文字が含まれ、英小文字も含まれ、かつ全ての文字が相異なるため素晴らしい文字列です。
入力例 2
Aa
出力例 2
Yes
A と a は違う文字であることに注意してください。この文字列は素晴らしい文字列です。
入力例 3
atcoder
出力例 3
No
英大文字が含まれていないため、素晴らしい文字列ではありません。
入力例 4
Perfect
出力例 4
No
2 文字目と 5 文字目が等しいため、素晴らしい文字列ではありません。
Score : 200 points
Problem Statement
Let us call a string consisting of uppercase and lowercase English alphabets a wonderful string if all of the following conditions are satisfied:
- The string contains an uppercase English alphabet.
- The string contains a lowercase English alphabet.
- All characters in the string are pairwise distinct.
For example, AtCoder and Aa are wonderful strings, while atcoder and Perfect are not.
Given a string S, determine if S is a wonderful string.
Constraints
- 1 \le |S| \le 100
- S is a string consisting of uppercase and lowercase English alphabets.
Input
Input is given from Standard Input in the following format:
S
Output
If S is a wonderful string, print Yes; otherwise, print No.
Sample Input 1
AtCoder
Sample Output 1
Yes
AtCoder is a wonderful string because it contains an uppercase English alphabet, a lowercase English alphabet, and all characters in the string are pairwise distinct.
Sample Input 2
Aa
Sample Output 2
Yes
Note that A and a are different characters. This string is a wonderful string.
Sample Input 3
atcoder
Sample Output 3
No
It is not a wonderful string because it does not contain an uppercase English alphabet.
Sample Input 4
Perfect
Sample Output 4
No
It is not a wonderful string because the 2-nd and the 5-th characters are the same.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
高橋君は医者のすぬけ君から N 種類の薬を処方されました。i 種類目の薬は(処方された日を含めて) a_i 日間、毎日 b_i 錠ずつ飲む必要があります。また、高橋君はこれ以外の薬を飲む必要がありません。
薬を処方された日を 1 日目とします。1 日目以降で、初めて高橋君がその日に飲む必要がある薬が K 錠以下になるのは何日目かを求めてください。
制約
- 1 \leq N \leq 3 \times 10^5
- 0 \leq K \leq 10^9
- 1 \leq a_i,b_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K a_1 b_1 \vdots a_N b_N
出力
1 日目以降で、初めて高橋君がその日に飲む必要がある薬が K 錠以下になるのが X 日目の時、 X を出力せよ。
入力例 1
4 8 6 3 2 5 1 9 4 2
出力例 1
3
1 日目には、高橋君は 1,2,3,4 種類目の薬をそれぞれ 3,5,9,2 錠飲む必要があります。よってこの日は 19 錠飲む必要があり、K(=8) 錠以下ではありません。
2 日目には、高橋君は 1,2,4 種類目の薬をそれぞれ 3,5,2 錠飲む必要があります。よってこの日は 10 錠飲む必要があり、K(=8) 錠以下ではありません。
3 日目には、高橋君は 1,4 種類目の薬をそれぞれ 3,2 錠飲む必要があります。よってこの日は 5 錠飲む必要があり、初めて K(=8) 錠以下になります。
以上より、3 が答えです。
入力例 2
4 100 6 3 2 5 1 9 4 2
出力例 2
1
入力例 3
15 158260522 877914575 2436426 24979445 61648772 623690081 33933447 476190629 62703497 211047202 71407775 628894325 31963982 822804784 50968417 430302156 82631932 161735902 80895728 923078537 7723857 189330739 10286918 802329211 4539679 303238506 17063340 492686568 73361868 125660016 50287940
出力例 3
492686569
Score : 350 points
Problem Statement
Snuke the doctor prescribed N kinds of medicine for Takahashi. For the next a_i days (including the day of the prescription), he has to take b_i pills of the i-th medicine. He does not have to take any other medicine.
Let the day of the prescription be day 1. On or after day 1, when is the first day on which he has to take K pills or less?
Constraints
- 1 \leq N \leq 3 \times 10^5
- 0 \leq K \leq 10^9
- 1 \leq a_i,b_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K a_1 b_1 \vdots a_N b_N
Output
If Takahashi has to take K pills or less on day X for the first time on or after day 1, print X.
Sample Input 1
4 8 6 3 2 5 1 9 4 2
Sample Output 1
3
On day 1, he has to take 3,5,9, and 2 pills of the 1-st, 2-nd, 3-rd, and 4-th medicine, respectively. In total, he has to take 19 pills on this day, which is not K(=8) pills or less.
On day 2, he has to take 3,5, and 2 pills of the 1-st, 2-nd, and 4-th medicine, respectively. In total, he has to take 10 pills on this day, which is not K(=8) pills or less.
On day 3, he has to take 3 and 2 pills of the 1-st and 4-th medicine, respectively. In total, he has to take 5 pills on this day, which is K(=8) pills or less for the first time.
Thus, the answer is 3.
Sample Input 2
4 100 6 3 2 5 1 9 4 2
Sample Output 2
1
Sample Input 3
15 158260522 877914575 2436426 24979445 61648772 623690081 33933447 476190629 62703497 211047202 71407775 628894325 31963982 822804784 50968417 430302156 82631932 161735902 80895728 923078537 7723857 189330739 10286918 802329211 4539679 303238506 17063340 492686568 73361868 125660016 50287940
Sample Output 3
492686569
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
ポエムオンラインジャッジ (Poem Online Judge, 以下 POJ と表記) は提出された文字列に得点をつけるオンラインジャッジです。
POJ に N 回の提出がありました。早い方から i 番目の提出では文字列 S_i が提出されて、得点は T_i でした。(同じ文字列が複数回提出される場合もあります)
ただし、POJ では 同じ文字列を提出しても得点が等しいとは限らない のに注意してください。
N 回の提出のうち、その提出よりも早い提出であって文字列が一致するものが存在しないような提出を オリジナル であると呼びます。
また、オリジナルな提出の中で最も得点が高いものを 最優秀賞 と呼びます。ただし、そのような提出が複数ある場合は、最も提出が早いものを最優秀賞とします。
最優秀賞は早い方から何番目の提出ですか?
制約
- 1 \leq N \leq 10^5
- S_i は英小文字からなる文字列
- S_i の長さは 1 以上 10 以下
- 0 \leq T_i \leq 10^9
- N, T_i は整数
入力
入力は以下の形式で標準入力から与えられる。
N S_1 T_1 S_2 T_2 \vdots S_N T_N
出力
答えを出力せよ。
入力例 1
3 aaa 10 bbb 20 aaa 30
出力例 1
2
以下では早い方から i 番目の提出を提出 i と呼びます。
オリジナルな提出は提出 1 と 提出 2 です。提出 3 は提出 1 と文字列が一致しているためオリジナルではありません。
オリジナルな提出のうち最も得点が高い提出は提出 2 です。よってこれが最優秀賞になります。
入力例 2
5 aaa 9 bbb 10 ccc 10 ddd 10 bbb 11
出力例 2
2
オリジナルな提出は提出 1・提出 2・提出 3・提出 4 です。
その中で最も得点が高い提出は提出 2・提出 3・提出 4 です。この場合はこの中でもっとも提出の早い提出 2 を最優秀賞とします。
このように、オリジナルな提出の中で最も得点が高い提出が複数ある場合は、さらにその中で最も提出が早いものを最優秀賞とするのに注意してください。
入力例 3
10 bb 3 ba 1 aa 4 bb 1 ba 5 aa 9 aa 2 ab 6 bb 5 ab 3
出力例 3
8
Score : 300 points
Problem Statement
Poem Online Judge (POJ) is an online judge that gives scores to submitted strings.
There were N submissions to POJ. In the i-th earliest submission, string S_i was submitted, and a score of T_i was given. (The same string may have been submitted multiple times.)
Note that POJ may not necessarily give the same score to submissions with the same string.
A submission is said to be an original submission if the string in the submission is never submitted in any earlier submission.
A submission is said to be the best submission if it is an original submission with the highest score. If there are multiple such submissions, only the earliest one is considered the best submission.
Find the index of the best submission.
Constraints
- 1 \leq N \leq 10^5
- S_i is a string consisting of lowercase English characters.
- S_i has a length between 1 and 10, inclusive.
- 0 \leq T_i \leq 10^9
- N and T_i are integers.
Input
Input is given from Standard Input in the following format:
N S_1 T_1 S_2 T_2 \vdots S_N T_N
Output
Print the answer.
Sample Input 1
3 aaa 10 bbb 20 aaa 30
Sample Output 1
2
We will refer to the i-th earliest submission as Submission i.
Original submissions are Submissions 1 and 2. Submission 3 is not original because it has the same string as that in Submission 1.
Among the original submissions, Submission 2 has the highest score. Therefore, this is the best submission.
Sample Input 2
5 aaa 9 bbb 10 ccc 10 ddd 10 bbb 11
Sample Output 2
2
Original submissions are Submissions 1, 2, 3, and 4.
Among them, Submissions 2, 3, and 4 have the highest scores. In this case, the earliest submission among them, Submission 2, is the best.
As in this sample, beware that if multiple original submissions have the highest scores, only the one with the earliest among them is considered the best submission.
Sample Input 3
10 bb 3 ba 1 aa 4 bb 1 ba 5 aa 9 aa 2 ab 6 bb 5 ab 3
Sample Output 3
8
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
配点 : 475 点
問題文
N 頂点 0 辺の無向グラフがあります。頂点には 1 から N の番号がつけられています。
Q 個のクエリが与えられるので、与えられた順に処理してください。各クエリは以下の 2 種類のいずれかです。
- タイプ 1 :
1 u vの形式で与えられる。頂点 u と頂点 v の間に辺を追加する。 - タイプ 2 :
2 v kの形式で与えられる。頂点 v と連結な頂点の中で、k 番目に頂点番号が大きいものを出力する。ただし、頂点 v と連結な頂点が k 個未満のときは-1を出力する。
制約
- 1\leq N,Q\leq 2\times 10^5
- タイプ 1 のクエリにおいて、1\leq u\lt v\leq N
- タイプ 2 のクエリにおいて、1\leq v\leq N, 1\leq k\leq 10
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
ただし、\mathrm{query}_i は i 個目のクエリであり、以下のいずれかの形式で与えられる。
1 u v
2 v k
出力
タイプ 2 のクエリの個数を q として、q 行出力せよ。i 行目には i 個目のタイプ 2 に対するクエリの答えを出力せよ。
入力例 1
4 10 1 1 2 2 1 1 2 1 2 2 1 3 1 1 3 1 2 3 1 3 4 2 1 1 2 1 3 2 1 5
出力例 1
2 1 -1 4 2 -1
- 1 個目のクエリについて、頂点 1 と頂点 2 の間に辺が追加されます。
- 2 個目のクエリについて、頂点 1 と連結な頂点は 1,2 の 2 つです。この中で 1 番目に大きい 2 を出力します。
- 3 個目のクエリについて、頂点 1 と連結な頂点は 1,2 の 2 つです。この中で 2 番目に大きい 1 を出力します。
- 4 個目のクエリについて、頂点 1 と連結な頂点は 1,2 の 2 つです。3 個未満なので
-1を出力します。 - 5 個目のクエリについて、頂点 1 と頂点 3 の間に辺が追加されます。
- 6 個目のクエリについて、頂点 2 と頂点 3 の間に辺が追加されます。
- 7 個目のクエリについて、頂点 3 と頂点 4 の間に辺が追加されます。
- 8 個目のクエリについて、頂点 1 と連結な頂点は 1,2,3,4 の 4 つです。この中で 1 番目に大きい 4 を出力します。
- 9 個目のクエリについて、頂点 1 と連結な頂点は 1,2,3,4 の 4 つです。この中で 3 番目に大きい 2 を出力します。
- 10 個目のクエリについて、頂点 1 と連結な頂点は 1,2,3,4 の 4 つです。5 個未満なので
-1を出力します。
入力例 2
6 20 1 3 4 1 3 5 2 1 1 2 3 1 1 1 5 2 6 9 2 1 3 2 6 1 1 4 6 2 2 1 2 6 2 2 4 7 1 1 4 2 6 2 2 3 4 1 2 5 2 4 1 1 1 6 2 3 3 2 1 3
出力例 2
1 5 -1 3 6 2 5 -1 5 3 6 4 4
Score : 475 points
Problem Statement
There is an undirected graph with N vertices and 0 edges. The vertices are numbered 1 to N.
You are given Q queries to process in order. Each query is of one of the following two types:
- Type 1: Given in the format
1 u v. Add an edge between vertices u and v. - Type 2: Given in the format
2 v k. Print the k-th largest vertex number among the vertices connected to vertex v. If there are fewer than k vertices connected to v, print-1.
Constraints
- 1 \leq N, Q \leq 2 \times 10^5
- In a Type 1 query, 1 \leq u < v \leq N.
- In a Type 2 query, 1 \leq v \leq N, 1 \leq k \leq 10.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Here, \mathrm{query}_i is the i-th query and is given in one of the following formats:
1 u v
2 v k
Output
Let q be the number of Type 2 queries. Print q lines. The i-th line should contain the answer to the i-th Type 2 query.
Sample Input 1
4 10 1 1 2 2 1 1 2 1 2 2 1 3 1 1 3 1 2 3 1 3 4 2 1 1 2 1 3 2 1 5
Sample Output 1
2 1 -1 4 2 -1
- In the first query, an edge is added between vertices 1 and 2.
- In the second query, two vertices are connected to vertex 1: 1 and 2. Among them, the 1-st largest vertex number is 2, which should be printed.
- In the third query, two vertices are connected to vertex 1: 1 and 2. Among them, the 2-nd largest vertex number is 1, which should be printed.
- In the fourth query, two vertices are connected to vertex 1: 1 and 2, which is fewer than 3, so print
-1. - In the fifth query, an edge is added between vertices 1 and 3.
- In the sixth query, an edge is added between vertices 2 and 3.
- In the seventh query, an edge is added between vertices 3 and 4.
- In the eighth query, four vertices are connected to vertex 1: 1,2,3,4. Among them, the 1-st largest vertex number is 4, which should be printed.
- In the ninth query, four vertices are connected to vertex 1: 1,2,3,4. Among them, the 3-rd largest vertex number is 2, which should be printed.
- In the tenth query, four vertices are connected to vertex 1: 1,2,3,4, which is fewer than 5, so print
-1.
Sample Input 2
6 20 1 3 4 1 3 5 2 1 1 2 3 1 1 1 5 2 6 9 2 1 3 2 6 1 1 4 6 2 2 1 2 6 2 2 4 7 1 1 4 2 6 2 2 3 4 1 2 5 2 4 1 1 1 6 2 3 3 2 1 3
Sample Output 2
1 5 -1 3 6 2 5 -1 5 3 6 4 4
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
すぬけくんは、AtCoder Land の新たな目玉アトラクションとして迷路を建設しようと考えています。 迷路は縦 N 行・横 M 列のグリッドとして表され、右上のマスの上端が入口、右下のマスの下端が出口です。 すぬけくんは、隣接するマスの間に適切に壁を配置することで迷路を作ります。
すぬけくんは簡単な迷路が大好きなので、入口から出口までの道順は枝分かれを一切持たずにちょうど K マスを通るようなものにしたいです。 そのような迷路を作ることが可能か判定し、可能ならば 1 つ構築してください。
例えば以下の図では、N=3,M=3 であり、実線で書かれているところに壁が配置されています(入口と出口を除く外周部には必ず壁が配置されるものとします)。 このとき、入口から出口までの道順は枝分かれを一切持たずにちょうど 7 マスを通っています。

厳密には以下の通りです。
縦 N 行・横 M 列のグリッドがあります。 上から i 行目、左から j 列目のマスを (i,j) と表記します。 あなたは、辺で隣接する任意の 2 マスの間それぞれについて壁を置くか置かないか決めることができます。 壁を置く場所をうまく定めることで以下の条件を満たすことができるか判定し、できるならば実際に 1 つ構築してください。
NM 頂点からなる無向グラフ G を考える。G の各頂点は 2 つの整数の組 (i,j)\ (1\leq i\leq N, 1\leq j\leq M) によって互いに相異なるラベルが付けられている。 相異なる 2 頂点 (i_1,j_1),(i_2,j_2) は、|i_1-i_2|+|j_1-j_2|=1 かつグリッド上の 2 マス (i_1,j_1),(i_2,j_2) の間に壁が置かれていない場合、またその場合に限り辺で結ばれている。
条件:K 頂点からなり 2 頂点 (1,M),(N,M) を結ぶような単純パスが存在し、また 2 頂点 (1,M),(N,M) を含む連結成分はこのパスのみからなる。
制約
- 2\leq N \leq 100
- 1\leq M \leq 100
- 1\leq K\leq NM
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M K
出力
条件を満たす壁の配置が存在しないならば No を出力し、存在するならばそのうちの 1 つを以下の形式で出力せよ。
条件を満たす壁の配置が複数存在する場合は、そのどれを出力してもよい。
複雑な出力形式のため、下記の出力例も参考にしてください。
Yes +++++ \dots +++S+ +o?o? \dots ?o?o+ +?+?+ \dots +?+?+ +o?o? \dots ?o?o+ +?+?+ \dots +?+?+ \vdots +o?o? \dots ?o?o+ +?+?+ \dots +?+?+ +o?o? \dots ?o?o+ +++++ \dots +++G+
ここで、S, G, +, o はそれぞれ入口、出口、壁、マスを表し、マスとマスの間にある ? は壁を置くことができる場所である。
横に隣接する 2 マスの間にある ? は、壁を置くならば | で、壁を置かないならば . で置き換えよ。
縦に隣接する 2 マスの間にある ? は、壁を置くならば - で、壁を置かないならば . で置き換えよ。
厳密には以下の通りである。
- 出力は 2N+2 行からなり、1 行目には文字列
Yesを、2 行目から 2N+2 行目には以下で説明されるように長さ 2M+1 の文字列を出力する。- 2 行目には、2M-1 個の
+,S,+をこの順に連結して出力する。 - 1+2i 行目 (1\leq i\leq N) には、
+,o, c_{i,1},o, c_{i,2}, \dots, c_{i,M-1},o,+をこの順に連結して出力する。 ここで、c_{i,j} はマス (i,j),(i,j+1) の間に壁を置くならば|、置かないならば.である。 - 2+2i 行目 (1\leq i\leq N-1) には、
+, r_{i,1},+, r_{i,2},+, \dots,+, r_{i,M},+をこの順に連結して出力する。 ここで、r_{i,j} はマス (i,j),(i+1,j) の間に壁を置くならば-、置かないならば.である。 - 2N+2 行目には、2M-1 個の
+,G,+をこの順に連結して出力する。
- 2 行目には、2M-1 個の
入力例 1
3 3 7
出力例 1
Yes +++++S+ +o.o.o+ +.+-+-+ +o.o.o+ +-+-+.+ +o.o|o+ +++++G+
問題文中の図と同じ壁の配置です。
入力例 2
3 3 2
出力例 2
No
入力例 3
4 1 4
出力例 3
Yes +S+ +o+ +.+ +o+ +.+ +o+ +.+ +o+ +G+
Score : 525 points
Problem Statement
Snuke is planning to build a maze as a new attraction in AtCoder Land. The maze is represented as a grid with N rows and M columns, with the top edge of the top-right cell being the entrance and the bottom edge of the bottom-right cell being the exit. He will create the maze by appropriately placing walls between adjacent cells.
He loves simple mazes, so he wants the path from the entrance to the exit to pass through exactly K cells without any branches. Determine if it is possible to create such a maze, and if possible, construct one.
For example, in the following figure, N=3 and M=3, and walls are placed at the solid lines (walls are always placed around the outer perimeter except for the entrance and exit). In this case, the path from the entrance to the exit passes through exactly 7 cells without any branches.

Below is a formal statement.
There is a grid with N rows and M columns. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left. For each pair of side-adjacent cells, you can decide whether to place a wall between them. Determine whether it is possible to place walls to satisfy the following condition, and if it is possible, construct one such placement.
Consider an undirected graph G with NM vertices. Each vertex of G is uniquely labeled by a pair of integers (i,j)\ (1\leq i\leq N, 1\leq j\leq M). Two distinct vertices (i_1,j_1) and (i_2,j_2) are connected by an edge if and only if |i_1-i_2|+|j_1-j_2|=1 and there is no wall between the corresponding cells (i_1,j_1) and (i_2,j_2) on the grid.
Condition: there exists a simple path with K vertices that connects the two vertices (1,M) and (N,M), and the connected component containing the vertices (1,M) and (N,M) consists only of this path.
Constraints
- 2\leq N \leq 100
- 1\leq M \leq 100
- 1\leq K\leq NM
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M K
Output
If there is no placement of walls that satisfies the condition, print No. Otherwise, print one such placement in the following format. If multiple valid placements exist, any of them can be printed.
See also the sample outputs below to better understand the complicated output format.
Yes +++++ \dots +++S+ +o?o? \dots ?o?o+ +?+?+ \dots +?+?+ +o?o? \dots ?o?o+ +?+?+ \dots +?+?+ \vdots +o?o? \dots ?o?o+ +?+?+ \dots +?+?+ +o?o? \dots ?o?o+ +++++ \dots +++G+
Here, S, G, +, and o represent the entrance, exit, a wall, and a cell, respectively, and ? between cells represents a position where a wall can be placed. Replace ? between two horizontally adjacent cells with | if a wall is placed, and . otherwise. Replace ? between two vertically adjacent cells with - if a wall is placed, and . otherwise.
Below is a formal instruction.
- The output should consist of 2N+2 lines. Line 1 should contain the string
Yes, and lines 2 to 2N+2 should contain strings of length 2M+1 as described below.- Line 2 should be a concatenation of
+repeated 2M-1 times,S, and+, in this order. - Line 1+2i (1\leq i\leq N) should be a concatenation of
+,o, c_{i,1},o, c_{i,2}, \dots, c_{i,M-1},o,+, in this order. Here, c_{i,j} is|if a wall is placed between cells (i,j) and (i,j+1), and.otherwise. - Line 2+2i (1\leq i\leq N-1) should be a concatenation of
+, r_{i,1},+, r_{i,2},+, \dots,+, r_{i,M},+, in this order. Here, r_{i,j} is-if a wall is placed between cells (i,j) and (i+1,j), and.otherwise. - Line 2N+2 should be a concatenation of
+repeated 2M-1 times,G, and+, in this order.
- Line 2 should be a concatenation of
Sample Input 1
3 3 7
Sample Output 1
Yes +++++S+ +o.o.o+ +.+-+-+ +o.o.o+ +-+-+.+ +o.o|o+ +++++G+
This is the same placement of walls as in the figure in the problem statement.
Sample Input 2
3 3 2
Sample Output 2
No
Sample Input 3
4 1 4
Sample Output 3
Yes +S+ +o+ +.+ +o+ +.+ +o+ +.+ +o+ +G+