実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 266 点
問題文
高橋君は、N 個の倉庫を管理する物流センターの責任者です。各倉庫には保管できる容量の上限があります。
各倉庫 i(1 \leq i \leq N)には現在 W_i キログラムの荷物が保管されており、その倉庫の容量上限は L_i キログラムです。初期状態において W_i \leq L_i であるとは限らず、既に容量上限を超えている倉庫が存在する場合もあります。
これから M 回の荷物の搬入・搬出作業が順に行われます。j 回目(1 \leq j \leq M)の作業では、倉庫 P_j に対して C_j キログラムの荷物の変動が発生します。C_j が正の場合は搬入(荷物が増加)、C_j が負の場合は搬出(荷物が減少)、C_j = 0 の場合は変動なしを意味します。同じ倉庫に対して複数回の作業が行われることもあります。
作業の途中や終了後に、倉庫の荷物の量が容量上限を超えたり負の値になったりしても、作業は中断されずそのまま続行されます。最終的な荷物の量が負になる場合も、そのまま通常通り容量超過の判定を行います。
すべての作業が終了した後に、容量超過となっている倉庫の個数を求めてください。倉庫 i が容量超過であるとは、倉庫 i の最終的な荷物の量が L_i より真に大きいことを指します(L_i と等しい場合は容量超過ではありません)。判定は、すべての作業終了後の最終的な荷物の量のみに基づいて行います。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 0 \leq W_i \leq 10^9
- 1 \leq L_i \leq 10^9
- W_i > L_i である場合もある
- 1 \leq P_j \leq N
- -10^9 \leq C_j \leq 10^9(C_j = 0 の場合もある)
- 入力はすべて整数
入力
N M W_1 L_1 W_2 L_2 \vdots W_N L_N P_1 C_1 P_2 C_2 \vdots P_M C_M
- 1 行目には、倉庫の個数を表す整数 N と、作業の回数を表す整数 M が、スペース区切りで与えられる。
- 2 行目から N + 1 行目では、各倉庫の情報が与えられる。
- 1 + i 行目では、倉庫 i の現在の荷物量 W_i と容量上限 L_i が、スペース区切りで与えられる。
- N + 2 行目から N + M + 1 行目では、各作業の情報が与えられる(M = 0 の場合、この部分は存在しない)。
- N + 1 + j 行目では、j 回目の作業対象の倉庫番号 P_j と、荷物の変動量 C_j が、スペース区切りで与えられる。
出力
すべての作業終了後に容量超過となっている倉庫の個数を 1 行で出力せよ。
入力例 1
3 4 50 100 80 100 30 50 1 30 2 25 3 15 2 10
出力例 1
1
入力例 2
5 6 100 200 150 150 0 100 200 300 50 80 2 10 3 50 5 20 1 150 3 60 5 15
出力例 2
4
入力例 3
10 12 500000000 1000000000 999999999 1000000000 0 100 750000000 800000000 100 100 0 1000000000 123456789 500000000 999999998 999999999 50 50 1000000000 1000000000 1 500000001 2 2 4 100000000 5 1 9 1 7 400000000 3 -50 8 5 6 1000000000 10 1 3 200 4 -50000000
出力例 3
8
Score : 266 pts
Problem Statement
Takahashi is the manager of a logistics center that oversees N warehouses. Each warehouse has an upper limit on its storage capacity.
Each warehouse i (1 \leq i \leq N) currently stores W_i kilograms of goods, and its capacity limit is L_i kilograms. It is not necessarily the case that W_i \leq L_i in the initial state; there may be warehouses that already exceed their capacity limit.
A total of M loading/unloading operations will be performed in order. In the j-th operation (1 \leq j \leq M), a change of C_j kilograms occurs at warehouse P_j. If C_j is positive, it represents loading (goods increase); if C_j is negative, it represents unloading (goods decrease); if C_j = 0, it means no change. Multiple operations may be performed on the same warehouse.
Even if the amount of goods in a warehouse exceeds its capacity limit or becomes negative during or after the operations, the operations continue without interruption. Even if the final amount of goods is negative, the overcapacity check is performed as usual.
After all operations are completed, determine the number of warehouses that are over capacity. Warehouse i is over capacity if the final amount of goods in warehouse i is strictly greater than L_i (if it equals L_i, it is not over capacity). The determination is based solely on the final amount of goods after all operations are completed.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 0 \leq W_i \leq 10^9
- 1 \leq L_i \leq 10^9
- It is possible that W_i > L_i
- 1 \leq P_j \leq N
- -10^9 \leq C_j \leq 10^9 (including the case C_j = 0)
- All input values are integers
Input
N M W_1 L_1 W_2 L_2 \vdots W_N L_N P_1 C_1 P_2 C_2 \vdots P_M C_M
- The first line contains two space-separated integers: N, the number of warehouses, and M, the number of operations.
- Lines 2 through N + 1 give the information for each warehouse.
- Line 1 + i contains the current amount of goods W_i and the capacity limit L_i of warehouse i, separated by a space.
- Lines N + 2 through N + M + 1 give the information for each operation (this part does not exist if M = 0).
- Line N + 1 + j contains the target warehouse number P_j and the change in goods C_j for the j-th operation, separated by a space.
Output
Print the number of warehouses that are over capacity after all operations are completed, on a single line.
Sample Input 1
3 4 50 100 80 100 30 50 1 30 2 25 3 15 2 10
Sample Output 1
1
Sample Input 2
5 6 100 200 150 150 0 100 200 300 50 80 2 10 3 50 5 20 1 150 3 60 5 15
Sample Output 2
4
Sample Input 3
10 12 500000000 1000000000 999999999 1000000000 0 100 750000000 800000000 100 100 0 1000000000 123456789 500000000 999999998 999999999 50 50 1000000000 1000000000 1 500000001 2 2 4 100000000 5 1 9 1 7 400000000 3 -50 8 5 6 1000000000 10 1 3 200 4 -50000000
Sample Output 3
8
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君は買い物リストに書かれた N 個の商品を、それぞれ 1 個ずつすべて購入したいと考えています。
買い物リストの i 番目 (1 \leq i \leq N) の商品のカテゴリは T_i です。なお、買い物リストには同じカテゴリの商品が複数含まれることもあります。
商品を購入できるお店は全部で M 店あります。j 番目 (1 \leq j \leq M) のお店はカテゴリ S_j の商品のみを取り扱っており、1 個あたり C_j 円で販売しています。同じカテゴリを取り扱うお店が複数存在することもあります。また、各お店では在庫の制限なく何個でも購入できます。
高橋君は買い物リストの商品 1 つ 1 つについて、その商品のカテゴリを取り扱っているお店の中から購入先を 1 つ選びます。同じカテゴリの商品であっても、商品ごとに異なるお店を選んで構いませんし、同じお店を繰り返し選んでも構いません。
買い物リストに含まれるカテゴリのうち、どのお店でも取り扱っていないものが 1 つでもある場合、すべての商品を購入することは不可能です。
すべての商品を購入できるかどうかを判定し、可能ならば合計購入金額の最小値を出力してください。ここで合計購入金額とは、買い物リストの N 個の商品それぞれについて選んだお店の価格を合計したものです。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq T_i \leq 10^9
- 1 \leq S_j \leq 10^9
- 1 \leq C_j \leq 10^9
- 入力はすべて整数である。
- 答えは 2 \times 10^{14} 以下であることが保証される(購入可能な場合)。
入力
N M T_1 T_2 \ldots T_N S_1 C_1 S_2 C_2 \vdots S_M C_M
- 1 行目には、買い物リストの商品の数 N と、お店の数 M が、スペース区切りで与えられる。
- 2 行目には、各商品のカテゴリを表す T_1, T_2, \ldots, T_N が、スペース区切りで与えられる。
- 続く M 行にわたって、各お店の情報が与えられる。
- 2 + j 行目 (1 \leq j \leq M) には、j 番目のお店が取り扱うカテゴリ S_j と、1 個あたりの価格 C_j が、スペース区切りで与えられる。
出力
すべての商品を購入できる場合は、合計購入金額の最小値を 1 行で出力せよ。すべての商品を購入することが不可能な場合は -1 を 1 行で出力せよ。
入力例 1
3 3 1 2 1 1 100 1 150 2 200
出力例 1
400
入力例 2
3 2 1 2 3 1 100 2 200
出力例 2
-1
入力例 3
8 7 5 1000000000 5 3 1000000000 3 5 3 5 500 5 300 1000000000 1000000000 1000000000 999999999 3 700 3 600 3 800
出力例 3
2000002698
Score : 300 pts
Problem Statement
Takahashi wants to purchase all N items written on his shopping list, buying exactly 1 of each.
The i-th item (1 \leq i \leq N) on the shopping list belongs to category T_i. Note that the shopping list may contain multiple items of the same category.
There are M shops in total where items can be purchased. The j-th shop (1 \leq j \leq M) sells only items of category S_j, at a price of C_j yen per item. Multiple shops may sell items of the same category. Each shop has unlimited stock, so any number of items can be purchased from it.
For each item on the shopping list, Takahashi chooses exactly one shop that handles that item's category to purchase it from. Even for items of the same category, he may choose a different shop for each item, or he may choose the same shop repeatedly.
If there exists even one category on the shopping list that is not handled by any shop, it is impossible to purchase all items.
Determine whether it is possible to purchase all items, and if so, output the minimum total purchase cost. Here, the total purchase cost is the sum of the prices at the chosen shops for each of the N items on the shopping list.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq T_i \leq 10^9
- 1 \leq S_j \leq 10^9
- 1 \leq C_j \leq 10^9
- All inputs are integers.
- It is guaranteed that the answer is at most 2 \times 10^{14} (when purchasing is possible).
Input
N M T_1 T_2 \ldots T_N S_1 C_1 S_2 C_2 \vdots S_M C_M
- The first line contains the number of items on the shopping list N and the number of shops M, separated by a space.
- The second line contains T_1, T_2, \ldots, T_N, representing the category of each item, separated by spaces.
- The following M lines contain information about each shop.
- The (2 + j)-th line (1 \leq j \leq M) contains the category S_j handled by the j-th shop and the price per item C_j, separated by a space.
Output
If it is possible to purchase all items, output the minimum total purchase cost on a single line. If it is impossible to purchase all items, output -1 on a single line.
Sample Input 1
3 3 1 2 1 1 100 1 150 2 200
Sample Output 1
400
Sample Input 2
3 2 1 2 3 1 100 2 200
Sample Output 2
-1
Sample Input 3
8 7 5 1000000000 5 3 1000000000 3 5 3 5 500 5 300 1000000000 1000000000 1000000000 999999999 3 700 3 600 3 800
Sample Output 3
2000002698
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 366 点
問題文
N \times N のグリッド上の迷路が与えられます。上から i 行目・左から j 列目のマスを (i, j) と表します。各マスには .、#、S、G のいずれかが書かれています。. は通行可能な空きマス、# は壁(通行不可能なマス)、S はスタート地点、G はゴール地点を表します。S と G のマスも通行可能です。
高橋君はスタート地点 S からゴール地点 G まで移動したいです。高橋君は現在いるマスから上下左右に隣接するマスへ 1 手で移動できます。ただし、グリッドの外や壁のマスへは移動できません。
スタート地点からゴール地点までの経路上のマスの数(スタート地点とゴール地点を含む)の最小値を求めてください。
制約
- 2 \leq N \leq 500
- N は整数
- グリッドの i 行目を表す文字列 c_i (1 \leq i \leq N) は
.、#、S、Gからなる長さ N の文字列である Sはグリッド全体でちょうど 1 つ存在するGはグリッド全体でちょうど 1 つ存在する- スタート地点
Sからゴール地点Gへ到達可能であることが保証される
入力
N c_1 c_2 \vdots c_N
- 1 行目には、グリッドの一辺の大きさを表す整数 N が与えられる。
- 続く N 行のうち i 行目 (1 \leq i \leq N) には、グリッドの上から i 行目の各マスの情報を表す長さ N の文字列 c_i が与えられる。c_i の j 文字目はマス (i, j) の情報を表す。
出力
スタート地点 S からゴール地点 G まで移動するときに通るマスの数(S と G を含む)の最小値を 1 行で出力せよ。
入力例 1
4 S... .#.. ..#G ....
出力例 1
6
入力例 2
3 S#. .#G ...
出力例 2
6
入力例 3
8 S..#.... ##.#.##. ...#...# .#####.# .....#.G .###.#.. ...#.... .#......
出力例 3
20
入力例 4
15 S.............. .#############. .............#. .###########.#. .#.........#.#. .#.#######.#.#. .#.#.....#.#.#. .#.#.###.#.#.#. .#.#.#G#.#.#.#. .#.#.#...#.#.#. .#.#.#####.#.#. .#.#.......#.#. .#.#########.#. .#...........#. ...............
出力例 4
63
入力例 5
2 SG ..
出力例 5
2
Score : 366 pts
Problem Statement
You are given a maze on an N \times N grid. The cell at the i-th row from the top and the j-th column from the left is denoted as (i, j). Each cell contains one of the following characters: ., #, S, or G. . represents a passable empty cell, # represents a wall (an impassable cell), S represents the start position, and G represents the goal position. The cells S and G are also passable.
Takahashi wants to move from the start position S to the goal position G. From his current cell, Takahashi can move in one step to an adjacent cell in one of the four directions (up, down, left, right). However, he cannot move outside the grid or into a wall cell.
Find the minimum number of cells on the path from the start position to the goal position (including the start position and the goal position).
Constraints
- 2 \leq N \leq 500
- N is an integer
- The string c_i (1 \leq i \leq N) representing the i-th row of the grid is a string of length N consisting of
.,#,S, andG - Exactly one
Sexists in the entire grid - Exactly one
Gexists in the entire grid - It is guaranteed that the goal position
Gis reachable from the start positionS
Input
N c_1 c_2 \vdots c_N
- The first line contains an integer N representing the side length of the grid.
- Over the following N lines, the i-th line (1 \leq i \leq N) contains a string c_i of length N representing the information of each cell in the i-th row from the top of the grid. The j-th character of c_i represents the information of cell (i, j).
Output
Print in one line the minimum number of cells visited (including S and G) when moving from the start position S to the goal position G.
Sample Input 1
4 S... .#.. ..#G ....
Sample Output 1
6
Sample Input 2
3 S#. .#G ...
Sample Output 2
6
Sample Input 3
8 S..#.... ##.#.##. ...#...# .#####.# .....#.G .###.#.. ...#.... .#......
Sample Output 3
20
Sample Input 4
15 S.............. .#############. .............#. .###########.#. .#.........#.#. .#.#######.#.#. .#.#.....#.#.#. .#.#.###.#.#.#. .#.#.#G#.#.#.#. .#.#.#...#.#.#. .#.#.#####.#.#. .#.#.......#.#. .#.#########.#. .#...........#. ...............
Sample Output 4
63
Sample Input 5
2 SG ..
Sample Output 5
2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
ある美術館には、長さ N の白黒タイルの並びが展示された部屋がいくつかあります。各部屋の展示は長さ N の 01 文字列で表され、0 は白タイル、1 は黒タイルを意味します。ただし、同じ文字が 3 個以上連続して現れる文字列は美観上の理由で採用されておらず、そのような部屋は存在しません。
条件を満たすすべての文字列を辞書順で小さい順に並べ、先頭から順に部屋番号 1, 2, 3, \ldots を付けます。ただし、辞書順では 0 は 1 より小さいものとします。
2 つの部屋の展示がちょうど 1 箇所だけ異なるとき(すなわち、一方の文字列のある 1 文字を反転すると他方の文字列になるとき)、その 2 つの部屋は通路で直接つながっています。通路は双方向に通行でき、つながっている 2 部屋のどちらからでも他方へ移動できます。なお、1 文字を反転した結果が条件を満たさない(同じ文字が 3 個以上連続する)場合、その文字列に対応する部屋は存在しないため、通路もありません。
高橋君は、文字列 s に対応する部屋から巡回を開始します。出発した部屋は巡回開始の時点で訪問済みとなります。その後、次の操作を繰り返します。
- 現在いる部屋に通路で直接つながっている未訪問の部屋が 1 つ以上存在するなら、その中で部屋番号が最も小さい部屋へ移動し、その部屋を訪問済みにする。
- そのような部屋が存在しないなら、巡回を終了する。それ以前に通った部屋へ戻って探索を続けることはしない。
文字列 t に対応する部屋を初めて訪問するのが何回目の移動後かを求めてください。出発時点を 0 回目の移動後と数えます(s = t の場合は 0 を出力します)。文字列 t に対応する部屋を訪問しないまま巡回が終了する場合は -1 を出力してください。
制約
- 1 \leq N \leq 26
- s は長さ N の
0と1からなる文字列である - t は長さ N の
0と1からなる文字列である - s には同じ文字が 3 個以上連続して現れない
- t には同じ文字が 3 個以上連続して現れない
入力
N s t
- 1 行目には、タイルの並びの長さを表す整数 N が与えられる。
- 2 行目には、出発する部屋に対応する文字列 s が与えられる。
- 3 行目には、目標の部屋に対応する文字列 t が与えられる。
出力
文字列 t に対応する部屋を初めて訪問するのが何回目の移動後かを整数で出力せよ。訪問しない場合は -1 を出力せよ。
入力例 1
3 001 100
出力例 1
4
入力例 2
4 0010 1010
出力例 2
-1
入力例 3
12 010110010110 110100110100
出力例 3
-1
入力例 4
26 00110011001100110011001100 01010101010101010101010101
出力例 4
-1
入力例 5
1 0 0
出力例 5
0
Score : 400 pts
Problem Statement
A certain museum has several rooms, each displaying a sequence of black and white tiles of length N. The display in each room is represented by a binary string of length N, where 0 means a white tile and 1 means a black tile. However, strings in which the same character appears 3 or more times consecutively are not adopted for aesthetic reasons, and no such rooms exist.
All strings satisfying the condition are sorted in lexicographic order, and room numbers 1, 2, 3, \ldots are assigned from the beginning. In lexicographic order, 0 is considered smaller than 1.
When the displays of two rooms differ in exactly 1 position (that is, flipping one character of one string yields the other string), those two rooms are directly connected by a corridor. Corridors are bidirectional, and one can move from either of the two connected rooms to the other. Note that if flipping one character results in a string that does not satisfy the condition (the same character appears 3 or more times consecutively), the room corresponding to that string does not exist, so no corridor exists either.
Takahashi begins his tour from the room corresponding to string s. The starting room is considered visited at the beginning of the tour. After that, he repeats the following operation:
- If there exists one or more unvisited rooms directly connected to the current room by a corridor, move to the room with the smallest room number among them, and mark that room as visited.
- If no such room exists, end the tour. He does not backtrack to previously visited rooms to continue exploration.
Determine after how many moves the room corresponding to string t is first visited. The starting point counts as after 0 moves (if s = t, output 0). If the tour ends without visiting the room corresponding to string t, output -1.
Constraints
- 1 \leq N \leq 26
- s is a string of length N consisting of
0and1 - t is a string of length N consisting of
0and1 - s does not contain the same character appearing 3 or more times consecutively
- t does not contain the same character appearing 3 or more times consecutively
Input
N s t
- The first line contains an integer N representing the length of the tile sequence.
- The second line contains the string s corresponding to the starting room.
- The third line contains the string t corresponding to the target room.
Output
Output the number of moves after which the room corresponding to string t is first visited, as an integer. If it is not visited, output -1.
Sample Input 1
3 001 100
Sample Output 1
4
Sample Input 2
4 0010 1010
Sample Output 2
-1
Sample Input 3
12 010110010110 110100110100
Sample Output 3
-1
Sample Input 4
26 00110011001100110011001100 01010101010101010101010101
Sample Output 4
-1
Sample Input 5
1 0 0
Sample Output 5
0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 466 点
問題文
高橋君は、N 人のエージェントにパスワードを送信して認証を行おうとしています。
パスワードは、1 以上 K 以下の整数からなる長さ L の列 S = (S_1, S_2, \dots, S_L) です。
高橋君の手元にはかすれたメモがあり、長さ L の列 B = (B_1, B_2, \dots, B_L) として読み取れます。ここで、
- B_j = 0 のとき、その位置の値は読み取れなかったことを意味し、S_j を 1 以上 K 以下の好きな整数に定めることができます。
- B_j \neq 0 のとき、S_j = B_j でなければなりません。
高橋君は送信を始める前に、すべての未確定の位置を埋めてパスワード S を完成させます。各位置は独立に 1 以上 K 以下の整数を選べます(異なる位置に同じ値を使ってもかまいません)。一度完成させた S は、その後変更できません。
送信には 1 台の暗号変換装置を使います。この装置は変換規則として \{1, 2, \dots, K\} 上の置換 C = (C_1, C_2, \dots, C_K) を保持しています。はじめ、C は恒等置換、すなわちすべての x(1 \leq x \leq K)について C_x = x です。
高橋君はパスワードを完成させた後、N 人全員に、自分で決めた順番でちょうど 1 回ずつ送信を行います。あるエージェント i への送信では、以下の処理がこの順に行われます。
- 送信と認証判定:装置の現在の変換規則 C を用いて、パスワード S の各要素が暗号化されて送られます。具体的には、エージェント i が受信する列は (C_{S_1}, C_{S_2}, \dots, C_{S_L}) です。この列がエージェント i の持つ認証コード T_i = (T_{i,1}, T_{i,2}, \dots, T_{i,L}) と完全に一致した場合、エージェント i の認証は成功します。
- 変換規則の更新:認証の成否にかかわらず、エージェント i が持つ置換 A_i = (A_{i,1}, A_{i,2}, \dots, A_{i,K}) を用いて、装置の変換規則が更新されます。新しい変換規則 C' は、すべての x(1 \leq x \leq K)について
C'_x = A_{i, C_x}
で定まります(すなわち、合成 A_i \circ C に置き換わります)。
高橋君は、パスワード S の完成のさせ方と、N 人への送信順の両方を自由に選べます。
これらを最適に選んだとき、認証が成功するエージェントの人数の最大値を求めてください。さらに、その最大値を達成できる完成済みのパスワード S のうち、辞書順で最小のものを求めてください。ここで「最大値を達成できる」とは、ある送信順が存在して、その送信順のもとで認証が成功するエージェントの人数が最大値に等しくなることを意味します。
長さ L の列 U と V について、U \neq V であるとき、最初に異なる位置を j として U_j < V_j ならば U は V より辞書順で小さいとします。
制約
- 1 \leq N \leq 8
- 1 \leq K \leq 8
- 1 \leq L \leq 200
- 0 \leq B_j \leq K(1 \leq j \leq L)
- 1 \leq T_{i,j} \leq K(1 \leq i \leq N, 1 \leq j \leq L)
- 1 \leq A_{i,k} \leq K(1 \leq i \leq N, 1 \leq k \leq K)
- A_i は \{1, 2, \dots, K\} の置換である(1 から K までの各整数をちょうど 1 回ずつ含む)
- 入力はすべて整数である
入力
入力は以下の形式で標準入力から与えられる。
N K L
B_1 B_2 \dots B_L
T_{1,1} T_{1,2} \dots T_{1,L}
A_{1,1} A_{1,2} \dots A_{1,K}
T_{2,1} T_{2,2} \dots T_{2,L}
A_{2,1} A_{2,2} \dots A_{2,K}
\vdots
T_{N,1} T_{N,2} \dots T_{N,L}
A_{N,1} A_{N,2} \dots A_{N,K}
- 1 行目には、エージェントの人数 N、値の種類数 K、パスワードの長さ L がスペース区切りで与えられる。
- 2 行目には、かすれたメモを表す列 B がスペース区切りで与えられる。
- 各 i(1 \leq i \leq N)について、続く 2 行でエージェント i の情報が与えられる。
- 2i + 1 行目には、エージェント i の認証コード T_i がスペース区切りで与えられる。
- 2i + 2 行目には、エージェント i の持つ置換 A_i がスペース区切りで与えられる。
出力
1 行目に、認証が成功するエージェントの人数の最大値を整数で出力してください。
2 行目に、その最大値を達成できる完成済みのパスワード S のうち辞書順で最小のものを、L 個の整数としてスペース区切りで出力してください。
入力例 1
2 3 4 0 2 0 1 1 2 3 1 2 1 3 2 1 3 2 2 3 1
出力例 1
2 1 2 3 1
入力例 2
3 2 5 0 0 1 0 2 1 1 1 2 2 2 1 2 2 1 1 2 1 2 1 2 2 1 1 2 1
出力例 2
1 1 1 1 2 2
入力例 3
5 5 12 0 3 0 0 5 1 0 2 0 0 4 0 1 3 2 4 5 1 2 2 3 4 4 5 2 3 4 5 1 2 1 1 5 4 3 3 2 5 1 4 2 1 3 5 2 4 5 3 5 1 5 1 4 2 4 3 4 1 5 4 3 2 1 3 4 2 2 1 5 1 3 2 5 5 4 3 1 2 5 4 4 5 3 4 2 2 5 1 1 2 3 3 4 5 1 3 2
出力例 3
1 1 3 2 4 5 1 2 2 3 4 4 5
入力例 4
8 8 40 0 2 0 4 0 6 0 8 1 0 3 0 5 0 7 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 0 2 4 6 8 0 0 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 2 3 4 5 6 7 8 1 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 1 2 3 4 5 6 7 8 1 3 5 7 2 4 6 8 2 4 6 8 1 3 5 7 2 4 6 8 1 3 5 7 2 4 6 8 1 3 5 7 2 4 6 8 1 3 5 7 2 4 6 8 1 3 5 7 4 1 2 3 8 5 6 7 5 1 6 2 7 3 8 4 5 1 6 2 7 3 8 4 5 1 6 2 7 3 8 4 5 1 6 2 7 3 8 4 5 1 6 2 7 3 8 4 5 6 7 8 1 2 3 4 1 2 1 4 2 6 3 8 1 4 3 6 5 8 7 2 4 1 6 2 8 3 1 4 2 5 3 6 4 7 5 8 6 1 2 4 6 8 7 5 2 1 4 3 6 5 8 7 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 8 3 4 1 2 7 8 5 6 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 1 1 1 1 2 2 2 2 3 4 5 6 7 8 1 2 1 2 3 4 5 6 7 8
出力例 4
1 1 2 1 4 2 6 3 8 1 4 3 6 5 8 7 2 4 1 6 2 8 3 1 4 2 5 3 6 4 7 5 8 6 1 2 4 6 8 7 5
入力例 5
1 1 1 0 1 1
出力例 5
1 1
Score : 466 pts
Problem Statement
Takahashi is trying to authenticate by sending a password to N agents.
The password is a sequence of length L consisting of integers between 1 and K inclusive: S = (S_1, S_2, \dots, S_L).
Takahashi has a faded memo, which can be read as a sequence of length L: B = (B_1, B_2, \dots, B_L). Here,
- If B_j = 0, it means the value at that position could not be read, and S_j can be set to any integer between 1 and K inclusive.
- If B_j \neq 0, then S_j = B_j must hold.
Before starting the transmission, Takahashi fills in all undetermined positions to complete the password S. Each position can independently be assigned any integer between 1 and K inclusive (the same value may be used at different positions). Once completed, S cannot be changed afterwards.
For transmission, a single cipher device is used. This device holds a conversion rule as a permutation C = (C_1, C_2, \dots, C_K) on \{1, 2, \dots, K\}. Initially, C is the identity permutation, i.e., C_x = x for all x (1 \leq x \leq K).
After completing the password, Takahashi transmits it to all N agents exactly once each, in an order he chooses. When transmitting to agent i, the following operations are performed in this order:
- Transmission and authentication check: Using the device's current conversion rule C, each element of the password S is encrypted and sent. Specifically, the sequence received by agent i is (C_{S_1}, C_{S_2}, \dots, C_{S_L}). If this sequence exactly matches agent i's authentication code T_i = (T_{i,1}, T_{i,2}, \dots, T_{i,L}), then agent i's authentication succeeds.
- Conversion rule update: Regardless of whether authentication succeeded or not, the device's conversion rule is updated using agent i's permutation A_i = (A_{i,1}, A_{i,2}, \dots, A_{i,K}). The new conversion rule C' is defined for all x (1 \leq x \leq K) by
C'_x = A_{i, C_x}
(i.e., it is replaced by the composition A_i \circ C).
Takahashi is free to choose both how to complete the password S and the order of transmission to the N agents.
Find the maximum number of agents whose authentication can succeed when both choices are made optimally. Furthermore, among all completed passwords S that achieve this maximum, find the lexicographically smallest one. Here, "achieves the maximum" means that there exists some transmission order under which the number of agents whose authentication succeeds equals the maximum.
For two sequences U and V of length L, if U \neq V, let j be the first position where they differ; then U is lexicographically smaller than V if U_j < V_j.
Constraints
- 1 \leq N \leq 8
- 1 \leq K \leq 8
- 1 \leq L \leq 200
- 0 \leq B_j \leq K (1 \leq j \leq L)
- 1 \leq T_{i,j} \leq K (1 \leq i \leq N, 1 \leq j \leq L)
- 1 \leq A_{i,k} \leq K (1 \leq i \leq N, 1 \leq k \leq K)
- A_i is a permutation of \{1, 2, \dots, K\} (contains each integer from 1 to K exactly once)
- All input values are integers
Input
Input is given from standard input in the following format.
N K L
B_1 B_2 \dots B_L
T_{1,1} T_{1,2} \dots T_{1,L}
A_{1,1} A_{1,2} \dots A_{1,K}
T_{2,1} T_{2,2} \dots T_{2,L}
A_{2,1} A_{2,2} \dots A_{2,K}
\vdots
T_{N,1} T_{N,2} \dots T_{N,L}
A_{N,1} A_{N,2} \dots A_{N,K}
- The first line contains the number of agents N, the number of value types K, and the password length L, separated by spaces.
- The second line contains the sequence B representing the faded memo, separated by spaces.
- For each i (1 \leq i \leq N), the following two lines give information about agent i:
- Line 2i + 1 contains agent i's authentication code T_i, separated by spaces.
- Line 2i + 2 contains agent i's permutation A_i, separated by spaces.
Output
On the first line, output the maximum number of agents whose authentication succeeds, as an integer.
On the second line, output the lexicographically smallest completed password S that achieves this maximum, as L integers separated by spaces.
Sample Input 1
2 3 4 0 2 0 1 1 2 3 1 2 1 3 2 1 3 2 2 3 1
Sample Output 1
2 1 2 3 1
Sample Input 2
3 2 5 0 0 1 0 2 1 1 1 2 2 2 1 2 2 1 1 2 1 2 1 2 2 1 1 2 1
Sample Output 2
1 1 1 1 2 2
Sample Input 3
5 5 12 0 3 0 0 5 1 0 2 0 0 4 0 1 3 2 4 5 1 2 2 3 4 4 5 2 3 4 5 1 2 1 1 5 4 3 3 2 5 1 4 2 1 3 5 2 4 5 3 5 1 5 1 4 2 4 3 4 1 5 4 3 2 1 3 4 2 2 1 5 1 3 2 5 5 4 3 1 2 5 4 4 5 3 4 2 2 5 1 1 2 3 3 4 5 1 3 2
Sample Output 3
1 1 3 2 4 5 1 2 2 3 4 4 5
Sample Input 4
8 8 40 0 2 0 4 0 6 0 8 1 0 3 0 5 0 7 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 0 2 4 6 8 0 0 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 2 3 4 5 6 7 8 1 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 1 2 3 4 5 6 7 8 1 3 5 7 2 4 6 8 2 4 6 8 1 3 5 7 2 4 6 8 1 3 5 7 2 4 6 8 1 3 5 7 2 4 6 8 1 3 5 7 2 4 6 8 1 3 5 7 4 1 2 3 8 5 6 7 5 1 6 2 7 3 8 4 5 1 6 2 7 3 8 4 5 1 6 2 7 3 8 4 5 1 6 2 7 3 8 4 5 1 6 2 7 3 8 4 5 6 7 8 1 2 3 4 1 2 1 4 2 6 3 8 1 4 3 6 5 8 7 2 4 1 6 2 8 3 1 4 2 5 3 6 4 7 5 8 6 1 2 4 6 8 7 5 2 1 4 3 6 5 8 7 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 8 3 4 1 2 7 8 5 6 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 1 1 1 1 2 2 2 2 3 4 5 6 7 8 1 2 1 2 3 4 5 6 7 8
Sample Output 4
1 1 2 1 4 2 6 3 8 1 4 3 6 5 8 7 2 4 1 6 2 8 3 1 4 2 5 3 6 4 7 5 8 6 1 2 4 6 8 7 5
Sample Input 5
1 1 1 0 1 1
Sample Output 5
1 1