E - Shapes

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

2 次元グリッド上に 2 つの図形 ST があります。グリッドは正方形のマスからなります。

SNN 列のグリッド内にあり、S_{i,j}# であるようなマス全体からなります。
TNN 列のグリッド内にあり、T_{i,j}# であるようなマス全体からなります。

ST90 度回転及び平行移動の繰り返しによって一致させることができるか判定してください。

制約

  • 1 \leq N \leq 200
  • S,T#. のみからなる
  • S,T1 つ以上 # を含む

入力

入力は以下の形式で標準入力から与えられる。

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}

出力

ST90 度回転及び平行移動の繰り返しによって一致させることができるとき 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.

F - Sowing Stones

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

1 から N まで番号がつけられた N 個のマスが一列に並んでいます。最初、 M 個のマスに石が入っており、マス X_i には A_i(1 \leq i \leq M) の石が入っています。

あなたは以下の操作を好きな回数( 0 回でもよい)行うことができます。

  • マス i (1 \leq i \leq N-1) に石があるとき、マス i から石を 1 つマス i+1 に移動させる。

N 個のマスすべてに石がちょうど 1 個ずつ入っている状態にするために必要な操作回数の最小値を求めてください。ただし、不可能な場合は -1 を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^{9}
  • 1 \leq M \leq 2 \times 10^{5}
  • M \leq N
  • 1 \leq X_i \leq N (1 \leq i \leq M)
  • X_i \neq X_j (1 \leq i < j \leq M)
  • 1 \leq A_i \leq 2 \times 10^{9} (1 \leq i \leq M)
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N M
X_1 X_2 \ldots X_M
A_1 A_2 \ldots A_M

出力

答えを出力せよ。


入力例 1

5 2
1 4
3 2

出力例 1

4

以下の 4 回の操作で、5 個のマスすべてに石がちょうど 1 個ずつ入っている状態にすることができます。

  • マス 1 の石を 1 つマス 2 に移動させる。
  • マス 2 の石を 1 つマス 3 に移動させる。
  • マス 4 の石を 1 つマス 5 に移動させる。
  • マス 1 の石を 1 つマス 2 に移動させる。

また、3 回以下の操作では 5 個のマスすべてに石がちょうど 1 個ずつ入っている状態にすることはできません。よって、4 を出力します。


入力例 2

10 3
1 4 8
4 2 4

出力例 2

-1

どのように操作を行っても 10 個のマスすべてに石がちょうど 1 個ずつ入っている状態にすることはできません。よって、-1 を出力します。

Score : 300 points

Problem Statement

There are N cells numbered from 1 to N in a row. Initially, M cells contain stones, and cell X_i contains A_i stones (1 \leq i \leq M).

You can perform the following operation any number of times (possibly zero):

  • If cell i (1 \leq i \leq N-1) contains a stone, move one stone from cell i to cell i+1.

Find the minimum number of operations required to reach a state where each of the N cells contains exactly one stone. If it is impossible, print -1.

Constraints

  • 2 \leq N \leq 2 \times 10^{9}
  • 1 \leq M \leq 2 \times 10^{5}
  • M \leq N
  • 1 \leq X_i \leq N (1 \leq i \leq M)
  • X_i \neq X_j (1 \leq i < j \leq M)
  • 1 \leq A_i \leq 2 \times 10^{9} (1 \leq i \leq M)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M
X_1 X_2 \ldots X_M
A_1 A_2 \ldots A_M

Output

Print the answer.


Sample Input 1

5 2
1 4
3 2

Sample Output 1

4

You can reach a state where each of the five cells contains exactly one stone with four operations as follows:

  • Move one stone from cell 1 to cell 2.
  • Move one stone from cell 2 to cell 3.
  • Move one stone from cell 4 to cell 5.
  • Move one stone from cell 1 to cell 2.

It is impossible to achieve the goal in three or fewer operations. Therefore, print 4.


Sample Input 2

10 3
1 4 8
4 2 4

Sample Output 2

-1

No matter how you perform the operations, you cannot reach a state where all ten cells contain exactly one stone. Therefore, print -1.

G - Another Sigma Problem

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

正整数 x,y に対して f(x,y) を以下で定義します。

  • 十進表記の x,y をそれぞれ文字列として解釈しこの順に連結して得られる文字列を z とする。z を十進表記の整数として解釈したときの値を f(x,y) とする。

例えば f(3,14)=314, f(100,1)=1001 です。

長さ N の正整数列 A=(A_1,\ldots,A_N) が与えられます。次の式の値を 998244353 で割ったあまりを求めてください。

\displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^N f(A_i,A_j)


制約

  • 2\leq N\leq 2\times 10^5
  • 1\leq A_i \leq 10^9
  • 入力される数値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N 
A_1 \ldots A_N

出力

答えを出力せよ。


入力例 1

3
3 14 15

出力例 1

2044
  • f(A_1,A_2)=314
  • f(A_1,A_3)=315
  • f(A_2,A_3)=1415

なので、答えは f(A_1,A_2)+f(A_1,A_3)+f(A_2,A_3) = 2044 です。


入力例 2

5
1001 5 1000000 1000000000 100000

出力例 2

625549048

式の値を 998244353 で割ったあまりを求めることに注意してください。

Score: 400 points

Problem Statement

For positive integers x and y, define f(x, y) as follows:

  • Interpret the decimal representations of x and y as strings and concatenate them in this order to obtain a string z. The value of f(x, y) is the value of z when interpreted as a decimal integer.

For example, f(3, 14) = 314 and f(100, 1) = 1001.

You are given a sequence of positive integers A = (A_1, \ldots, A_N) of length N. Find the value of the following expression modulo 998244353:

\displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^N f(A_i,A_j).


Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the answer.


Sample Input 1

3
3 14 15

Sample Output 1

2044
  • f(A_1, A_2) = 314
  • f(A_1, A_3) = 315
  • f(A_2, A_3) = 1415

Thus, the answer is f(A_1, A_2) + f(A_1, A_3) + f(A_2, A_3) = 2044.


Sample Input 2

5
1001 5 1000000 1000000000 100000

Sample Output 2

625549048

Be sure to calculate the value modulo 998244353.

H - Our clients, please wait a moment

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

ある国には都市が N 個あります。
あなたは、都市 1 にある営業所から 0 個以上の都市を経由して都市 N にある訪問先へ移動しようとしています。
移動手段は社用車と電車の 2 種類があります。都市 i から都市 j へ移動するときの所要時間は以下の通りです。

  • 社用車を使った場合 : D_{i,j} \times A
  • 電車を使った場合 : D_{i,j} \times B + C

ただし、社用車から電車に乗り換えることはできますが、電車から社用車に乗り換えることはできません。
また、乗り換えは各都市のみで行え、乗り換えに時間はかかりません。

都市 1 から都市 N に移動するのにかかる時間は最短で何分ですか?

制約

  • 2 \leq N \leq 1000
  • 1 \leq A, B, C \leq 10^6
  • D_{i,j} \leq 10^6
  • D_{i,i} = 0
  • D_{i,j} = D_{j,i} > 0 (i \neq j)
  • 入力される数値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N A B C
D_{1,1} D_{1,2} \ldots D_{1,N}
D_{2,1} D_{2,2} \ldots D_{2,N}
\vdots
D_{N,1} D_{N,2} \ldots D_{N,N}

出力

答えを整数として出力せよ。


入力例 1

4 8 5 13
0 6 2 15
6 0 3 5
2 3 0 13
15 5 13 0

出力例 1

78

以下のように移動することで合計 78 分で都市 1 から都市 4 に移動することができます。

  • 都市 1 から都市 3 まで社用車で移動する。この移動には 2 \times 8 = 16 分かかる。
  • 都市 3 から都市 2 まで社用車で移動する。この移動には 3 \times 8 = 24 分かかる。
  • 都市 2 から都市 4 まで電車で移動する。この移動には 5 \times 5 + 13 = 38 分かかる。

78 分未満の時間で都市 1 から都市 4 に移動することはできません。


入力例 2

3 1 1000000 1000000
0 10 1
10 0 10
1 10 0

出力例 2

1

入力例 3

5 954257 954213 814214
0 84251 214529 10017 373342
84251 0 91926 32336 164457
214529 91926 0 108914 57762
10017 32336 108914 0 234705
373342 164457 57762 234705 0

出力例 3

168604826785

Score : 450 points

Problem Statement

There are N cities in a certain country.
You will travel from your office in city 1 to a destination in city N, via zero or more cities.
Two types of transportation are available: company car and train. The time required to travel from city i to city j is as follows:

  • D_{i,j} \times A minutes by company car, and
  • D_{i,j} \times B + C minutes by train.

You can switch from company car to train, but not vice versa.
You can do so without spending time, but only in a city.

What is the minimum time in minutes to travel from city 1 to city N?

Constraints

  • 2 \leq N \leq 1000
  • 1 \leq A, B, C \leq 10^6
  • D_{i,j} \leq 10^6
  • D_{i,i} = 0
  • D_{i,j} = D_{j,i} > 0 (i \neq j)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N A B C
D_{1,1} D_{1,2} \ldots D_{1,N}
D_{2,1} D_{2,2} \ldots D_{2,N}
\vdots
D_{N,1} D_{N,2} \ldots D_{N,N}

Output

Print the answer as an integer.


Sample Input 1

4 8 5 13
0 6 2 15
6 0 3 5
2 3 0 13
15 5 13 0

Sample Output 1

78

You can travel from city 1 to city 4 in a total of 78 minutes by moving as follows.

  • Travel by company car from city 1 to city 3. This takes 2 \times 8 = 16 minutes.
  • Travel by company car from city 3 to city 2. This takes 3 \times 8 = 24 minutes.
  • Travel by train from city 2 to city 4. This takes 5 \times 5 + 13 = 38 minutes.

It is impossible to travel from city 1 to city 4 in less than 78 minutes.


Sample Input 2

3 1 1000000 1000000
0 10 1
10 0 10
1 10 0

Sample Output 2

1

Sample Input 3

5 954257 954213 814214
0 84251 214529 10017 373342
84251 0 91926 32336 164457
214529 91926 0 108914 57762
10017 32336 108914 0 234705
373342 164457 57762 234705 0

Sample Output 3

168604826785
I - Falling Bars

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 525

問題文

HW 列のグリッドがあります。 このグリッドの上から i\ (1\leq i\leq H) 行目、左から j\ (1\leq j\leq W) 列目のマスを (i,j) と表記します。

1 から N までの番号が付けられた N 個の横長のバーがグリッド上に置かれています。 バー i1\times 1 のブロックが横に L_i 個繋がった形をしており、その左端のブロックは最初マス (R_i,C_i) 上にあります。 すなわち、バー i は最初マス (R_i,C_i), (R_i,C_i+1), \dots, (R_i,C_i+L_i-1) を占めています。 ここで、相異なる 2 つのバーに占められているマスは存在しないことが保証されます。

現在の時刻は t=0 です。 非負整数 n を用いて t=0.5+n と表されるようなすべての時刻において、i=1,2,\dots,N の順に以下のことが起こります。

  • バー i が一番下の行(H 行目)になく、かつバー i が占める各マスの 1 つ下のマスをどのバーも占めていない場合、バー i 全体が 1 マス分下に移動する。 すなわち、その時点でバー i が占めているマスが (r,C_i),(r,C_i+1),\dots,(r,C_i+L_i-1)\ (r < H) であり、どの j\ (0\leq j\leq L_i-1) についてもマス (r+1,C_i+j) を占めているバーが存在しないならば、 バー i の占めるマスが (r+1,C_i),(r+1,C_i+1),\dots,(r+1,C_i+L_i-1) に変化する。
  • そうでないならば、何も起こらない。

t=10^{100} においてバー i が占めているマスを (R'_i,C_i), (R'_i,C_i+1), \dots, (R'_i,C_i+L_i-1) とします。 R'_1,R'_2,\dots,R'_N を求めてください。

制約

  • 1\leq H,W \leq 2\times 10^5
  • 1\leq N \leq 2\times 10^5
  • 1\leq R_i\leq H
  • 1\leq C_i\leq W
  • 1\leq L_i\leq W-C_i+1
  • 与えられる初期状態において、相異なる 2 つのバーに占められているマスは存在しない
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

H W N
R_1 C_1 L_1
R_2 C_2 L_2
\vdots
R_N C_N L_N

出力

N 行出力せよ。 i\ (1\leq i \leq N) 行目には、R'_i を出力せよ。


入力例 1

4 4 4
1 2 3
3 2 2
2 1 2
2 4 1

出力例 1

2
4
3
4

以下の 3 つの図は左から順に t=0,1,2 でのグリッドの様子を表しています。 色の塗られた長方形は各バーを表し、長方形の中に書かれた数字はそのバーの番号です。

グリッドの状態の変化は以下の通り説明されます。

  • t=0.5:
    • i=1: バー 1 が占める各マスの 1 つ下のマスである (2,2),(2,3),(2,4) のうち、(2,2) がバー 3 に、(2,4) がバー 4 にそれぞれ占められているため、何も起こらない。
    • i=2: バー 2 が占める各マスの 1 つ下のマスである (4,2),(4,3) がいずれも他のバーに占められていないため、バー 2 全体が 1 マス分下に移動する。
    • i=3: バー 3 が占める各マスの 1 つ下のマスである (3,1),(3,2) がいずれも他のバーに占められていないため、バー 3 全体が 1 マス分下に移動する。
    • i=4: バー 4 が占めるマスの 1 つ下のマスである (3,4) が他のバーに占められていないため、バー 4 全体が 1 マス分下に移動する。
  • t=1.5:
    • i=1: バー 1 が占める各マスの 1 つ下のマスである (2,2),(2,3),(2,4) がいずれも他のバーに占められていないため、バー 1 全体が 1 マス分下に移動する。
    • i=2: バー 2 は一番下の行にあるため、何も起こらない。
    • i=3: バー 3 が占める各マスの 1 つ下のマスである (4,1),(4,2) のうち、(4,2) がバー 2 に占められているため、何も起こらない。
    • i=4: バー 4 が占めるマスの 1 つ下のマスである (4,4) が他のバーに占められていないため、バー 4 全体が 1 マス分下に移動する。

t=2.5,3.5,\dots においては 1 つ下のマスがすべて空いているようなバーが存在せず、何も起こらないため、t=10^{100} でのグリッドの状態は t=2 でのグリッドの状態(上図における一番右の状態)と同じです。

よって、R'_1=2,R'_2=4,R'_3=3,R'_4=4 です。


入力例 2

382 382 3
3 3 3
8 8 8
2 2 2

出力例 2

382
382
381

入力例 3

5 10 8
2 2 1
4 3 1
4 8 2
1 2 2
2 5 3
5 4 3
4 5 2
1 5 2

出力例 3

5
5
5
4
3
5
4
2

Score: 525 points

Problem Statement

There is a grid with H rows and W columns. Let (i,j) denote the cell at the i-th row from the top and the j-th column from the left.

There are N horizontal bars numbered from 1 to N placed on the grid. Bar i consists of L_i blocks of size 1 \times 1 connected horizontally, and its leftmost block is initially at cell (R_i, C_i). That is, initially, bar i occupies the cells (R_i, C_i), (R_i, C_i + 1), \dots, (R_i, C_i + L_i - 1). It is guaranteed that there is no cell occupied by two different bars.

The current time is t = 0. At every time t = 0.5 + n for some non-negative integer n, the following occurs in order of i = 1, 2, \dots, N:

  • If bar i is not on the bottom row (the H-th row), and none of the cells directly below the cells occupied by bar i is occupied by any bar, then bar i moves down by one cell. That is, if at that time bar i occupies the cells (r,C_i),(r,C_i+1),\dots,(r,C_i+L_i-1)\ (r < H), and the cell (r + 1, C_i + j) is not occupied by any bar for all j (0 \leq j \leq L_i - 1), then bar i now occupies (r + 1, C_i), (r + 1, C_i + 1), \dots, (r + 1, C_i + L_i - 1).
  • Otherwise, nothing happens.

Let (R'_i, C_i), (R'_i, C_i + 1), \dots, (R'_i, C_i + L_i - 1) be the cells occupied by bar i at time t = 10^{100}. Find R'_1, R'_2, \dots, R'_N.

Constraints

  • 1 \leq H, W \leq 2 \times 10^5
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq R_i \leq H
  • 1 \leq C_i \leq W
  • 1 \leq L_i \leq W - C_i + 1
  • In the initial state, there is no cell occupied by two different bars.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

H W N
R_1 C_1 L_1
R_2 C_2 L_2
\vdots
R_N C_N L_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain R'_i.


Sample Input 1

4 4 4
1 2 3
3 2 2
2 1 2
2 4 1

Sample Output 1

2
4
3
4

The following three diagrams represent the grid at times t = 0, 1, and 2 from left to right. Colored rectangles represent the bars, and the number inside each rectangle indicates its bar number.

The changes in the grid state are explained as follows:

  • At t = 0.5:
    • i = 1: The cells directly below bar 1 are (2,2),(2,3),(2,4). Among these, (2,2) is occupied by bar 3 and (2,4) is occupied by bar 4, so nothing happens.
    • i = 2: The cells directly below bar 2 are (4,2),(4,3), which are not occupied by any other bar, so bar 2 moves down by one cell.
    • i = 3: The cells directly below bar 3 are (3,1),(3,2), which are not occupied by any other bar, so bar 3 moves down by one cell.
    • i = 4: The cell directly below bar 4 is (3,4), which is not occupied by any other bar, so bar 4 moves down by one cell.
  • At t = 1.5:
    • i = 1: The cells directly below bar 1 are (2,2),(2,3),(2,4), which are not occupied by any other bar, so bar 1 moves down by one cell.
    • i = 2: Bar 2 is on the bottom row, so nothing happens.
    • i = 3: The cells directly below bar 3 are (4,1),(4,2). Among these, (4,2) is occupied by bar 2, so nothing happens.
    • i = 4: The cell directly below bar 4 is (4,4), which is not occupied by any other bar, so bar 4 moves down by one cell.

At times t = 2.5, 3.5, \dots, there is no bar such that the cells directly below it are all unoccupied, so nothing happens. Thus, the grid at time t = 10^{100} is the same as at t = 2 (the rightmost diagram above).

Therefore, R'_1 = 2, R'_2 = 4, R'_3 = 3, R'_4 = 4.


Sample Input 2

382 382 3
3 3 3
8 8 8
2 2 2

Sample Output 2

382
382
381

Sample Input 3

5 10 8
2 2 1
4 3 1
4 8 2
1 2 2
2 5 3
5 4 3
4 5 2
1 5 2

Sample Output 3

5
5
5
4
3
5
4
2