実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
2 次元グリッド上に 2 つの図形 S と T があります。グリッドは正方形のマスからなります。
S は N 行 N 列のグリッド内にあり、S_{i,j} が # であるようなマス全体からなります。
T も N 行 N 列のグリッド内にあり、T_{i,j} が # であるようなマス全体からなります。
S と T を 90 度回転及び平行移動の繰り返しによって一致させることができるか判定してください。
制約
- 1 \leq N \leq 200
- S,T は
#と.のみからなる - S,T は 1 つ以上
#を含む
入力
入力は以下の形式で標準入力から与えられる。
N
S_{1,1}S_{1,2}\ldots S_{1,N}
\vdots
S_{N,1}S_{N,2}\ldots S_{N,N}
T_{1,1}T_{1,2}\ldots T_{1,N}
\vdots
T_{N,1}T_{N,2}\ldots T_{N,N}
出力
S と T を90 度回転及び平行移動の繰り返しによって一致させることができるとき Yes を、そうでないとき No を出力せよ。
入力例 1
5 ..... ..#.. .###. ..... ..... ..... ..... ....# ...## ....#
出力例 1
Yes
S を左回りに 90 度回転させ、平行移動することで T に一致させることができます。
入力例 2
5 ##### ##..# #..## ##### ..... ##### #..## ##..# ##### .....
出力例 2
No
90 度回転と平行移動の繰り返しによって一致させることはできません。
入力例 3
4 #... ..#. ..#. .... #... #... ..#. ....
出力例 3
Yes
S 及び T は連結とは限りません。
入力例 4
4 #... .##. ..#. .... ##.. #... ..#. ....
出力例 4
No
回転や移動の操作は連結成分ごとにできるわけではなく、S,T 全体に対して行うことに注意してください。
Score : 300 points
Problem Statement
We have two figures S and T on a two-dimensional grid with square cells.
S lies within a grid with N rows and N columns, and consists of the cells where S_{i,j} is #.
T lies within the same grid with N rows and N columns, and consists of the cells where T_{i,j} is #.
Determine whether it is possible to exactly match S and T by 90-degree rotations and translations.
Constraints
- 1 \leq N \leq 200
- Each of S and T consists of
#and.. - Each of S and T contains at least one
#.
Input
Input is given from Standard Input in the following format:
N
S_{1,1}S_{1,2}\ldots S_{1,N}
\vdots
S_{N,1}S_{N,2}\ldots S_{N,N}
T_{1,1}T_{1,2}\ldots T_{1,N}
\vdots
T_{N,1}T_{N,2}\ldots T_{N,N}
Output
Print Yes if it is possible to exactly match S and T by 90-degree rotations and translations, and No otherwise.
Sample Input 1
5 ..... ..#.. .###. ..... ..... ..... ..... ....# ...## ....#
Sample Output 1
Yes
We can match S to T by rotating it 90-degrees counter-clockwise and translating it.
Sample Input 2
5 ##### ##..# #..## ##### ..... ##### #..## ##..# ##### .....
Sample Output 2
No
It is impossible to match them by 90-degree rotations and translations.
Sample Input 3
4 #... ..#. ..#. .... #... #... ..#. ....
Sample Output 3
Yes
Each of S and T may not be connected.
Sample Input 4
4 #... .##. ..#. .... ##.. #... ..#. ....
Sample Output 4
No
Note that it is not allowed to rotate or translate just a part of a figure; it is only allowed to rotate or translate a whole figure.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
10^{100} 行 7 列の行列 A があり、任意の整数対 (i,j)\ (1 \leq i \leq 10^{100}, 1 \leq j \leq 7) についてその (i,j) 成分は (i-1) \times 7 + j です。
N 行 M 列の行列 B が与えられるので、B が A から一部の矩形領域を(向きを変えずに)切り出したものであるかを判定してください。
制約
- 1 \leq N \leq 10^4
- 1 \leq M \leq 7
- 1 \leq B_{i,j} \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M
B_{1,1} B_{1,2} \ldots B_{1,M}
B_{2,1} B_{2,2} \ldots B_{2,M}
\hspace{1.6cm}\vdots
B_{N,1} B_{N,2} \ldots B_{N,M}
出力
B が A から一部の矩形領域を切り出したものであれば Yes と、そうでないなら No と出力せよ。
入力例 1
2 3 1 2 3 8 9 10
出力例 1
Yes
与えられる B は、A の左上 2 行 3 列を切り出したものとなっています。
入力例 2
2 1 1 2
出力例 2
No
与えられる B を 90 度回転させると A の左上 1 行 2 列と一致しますが、問題文中に「向きを変えずに」とある通り回転による一致は認められていないため、答えは No となります。
入力例 3
10 4 1346 1347 1348 1349 1353 1354 1355 1356 1360 1361 1362 1363 1367 1368 1369 1370 1374 1375 1376 1377 1381 1382 1383 1384 1388 1389 1390 1391 1395 1396 1397 1398 1402 1403 1404 1405 1409 1410 1411 1412
出力例 3
Yes
Score : 300 points
Problem Statement
There is a 10^{100} \times 7 matrix A, where the (i,j)-th entry is (i-1) \times 7 + j for every pair of integers (i,j)\ (1 \leq i \leq 10^{100}, 1 \leq j \leq 7).
Given an N \times M matrix B, determine whether B is some (unrotated) rectangular part of A.
Constraints
- 1 \leq N \leq 10^4
- 1 \leq M \leq 7
- 1 \leq B_{i,j} \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
B_{1,1} B_{1,2} \ldots B_{1,M}
B_{2,1} B_{2,2} \ldots B_{2,M}
\hspace{1.6cm}\vdots
B_{N,1} B_{N,2} \ldots B_{N,M}
Output
If B is some rectangular part of A, print Yes; otherwise, print No.
Sample Input 1
2 3 1 2 3 8 9 10
Sample Output 1
Yes
The given matrix B is the top-left 2 \times 3 submatrix of A.
Sample Input 2
2 1 1 2
Sample Output 2
No
Although the given matrix B would match the top-left 1 \times 2 submatrix of A after rotating 90 degrees, the Problem Statement asks whether B is an unrotated part of A, so the answer is No.
Sample Input 3
10 4 1346 1347 1348 1349 1353 1354 1355 1356 1360 1361 1362 1363 1367 1368 1369 1370 1374 1375 1376 1377 1381 1382 1383 1384 1388 1389 1390 1391 1395 1396 1397 1398 1402 1403 1404 1405 1409 1410 1411 1412
Sample Output 3
Yes
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
高橋くんは睡眠記録をつけています。 睡眠記録は奇数長の数列 A=(A _ 1(=0), A _ 2,\ldots,A _ N) で表され、奇数番目は起床時刻を、偶数番目は就寝時刻を表しています。 より厳密には、睡眠記録をつけている間に高橋くんは次のような睡眠をとりました。
- すべての 1\leq i\leq\dfrac{N-1}2 を満たす整数 i について、睡眠記録をつけ始めてから A _ {2i} 分後ちょうどに寝て、A _ {2i+1} 分後ちょうどに起きた。
- それ以外の時間に寝ることも起きることもなかった。
次の Q 個の質問に答えてください。 i 番目の質問では、0\leq l _ i\leq r _ i\leq A _ N を満たす整数の組 (l _ i,r _ i) が与えられます。
- 睡眠記録をつけ始めてから l _ i 分後ちょうどから r _ i 分後ちょうどまでの r _ i-l _ i 分のうち、高橋くんが寝ていたのは何分間ですか?
制約
- 3\leq N\lt2\times10^5
- N は奇数
- 0=A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq10^9
- 1\leq Q\leq2\times10^5
- 0\leq l _ i\leq r _ i\leq A _ N\ (1\leq i\leq Q)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A _ 1 A _ 2 \ldots A _ N Q l _ 1 r _ 1 l _ 2 r _ 2 \vdots l _ Q r _ Q
出力
答えを Q 行で出力せよ。 i 行目には i 番目の質問の答えを整数として出力せよ。
入力例 1
7 0 240 720 1320 1440 1800 2160 3 480 1920 720 1200 0 2160
出力例 1
480 0 960
高橋くんは、以下の図のように睡眠をとりました。

それぞれの質問の答えは以下のようになります。
- 睡眠記録をつけ始めてから 480 分後から 1920 分後の間、高橋くんは 480 分後から 720 分後、1320 分後から 1440 分後、1800 分後から 1920 分後の 3 つの睡眠をとりました。睡眠時間の合計は 240+120+120=480 分です。
- 睡眠記録をつけ始めてから 720 分後から 1200 分後の間、高橋くんは睡眠をとりませんでした。睡眠時間の合計は 0 分です。
- 睡眠記録をつけ始めてから 0 分後から 2160 分後の間、高橋くんは 240 分後から 720 分後、1320 分後から 1440 分後、1800 分後から 2160 分後の 3 つの睡眠をとりました。睡眠時間の合計は 480+120+360=960 分です。
よって、それぞれの行に 480,0,960 と出力してください。
入力例 2
21 0 20 62 192 284 310 323 324 352 374 409 452 486 512 523 594 677 814 838 946 1000 10 77 721 255 541 478 970 369 466 343 541 42 165 16 618 222 592 730 983 338 747
出力例 2
296 150 150 49 89 20 279 183 61 177
Score : 450 points
Problem Statement
Takahashi keeps a sleep log. The log is represented as an odd-length sequence A=(A _ 1(=0), A _ 2,\ldots,A _ N), where odd-numbered elements represent times he got up, and even-numbered elements represent times he went to bed. More formally, he had the following sleep sessions after starting the sleep log.
- For every integer i such that 1\leq i\leq\dfrac{N-1}2, he fell asleep exactly A _ {2i} minutes after starting the sleep log and woke up exactly A _ {2i+1} minutes after starting the sleep log.
- He did not fall asleep or wake up at any other time.
Answer the following Q questions. For the i-th question, you are given a pair of integers (l _ i,r _ i) such that 0\leq l _ i\leq r _ i\leq A _ N.
- What is the total number of minutes for which Takahashi was asleep during the r _ i-l _ i minutes from exactly l _ i minutes to r _ i minutes after starting the sleep log?
Constraints
- 3\leq N\lt2\times10^5
- N is odd.
- 0=A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq10^9
- 1\leq Q\leq2\times10^5
- 0\leq l _ i\leq r _ i\leq A _ N\ (1\leq i\leq Q)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A _ 1 A _ 2 \ldots A _ N Q l _ 1 r _ 1 l _ 2 r _ 2 \vdots l _ Q r _ Q
Output
Print the answer in Q lines. The i-th line should contain an integer answering to the i-th question.
Sample Input 1
7 0 240 720 1320 1440 1800 2160 3 480 1920 720 1200 0 2160
Sample Output 1
480 0 960
Takahashi slept as shown in the following figure.

The answers to each question are as follows.
- Between 480 minutes and 1920 minutes after starting the sleep log, Takahashi slept from 480 minutes to 720 minutes, from 1320 minutes to 1440 minutes, and from 1800 minutes to 1920 minutes in 3 sleep sessions. The total sleep time is 240+120+120=480 minutes.
- Between 720 minutes and 1200 minutes after starting the sleep log, Takahashi did not sleep. The total sleep time is 0 minutes.
- Between 0 minutes and 2160 minutes after starting the sleep log, Takahashi slept from 240 minutes to 720 minutes, from 1320 minutes to 1440 minutes, and from 1800 minutes to 2160 minutes in 3 sleep sessions. The total sleep time is 480+120+360=960 minutes.
Therefore, the three lines of the output should contain 480, 0, and 960.
Sample Input 2
21 0 20 62 192 284 310 323 324 352 374 409 452 486 512 523 594 677 814 838 946 1000 10 77 721 255 541 478 970 369 466 343 541 42 165 16 618 222 592 730 983 338 747
Sample Output 2
296 150 150 49 89 20 279 183 61 177
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジプログラムが入出力を介して対話を行う形式の問題)です。
縦横 N マスのチェス盤と N 個のルークの駒があります。以下では上から i 行目、左から j 列目のマスを (i, j) と表します。
チェス盤のマスにルークを置くことを考えます。ただし、あなたは次の条件をすべて満たすようにルークをチェス盤に置く必要があります。
- 1 つの行に 2 個以上のルークが存在しない。
- 1 つの列に 2 個以上のルークが存在しない。
今、チェス盤に N-1 個のルークが上の条件をすべて満たした状態で置かれています。あなたはルークが置かれていないマスを 1 つ選び、そのマスにルークを置くことにしました。(上の条件をすべて満たすようにルークを置くことができるマスは少なくとも 1 つ以上存在することが証明できます。)
ただし、あなたはチェス盤のどのマスにルークが置かれているかを直接知ることはできません。
そのかわりに、ジャッジシステムに対して以下の質問を 20 回まで行うことができます。
- 整数 A, B, C, D を 1 \leq A \leq B \leq N, 1 \leq C \leq D \leq N を満たすように選ぶ。そして、A \leq i \leq B, C \leq j \leq D を満たすマス (i, j) からなる長方形領域に置かれているルークの個数を聞く。
ルークを置くことができるマスを見つけてください。
制約
- 2 \leq N \leq 10^3
- N は整数
入出力
この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジプログラムが入出力を介して対話を行う形式の問題)です。
最初に、チェス盤のサイズ N を標準入力から受け取ってください。
N
次に、ルークを置くことができるマスが見つかるまで質問を繰り返してください。
質問は、以下の形式で標準出力に出力してください。
? A B C D
これに対する応答は、次の形式で標準入力から与えられます。
T
ここで、T は質問に対する答えです。ただし、不正な質問を行った、あるいは質問の回数が 20 回を超えた場合は T は -1 となります。
ジャッジが -1 を返した場合、提出はすでに不正解とみなされています。この場合、ただちにプログラムを終了してください。
ルークを置くことができるマスを見つけたら、そのマスを (X, Y) として、解答を以下の形式で出力してください。その後、ただちにプログラムを終了してください。
! X Y
答えが複数ある場合、どれを出力しても正解とみなされます。
注意点
- 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
- 対話の途中で不正な出力を行った場合のジャッジ結果は不定です。
- 解答を出力したらただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。
入出力例
以下は、N=3 で (1, 2), (2, 1) にルークが置かれている場合の入出力例です。
| 入力 | 出力 | 説明 |
|---|---|---|
3 |
まず整数 N が与えられます。 | |
? 1 2 1 3 |
(A,B,C,D)=(1,2,1,3) として質問を行います。 | |
2 |
質問の答えは 2 なので、ジャッジはその値を返します。 | |
? 2 3 1 1 |
(A,B,C,D)=(2,3,1,1) として質問を行います。 | |
1 |
質問の答えは 1 なので、ジャッジはその値を返します。 | |
? 1 3 3 3 |
(A,B,C,D)=(1,3,3,3) として質問を行います。 | |
0 |
質問の答えは 0 なので、ジャッジはその値を返します。 | |
! 3 3 |
答えは (3, 3) だとわかったので、それを出力します。 |
Score : 500 points
Problem Statement
This is an interactive task (where your program interacts with the judge's program via input and output).
We have an N-by-N chessboard and N rooks. Below, the square at the i-th row from the top and j-th column from the left is denoted by (i, j).
Consider placing the rooks on squares of the chessboard. Here, you have to place the rooks so that all of the following conditions are satisfied.
- No row contains two or more rooks.
- No column contains two or more rooks.
Now, N-1 rooks are placed on the chessboard so that all of the above conditions are satisfied. You will choose a square that is not occupied by a rook and place a rook on that square. (It can be proved that there is at least one square on which a rook can be placed under the conditions.)
However, you cannot directly see which squares of the chessboard are occupied by a rook.
Instead, you may ask at most 20 questions to the judge in the following manner.
- You choose integers A, B, C, and D such that 1 \leq A \leq B \leq N, 1 \leq C \leq D \leq N, and ask the number of rooks in the rectangular region formed by the squares (i, j) such that A \leq i \leq B, C \leq j \leq D.
Find a square to place a rook.
Constraints
- 2 \leq N \leq 10^3
- N is an integer.
Input and Output
This is an interactive task (where your program interacts with the judge's program via input and output).
First, receive the size of the chessboard, N, from Standard Input.
N
Next, repeat asking a question until you find a square to place a rook.
A question should be printed to Standard Output in the following format:
? A B C D
The response will be given from Standard Input in the following format:
T
Here, T is the response to the question, or -1 if the question is invalid or more than 20 questions have been asked.
When the judge returns -1, the submission is already regarded as incorrect. In this case, terminate the program immediately.
When you find a square to place a rook, let (X, Y) be that square and print an answer in the following format. Then, terminate the program immediately.
! X Y
If there are multiple appropriate answers, any of them will be accepted.
Notes
- Each time you print something, end it with a newline and then flush Standard Output. Otherwise, you may get a TLE verdict.
- If an invalid output is printed during the interaction, the verdict will be indeterminate.
- Terminate the program immediately after printing an answer. Otherwise, the verdict will be indeterminate.
Sample Interaction
Below is an interaction where N=3 and the rooks are placed on (1, 2) and (2, 1).
| Input | Output | Description |
|---|---|---|
3 |
Judge first gives the integer N. | |
? 1 2 1 3 |
Participant asks a question with (A,B,C,D)=(1,2,1,3). | |
2 |
Judge returns the answer to the question, which is 2. | |
? 2 3 1 1 |
Participant asks a question with (A,B,C,D)=(2,3,1,1). | |
1 |
Judge returns the answer to the question, which is 1. | |
? 1 3 3 3 |
Participant asks a question with (A,B,C,D)=(1,3,3,3). | |
0 |
Judge returns the answer to the question, which is 0. | |
! 3 3 |
Participant finds the answer to be (3, 3) and prints it. |
実行時間制限: 3 sec / メモリ制限: 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.