Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
整数 N が与えられます。
N が -2^{31} 以上かつ 2^{31} 未満ならば Yes を、そうでないならば No を出力してください。
制約
- -2^{63} \leq N < 2^{63}
- N は整数である。
入力
入力は以下の形式で標準入力から与えられる。
N
出力
N が -2^{31} 以上かつ 2^{31} 未満ならば Yes を、そうでないならば No を出力せよ。
入力例 1
10
出力例 1
Yes
10 は -2^{31} 以上かつ 2^{31} 未満であるので、Yes を出力します。
入力例 2
-9876543210
出力例 2
No
-9876543210 は -2^{31} 未満であるので、No を出力します。
入力例 3
483597848400000
出力例 3
No
483597848400000 は 2^{31} 以上であるので、No を出力します。
Score : 100 points
Problem Statement
You are given an integer N.
If N is between -2^{31} and 2^{31}-1 (inclusive), print Yes; otherwise, print No.
Constraints
- -2^{63} \leq N < 2^{63}
- N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
If N is between -2^{31} and 2^{31}-1 (inclusive), print Yes; otherwise, print No.
Sample Input 1
10
Sample Output 1
Yes
10 is between -2^{31} and 2^{31}-1, so Yes should be printed.
Sample Input 2
-9876543210
Sample Output 2
No
-9876543210 is less than -2^{31}, so No should be printed.
Sample Input 3
483597848400000
Sample Output 3
No
483597848400000 is greater than 2^{31}-1, so No should be printed.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
明日からの 7 日間の天気予報を表す文字列 S が与えられます。
i 日後の予報は S の i 文字目が o であるとき晴れ、x であるとき雨です。
N 日後の天気予報が晴れかどうかを教えてください。
制約
- N は 1 以上 7 以下の整数
- S は長さ 7 の文字列であり、
oとxのみからなる
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
N 日後の天気予報が晴れであるとき Yes を、雨であるとき No を出力せよ。
入力例 1
4 oooxoox
出力例 1
No
明日からの 7 日間の天気予報は順に、晴れ、晴れ、晴れ、雨、晴れ、晴れ、雨です。
特に、今日から 4 日後の天気予報は雨です。
入力例 2
7 ooooooo
出力例 2
Yes
Score : 100 points
Problem Statement
You are given a string S, which represents a weather forecast for the seven days starting tomorrow.
The i-th of those seven days is forecast to be sunny if the i-th character of S is o, and rainy if that character is x.
Tell us whether the N-th day is forecast to be sunny.
Constraints
- N is an integer between 1 and 7 (inclusive).
- S is a string of length 7 consisting of
oandx.
Input
Input is given from Standard Input in the following format:
N S
Output
Print Yes if the N-th of the seven days starting tomorrow is forecast to be sunny, and No if that day is forecast to be rainy.
Sample Input 1
4 oooxoox
Sample Output 1
No
The forecast for each of the seven days starting tomorrow is as follows: sunny, sunny, sunny, rainy, sunny, sunny, rainy.
In particular, the fourth day is forecast to be rainy.
Sample Input 2
7 ooooooo
Sample Output 2
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
各要素が 0 あるいは 1 である N 行 N 列の行列 A, B が与えられます。
A の i 行目 j 列目の要素を A_{i,j}、B の i 行目 j 列目の要素を B_{i,j} で表します。
A を適切に回転することで、 A_{i,j} = 1 であるすべての整数の組 (i, j) について B_{i,j} = 1 が成り立っているようにできるか判定してください。
ただし、A を回転するとは、以下の操作を好きな回数(0 回でもよい)繰り返すことをいいます。
- 1 \leq i, j \leq N を満たすすべての整数の組 (i, j) について同時に A_{i,j} を A_{N + 1 - j,i} で置き換える
制約
- 1 \leq N \leq 100
- A, B の各要素は 0 か 1 のいずれか
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_{1,1} A_{1,2} \ldots A_{1,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
B_{1,1} B_{1,2} \ldots B_{1,N}
\vdots
B_{N,1} B_{N,2} \ldots B_{N,N}
出力
A を適切に回転することで、A_{i,j} = 1 であるすべての整数の組 (i, j) について B_{i,j} = 1 が成り立っているようにできる場合 Yes を、そうでない場合 No を出力せよ。
入力例 1
3 0 1 1 1 0 0 0 1 0 1 1 0 0 0 1 1 1 1
出力例 1
Yes
はじめ、A は
0 1 1 1 0 0 0 1 0
です。
1 回操作を行うと、A は
0 1 0 1 0 1 0 0 1
となります。
もう 1 度操作を行うと、A は
0 1 0 0 0 1 1 1 0
となります。
このとき、A_{i,j} = 1 であるすべての整数の組 (i, j) について B_{i,j} = 1 が成り立っているので、Yes を出力します。
入力例 2
2 0 0 0 0 1 1 1 1
出力例 2
Yes
入力例 3
5 0 0 1 1 0 1 0 0 1 0 0 0 1 0 1 0 1 0 1 0 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 0 0 1 1 1 1 0 1 0 1 1 1 0 1 0
出力例 3
No
Score : 200 points
Problem Statement
You are given N-by-N matrices A and B where each element is 0 or 1.
Let A_{i,j} and B_{i,j} denote the element at the i-th row and j-th column of A and B, respectively.
Determine whether it is possible to rotate A so that B_{i,j} = 1 for every pair of integers (i, j) such that A_{i,j} = 1.
Here, to rotate A is to perform the following operation zero or more times:
- for every pair of integers (i, j) such that 1 \leq i, j \leq N, simultaneously replace A_{i,j} with A_{N + 1 - j,i}.
Constraints
- 1 \leq N \leq 100
- Each element of A and B is 0 or 1.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N
A_{1,1} A_{1,2} \ldots A_{1,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
B_{1,1} B_{1,2} \ldots B_{1,N}
\vdots
B_{N,1} B_{N,2} \ldots B_{N,N}
Output
If it is possible to rotate A so that B_{i,j} = 1 for every pair of integers (i, j) such that A_{i,j} = 1, print Yes; otherwise, print No.
Sample Input 1
3 0 1 1 1 0 0 0 1 0 1 1 0 0 0 1 1 1 1
Sample Output 1
Yes
Initially, A is :
0 1 1 1 0 0 0 1 0
After performing the operation once, A is :
0 1 0 1 0 1 0 0 1
After performing the operation once again, A is :
0 1 0 0 0 1 1 1 0
Here, B_{i,j} = 1 for every pair of integers (i, j) such that A_{i,j} = 1, so you should print Yes.
Sample Input 2
2 0 0 0 0 1 1 1 1
Sample Output 2
Yes
Sample Input 3
5 0 0 1 1 0 1 0 0 1 0 0 0 1 0 1 0 1 0 1 0 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 0 0 1 1 1 1 0 1 0 1 1 1 0 1 0
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋くんは回転寿司で N 皿の料理を食べました。 i 皿目の色は文字列 C_i で表されます。
また、料理の価格は皿の色と対応し、 i=1,\ldots,M のそれぞれについて、色が文字列 D_i の皿の料理は一皿 P_i 円です。また、D_1,\ldots,D_M のいずれとも異なる色の皿の料理は一皿 P_0 円です。
高橋くんが食べた料理の価格の合計を求めてください。
制約
- 1\leq N,M\leq 100
- C_i,D_i は英小文字からなる長さ 1 以上 20 以下の文字列
- D_1,\ldots,D_M はすべて異なる
- 1\leq P_i\leq 10000
- N,M,P_i は整数
入力
入力は以下の形式で標準入力から与えられる。
N M C_1 \ldots C_N D_1 \ldots D_M P_0 P_1 \ldots P_M
出力
答えを整数として出力せよ。
入力例 1
3 2 red green blue blue red 800 1600 2800
出力例 1
5200
blue の皿は P_1 = 1600 円、red の皿は P_2 = 2800 円、green の皿は P_0 = 800 円です。
高橋くんが食べた料理の価格の合計は、2800+800+1600=5200 円です。
入力例 2
3 2 code queen atcoder king queen 10 1 1
出力例 2
21
Score : 200 points
Problem Statement
Takahashi ate N plates of sushi at a sushi restaurant. The color of the i-th plate is represented by a string C_i.
The price of a sushi corresponds to the color of the plate. For each i=1,\ldots,M, the sushi on a plate whose color is represented by a string D_i is worth P_i yen a plate (yen is the currency of Japan). If the color does not coincide with any of D_1,\ldots, and D_M, it is worth P_0 yen a plate.
Find the total amount of the prices of sushi that Takahashi ate.
Constraints
- 1\leq N,M\leq 100
- C_i and D_i are strings of length between 1 and 20, inclusive, consisting of lowercase English letters.
- D_1,\ldots, and D_M are distinct.
- 1\leq P_i\leq 10000
- N, M, and P_i are integers.
Input
The input is given from Standard Input in the following format:
N M C_1 \ldots C_N D_1 \ldots D_M P_0 P_1 \ldots P_M
Output
Print the answer as an integer.
Sample Input 1
3 2 red green blue blue red 800 1600 2800
Sample Output 1
5200
A blue plate, red plate, and green plate are worth P_1 = 1600, P_2 = 2800, and P_0 = 800 yen, respectively.
The total amount of the prices of the sushi that he ate is 2800+800+1600=5200 yen.
Sample Input 2
3 2 code queen atcoder king queen 10 1 1
Sample Output 2
21
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
二次元平面の点 (0,0) に高橋君がいます。初め、高橋君の体力は H です。また、二次元平面には M 個の体力を回復するアイテムがあり、i 個目のアイテムは点 (x_i,y_i) に置いてあります。
高橋君は、これから N 回の移動をします。i 回目の移動は以下の方法で行われます。
-
今高橋君がいる点を (x,y) とする。体力を 1 消費し、S の i 番目の文字 S_i に応じて以下の点に移動する。
- S_i が
Rのとき: (x+1,y) - S_i が
Lのとき: (x-1,y) - S_i が
Uのとき: (x,y+1) - S_i が
Dのとき: (x,y-1)
- S_i が
-
高橋君の体力が負になった場合、高橋君は倒れてしまい、移動をやめる。そうでない場合、移動した点にアイテムがあり、かつ高橋君の体力が K 未満ならば、移動した点に置かれたアイテムを消費し、高橋君の体力が K になる。
高橋君が一度も倒れることなく N 回の移動を行えるか判定してください。
制約
- 1\leq N,M,H,K\leq 2\times 10^5
- S は
R,L,U,Dからなる長さ N の文字列 - |x_i|,|y_i| \leq 2\times 10^5
- (x_i,y_i) は互いに異なる
- S 以外の入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M H K S x_1 y_1 \vdots x_M y_M
出力
高橋君が一度も倒れることなく N 回の移動を行える場合 Yes を、そうでない場合 No を出力せよ。
入力例 1
4 2 3 1 RUDL -1 -1 1 0
出力例 1
Yes
初め高橋君の体力は 3 です。以下で移動を説明します。
-
1 回目の移動: S_i が
Rなので点 (1,0) に移動する。高橋君の体力は 2 に減る。点 (1,0) にはアイテムが置いてあるが、高橋君の体力は K=1 以上なのでアイテムは消費されない。 -
2 回目の移動: S_i が
Uなので点 (1,1) に移動する。高橋君の体力は 1 に減る。 -
3 回目の移動: S_i が
Dなので点 (1,0) に移動する。高橋君の体力は 0 に減る。点 (1,0) にはアイテムが置いてあり、体力は K=1 未満なのでアイテムを消費し、体力が 1 になる。 -
4 回目の移動: S_i が
Lなので点 (0,0) に移動する。高橋君の体力は 0 に減る。
以上より、高橋君は倒れずに 4 回の移動を行えるので、Yes を出力してください。体力は 0 になってもいいことに注意してください。
入力例 2
5 2 1 5 LDRLD 0 0 -1 -1
出力例 2
No
初め高橋君の体力は 1 です。以下で移動を説明します。
-
1 回目の移動: S_i が
Lなので点 (-1,0) に移動する。高橋君の体力は 0 に減る。 -
2 回目の移動: S_i が
Dなので点 (-1,-1) に移動する。高橋君の体力は -1 に減る。体力が -1 になってしまったので、高橋君は倒れてしまい、移動をやめる。
以上より、高橋君は倒れてしまうので、No を出力してください。
高橋君がはじめいる点 (0,0) にはアイテムが置いてありますが、移動後にアイテムは消費されるので、1 回目の移動前にアイテムを消費しないことに注意してください。
Score : 300 points
Problem Statement
On a two-dimensional plane, Takahashi is initially at point (0, 0), and his initial health is H. M items to recover health are placed on the plane; the i-th of them is placed at (x_i,y_i).
Takahashi will make N moves. The i-th move is as follows.
-
Let (x,y) be his current coordinates. He consumes a health of 1 to move to the following point, depending on S_i, the i-th character of S:
- (x+1,y) if S_i is
R; - (x-1,y) if S_i is
L; - (x,y+1) if S_i is
U; - (x,y-1) if S_i is
D.
- (x+1,y) if S_i is
-
If Takahashi's health has become negative, he collapses and stops moving. Otherwise, if an item is placed at the point he has moved to, and his health is strictly less than K, then he consumes the item there to make his health K.
Determine if Takahashi can complete the N moves without being stunned.
Constraints
- 1\leq N,M,H,K\leq 2\times 10^5
- S is a string of length N consisting of
R,L,U, andD. - |x_i|,|y_i| \leq 2\times 10^5
- (x_i, y_i) are pairwise distinct.
- All values in the input are integers, except for S.
Input
The input is given from Standard Input in the following format:
N M H K S x_1 y_1 \vdots x_M y_M
Output
Print Yes if he can complete the N moves without being stunned; print No otherwise.
Sample Input 1
4 2 3 1 RUDL -1 -1 1 0
Sample Output 1
Yes
Initially, Takahashi's health is 3. We describe the moves below.
-
1-st move: S_i is
R, so he moves to point (1,0). His health reduces to 2. Although an item is placed at point (1,0), he do not consume it because his health is no less than K=1. -
2-nd move: S_i is
U, so he moves to point (1,1). His health reduces to 1. -
3-rd move: S_i is
D, so he moves to point (1,0). His health reduces to 0. An item is placed at point (1,0), and his health is less than K=1, so he consumes the item to make his health 1. -
4-th move: S_i is
L, so he moves to point (0,0). His health reduces to 0.
Thus, he can make the 4 moves without collapsing, so Yes should be printed. Note that the health may reach 0.
Sample Input 2
5 2 1 5 LDRLD 0 0 -1 -1
Sample Output 2
No
Initially, Takahashi's health is 1. We describe the moves below.
-
1-st move: S_i is
L, so he moves to point (-1,0). His health reduces to 0. -
2-nd move: S_i is
D, so he moves to point (-1,-1). His health reduces to -1. Now that the health is -1, he collapses and stops moving.
Thus, he will be stunned, so No should be printed.
Note that although there is an item at his initial point (0,0), he does not consume it before the 1-st move, because items are only consumed after a move.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
回転テーブルの周りに人 0、人 1、\ldots、人 N-1 がこの順番で反時計回りに等間隔で並んでいます。また、人 i の目の前には料理 p_i が置かれています。
あなたは次の操作を 0 回以上何度でも行うことが出来ます。
- 回転テーブルを反時計回りに 1 周の \frac{1}{N} だけ回す。これによって、(この操作の直前に)人 i の目の前にあった料理は人 (i+1) \bmod N の目の前に移動する。
操作を完了させた後において、人 i は料理 i が人 (i-1) \bmod N、人 i、人 (i+1) \bmod N のいずれかの目の前に置かれていると喜びます。
喜ぶ人数の最大値を求めてください。
a \bmod m とは
整数 a と正整数 m に対し、a \bmod m は a-x が m の倍数となるような 0 以上 m 未満の整数 x を表します。(このような x は一意に定まることが証明できます)制約
- 3 \leq N \leq 2 \times 10^5
- 0 \leq p_i \leq N-1
- i \neq j ならば p_i \neq p_j
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
p_0 \ldots p_{N-1}
出力
答えを出力せよ。
入力例 1
4 1 2 0 3
出力例 1
4
操作を 1 回行うと下の画像のようになります。

この時、4 人が喜ぶことを以下のように確かめられます。
- 人 0 は料理 0 が人 3\ (=(0-1) \bmod 4) の目の前に置かれているので喜びます。
- 人 1 は料理 1 が人 1\ (=1) の目の前に置かれているので喜びます。
- 人 2 は料理 2 が人 2\ (=2) の目の前に置かれているので喜びます。
- 人 3 は料理 3 が人 0\ (=(3+1) \bmod 4) の目の前に置かれているので喜びます。
5 人以上が喜ぶことは無いため、答えは 4 です。
入力例 2
3 0 1 2
出力例 2
3
入力例 3
10 3 9 6 1 7 2 8 0 5 4
出力例 3
5
Score : 300 points
Problem Statement
Person 0, Person 1, \ldots, and Person (N-1) are sitting around a turntable in their counterclockwise order, evenly spaced. Dish p_i is in front of Person i on the table.
You may perform the following operation 0 or more times:
- Rotate the turntable by one N-th of a counterclockwise turn. As a result, the dish that was in front of Person i right before the rotation is now in front of Person (i+1) \bmod N.
When you are finished, Person i is happy if Dish i is in front of Person (i-1) \bmod N, Person i, or Person (i+1) \bmod N.
Find the maximum possible number of happy people.
What is a \bmod m?
For an integer a and a positive integer m, a \bmod m denotes the integer x between 0 and (m-1) (inclusive) such that (a-x) is a multiple of m. (It can be proved that such x is unique.)Constraints
- 3 \leq N \leq 2 \times 10^5
- 0 \leq p_i \leq N-1
- p_i \neq p_j if i \neq j.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
p_0 \ldots p_{N-1}
Output
Print the answer.
Sample Input 1
4 1 2 0 3
Sample Output 1
4
The figure below shows the table after one operation.

Here, there are four happy people:
- Person 0 is happy because Dish 0 is in front of Person 3\ (=(0-1) \bmod 4);
- Person 1 is happy because Dish 1 is in front of Person 1\ (=1);
- Person 2 is happy because Dish 2 is in front of Person 2\ (=2);
- Person 3 is happy because Dish 3 is in front of Person 0\ (=(3+1) \bmod 4).
There cannot be five or more happy people, so the answer is 4.
Sample Input 2
3 0 1 2
Sample Output 2
3
Sample Input 3
10 3 9 6 1 7 2 8 0 5 4
Sample Output 3
5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
0 から N-1 までの番号がつけられた N 個のマスが並んでいます。 今から、すぬけくんが以下の手順に従って全てのマスに印をつけていきます。
- マス 0 に印をつける。
-
次の i - iii の手順を N−1 回繰り返す。
- 最後に印をつけたマスの番号を A としたとき、変数 x を (A+D) \bmod N で初期化する。
- マス x に印が付いている限り、 x を (x+1) \bmod N に更新することを繰り返す。
- マス x に印をつける。
すぬけくんが K 番目に印をつけるマスの番号を求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\leq T \leq 10^5
- 1\leq K\leq N \leq 10^9
- 1\leq D \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで、\mathrm{test}_i は i 番目のテストケースを意味する。
T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T
各テストケースは以下の形式で与えられる。
N D K
出力
T 行出力せよ。
i\ (1\leq i \leq T) 行目には、i 番目のテストケースに対する答えを出力せよ。
入力例 1
9 4 2 1 4 2 2 4 2 3 4 2 4 5 8 1 5 8 2 5 8 3 5 8 4 5 8 5
出力例 1
0 2 1 3 0 3 1 4 2
N=4,D=2 のとき、すぬけくんは以下のように印をつけていきます。
- マス 0 に印をつける。
- (1回目) x=(0+2)\bmod 4=2 と初期化する。マス 2 は印がついていないので、印をつける。
(2回目) x=(2+2)\bmod 4=0 と初期化する。マス 0 は印がついているので、x=(0+1)\bmod 4=1 と更新する。マス 1 は印がついていないので、印をつける。
(3回目) x=(1+2)\bmod 4=3 と初期化する。マス 3 は印がついていないので、印をつける。
Score : 400 points
Problem Statement
There are N squares indexed 0 through (N-1) arranged in a line. Snuke is going to mark every square by the following procedure.
- Mark square 0.
-
Repeat the following steps i - iii (N-1) times.
- Initialize a variable x with (A+D) \bmod N, where A is the index of the square marked last time.
- While square x is marked, repeat replacing x with (x+1) \bmod N.
- Mark square x.
Find the index of the square that Snuke marks for the K-th time.
Given T test cases, find the answer for each of them.
Constraints
- 1\leq T \leq 10^5
- 1\leq K\leq N \leq 10^9
- 1\leq D \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format, where \mathrm{test}_i denotes the i-th test case:
T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T
Each test case is given in the following format:
N D K
Output
Print T lines.
The i-th (1\leq i \leq T) line should contain the answer to the i-th test case.
Sample Input 1
9 4 2 1 4 2 2 4 2 3 4 2 4 5 8 1 5 8 2 5 8 3 5 8 4 5 8 5
Sample Output 1
0 2 1 3 0 3 1 4 2
If N=4 and D=2, Snuke marks the squares as follows.
- Mark square 0.
- (1-st iteration) Let x=(0+2)\bmod 4=2. Since square 2 is not marked, mark it.
(2-nd iteration) Let x=(2+2)\bmod 4=0. Since square 0 is marked, let x=(0+1)\bmod 4=1. Since square 1 is not marked, mark it.
(3-rd iteration) Let x=(1+2)\bmod 4=3. Since square 3 is not marked, mark it.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
縦 H 行、横 W 列のマス目があります。上から i 行目、左から j 列目のマスをマス (i, j) と呼びます。
それぞれのマスには整数が書かれています。 i = 1, 2, \ldots, N について、マス (r_i, c_i) には正整数 a_i が書かれており、それら以外のマスには 0 が書かれています。
はじめ、マス (R, C) にコマが置かれています。 高橋君は、コマを「いま置かれているマスから別のマスに移動させる」ことを好きな回数だけ行うことができます。 ただし、コマを移動する際には下記の 2 つの条件をともに満たさなければなりません。
- 移動先のマスに書かれている整数は、移動前のマスに書かれている整数より真に大きい。
- 移動先のマスは移動前のマスと同じ行にある、または、移動先のマスは移動前のマスと同じ列にある。
i = 1, 2, \ldots, N のそれぞれについて、(R, C) = (r_i, c_i) の場合に高橋君がコマの移動を行うことができる回数の最大値を出力してください。
制約
- 2 \leq H, W \leq 2 \times 10^5
- 1 \leq N \leq \min(2 \times 10^5, HW)
- 1 \leq r_i \leq H
- 1 \leq c_i \leq W
- 1 \leq a_i \leq 10^9
- i \neq j \Rightarrow (r_i, c_i) \neq (r_j, c_j)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W N r_1 c_1 a_1 r_2 c_2 a_2 \vdots r_N c_N a_N
出力
N 行出力せよ。 i = 1, 2, \ldots, N について、i 行目には (R, C) = (r_i, c_i) の場合に高橋君がコマの移動を行うことができる回数の最大値を出力せよ。
入力例 1
3 3 7 1 1 4 1 2 7 2 1 3 2 3 5 3 1 2 3 2 5 3 3 5
出力例 1
1 0 2 0 3 1 0
マス目に書かれた整数は下記の通りです。
4 7 0 3 0 5 2 5 5
- (R, C) = (r_1, c_1) = (1, 1) の場合、(1, 1) \rightarrow (1, 2) と移動すると、コマの移動を 1 回行うことができます。
- (R, C) = (r_2, c_2) = (1, 2) の場合、一度もコマの移動を行うことができません。
- (R, C) = (r_3, c_3) = (2, 1) の場合、(2, 1) \rightarrow (1, 1) \rightarrow (1, 2) と移動すると、コマの移動を 2 回行うことができます。
- (R, C) = (r_4, c_4) = (2, 3) の場合、一度もコマの移動を行うことができません。
- (R, C) = (r_5, c_5) = (3, 1) の場合、(3, 1) \rightarrow (2, 1) \rightarrow (1, 1) \rightarrow (1, 2) と移動すると、コマの移動を 3 回行うことができます。
- (R, C) = (r_6, c_6) = (3, 2) の場合、(3, 2) \rightarrow (1, 2) と移動すると、コマの移動を 1 回行うことができます。
- (R, C) = (r_7, c_7) = (3, 3) の場合、一度もコマの移動を行うことができません。
入力例 2
5 7 20 2 7 8 2 6 4 4 1 9 1 5 4 2 2 7 5 5 2 1 7 2 4 6 6 1 4 1 2 1 10 5 6 9 5 3 3 3 7 9 3 6 3 4 3 4 3 3 10 4 2 1 3 5 4 1 2 6 4 7 9
出力例 2
2 4 1 5 3 6 6 2 7 0 0 4 1 5 3 0 5 2 4 0
Score : 500 points
Problem Statement
We have a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.
Each square contains an integer. For each i = 1, 2, \ldots, N, the square (r_i, c_i) contains a positive integer a_i. The other square contains a 0.
Initially, there is a piece on the square (R, C). Takahashi can move the piece to a square other than the square it occupies now, any number of times. However, when moving the piece, both of the following conditions must be satisfied.
- The integer written on the square to which the piece is moved is strictly greater than the integer written on the square from which the piece is moved.
- The squares to and from which the piece is moved are in the same row or the same column.
For each i = 1, 2, \ldots, N, print the maximum number of times Takahashi can move the piece when (R, C) = (r_i, c_i).
Constraints
- 2 \leq H, W \leq 2 \times 10^5
- 1 \leq N \leq \min(2 \times 10^5, HW)
- 1 \leq r_i \leq H
- 1 \leq c_i \leq W
- 1 \leq a_i \leq 10^9
- i \neq j \Rightarrow (r_i, c_i) \neq (r_j, c_j)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
H W N r_1 c_1 a_1 r_2 c_2 a_2 \vdots r_N c_N a_N
Output
Print N lines. For each i = 1, 2, \ldots, N, the i-th line should contain the maximum number of times Takahashi can move the piece when (R, C) = (r_i, c_i).
Sample Input 1
3 3 7 1 1 4 1 2 7 2 1 3 2 3 5 3 1 2 3 2 5 3 3 5
Sample Output 1
1 0 2 0 3 1 0
The grid contains the following integers.
4 7 0 3 0 5 2 5 5
- When (R, C) = (r_1, c_1) = (1, 1), you can move the piece once by moving it as (1, 1) \rightarrow (1, 2).
- When (R, C) = (r_2, c_2) = (1, 2), you cannot move the piece at all.
- When (R, C) = (r_3, c_3) = (2, 1), you can move the piece twice by moving it as (2, 1) \rightarrow (1, 1) \rightarrow (1, 2).
- When (R, C) = (r_4, c_4) = (2, 3), you cannot move the piece at all.
- When (R, C) = (r_5, c_5) = (3, 1), you can move the piece three times by moving it as (3, 1) \rightarrow (2, 1) \rightarrow (1, 1) \rightarrow (1, 2).
- When (R, C) = (r_6, c_6) = (3, 2), you can move the piece once by moving it as (3, 2) \rightarrow (1, 2).
- When (R, C) = (r_7, c_7) = (3, 3), you cannot move the piece at all.
Sample Input 2
5 7 20 2 7 8 2 6 4 4 1 9 1 5 4 2 2 7 5 5 2 1 7 2 4 6 6 1 4 1 2 1 10 5 6 9 5 3 3 3 7 9 3 6 3 4 3 4 3 3 10 4 2 1 3 5 4 1 2 6 4 7 9
Sample Output 2
2 4 1 5 3 6 6 2 7 0 0 4 1 5 3 0 5 2 4 0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
縦 H 行、横 W 列のマス目があります。上から i 行目、左から j 列目のマスをマス (i, j) と呼びます。
1 \leq i \leq H かつ 1 \leq j \leq W を満たす整数の組 (i, j) それぞれについて、マス (i, j) には正整数 A_{i, j} が書かれています。また、すべてのマスは白色に塗られています。
高橋君は、縦 h_1 行、横 w_1 列の長方形の黒いスタンプを持っており、青木君は、縦 h_2 行、横 w_2 列の長方形の白いスタンプを持っています。
2 人はこれらのスタンプとマス目を使って対戦ゲームをします。
まず高橋君が、持っている黒いスタンプを 1 回使って、マス目の縦 h_1 行、横 w_1 列の長方形領域を黒色に塗りつぶします。
すなわち、1 \leq i \leq H - h_1 + 1 かつ 1 \leq j \leq W - w_1 + 1 を満たす整数の組 (i, j) を一つ選び、
i \leq r \leq i + h_1 - 1 かつ j \leq c \leq j + w_1 - 1 を満たすすべてのマス (r, c) を黒色に塗りつぶします。
次に青木君が、持っている白いスタンプを 1 回使って、マス目の縦 h_2 行、横 w_2 列の長方形領域を白色に塗りつぶします。
すなわち、1 \leq i \leq H - h_2 + 1 かつ 1 \leq j \leq W - w_2 + 1 を満たす整数の組 (i, j) を一つ選び、
i \leq r \leq i + h_2 - 1 かつ j \leq c \leq j + w_2 - 1 を満たすすべてのマス (r, c) を白色に塗りつぶします。
このとき、青木君が白色に塗るマスがすでに高橋君によって黒色に塗られていた場合は、そのマスの色は白で上書きされます。
上記の通りに高橋君と青木君がスタンプを 1 回ずつ使った後の、黒色に塗られたマスに書かれた整数の合計を、ゲームの「スコア」とします。 高橋君はスコアが出来るだけ大きくなるように、青木君はスコアが出来るだけ小さくなるように、それぞれ最適な戦略をとります。 ゲームのスコアがいくらになるかを求めて下さい。
制約
- 2 \leq H, W \leq 1000
- 1 \leq h_1, h_2 \leq H
- 1 \leq w_1, w_2 \leq W
- 1 \leq A_{i, j} \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W h_1 w_1 h_2 w_2
A_{1, 1} A_{1, 2} \cdots A_{1, W}
A_{2, 1} A_{2, 2} \cdots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \cdots A_{H, W}
出力
高橋君はスコアが出来るだけ大きくなるように、青木君はスコアが出来るだけ小さくなるように、それぞれ最適な戦略をとるときの、ゲームのスコアを出力せよ。
入力例 1
3 4 2 3 3 1 3 1 4 1 5 9 2 6 5 3 5 8
出力例 1
19
ゲームは以下の通りに進行します。
- はじめ、すべてのマスは白色で塗られています。
- まず高橋君が、持っている縦 2 行横 3 列の黒いスタンプを使って、マス (2, 2), (2, 3), (2 ,4), (3, 2), (3, 3), (3, 4) の 6 つのマスを黒色で塗りつぶします。
- 次に青木君が、持っている縦 3 行横 1 列の白いスタンプを使って、マス (1, 4), (2, 4), (3, 4) を白色で塗りつぶします。
- 最終的に黒色で塗られているマスは、マス (2, 2), (2, 3), (3, 2), (3, 3) の 4 つであるため、ゲームのスコアは 9 + 2 + 3 + 5 = 19 です。
入力例 2
3 4 2 3 3 4 3 1 4 1 5 9 2 6 5 3 5 8
出力例 2
0
青木君がスタンプを使った後、すべてのマスは白色であり、ゲームのスコアは 0 となります。
入力例 3
10 10 3 7 2 3 9 7 19 7 10 4 13 9 4 8 10 15 16 3 18 19 17 12 13 2 12 18 4 9 13 13 6 13 5 2 16 12 2 14 18 17 14 7 8 12 12 13 17 12 14 15 19 7 13 15 5 2 16 10 4 6 1 2 7 8 10 14 14 10 9 13 11 4 9 19 16 12 3 19 19 6 2 19 14 20 15 3 19 19 2 10 1 4 3 15 13 20 5 6 19 1 7 17 10 19
出力例 3
180
Score : 500 points
Problem Statement
There is a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.
For each pair of integers (i, j) such that 1 \leq i \leq H and 1 \leq j \leq W, (i, j) contains a positive integer A_{i,j}. Additionally, every square is painted white.
Takahashi has a rectangular black stamp that can cover h_1 rows and w_1 columns, and Aoki has a rectangular white stamp that can cover h_2 rows and w_2 columns. Using these stamps and the grid, they will play a game against each other.
First, Takahashi presses his black stamp to paint a rectangular region with h_1 rows and w_1 columns black.
That is, he chooses a pair of integers (i, j) such that 1 \leq i \leq H - h_1 + 1 and 1 \leq j \leq W - w_1 + 1, and paints black every square (r, c) such that i \leq r \leq i + h_1 - 1 and j \leq c \leq j + w_1 - 1.
Second, Aoki presses his white stamp to paint a rectangular region with h_2 rows and w_2 columns white.
That is, he chooses a pair of integers (i, j) such that 1 \leq i \leq H - h_2 + 1 and 1 \leq j \leq W - w_2 + 1, and paints white every square (r, c) such that i \leq r \leq i + h_2 - 1 and j \leq c \leq j + w_2 - 1.
If some of these squares are already painted black by Takahashi, they will also become white.
The game's score is defined as the sum of the integers written on the squares painted black after Takahashi and Aoki press their stamps as described above. Takahashi follows the optimal strategy to make the score as large as possible, while Aoki follows the optimal strategy to make the score as small as possible. What will the game's score be?
Constraints
- 2 \leq H, W \leq 1000
- 1 \leq h_1, h_2 \leq H
- 1 \leq w_1, w_2 \leq W
- 1 \leq A_{i, j} \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
H W h_1 w_1 h_2 w_2
A_{1, 1} A_{1, 2} \cdots A_{1, W}
A_{2, 1} A_{2, 2} \cdots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \cdots A_{H, W}
Output
Print the game's score when Takahashi follows the optimal strategy to make the score as large as possible and Aoki follows the optimal strategy to make the score as small as possible.
Sample Input 1
3 4 2 3 3 1 3 1 4 1 5 9 2 6 5 3 5 8
Sample Output 1
19
The game will go as follows.
- Initially, all squares are painted white.
- First, Takahashi presses his 2 \times 3 black stamp to paint the following six squares black: (2, 2), (2, 3), (2 ,4), (3, 2), (3, 3), (3, 4).
- Second, Aoki presses his 3 \times 1 white stamp to paint the following three squares white: (1, 4), (2, 4), (3, 4).
- Eventually, the following four squares are painted black: (2, 2), (2, 3), (3, 2), (3, 3), for a score of 9 + 2 + 3 + 5 = 19.
Sample Input 2
3 4 2 3 3 4 3 1 4 1 5 9 2 6 5 3 5 8
Sample Output 2
0
After Aoki presses his stamp, all squares will be white, for a score of 0.
Sample Input 3
10 10 3 7 2 3 9 7 19 7 10 4 13 9 4 8 10 15 16 3 18 19 17 12 13 2 12 18 4 9 13 13 6 13 5 2 16 12 2 14 18 17 14 7 8 12 12 13 17 12 14 15 19 7 13 15 5 2 16 10 4 6 1 2 7 8 10 14 14 10 9 13 11 4 9 19 16 12 3 19 19 6 2 19 14 20 15 3 19 19 2 10 1 4 3 15 13 20 5 6 19 1 7 17 10 19
Sample Output 3
180