実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
1 以上 9 以下の整数 N が入力として与えられます。
数字 N を N 個繋げて得られる文字列を出力してください。
制約
- N は 1 以上 9 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
3
出力例 1
333
3 を 3 個繋げて得られる文字列は 333
です。
入力例 2
9
出力例 2
999999999
Score : 100 points
Problem Statement
You are given an integer N between 1 and 9, inclusive, as input.
Concatenate N copies of the digit N and print the resulting string.
Constraints
- N is an integer between 1 and 9, inclusive.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
3
Sample Output 1
333
Concatenate three copies of the digit 3 to yield the string 333
.
Sample Input 2
9
Sample Output 2
999999999
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
ABC400 を記念した式典において、400 人の高橋君を A 行 B 列の長方形状に隙間なく並べようとしています。
正整数 A が与えられるので、このような並べ方ができるような正整数 B の値を出力してください。ただし、そのような正整数 B が存在しない場合には -1 を出力してください。
制約
- A は 1 以上 400 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
A
出力
問題文の指示に従って B の値あるいは -1 を出力せよ。
入力例 1
10
出力例 1
40
400 人の高橋君を 10 行 40 列の長方形状に並べることができます。
入力例 2
11
出力例 2
-1
入力例 3
400
出力例 3
1
Score : 100 points
Problem Statement
In the ceremony commemorating ABC400, we want to arrange 400 people in a rectangular formation of A rows and B columns without any gaps.
You are given a positive integer A. Print the value of a positive integer B for which such an arrangement is possible. If there is no such positive integer B, print -1.
Constraints
- A is an integer between 1 and 400, inclusive.
Input
The input is given from Standard Input in the following format:
A
Output
Print the value of B or -1 as specified by the problem statement.
Sample Input 1
10
Sample Output 1
40
We can arrange 400 people in 10 rows and 40 columns.
Sample Input 2
11
Sample Output 2
-1
Sample Input 3
400
Sample Output 3
1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
英小文字からなる長さ N の文字列 S が与えられます。 S の x 文字目 (1 \le x \le N) は S_x です。
i=1,2,\dots,N-1 それぞれについて、以下の条件を全て満たす最大の非負整数 l を求めてください。
- l+i \le N である。
- 全ての 1 \le k \le l を満たす整数 k について、 S_{k} \neq S_{k+i} を満たす。
なお、 l=0 は常に条件を満たすことに注意してください。
制約
- N は 2 \le N \le 5000 を満たす整数
- S は英小文字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
N-1 行にわたって出力せよ。そのうち x 行目 (1 \le x < N) には i=x とした場合の答えを整数として出力せよ。
入力例 1
6 abcbac
出力例 1
5 1 2 0 1
この入力では、 S= abcbac
です。
- i=1 のとき、 S_1 \neq S_2, S_2 \neq S_3, \dots ,S_5 \neq S_6 であるため、 最大値は l=5 です。
- i=2 のとき、 S_1 \neq S_3 ですが S_2 = S_4 であるため、 最大値は l=1 です。
- i=3 のとき、 S_1 \neq S_4, S_2 \neq S_5 ですが S_3 = S_6 であるため、 最大値は l=2 です。
- i=4 のとき、 S_1 = S_5 であるため、 最大値は l=0 です。
- i=5 のとき、 S_1 \neq S_6 であるため、 最大値は l=1 です。
Score : 200 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters. The x-th (1 \le x \le N) character of S is S_x.
For each i=1,2,\dots,N-1, find the maximum non-negative integer l that satisfies all of the following conditions:
- l+i \le N, and
- for all integers k such that 1 \le k \le l, it holds that S_{k} \neq S_{k+i}.
Note that l=0 always satisfies the conditions.
Constraints
- N is an integer such that 2 \le N \le 5000.
- S 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
Output
Print (N-1) lines. The x-th (1 \le x < N) line should contain the answer as an integer when i=x.
Sample Input 1
6 abcbac
Sample Output 1
5 1 2 0 1
In this input, S= abcbac
.
- When i=1, we have S_1 \neq S_2, S_2 \neq S_3, \dots, and S_5 \neq S_6, so the maximum value is l=5.
- When i=2, we have S_1 \neq S_3 but S_2 = S_4, so the maximum value is l=1.
- When i=3, we have S_1 \neq S_4 and S_2 \neq S_5 but S_3 = S_6, so the maximum value is l=2.
- When i=4, we have S_1 = S_5, so the maximum value is l=0.
- When i=5, we have S_1 \neq S_6, so the maximum value is l=1.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
高橋君には、穴 1,2,\dots,N に 1 本ずつ、全部で N 本の歯が生えています。
歯医者の青木君は、これらの歯と穴に対して、 Q 回の治療を行います。
i 回目の治療では、穴 T_i を治療します。治療内容は次の通りです。
- 穴 T_i に歯が生えている場合、穴 T_i から歯を抜く。
- そうでない ( 穴 T_i に歯が生えていない) 場合、穴 T_i に歯を生やす。
全ての治療が終わった後、高橋君に生えている歯の本数は何本ですか?
制約
- 入力は全て整数
- 1 \le N,Q \le 1000
- 1 \le T_i \le N
入力
入力は以下の形式で標準入力から与えられる。
N Q T_1 T_2 \dots T_Q
出力
答えを整数として出力せよ。
入力例 1
30 6 2 9 18 27 18 9
出力例 1
28
高橋君には最初 30 本の歯が生えており、青木君は 6 回の治療を行います。
- 1 回目の治療では穴 2 を治療します。 穴 2 に歯が生えているため、歯を抜きます。
- 2 回目の治療では穴 9 を治療します。 穴 9 に歯が生えているため、歯を抜きます。
- 3 回目の治療では穴 18 を治療します。 穴 18 に歯が生えているため、歯を抜きます。
- 4 回目の治療では穴 27 を治療します。 穴 27 に歯が生えているため、歯を抜きます。
- 5 回目の治療では穴 18 を治療します。 穴 18 に歯が生えていないため、歯を生やします。
- 6 回目の治療では穴 9 を治療します。 穴 9 に歯が生えていないため、歯を生やします。
最終的な歯の本数は 28 本です。
入力例 2
1 7 1 1 1 1 1 1 1
出力例 2
0
入力例 3
9 20 9 5 1 2 2 2 8 9 2 1 6 2 6 5 8 7 8 5 9 8
出力例 3
5
Score: 200 points
Problem Statement
Takahashi has N teeth, one in each of the holes numbered 1, 2, \dots, N.
Dentist Aoki will perform Q treatments on these teeth and holes.
In the i-th treatment, hole T_i is treated as follows:
- If there is a tooth in hole T_i, remove the tooth from hole T_i.
- If there is no tooth in hole T_i (i.e., the hole is empty), grow a tooth in hole T_i.
After all treatments are completed, how many teeth does Takahashi have?
Constraints
- All input values are integers.
- 1 \le N, Q \le 1000
- 1 \le T_i \le N
Input
The input is given from Standard Input in the following format:
N Q T_1 T_2 \dots T_Q
Output
Print the number of teeth as an integer.
Sample Input 1
30 6 2 9 18 27 18 9
Sample Output 1
28
Initially, Takahashi has 30 teeth, and Aoki performs six treatments.
- In the first treatment, hole 2 is treated. There is a tooth in hole 2, so it is removed.
- In the second treatment, hole 9 is treated. There is a tooth in hole 9, so it is removed.
- In the third treatment, hole 18 is treated. There is a tooth in hole 18, so it is removed.
- In the fourth treatment, hole 27 is treated. There is a tooth in hole 27, so it is removed.
- In the fifth treatment, hole 18 is treated. There is no tooth in hole 18, so a tooth is grown.
- In the sixth treatment, hole 9 is treated. There is no tooth in hole 9, so a tooth is grown.
The final count of teeth is 28.
Sample Input 2
1 7 1 1 1 1 1 1 1
Sample Output 2
0
Sample Input 3
9 20 9 5 1 2 2 2 8 9 2 1 6 2 6 5 8 7 8 5 9 8
Sample Output 3
5
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
H 行 W 列のマス目が与えられます。
以下、上から i 行目 (1\leq i\leq H) かつ左から j 列目 (1\leq j\leq W) のマスをマス (i,j) で表します。
マス目の状態は H 個の長さ W の文字列 S_1,S_2, \ldots, S_H によって以下のように表されます。
- S_i の j 文字目が
#
のとき、マス (i,j) は黒く塗られている。 - S_i の j 文字目が
.
のとき、マス (i,j) は白く塗られている。 - S_i の j 文字目が
?
のとき、マス (i,j) は塗られていない。
高橋君はまだ塗られていないマスをそれぞれ白または黒で塗ることで、黒マス全体が長方形をなすようにしたいです。
より具体的には、ある 4 つの整数の組 (a,b,c,d) (1\leq a\leq b\leq H, 1\leq c\leq d\leq W) が存在して、次が成り立つようにしたいです。
マス (i,j) (1\leq i\leq H, 1\leq j\leq W) は、 a\leq i\leq b かつ c\leq j\leq d をみたすとき、黒く塗られている。
そうでないとき、白く塗られている。
そのようなことが可能か判定してください。
制約
- 1\leq H,W\leq 1000
- H, W は整数
- S_i は
#
,.
,?
のみからなる長さ W の文字列 - 黒く塗られたマスが 1 つ以上存在する。
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H
出力
まだ塗られていないマスをそれぞれ白または黒で塗ることで、黒マス全体が長方形をなすようにできるならば Yes
を、そうでないならば No
を出力せよ。
入力例 1
3 5 .#?#. .?#?. ?...?
出力例 1
Yes
マス目は以下の状態になっています。?
のマスがまだ塗られていないマスです。
マス (1,3), (2,2), (2,4) を黒く塗り、マス (3,1), (3,5) を白く塗ることで、 以下のように黒マス全体が長方形をなすようにできます。
よって、Yes
を出力します。
入力例 2
3 3 ?## #.# ##?
出力例 2
No
黒マス全体が長方形をなすためには、少なくともマス (2,2) を黒く塗る必要がありますがすでに白く塗られています。
よって、黒マス全体が長方形をなすようにマス目を塗ることはできないため、No
を出力します。
入力例 3
1 1 #
出力例 3
Yes
Score : 300 points
Problem Statement
You are given a grid of H rows and W columns.
Let (i,j) denote the cell at row i (1 \leq i \leq H) from the top and column j (1 \leq j \leq W) from the left.
The state of the grid is represented by H strings S_1, S_2, \ldots, S_H, each of length W, as follows:
- If the j-th character of S_i is
#
, cell (i,j) is painted black. - If the j-th character of S_i is
.
, cell (i,j) is painted white. - If the j-th character of S_i is
?
, cell (i,j) is not yet painted.
Takahashi wants to paint each not-yet-painted cell white or black so that all the black cells form a rectangle.
More precisely, he wants there to exist a quadruple of integers (a,b,c,d) (1 \leq a \leq b \leq H, 1 \leq c \leq d \leq W) such that:
For each cell (i,j) (1 \leq i \leq H, 1 \leq j \leq W), if a \leq i \leq b and c \leq j \leq d, the cell is black;
otherwise, the cell is white.
Determine whether this is possible.
Constraints
- 1 \leq H, W \leq 1000
- H and W are integers.
- Each S_i is a string of length W consisting of
#
,.
,?
. - There is at least one cell that is already painted black.
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H
Output
If it is possible to paint all the not-yet-painted cells so that the black cells form a rectangle, print Yes
; otherwise, print No
.
Sample Input 1
3 5 .#?#. .?#?. ?...?
Sample Output 1
Yes
The grid is in the following state. ?
indicates a cell that are not yet painted.
By painting cells (1,3), (2,2), and (2,4) black and cells (3,1) and (3,5) white, the black cells can form a rectangle as follows:
Therefore, print Yes
.
Sample Input 2
3 3 ?## #.# ##?
Sample Output 2
No
To form a rectangle with all black cells, you would need to paint cell (2,2) black, but it is already painted white.
Therefore, it is impossible to make all black cells form a rectangle, so print No
.
Sample Input 3
1 1 #
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
正整数 N が与えられます。
正整数の組 (A,B,C,D) であって、AB + CD = N を満たすものの個数を求めてください。
なお、本問の制約の下、答えが 9 \times 10^{18} 以下であることが証明できます。
制約
- 2 \leq N \leq 2 \times 10^5
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
4
出力例 1
8
(A,B,C,D) として以下の 8 個が考えられます。
- (A,B,C,D)=(1,1,1,3)
- (A,B,C,D)=(1,1,3,1)
- (A,B,C,D)=(1,2,1,2)
- (A,B,C,D)=(1,2,2,1)
- (A,B,C,D)=(1,3,1,1)
- (A,B,C,D)=(2,1,1,2)
- (A,B,C,D)=(2,1,2,1)
- (A,B,C,D)=(3,1,1,1)
入力例 2
292
出力例 2
10886
入力例 3
19876
出力例 3
2219958
Score : 300 points
Problem Statement
You are given a positive integer N.
Find the number of quadruples of positive integers (A,B,C,D) such that AB + CD = N.
Under the constraints of this problem, it can be proved that the answer is at most 9 \times 10^{18}.
Constraints
- 2 \leq N \leq 2 \times 10^5
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
4
Sample Output 1
8
Here are the eight desired quadruples.
- (A,B,C,D)=(1,1,1,3)
- (A,B,C,D)=(1,1,3,1)
- (A,B,C,D)=(1,2,1,2)
- (A,B,C,D)=(1,2,2,1)
- (A,B,C,D)=(1,3,1,1)
- (A,B,C,D)=(2,1,1,2)
- (A,B,C,D)=(2,1,2,1)
- (A,B,C,D)=(3,1,1,1)
Sample Input 2
292
Sample Output 2
10886
Sample Input 3
19876
Sample Output 3
2219958
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
鳩 1, 鳩 2,\ldots, 鳩 N の N 羽の鳩と、巣 1, 巣 2,\ldots, 巣 N の N 個の巣があります。
はじめ、鳩 i (1\leq i\leq N) は巣 i にいます。
鳩たちに対して Q 回の操作を行います。
操作は次の 3 種類のうちのいずれかです。
- 種類 1 : 整数 a,b (1\leq a\leq N,1\leq b\leq N) が与えられる。鳩 a を今いる巣から取り出し、巣 b へ移動する。
- 種類 2 : 整数 a,b (1\leq a\lt b\leq N) が与えられる。巣 a にいる鳩をすべて巣 b へ移動し、巣 b にいる鳩をすべて巣 a へ移動する。これら 2 つの移動は一斉に行われる。
- 種類 3 : 整数 a (1\leq a\leq N) が与えられる。鳩 a が今いる巣の番号を報告する。
Q 回の操作を順に行ったときの、種類 3 の各操作における報告の結果を出力してください。
制約
- 1\leq N\leq10 ^ 6
- 1\leq Q\leq3\times10 ^ 5
- 入力される操作はすべて種類 1, 種類 2, 種類 3 のいずれかである。
- 入力される操作は問題文中の制約を満たす。
- 入力される操作のうちに種類 3 の操作が 1 つ以上含まれる。
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q op _ 1 op _ 2 \vdots op _ Q
ただし、i+1 行目の入力 op _ i は i 番目の操作を表しており、op _ i は次のいずれかの形式である。
i 番目の操作が種類 1 の操作のとき、i+1 行目は次の形式で与えられる。
1 a b
これは、与えられる整数を a と b として種類 1 の操作を行うことを表している。
i 番目の操作が種類 2 の操作のとき、i+1 行目は次の形式で与えられる。
2 a b
これは、与えられる整数を a と b として種類 2 の操作を行うことを表している。
i 番目の操作が種類 3 の操作のとき、i+1 行目は次の形式で与えられる。
3 a
これは、与えられる整数を a として種類 3 の操作を行うことを表している。
出力
与えられる種類 3 の操作の個数を q 個として、q 行にわたって出力せよ。
i 行目 (1\leq i\leq q) には、種類 3 の操作のうち i 番目の操作で報告される巣の番号を出力せよ。
入力例 1
6 8 1 2 4 1 3 6 3 2 2 4 5 3 2 1 4 2 3 4 3 2
出力例 1
4 5 2 5
与えられる操作で、鳩は以下の図のように移動します。
種類 3 の操作でそれぞれ報告すべき巣の番号は 4,5,2,5 なので、4
5
2
5
を 4 行にわたって出力してください。
入力例 2
1 2 1 1 1 3 1
出力例 2
1
種類 1 の操作で、鳩を取り出した巣にそのまま戻す場合もあります。
入力例 3
30 15 3 3 2 8 30 2 12 15 2 2 17 1 19 1 2 7 30 3 12 3 8 2 25 26 1 13 10 1 16 10 2 16 29 2 1 21 2 6 11 1 21 8
出力例 3
3 15 7
Score : 350 points
Problem Statement
There are N pigeons labeled 1, 2, \ldots, N and N nests labeled 1, 2, \ldots, N.
Initially, pigeon i (1 \leq i \leq N) is in nest i.
You will perform Q operations on these pigeons.
There are three types of operations:
- Type 1: You are given integers a and b (1 \leq a \leq N, 1 \leq b \leq N). Take pigeon a out of its current nest and move it to nest b.
- Type 2: You are given integers a and b (1 \leq a < b \leq N). Move all pigeons in nest a to nest b, and move all pigeons in nest b to nest a. These two moves happen simultaneously.
- Type 3: You are given an integer a (1 \leq a \leq N). Pigeon a reports the label of the nest it is currently in.
Print the result of each Type 3 operation in the order the operations are given.
Constraints
- 1 \leq N \leq 10^6
- 1 \leq Q \leq 3 \times 10^5
- Every operation is of Type 1, 2, or 3.
- All operations are valid according to the problem statement.
- There is at least one Type 3 operation.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q op _ 1 op _ 2 \vdots op _ Q
Here, op _ i on the line i+1 represents the i-th operation in one of the following formats.
If the i-th operation is of Type 1, op _ i is in the following format:
1 a b
This corresponds to a Type 1 operation with the given integers being a and b.
If the i-th operation is of Type 2, op _ i is in the following format:
2 a b
This corresponds to a Type 2 operation with the given integers being a and b.
If the i-th operation is of Type 3, op _ i is in the following format:
3 a
This corresponds to a Type 3 operation with the given integer being a.
Output
Let the number of Type 3 operations be q. Print q lines. The i-th line (1 \leq i \leq q) should contain the nest number reported in the i-th Type 3 operation.
Sample Input 1
6 8 1 2 4 1 3 6 3 2 2 4 5 3 2 1 4 2 3 4 3 2
Sample Output 1
4 5 2 5
In the operations given, the pigeons move as shown in the figure below:
The nest numbers to be reported for the Type 3 operations are 4,5,2,5. Print 4
, 5
, 2
, 5
on separate lines.
Sample Input 2
1 2 1 1 1 3 1
Sample Output 2
1
The destination nest of a Type 1 operation may be the same as the nest the pigeon is currently in.
Sample Input 3
30 15 3 3 2 8 30 2 12 15 2 2 17 1 19 1 2 7 30 3 12 3 8 2 25 26 1 13 10 1 16 10 2 16 29 2 1 21 2 6 11 1 21 8
Sample Output 3
3 15 7
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
H \times W の大きさの島があり、島は周りを海で囲まれています。
島は 縦 H 個 \times 横 W 個の 1\times 1 の区画に分けられており、上から i 番目かつ左から j 番目の区画の(現在の海面を基準にした)標高は A_{i,j} です。
現在から 1 年ごとに海面の高さが 1 ずつ上昇します。
このとき、海または海に沈んだ区画に上下左右に隣接する区画であって、標高が海面の高さ 以下 の区画は海に沈みます。
ここで、ある区画が新しく海に沈んだときそれと上下左右に隣接する区画であって海面の高さ以下のものも同時に海に沈み、これによって新しく沈んだ区画についてもこれは繰り返されます。
i=1,2,\ldots, Y それぞれについて、現在から i 年後に、島のうち海に沈まず残っている部分の面積を求めてください。
制約
- 1\leq H,W\leq 1000
- 1\leq Y\leq 10^5
- 1\leq A_{i,j}\leq 10^5
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W Y 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}
出力
Y 行出力せよ。 i 行目 (1\leq i\leq Y) には現在から i 年後に海に沈まず残っている島の面積を出力せよ。
入力例 1
3 3 5 10 2 10 3 1 4 10 5 10
出力例 1
9 7 6 5 4
島の上から i 番目かつ左から j 番目の区画を (i,j) で表します。このとき、次のようになります。
- 1 年後、海面は現在より 1 上昇しますが、海に面している標高 1 の区画は存在しないため、どの区画も沈みません。よって、1 行目には 9 を出力します。
- 2 年後、海面は現在より 2 上昇し、(1,2) が海に沈みます。これによって、(2,2) は海に沈んだ区画に隣接する区画となりますが、その標高は 2 以下であるため、これも海に沈みます。これら以外にこの時点で他に沈む区画はありません。よって、2 つの区画が沈むため、2 行目には 9-2=7 を出力します。
- 3 年後、海面は現在より 3 上昇し、(2,1) が新しく海に沈みます。他に沈む区画はありません。よって、3 行目には 6 を出力します。
- 4 年後、海面は現在より 4 上昇し、(2,3) が新しく海に沈みます。他に沈む区画はありません。よって、4 行目には 5 を出力します。
- 5 年後、海面は現在より 5 上昇し、(3,2) が新しく海に沈みます。他に沈む区画はありません。よって、5 行目には 4 を出力します。
よって、9,7,6,5,4 をこの順に各行に出力します。
入力例 2
3 5 3 2 2 3 3 3 2 1 2 1 3 2 2 3 3 3
出力例 2
15 7 0
Score : 450 points
Problem Statement
There is an island of size H \times W, surrounded by the sea.
The island is divided into H rows and W columns of 1 \times 1 sections, and the elevation of the section at the i-th row from the top and the j-th column from the left (relative to the current sea level) is A_{i,j}.
Starting from now, the sea level rises by 1 each year.
Here, a section that is vertically or horizontally adjacent to the sea or a section sunk into the sea and has an elevation not greater than the sea level will sink into the sea.
Here, when a section newly sinks into the sea, any vertically or horizontally adjacent section with an elevation not greater than the sea level will also sink into the sea simultaneously, and this process repeats for the newly sunk sections.
For each i=1,2,\ldots, Y, find the area of the island that remains above sea level i years from now.
Constraints
- 1 \leq H, W \leq 1000
- 1 \leq Y \leq 10^5
- 1 \leq A_{i,j} \leq 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W Y 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
Print Y lines. The i-th line (1 \leq i \leq Y) should contain the area of the island that remains above sea level i years from now.
Sample Input 1
3 3 5 10 2 10 3 1 4 10 5 10
Sample Output 1
9 7 6 5 4
Let (i,j) denote the section at the i-th row from the top and the j-th column from the left. Then, the following happens:
- After 1 year, the sea level is higher than now by 1, but there are no sections with an elevation of 1 that are adjacent to the sea, so no sections sink. Thus, the first line should contain 9.
- After 2 years, the sea level is higher than now by 2, and (1,2) sinks into the sea. This makes (2,2) adjacent to a sunken section, and its elevation is not greater than 2, so it also sinks. No other sections sink at this point. Thus, two sections sink, and the second line should contain 9-2=7.
- After 3 years, the sea level is higher than now by 3, and (2,1) sinks into the sea. No other sections sink. Thus, the third line should contain 6.
- After 4 years, the sea level is higher than now by 4, and (2,3) sinks into the sea. No other sections sink. Thus, the fourth line should contain 5.
- After 5 years, the sea level is higher than now by 5, and (3,2) sinks into the sea. No other sections sink. Thus, the fifth line should contain 4.
Therefore, print 9, 7, 6, 5, 4 in this order, each on a new line.
Sample Input 2
3 5 3 2 2 3 3 3 2 1 2 1 3 2 2 3 3 3
Sample Output 2
15 7 0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
キーエンスの工場長であるあなたは、ベルトコンベア上のいくつかの区間をセンサーによって監視したいと考えています。 あなたが監視したい区間は全部で N 個あり、i 個目の区間の長さは D_i メートルです。
センサーには 2 種類の候補があり、それぞれのセンサーに関する情報は以下の通りです。
- センサー j\ (1\leq j \leq 2) : 長さ L_j メートルの区間を監視できる。 価格は 1 個あたり C_j であり、全体で最大 K_j 個まで使用することができる。
1 つの区間をいくつかの区間に分割して監視することもできます。 また、センサーが監視する区間が重なっていたり、監視したい区間の長さより余分に監視していたりしても問題はありません。 例えば、L_1=4,L_2=2 であるとき、センサー 1 を 1 つ使って長さ 3 メートルの区間を監視したり、センサー 1,2 を 1 つずつ使って長さ 5 メートルの区間を監視したりすることが可能です。
N 個の区画をすべて監視することが可能であるか判定し、可能ならば必要なセンサーの価格の総和の最小値を求めてください。
制約
- 1\leq N \leq 100
- 1\leq D_i,L_j \leq 10^5
- 1\leq C_j \leq 10^9
- 1\leq K_j \leq 10^3
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N D_1 D_2 \dots D_N L_1 C_1 K_1 L_2 C_2 K_2
出力
N 個の区画をすべて監視することが不可能ならば -1
を、可能ならば必要なセンサーの価格の総和の最小値を出力せよ。
入力例 1
3 3 5 10 4 3 3 2 2 6
出力例 1
17
以下のようにすることで、センサー 1 を 3 つ、センサー 2 を 4 つ使ってすべての区間を監視できます。
- センサー 1 を 1 つ使って 1 個目の区間を監視する。
- センサー 1,2 を 1 つずつ使って 2 個目の区間を監視する。
- センサー 1 を 1 つ、センサー 2 を 3 つ使って 3 個目の区間を監視する。
このとき、必要なセンサーの価格の総和は 3\times 3 + 2\times 4 = 17 であり、これが最小です。
入力例 2
3 3 5 10 4 3 3 2 2 3
出力例 2
-1
入力例 3
2 4 8 3 1 100 4 10000 100
出力例 3
5
1 つも使わない種類のセンサーがあっても構いません。
Score : 500 points
Problem Statement
As the factory manager of Keyence, you want to monitor several sections on a conveyor belt. There are a total of N sections you want to monitor, and the length of the i-th section is D_i meters.
There are two types of sensors to choose from, and below is some information about each sensor.
- Type-j sensor (1\leq j \leq 2): Can monitor a section of length L_j meters. The price is C_j per sensor, and you can use at most K_j sensors of this type in total.
You can divide one section into several sections for monitoring. It is fine if the sections monitored by the sensors overlap, or if they monitor more than the length of the section you want to monitor. For example, when L_1=4 and L_2=2, you can use one type-1 sensor to monitor a section of length 3 meters, or use one type-1 and one type-2 sensor to monitor a section of length 5 meters.
Determine whether it is possible to monitor all N sections, and if it is possible, find the minimum total cost of the necessary sensors.
Constraints
- 1\leq N \leq 100
- 1\leq D_i,L_j \leq 10^5
- 1\leq C_j \leq 10^9
- 1\leq K_j \leq 10^3
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N D_1 D_2 \dots D_N L_1 C_1 K_1 L_2 C_2 K_2
Output
If it is impossible to monitor all N sections, print -1
. Otherwise, print the minimum total cost of the necessary sensors.
Sample Input 1
3 3 5 10 4 3 3 2 2 6
Sample Output 1
17
You can monitor all sections by using three type-1 sensors and four type-2 sensors as follows.
- Use one type-1 sensor to monitor the first section.
- Use one type-1 and one type-2 sensor to monitor the second section.
- Use one type-1 and three type-2 sensors to monitor the third section.
In this case, the total cost of the necessary sensors is 3\times 3 + 2\times 4 = 17, which is the minimum.
Sample Input 2
3 3 5 10 4 3 3 2 2 3
Sample Output 2
-1
Sample Input 3
2 4 8 3 1 100 4 10000 100
Sample Output 3
5
It is fine if one type of sensor is not used at all.