A - Painting

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 100

問題文

HW 列の マス目があり、最初すべてのマスは白色です。

あなたは、このマス目に何回かペイント操作を施すことにしました。 1 回のペイント操作では、以下の 2 種類の作業のうちいずれか 1 つが行えます。

  • 行をひとつ選び、その行に含まれるマスをすべて黒く塗る。
  • 列をひとつ選び、その列に含まれるマスをすべて黒く塗る。

黒く塗られているマスの個数が N 個以上となるようにするためには、最小で何回のペイント操作が必要ですか。 なお、制約の項で記述される条件のもとで、何回かペイント操作を行うことで 黒く塗られているマスの個数が N 個以上となるようにできることが保証されます。

制約

  • 1 \leq H \leq 100
  • 1 \leq W \leq 100
  • 1 \leq N \leq H \times W
  • 入力値はすべて整数である。

入力

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

H
W
N

出力

ペイント操作の回数の最小値を出力せよ。


入力例 1

3
7
10

出力例 1

2

「行をひとつ選び、その行に含まれるマスをすべて黒く塗る」という操作を異なる行に対して 1 回ずつ、 合計 2 回行うことで、黒く塗られているマスの個数を 14 にできます。


入力例 2

14
12
112

出力例 2

8

入力例 3

2
100
200

出力例 3

2

Score : 100 points

Problem Statement

We have a grid with H rows and W columns, where all the squares are initially white.

You will perform some number of painting operations on the grid. In one operation, you can do one of the following two actions:

  • Choose one row, then paint all the squares in that row black.
  • Choose one column, then paint all the squares in that column black.

At least how many operations do you need in order to have N or more black squares in the grid? It is guaranteed that, under the conditions in Constraints, having N or more black squares is always possible by performing some number of operations.

Constraints

  • 1 \leq H \leq 100
  • 1 \leq W \leq 100
  • 1 \leq N \leq H \times W
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H
W
N

Output

Print the minimum number of operations needed.


Sample Input 1

3
7
10

Sample Output 1

2

You can have 14 black squares in the grid by performing the "row" operation twice, on different rows.


Sample Input 2

14
12
112

Sample Output 2

8

Sample Input 3

2
100
200

Sample Output 3

2
B - Robot Arms

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 200

問題文

ある工場では、 数直線上に N 個のロボットが設置されています。 ロボット i は座標 X_i に設置されており、数直線の正負の方向にそれぞれ長さ L_i の腕を伸ばすことができます。

これらのロボットのうちいくつか (0 個以上) を取り除き、 残ったどの 2 つのロボットについても、腕が動く範囲が共通部分を持たないようにしたいと思います。 ただし、各 i (1 \leq i \leq N) に対して、 ロボット i の腕が動く範囲とは 数直線上の座標が X_i - L_i より大きく X_i + L_i 未満の部分を指します。

取り除かずに残せるロボットの個数の最大値を求めてください。

制約

  • 1 \leq N \leq 100,000
  • 0 \leq X_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq L_i \leq 10^9 (1 \leq i \leq N)
  • i \neq j のとき、X_i \neq X_j
  • 入力値はすべて整数である。

入力

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

N
X_1 L_1
X_2 L_2
\vdots
X_N L_N

出力

残せるロボットの個数の最大値を出力せよ。


入力例 1

4
2 4
4 3
9 3
100 5

出力例 1

3

ロボット 2 を取り除くことで、これ以外の 3 個のロボットを残すことができます。


入力例 2

2
8 20
1 10

出力例 2

1

入力例 3

5
10 1
2 1
4 1
6 1
8 1

出力例 3

5

Score : 200 points

Problem Statement

In a factory, there are N robots placed on a number line. Robot i is placed at coordinate X_i and can extend its arms of length L_i in both directions, positive and negative.

We want to remove zero or more robots so that the movable ranges of arms of no two remaining robots intersect. Here, for each i (1 \leq i \leq N), the movable range of arms of Robot i is the part of the number line between the coordinates X_i - L_i and X_i + L_i, excluding the endpoints.

Find the maximum number of robots that we can keep.

Constraints

  • 1 \leq N \leq 100,000
  • 0 \leq X_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq L_i \leq 10^9 (1 \leq i \leq N)
  • If i \neq j, X_i \neq X_j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 L_1
X_2 L_2
\vdots
X_N L_N

Output

Print the maximum number of robots that we can keep.


Sample Input 1

4
2 4
4 3
9 3
100 5

Sample Output 1

3

By removing Robot 2, we can keep the other three robots.


Sample Input 2

2
8 20
1 10

Sample Output 2

1

Sample Input 3

5
10 1
2 1
4 1
6 1
8 1

Sample Output 3

5
C - Subarray Sum

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 400

問題文

3 つの整数 N, K, S が与えられます。

1 以上 10^9 以下の整数からなる長さ N の数列 A_1, A_2, ..., A_N であって、 以下の条件を満たすものをひとつ求めてください。 なお、制約の項で記述される条件のもとで、このような数列は必ず存在することが証明できます。

  • 1 \leq l \leq r \leq N を満たす整数の組 (l, r) であって、 A_l + A_{l + 1} + \cdots + A_r = S を満たすものはちょうど K 個ある。

制約

  • 1 \leq N \leq 10^5
  • 0 \leq K \leq N
  • 1 \leq S \leq 10^9
  • 入力値はすべて整数である。

入力

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

N K S

出力

条件を満たす数列を以下の形式で出力せよ。

A_1 A_2 ... A_N

入力例 1

4 2 3

出力例 1

1 2 3 4

問題文の条件を満たす (l, r)(1, 2)(3, 3)2 個あります。


入力例 2

5 3 100

出力例 2

50 50 50 30 70

Score : 400 points

Problem Statement

Given are three integers N, K, and S.

Find a sequence A_1, A_2, ..., A_N of N integers between 1 and 10^9 (inclusive) that satisfies the condition below. We can prove that, under the conditions in Constraints, such a sequence always exists.

  • There are exactly K pairs (l, r) of integers such that 1 \leq l \leq r \leq N and A_l + A_{l + 1} + \cdots + A_r = S.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq K \leq N
  • 1 \leq S \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K S

Output

Print a sequence satisfying the condition, in the following format:

A_1 A_2 ... A_N

Sample Input 1

4 2 3

Sample Output 1

1 2 3 4

Two pairs (l, r) = (1, 2) and (3, 3) satisfy the condition in the statement.


Sample Input 2

5 3 100

Sample Output 2

50 50 50 30 70
D - Swap and Flip

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 700

問題文

N 枚のカードがあり、1, 2, ..., N の番号がついています。 カード i (1 \leq i \leq N) の片方の面には赤い文字で整数 A_i が、 もう片方の面には青い文字で整数 B_i が書かれています。 最初、これらのカードは赤い文字が書かれた面を表にして 左から右に番号順に一列に並んでいます。

以下の操作を繰り返すことで、カードの表側の面に書かれた整数の列が左から右に広義単調増加となる (すなわち、各 i (1 \leq i \leq N - 1) に対して、左から i + 1 枚目のカードの表側の面に書かれた整数が i 枚目のカードの表側の面に書かれた整数以上である) ようにすることが可能かどうか判定してください。 さらに、可能である場合、必要な操作の回数の最小値を求めてください。

  • 整数 i (1 \leq i \leq N - 1) を一つ選ぶ。 左から i 番目のカードと i + 1 番目のカードの位置を入れ替え、さらにこれら 2 枚のカードを裏返す。

制約

  • 1 \leq N \leq 18
  • 1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)
  • 入力値はすべて整数である。

入力

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

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

出力

単調増加となるようにすることが不可能である場合、-1 と出力せよ。 可能である場合、必要な操作の回数の最小値を出力せよ。


入力例 1

3
3 4 3
3 2 3

出力例 1

1

i = 1 として操作を 1 回行うと、 カードの表側の面に書かれた整数の列は [2, 3, 3] となり、単調増加となります。


入力例 2

2
2 1
1 2

出力例 2

-1

何回操作を行っても、 カードの表側の面に書かれた整数の列は [2, 1] のままであり、これは単調増加ではありません。


入力例 3

4
1 2 3 4
5 6 7 8

出力例 3

0

操作を行う必要がない場合もあります。


入力例 4

5
28 15 22 43 31
20 22 43 33 32

出力例 4

-1

入力例 5

5
4 46 6 38 43
33 15 18 27 37

出力例 5

3

Score : 700 points

Problem Statement

We have N cards numbered 1, 2, ..., N. Card i (1 \leq i \leq N) has an integer A_i written in red ink on one side and an integer B_i written in blue ink on the other side. Initially, these cards are arranged from left to right in the order from Card 1 to Card N, with the red numbers facing up.

Determine whether it is possible to have a non-decreasing sequence facing up from left to right (that is, for each i (1 \leq i \leq N - 1), the integer facing up on the (i+1)-th card from the left is not less than the integer facing up on the i-th card from the left) by repeating the operation below. If the answer is yes, find the minimum number of operations required to achieve it.

  • Choose an integer i (1 \leq i \leq N - 1). Swap the i-th and (i+1)-th cards from the left, then flip these two cards.

Constraints

  • 1 \leq N \leq 18
  • 1 \leq A_i, B_i \leq 50 (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 A_2 ... A_N
B_1 B_2 ... B_N

Output

If it is impossible to have a non-decreasing sequence, print -1. If it is possible, print the minimum number of operations required to achieve it.


Sample Input 1

3
3 4 3
3 2 3

Sample Output 1

1

By doing the operation once with i = 1, we have a sequence [2, 3, 3] facing up, which is non-decreasing.


Sample Input 2

2
2 1
1 2

Sample Output 2

-1

After any number of operations, we have the sequence [2, 1] facing up, which is not non-decreasing.


Sample Input 3

4
1 2 3 4
5 6 7 8

Sample Output 3

0

No operation may be required.


Sample Input 4

5
28 15 22 43 31
20 22 43 33 32

Sample Output 4

-1

Sample Input 5

5
4 46 6 38 43
33 15 18 27 37

Sample Output 5

3
E - Bichromization

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 900

問題文

N 頂点 M 辺の連結な無向グラフがあります。 このグラフの辺 i (1 \leq i \leq M) は頂点 U_i と頂点 V_i を双方向に結んでいます。 また、N 個の整数 D_1, D_2, ..., D_N が与えられます。

このグラフの各頂点に白または黒の色を割り当て、さらに 各辺に 1 以上 10^9 以下の整数の重みを割り当てる方法であって、以下の条件を満たすものが存在するかどうか判定してください。 さらに、存在する場合、そのような割り当てをひとつ求めてください。

  • 白および黒が割り当てられた頂点がそれぞれ少なくとも 1 個以上存在する。
  • 各頂点 v (1 \leq v \leq N) に対して以下の条件が成り立つ。
    • 頂点 v からグラフの辺を通って頂点 v と異なる色が割り当てられた頂点に移動する際にかかる最小のコストはちょうど D_v である。

なお、グラフ上の移動にかかるコストとは、 移動の際に通る辺の重みの和のことです。

制約

  • 2 \leq N \leq 100,000
  • 1 \leq M \leq 200,000
  • 1 \leq D_i \leq 10^9
  • 1 \leq U_i, V_i \leq N
  • 与えられるグラフは連結であり、自己ループや多重辺を持たない。
  • 入力値はすべて整数である。

入力

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

N M
D_1 D_2 ... D_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M

出力

割り当てが不可能である場合、-1 と一行に出力せよ。

可能である場合、 割り当て方をひとつ、 以下の形式で出力せよ。

S
C_1
C_2
\vdots
C_M

ただし、

  • 1 行目の出力 S は長さ N の文字列とせよ。 その i 番目 (1 \leq i \leq N) の文字は、頂点 i に白色を割り当てる場合は W とし、黒色を割り当てる場合は B とせよ。
  • i + 1 行目 (1 \leq i \leq M) の出力 C_i は辺 i に割り当てる重みとせよ(整数として出力すること)。

入力例 1

5 5
3 4 3 5 7
1 2
1 3
3 2
4 2
4 5

出力例 1

BWWBB
4
3
1
5
2

出力例のように色と重みを割り当てた場合、たとえば頂点 5 からグラフの辺を通って頂点 5 と異なる色が割り当てられた頂点に最小のコストで移動するには、 頂点 5 \to 頂点 4 \to 頂点 2 と移動すればよく、この移動のコストは 7 となるので、条件を満たします。 他の頂点についても条件を満たすことが確かめられます。


入力例 2

5 7
1 2 3 4 5
1 2
1 3
1 4
2 3
2 5
3 5
4 5

出力例 2

-1

入力例 3

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

出力例 3

BBBW
1
1
1
2
1
1

Score : 900 points

Problem Statement

We have a connected undirected graph with N vertices and M edges. Edge i in this graph (1 \leq i \leq M) connects Vertex U_i and Vertex V_i bidirectionally. We are additionally given N integers D_1, D_2, ..., D_N.

Determine whether the conditions below can be satisfied by assigning a color - white or black - to each vertex and an integer weight between 1 and 10^9 (inclusive) to each edge in this graph. If the answer is yes, find one such assignment of colors and integers, too.

  • There is at least one vertex assigned white and at least one vertex assigned black.
  • For each vertex v (1 \leq v \leq N), the following holds.
    • The minimum cost to travel from Vertex v to a vertex whose color assigned is different from that of Vertex v by traversing the edges is equal to D_v.

Here, the cost of traversing the edges is the sum of the weights of the edges traversed.

Constraints

  • 2 \leq N \leq 100,000
  • 1 \leq M \leq 200,000
  • 1 \leq D_i \leq 10^9
  • 1 \leq U_i, V_i \leq N
  • The given graph is connected and has no self-loops or multiple edges.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
D_1 D_2 ... D_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M

Output

If there is no assignment satisfying the conditions, print a single line containing -1.

If such an assignment exists, print one such assignment in the following format:

S
C_1
C_2
\vdots
C_M

Here,

  • the first line should contain the string S of length N. Its i-th character (1 \leq i \leq N) should be W if Vertex i is assigned white and B if it is assigned black.
  • The (i + 1)-th line (1 \leq i \leq M) should contain the integer weight C_i assigned to Edge i.

Sample Input 1

5 5
3 4 3 5 7
1 2
1 3
3 2
4 2
4 5

Sample Output 1

BWWBB
4
3
1
5
2

Assume that we assign the colors and integers as the sample output, and let us consider Vertex 5, for example. To travel from Vertex 5, which is assigned black, to a vertex that is assigned white with the minimum cost, we should make these moves: Vertex 5 \to Vertex 4 \to Vertex 2. The total cost of these moves is 7, which satisfies the condition. We can also verify that the condition is satisfied for other vertices.


Sample Input 2

5 7
1 2 3 4 5
1 2
1 3
1 4
2 3
2 5
3 5
4 5

Sample Output 2

-1

Sample Input 3

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

Sample Output 3

BBBW
1
1
1
2
1
1
F - Monochromization

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 1100

問題文

H \times W のマス目があり、各マスは初期状態で白または黒に塗られています。 初期状態における各マスの塗られ方を表す文字列 A_1, A_2, ..., A_H が与えられます。 これらの文字列は、各 (i, j) (1 \leq i \leq H, 1 \leq j \leq W) について、 文字列 A_ij 文字目が . ならば ij 列のマスは白に、 # ならば ij 列のマスは黒に塗られていることを表します。

このマス目の各マスの白または黒による塗られ方 (全部で 2^{HW} 個あります) であって、 初期状態から以下の操作を好きな順番で好きな回数 (0 回以上) 繰り返して得られるものの個数を 998 244 353 で割ったあまりを求めてください。

  • ある行を一つ選び、その行に含まれるすべてのマスを白く塗る。
  • ある行を一つ選び、その行に含まれるすべてのマスを黒く塗る。
  • ある列を一つ選び、その列に含まれるすべてのマスを白く塗る。
  • ある列を一つ選び、その列に含まれるすべてのマスを黒く塗る。

制約

  • 1 \leq H, W \leq 10
  • |A_i| = W (1 \leq i \leq H)
  • すべての A_i は文字 . と文字 # だけからなる。
  • H および W は整数である。

入力

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

H W
A_1
A_2
\vdots
A_H

出力

答えを出力せよ。


入力例 1

2 2
#.
.#

出力例 1

15

たとえば、2 行目を黒く塗って得られるマス目は以下のとおりです。

#.
##

入力例 2

3 3
...
...
...

出力例 2

230

入力例 3

2 4
#...
...#

出力例 3

150

入力例 4

6 7
.......
.......
.#.....
..#....
.#.#...
.......

出力例 4

203949910

Score : 1100 points

Problem Statement

We have an H \times W grid, where each square is painted white or black in the initial state. Given are strings A_1, A_2, ..., A_H representing the colors of the squares in the initial state. For each pair (i, j) (1 \leq i \leq H, 1 \leq j \leq W), if the j-th character of A_i is ., the square at the i-th row and j-th column is painted white; if that character is #, that square is painted black.

Among the 2^{HW} ways for each square in the grid to be painted white or black, how many can be obtained from the initial state by performing the operations below any number of times (possibly zero) in any order? Find this count modulo 998,244,353.

  • Choose one row, then paint all the squares in that row white.
  • Choose one row, then paint all the squares in that row black.
  • Choose one column, then paint all the squares in that column white.
  • Choose one column, then paint all the squares in that column black.

Constraints

  • 1 \leq H, W \leq 10
  • |A_i| = W (1 \leq i \leq H)
  • All strings A_i consist of . and #.
  • H and W are integers.

Input

Input is given from Standard Input in the following format:

H W
A_1
A_2
\vdots
A_H

Output

Print the answer.


Sample Input 1

2 2
#.
.#

Sample Output 1

15

For example, if we paint the second row black, the grid becomes:

#.
##

Sample Input 2

3 3
...
...
...

Sample Output 2

230

Sample Input 3

2 4
#...
...#

Sample Output 3

150

Sample Input 4

6 7
.......
.......
.#.....
..#....
.#.#...
.......

Sample Output 4

203949910