Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英大文字からなる長さ 3 の文字列 S が与えられます。S が ACE、BDF、CEG、DFA、EGB、FAC、GBD のいずれかと等しいとき Yes を、そうでないとき No を出力してください。
制約
- S は英大文字からなる長さ 3 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S が ACE、BDF、CEG、DFA、EGB、FAC、GBD のいずれかと等しいとき Yes を、そうでないとき No を出力せよ。
入力例 1
ABC
出力例 1
No
S = ABC のとき、S は ACE、BDF、CEG、DFA、EGB、FAC、GBD のいずれとも等しくないので No を出力します。
入力例 2
FAC
出力例 2
Yes
入力例 3
XYX
出力例 3
No
Score : 100 points
Problem Statement
Given a length-3 string S consisting of uppercase English letters, print Yes if S equals one of ACE, BDF, CEG, DFA, EGB, FAC, and GBD; print No otherwise.
Constraints
- S is a length-3 string consisting of uppercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Print Yes if S equals one of ACE, BDF, CEG, DFA, EGB, FAC, and GBD; print No otherwise.
Sample Input 1
ABC
Sample Output 1
No
When S = ABC, S does not equal any of ACE, BDF, CEG, DFA, EGB, FAC, and GBD, so No should be printed.
Sample Input 2
FAC
Sample Output 2
Yes
Sample Input 3
XYX
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英小文字からなる長さ N の文字列 S が与えられます。S の末尾の文字を出力してください。
制約
- N は整数
- 1 ≤ N ≤ 1000
- S は英小文字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
S の末尾の文字を出力せよ。
入力例 1
5 abcde
出力例 1
e
S = {}abcde です。S の末尾の文字は e なので、e を出力します。
入力例 2
1 a
出力例 2
a
Score : 100 points
Problem Statement
Given a string S of length N consisting of lowercase English alphabets, print the last character of S.
Constraints
- N is an integer.
- 1 ≤ N ≤ 1000
- S is a string of length N consisting of lowercase English alphabets.
Input
Input is given from Standard Input in the following format:
N S
Output
Print the last character of S.
Sample Input 1
5 abcde
Sample Output 1
e
The last character of S = {}abcde is e, so e should be printed.
Sample Input 2
1 a
Sample Output 2
a
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
N 人の人があるコンテストに参加し、i 位の人のハンドルネームは S_i でした。
上位 K 人のハンドルネームを辞書順に出力してください。
辞書順とは?
辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、相異なる文字列 S と文字列 T の大小を判定するアルゴリズムを以下に説明します。
以下では「 S の i 文字目の文字」を S_i のように表します。また、 S が T より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。
- S と T のうち長さが短い方の文字列の長さを L とします。i=1,2,\dots,L に対して S_i と T_i が一致するか調べます。
- S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_j と T_j を比較して、 S_j がアルファベット順で T_j より小さい場合は S \lt T 、大きい場合は S \gt T と決定して、アルゴリズムを終了します。
- S_i \neq T_i である i が存在しない場合、 S と T の長さを比較して、S が T より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。
制約
- 1 \leq K \leq N \leq 100
- K, N は整数
- S_i は英小文字からなる長さ 10 以下の文字列
- i \neq j ならば S_i \neq S_j
入力
入力は以下の形式で標準入力から与えられる。
N K S_1 S_2 \vdots S_N
出力
答えを改行区切りで出力せよ。
入力例 1
5 3 abc aaaaa xyz a def
出力例 1
aaaaa abc xyz
このコンテストには 5 人が参加し、1 位の人のハンドルネームは abc 、2 位の人のハンドルネームは aaaaa 、3 位の人のハンドルネームは xyz 、4 位の人のハンドルネームは a 、5 位の人のハンドルネームは def でした。
上位 3 人のハンドルネームは abc、aaaaa、xyz であるため、これを辞書順に並べ替えて aaaaa 、abc 、xyz の順に出力します。
入力例 2
4 4 z zyx zzz rbg
出力例 2
rbg z zyx zzz
入力例 3
3 1 abc arc agc
出力例 3
abc
Score : 200 points
Problem Statement
There were N participants in a contest. The participant ranked i-th had the nickname S_i.
Print the nicknames of the top K participants in lexicographical order.
What is lexicographical order?
Simply put, the lexicographical order is the order of words in a dictionary. As a formal description, below is an algorithm to order distinct strings S and T.
Let S_i denote the i-th character of a string S. We write S \lt T if S is lexicographically smaller than T, and S \gt T if S is larger.
- Let L be the length of the shorter of S and T. For i=1,2,\dots,L, check whether S_i equals T_i.
- If there is an i such that S_i \neq T_i, let j be the smallest such i. Compare S_j and T_j. If S_j is alphabetically smaller than T_j, we get S \lt T; if S_j is larger, we get S \gt T.
- If there is no i such that S_i \neq T_i, compare the lengths of S and T. If S is shorter than T, we get S \lt T; if S is longer, we get S \gt T.
Constraints
- 1 \leq K \leq N \leq 100
- K and N are integers.
- S_i is a string of length 10 consisting of lowercase English letters.
- S_i \neq S_j if i \neq j.
Input
The input is given from Standard Input in the following format:
N K S_1 S_2 \vdots S_N
Output
Print the nicknames, separated by newlines.
Sample Input 1
5 3 abc aaaaa xyz a def
Sample Output 1
aaaaa abc xyz
This contest had five participants. The participants ranked first, second, third, fourth, and fifth had the nicknames abc, aaaaa, xyz, a, and def, respectively.
The nicknames of the top three participants were abc, aaaaa, xyz, so print these in lexicographical order: aaaaa, abc, xyz.
Sample Input 2
4 4 z zyx zzz rbg
Sample Output 2
rbg z zyx zzz
Sample Input 3
3 1 abc arc agc
Sample Output 3
abc
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
長さ N の正整数列 a = (a_1, a_2, \dots, a_N) には何種類の整数が現れますか?
制約
- 1 \leq N \leq 1000
- 1 \leq a_i \leq 10^9 \, (1 \leq i \leq N)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N a_1 \ldots a_N
出力
答えを出力せよ。
入力例 1
6 1 4 1 2 2 1
出力例 1
3
1, 2, 4 の 3 種類の整数が現れます。
入力例 2
1 1
出力例 2
1
入力例 3
11 3 1 4 1 5 9 2 6 5 3 5
出力例 3
7
Score : 200 points
Problem Statement
In a sequence of N positive integers a = (a_1, a_2, \dots, a_N), how many different integers are there?
Constraints
- 1 \leq N \leq 1000
- 1 \leq a_i \leq 10^9 \, (1 \leq i \leq N)
- 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 4 1 2 2 1
Sample Output 1
3
There are three different integers: 1, 2, 4.
Sample Input 2
1 1
Sample Output 2
1
Sample Input 3
11 3 1 4 1 5 9 2 6 5 3 5
Sample Output 3
7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
ボールがいくつか刺さった棒が N 本あり、各ボールには英小文字が 1 個書かれています。
i = 1, 2, \ldots, N について、i 番目の棒に刺さった各ボールの英小文字は、文字列 S_i によって表されます。 具体的には、i 番目の棒には文字列 S_i の長さ |S_i| に等しい個数のボールが刺さっており、 刺さっているボールの英小文字を、棒のある端から順に並べたものは文字列 S_i と等しいです。
2 つの棒は、一方の棒に刺さっているボールの英小文字をどちらかの端から並べた列と、もう一方の棒に刺さっているボールの英小文字をどちらかの端から並べた列が一致するとき、同じ棒とみなされます。 より形式的には、1 以上 N 以下の整数 i, j について、i 本目の棒と j 本目の棒は、S_i が S_j と一致するか、S_i が S_j を前後反転したものと一致するとき、かつそのときに限り、同じとみなされます。
N 本の棒の中に、何種類の異なる棒があるかを出力してください。
制約
- N は整数
- 2 \leq N \leq 2 \times 10^5
- S_i は英小文字のみからなる文字列
- |S_i| \geq 1
- \sum_{i = 1}^N |S_i| \leq 2 \times 10^5
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
6 a abc de cba de abc
出力例 1
3
- S_2 =
abcが S_4 =cbaを前後反転したものと一致するため、2 番目の棒と 4 番目の棒は同じとみなされます。 - S_2 =
abcが S_6 =abcと一致するため、2 番目の棒と 6 番目の棒は同じとみなされます。 - S_3 =
deが S_5 =deと一致するため、3 番目の棒と 5 番目の棒は同じとみなされます。
よって、6 本の棒の中に、1 本目の棒、2 本目の棒( 4, 6 本目の棒と同じ)、3 本目の棒( 5 本目の棒と同じ)の 3 種類の異なる棒があります。
Score : 300 points
Problem Statement
There are N sticks with several balls stuck onto them. Each ball has a lowercase English letter written on it.
For each i = 1, 2, \ldots, N, the letters written on the balls stuck onto the i-th stick are represented by a string S_i. Specifically, the number of balls stuck onto the i-th stick is the length |S_i| of the string S_i, and S_i is the sequence of letters on the balls starting from one end of the stick.
Two sticks are considered the same when the sequence of letters on the balls starting from one end of one stick is equal to the sequence of letters starting from one end of the other stick. More formally, for integers i and j between 1 and N, inclusive, the i-th and j-th sticks are considered the same if and only if S_i equals S_j or its reversal.
Print the number of different sticks among the N sticks.
Constraints
- N is an integer.
- 2 \leq N \leq 2 \times 10^5
- S_i is a string consisting of lowercase English letters.
- |S_i| \geq 1
- \sum_{i = 1}^N |S_i| \leq 2 \times 10^5
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print the answer.
Sample Input 1
6 a abc de cba de abc
Sample Output 1
3
- S_2 =
abcequals the reversal of S_4 =cba, so the second and fourth sticks are considered the same. - S_2 =
abcequals S_6 =abc, so the second and sixth sticks are considered the same. - S_3 =
deequals S_5 =de, so the third and fifth sticks are considered the same.
Therefore, there are three different sticks among the six: the first, second (same as the fourth and sixth), and third (same as the fifth).
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
1 から N までの番号が付けられた N 個のおもちゃと、1 から N-1 までの番号が付けられた N-1 個の箱があります。 おもちゃ i\ (1\leq i\leq N) の大きさは A_i、箱 i\ (1\leq i\leq N-1) の大きさは B_i です。
すべてのおもちゃを別々の箱にしまいたいと考えている高橋君は、今から以下の操作を順に行うことにしました。
- 任意の正整数 x を選んで、大きさ x の箱を 1 つ購入する。
- N 個のおもちゃそれぞれを、(元々あった箱と新しく購入した箱を合わせた)N 個の箱のいずれかに入れる。 ただし、各おもちゃはそのおもちゃの大きさ以上の大きさを持つ箱にしか入れることはできず、また 1 つの箱に 2 つ以上のおもちゃを入れることもできない。
高橋君は、操作 1 で十分な大きさの箱を購入することで操作 2 が実行できるようにしたいですが、大きな箱ほど値段が高いため、可能な限り小さな箱を購入したいです。
高橋君が操作 2 を実行できるような x の値が存在するか判定し、存在するならばその最小値を求めてください。
制約
- 2\leq N \leq 2\times 10^5
- 1\leq A_i,B_i \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_{N-1}
出力
高橋君が操作 2 を実行できるような x の値が存在するならばその最小値を、存在しないならば -1 を出力せよ。
入力例 1
4 5 2 3 7 6 2 8
出力例 1
3
x=3 とした場合(すなわち、操作 1 で大きさ 3 の箱を購入した場合)を考えます。
新しく購入した箱を箱 4 と呼ぶことにすると、おもちゃ 1,\dots,4 の大きさはそれぞれ 5,2,3,7、箱 1,\dots,4 の大きさはそれぞれ 6,2,8,3 となります。 よって、おもちゃ 1 を箱 1 に、おもちゃ 2 を箱 2 に、おもちゃ 3 を箱 4 に、おもちゃ 4 を箱 3 にそれぞれ入れることができます。
逆に、x\leq 2 のときは N 個のおもちゃすべてを別々の箱に入れることができません。 よって答えは 3 です。
入力例 2
4 3 7 2 5 8 1 6
出力例 2
-1
操作 1 でどのような大きさの箱を購入したとしても、箱 2 に入れられる大きさのおもちゃが存在しないため、操作 2 を実行することができません。
入力例 3
8 2 28 17 39 57 56 37 32 34 27 73 28 76 61 27
出力例 3
37
Score : 350 points
Problem Statement
There are N toys numbered from 1 to N, and N-1 boxes numbered from 1 to N-1. Toy i\ (1 \leq i \leq N) has a size of A_i, and box i\ (1 \leq i \leq N-1) has a size of B_i.
Takahashi wants to store all the toys in separate boxes, and he has decided to perform the following steps in order:
- Choose an arbitrary positive integer x and purchase one box of size x.
- Place each of the N toys into one of the N boxes (the N-1 existing boxes plus the newly purchased box). Here, each toy can only be placed in a box whose size is not less than the toy's size, and no box can contain two or more toys.
He wants to execute step 2 by purchasing a sufficiently large box in step 1, but larger boxes are more expensive, so he wants to purchase the smallest possible box.
Determine whether there exists a value of x such that he can execute step 2, and if it exists, find the minimum such x.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 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
A_1 A_2 \dots A_N
B_1 B_2 \dots B_{N-1}
Output
If there exists a value of x such that Takahashi can execute step 2, print the minimum such x. Otherwise, print -1.
Sample Input 1
4 5 2 3 7 6 2 8
Sample Output 1
3
Consider the case where x=3 (that is, he purchases a box of size 3 in step 1).
If the newly purchased box is called box 4, toys 1,\dots,4 have sizes of 5, 2, 3, and 7, respectively, and boxes 1,\dots,4 have sizes of 6, 2, 8, and 3, respectively. Thus, toy 1 can be placed in box 1, toy 2 in box 2, toy 3 in box 4, and toy 4 in box 3.
On the other hand, if x \leq 2, it is impossible to place all N toys into separate boxes. Therefore, the answer is 3.
Sample Input 2
4 3 7 2 5 8 1 6
Sample Output 2
-1
No matter what size of box is purchased in step 1, no toy can be placed in box 2, so it is impossible to execute step 2.
Sample Input 3
8 2 28 17 39 57 56 37 32 34 27 73 28 76 61 27
Sample Output 3
37
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
正整数 N に対して、N を N 個つなげた整数を V_N とします。
より厳密には、N を文字列とみなしたものを N 個連結し、
それを再度整数とみなしたものを V_N とします。
例えば、V_3=333, V_{10}=10101010101010101010 です。
V_N を 998244353 で割った余りを求めてください。
制約
- 1\leq N\leq 10^{18}
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
V_N を 998244353 で割った余りを出力せよ。
入力例 1
5
出力例 1
55555
V_5=55555 を 998244353 で割った余りは 55555 です。
入力例 2
9
出力例 2
1755646
V_9=999999999 を 998244353 で割った余りは 1755646 です。
入力例 3
10000000000
出力例 3
468086693
入力が 32 bit 整数型に収まらない可能性があることに注意してください。
Score : 350 points
Problem Statement
For a positive integer N, let V_N be the integer formed by concatenating N exactly N times.
More precisely, consider N as a string, concatenate N copies of it, and treat the result as an integer to get V_N.
For example, V_3=333 and V_{10}=10101010101010101010.
Find the remainder when V_N is divided by 998244353.
Constraints
- 1 \leq N \leq 10^{18}
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print the remainder when V_N is divided by 998244353.
Sample Input 1
5
Sample Output 1
55555
The remainder when V_5=55555 is divided by 998244353 is 55555.
Sample Input 2
9
Sample Output 2
1755646
The remainder when V_9=999999999 is divided by 998244353 is 1755646.
Sample Input 3
10000000000
Sample Output 3
468086693
Note that the input may not fit into a 32-bit integer type.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
正整数 N および、英小文字からなる長さ N の文字列 S,T が与えられます。
以下の操作を繰り返し(0 回でも良い)行うことで、S を T と一致させることが可能かどうか判定してください。 可能な場合は、そのために必要な操作回数の最小値も求めてください。
- 英小文字 x,y を選び、S に含まれる 全て の x をそれぞれ y で置き換える。
制約
- 1\leq N \leq 2\times 10^5
- N は整数
- S,T はそれぞれ英小文字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S T
出力
S を T と一致させることが可能ならばそのために必要な操作回数の最小値を、不可能ならば -1 を出力せよ。
入力例 1
6 afbfda bkckbb
出力例 1
4
以下のように操作を 4 回行うことで、S を T と一致させることができます。
- x=
b, y=cとして操作を行う。S=afcfdaとなる。 - x=
a, y=bとして操作を行う。S=bfcfdbとなる。 - x=
f, y=kとして操作を行う。S=bkckdbとなる。 - x=
d, y=bとして操作を行う。S=bkckbbとなり、T と一致する。
3 回以下の操作で S を T と一致させることはできないため、必要な操作回数の最小値は 4 です。
入力例 2
4 abac abac
出力例 2
0
S と T が既に一致しているため、操作を行う必要はありません。
入力例 3
4 abac abrc
出力例 3
-1
どのように操作を繰り返し行っても、S を T と一致させることはできません。
入力例 4
4 abac bcba
出力例 4
4
Score : 500 points
Problem Statement
You are given a positive integer N and two strings S and T, each of length N and consisting of lowercase English letters.
Determine whether it is possible to make S identical to T by repeating the operation below any number of times (possibly zero). If it is possible, also find the minimum number of operations required.
- Choose two lowercase English letters x, y and replace every occurrence of x in S with y.
Constraints
- 1\leq N \leq 2\times 10^5
- N is an integer.
- Each of S and T is a string of length N, consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N S T
Output
If it is possible to make S identical to T, print the minimum number of operations required. Otherwise, print -1.
Sample Input 1
6 afbfda bkckbb
Sample Output 1
4
By performing the operation four times in the following way, you can make S identical to T:
- Choose x=
band y=c. S becomesafcfda. - Choose x=
aand y=b. S becomesbfcfdb. - Choose x=
fand y=k. S becomesbkckdb. - Choose x=
dand y=b. S becomesbkckbb, which is identical to T.
It cannot be done with fewer than four operations, so the minimum number of operations required is 4.
Sample Input 2
4 abac abac
Sample Output 2
0
S and T are already identical, so no operations are required.
Sample Input 3
4 abac abrc
Sample Output 3
-1
No matter how you repeat the operation, it is impossible to make S identical to T.
Sample Input 4
4 abac bcba
Sample Output 4
4
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
非負整数を要素とする H 行 W 列の行列 A が与えられます。 1 \leq i \leq H かつ 1 \leq j \leq W を満たす整数の組 (i, j) について、 A の i 行目 j 列目の要素を A_{i, j} で表します。
A に対して以下の手順を行います。
- まず、A の要素のうち 0 であるものそれぞれを、任意の正の整数で置き換える( 0 である要素が複数ある場合、それぞれを異なる正の整数で置き換えることもできます)。
-
その後、「下記の 2 つの操作のどちらかを行うこと」を好きな回数( 0 回でも良い)だけ行う。
- 1 \leq i \lt j \leq H を満たす整数の組 (i, j) を選び、A の i 行目と j 行目を入れ替える。
- 1 \leq i \lt j \leq W を満たす整数の組 (i, j) を選び、A の i 列目と j 列目を入れ替える。
A が次の条件を満たすようにすることができるかどうかを判定してください。
-
A_{1, 1} \leq A_{1, 2} \leq \cdots \leq A_{1, W} \leq A_{2, 1} \leq A_{2, 2} \leq \cdots \leq A_{2, W} \leq A_{3, 1} \leq \cdots \leq A_{H, 1} \leq A_{H, 2} \leq \cdots \leq A_{H, W}
-
言い換えると、1 \leq i, i' \leq H および 1 \leq j, j' \leq W を満たす任意の 2 つの整数の組 (i, j) と (i', j') について、下記の 2 つの条件がともに成り立つ。
- i \lt i' ならば A_{i, j} \leq A_{i', j'}
- 「 i = i' かつ j \lt j' 」ならば A_{i, j} \leq A_{i', j'}
制約
- 2 \leq H, W
- H \times W \leq 10^6
- 0 \leq A_{i, j} \leq H \times W
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W
A_{1, 1} A_{1, 2} \ldots A_{1, W}
A_{2, 1} A_{2, 2} \ldots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \ldots A_{H, W}
出力
A が問題文中の条件を満たすようにできる場合は Yes を、できない場合は No を出力せよ。
入力例 1
3 3 9 6 0 0 4 0 3 0 3
出力例 1
Yes
以下の手順で操作を行うことで、A が問題文中の条件を満たすようにすることができるため、Yes を出力します。
- まず、A の 0 である要素を下記の通りに置き換える。
9 6 8 5 4 4 3 1 3
- 2 列目と 3 列目を入れ替える。その結果、A は下記の通りとなる。
9 8 6 5 4 4 3 3 1
- 1 行目と 3 行目を入れ替える。その結果、A は下記の通りとなる。
3 3 1 5 4 4 9 8 6
- 1 列目と 3 列目を入れ替える。その結果、A は下記の通りとなり、問題文中の条件を満たすようになる。
1 3 3 4 4 5 6 8 9
入力例 2
2 2 2 1 1 2
出力例 2
No
どのように操作を行っても A が問題文中の条件を満たすようにすることはできないため、No を出力します。
Score : 500 points
Problem Statement
You are given a matrix A whose elements are non-negative integers. For a pair of integers (i, j) such that 1 \leq i \leq H and 1 \leq j \leq W, let A_{i, j} denote the element at the i-th row and j-th column of A.
Let us perform the following procedure on A.
- First, replace each element of A that is 0 with an arbitrary positive integer (if multiple elements are 0, they may be replaced with different positive integers).
-
Then, repeat performing one of the two operations below, which may be chosen each time, as many times as desired (possibly zero).
- Choose a pair of integers (i, j) such that 1 \leq i \lt j \leq H and swap the i-th and j-th rows of A.
- Choose a pair of integers (i, j) such that 1 \leq i \lt j \leq W and swap the i-th and j-th columns of A.
Determine whether A can be made to satisfy the following condition.
- A_{1, 1} \leq A_{1, 2} \leq \cdots \leq A_{1, W} \leq A_{2, 1} \leq A_{2, 2} \leq \cdots \leq A_{2, W} \leq A_{3, 1} \leq \cdots \leq A_{H, 1} \leq A_{H, 2} \leq \cdots \leq A_{H, W}.
-
In other words, for every two pairs of integers (i, j) and (i', j') such that 1 \leq i, i' \leq H and 1 \leq j, j' \leq W, both of the following conditions are satisfied.
- If i \lt i', then A_{i, j} \leq A_{i', j'}.
- If i = i' and j \lt j', then A_{i, j} \leq A_{i', j'}.
Constraints
- 2 \leq H, W
- H \times W \leq 10^6
- 0 \leq A_{i, j} \leq H \times W
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
H W
A_{1, 1} A_{1, 2} \ldots A_{1, W}
A_{2, 1} A_{2, 2} \ldots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \ldots A_{H, W}
Output
If A can be made to satisfy the condition in the problem statement, print Yes; otherwise, print No.
Sample Input 1
3 3 9 6 0 0 4 0 3 0 3
Sample Output 1
Yes
One can perform the operations as follows to make A satisfy the condition in the problem statement, so you should print Yes.
- First, replace the elements of A that are 0, as shown below:
9 6 8 5 4 4 3 1 3
- Swap the second and third columns. Then, A becomes:
9 8 6 5 4 4 3 3 1
- Swap the first and third rows. Then, A becomes:
3 3 1 5 4 4 9 8 6
- Swap the first and third columns. Then, A becomes the following and satisfies the condition in the problem statement.
1 3 3 4 4 5 6 8 9
Sample Input 2
2 2 2 1 1 2
Sample Output 2
No
There is no way to perform the operations to make A satisfy the condition in the problem statement, so you should print No.