Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
N 個の相異なる文字列 S_1, \dots, S_N が与えられます。これらの文字列はいずれも、0, 1 のみからなる長さ M の文字列です。
0, 1 のみからなる長さ M の文字列 T のうち、以下の条件を満たすものが存在するか判定し、あれば一例を構築してください。
- どの文字列 S_i も、T と少なくとも 1 箇所で文字が一致する。
- より厳密には、任意の整数 i (1 \le i \le N) に対して、ある整数 x_i (1 \le x_i \le M) が存在し、S_i の x_i 文字目と T の x_i 文字目が一致する。
制約
- N,M は整数
- 1 \leq N \leq 2 \times 10^4
- 1 \leq M \leq 100
- S_i は
0,1のみからなる長さ M の文字列 - S_1, \dots, S_N は相異なる
入力
入力は以下の形式で標準入力から与えられる。
N M S_1 \vdots S_N
出力
問題文中の条件を満たす T が存在しないならば、No を出力せよ。
問題文中の条件を満たす T が存在するならば、以下の形式で出力せよ。
Yes T
条件を満たす T が複数存在する場合、どれを出力しても正解と判定される。
入力例 1
5 3 000 111 110 100 011
出力例 1
Yes 101
T を 101 とすれば、以下が成り立ちます。
- S_1 の 2 文字目と T の 2 文字目は一致する。
- S_2 の 1 文字目と T の 1 文字目は一致する。
- S_3 の 1 文字目と T の 1 文字目は一致する。
- S_4 の 2 文字目と T の 2 文字目は一致する。
- S_5 の 3 文字目と T の 3 文字目は一致する。
入力例 2
4 2 00 01 10 11
出力例 2
No
条件を満たす T は存在しません。
入力例 3
9 50 00001000011111100011111000011111000001000001111100 00010100010000010100000100100000100011000010000010 00100010010000010100000000000000100101000010000010 01000001011111100100000000011111000001000001111110 01111111010001000100000000100000000001000000000010 01000001010000100100000100100000000001000010000010 01000001010000010011111000111111100111110001111100 00000000000000000000000000000000000000000000000000 11111111111111111111111111111111111111111111111111
出力例 3
Yes 10101010101010101010101011010101011010101101010101
Score : 400 points
Problem Statement
You are given N distinct strings S_1, \dots, S_N. Each of these strings is a string of length M consisting of 0 and 1.
Determine whether there exists a string T of length M consisting of 0 and 1 satisfying the following condition, and if so, construct one example.
- Every string S_i matches T in at least one position.
- More formally, for every integer i (1 \le i \le N), there exists an integer x_i (1 \le x_i \le M) such that the x_i-th character of S_i and the x_i-th character of T are the same.
Constraints
- N and M are integers.
- 1 \leq N \leq 2 \times 10^4
- 1 \leq M \leq 100
- S_i is a string of length M consisting of
0and1. - S_1, \dots, S_N are distinct.
Input
The input is given from Standard Input in the following format:
N M S_1 \vdots S_N
Output
If no T satisfying the condition in the problem statement exists, output No.
If a T satisfying the condition in the problem statement exists, output in the following format:
Yes T
If multiple T satisfying the condition exist, any of them will be accepted.
Sample Input 1
5 3 000 111 110 100 011
Sample Output 1
Yes 101
If we set T to 101, the following holds:
- The 2nd character of S_1 and the 2nd character of T are the same.
- The 1st character of S_2 and the 1st character of T are the same.
- The 1st character of S_3 and the 1st character of T are the same.
- The 2nd character of S_4 and the 2nd character of T are the same.
- The 3rd character of S_5 and the 3rd character of T are the same.
Sample Input 2
4 2 00 01 10 11
Sample Output 2
No
No T satisfies the condition.
Sample Input 3
9 50 00001000011111100011111000011111000001000001111100 00010100010000010100000100100000100011000010000010 00100010010000010100000000000000100101000010000010 01000001011111100100000000011111000001000001111110 01111111010001000100000000100000000001000000000010 01000001010000100100000100100000000001000010000010 01000001010000010011111000111111100111110001111100 00000000000000000000000000000000000000000000000000 11111111111111111111111111111111111111111111111111
Sample Output 3
Yes 10101010101010101010101011010101011010101101010101
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
整数 N と (1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N) が与えられます。
(1,2,\ldots,N) の順列 Q=(Q_1,Q_2,\ldots,Q_N) に対して、以下の操作をちょうど 1 回行うことにより得られる順列の中で辞書順最小の順列を Q'=(Q_1',Q_2',\ldots,Q_N') とします:
- 1\le l\le r\le N を満たす整数の組 (l,r) を選び、Q_l,Q_{l+1},\ldots,Q_r を反転させる。より厳密には、Q を (Q_1,Q_2,\ldots,Q_{l-1} , Q_r,Q_{r-1},\ldots,Q_l,Q_{r+1},Q_{r+2},\ldots,Q_N) で置き換える。
Q'=P となる (1,2,\ldots,N) の順列 Q の個数を 998244353 で割ったあまりを求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T
- 1\le N\le 5\times 10^5
- P は (1,2,\ldots,N) の順列
- 全てのテストケースにおける N の総和は 5\times 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N P_1 P_2 \ldots P_N
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
入力例 1
4 3 1 3 2 1 1 4 4 3 2 1 6 1 2 6 4 5 3
出力例 1
2 1 0 9
1 番目のテストケースについて考えます。
例えば Q=(2,3,1) の場合、(l,r)=(1,3) を選ぶことで (1,3,2) が得られます。(1,3,2) より辞書順で小さい順列は得られないので、Q'=(1,3,2) となります。したがって、Q=(2,3,1) は Q'=P を満たします。
Q'=P となる Q は Q=(2,3,1),(3,1,2) の 2 個です。
Score : 400 points
Problem Statement
You are given an integer N and a permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N).
For a permutation Q=(Q_1,Q_2,\ldots,Q_N) of (1,2,\ldots,N), let Q'=(Q_1',Q_2',\ldots,Q_N') be the lexicographically smallest permutation obtainable by performing the following operation exactly once:
- Choose a pair of integers (l,r) satisfying 1\le l\le r\le N, and reverse Q_l,Q_{l+1},\ldots,Q_r. More precisely, replace Q with (Q_1,Q_2,\ldots,Q_{l-1},Q_r,Q_{r-1},\ldots,Q_l,Q_{r+1},Q_{r+2},\ldots,Q_N).
Find the number, modulo 998244353, of permutations Q of (1,2,\ldots,N) such that Q'=P.
You are given T test cases; solve each of them.
Constraints
- 1\le T
- 1\le N\le 5\times 10^5
- P is a permutation of (1,2,\ldots,N).
- The sum of N over all test cases is at most 5\times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N P_1 P_2 \ldots P_N
Output
Output the answers for the test cases in order, separated by newlines.
Sample Input 1
4 3 1 3 2 1 1 4 4 3 2 1 6 1 2 6 4 5 3
Sample Output 1
2 1 0 9
Consider the first test case.
For example, when Q=(2,3,1), choosing (l,r)=(1,3) gives (1,3,2). No permutation lexicographically smaller than (1,3,2) can be obtained, so we have Q'=(1,3,2). Thus, Q=(2,3,1) satisfies Q'=P.
There are two permutations Q such that Q'=P: Q=(2,3,1),(3,1,2).
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
注:この問題は G 問題とほとんど同じ設定です。問題文中の相違点を赤い太字で示します。
H 行 W 列のグリッドで表される地下アパートがあります。上から i 行目・左から j 列目のマスをマス (i,j) と表記します。
地下アパートの出入り口はマス (1,1) にあります。
地下アパートにはドアが N 個あります。k 個目のドアはマス (A_k, B_k) にあります。
地下アパート内で、セールスマンは以下の 2 種類の移動を好きな順番で何回でも行えます。
- 現在いるマスから左か右に隣接するマスに徒歩で移動できる。この移動はコストを 1 消費する。
- 現在いるマスが 1 列目または W 列目の場合、そこから上か下に隣接するマスにエレベーターで移動できる。この移動はコストを \mathbf{0} 消費する。
セールスマンの目標は、マス (1,1) から出発して移動を繰り返し、ドアのある N 個のマスすべてを 1 回以上訪問してからマス (1,1) に到着することです。
セールスマンが目標を達成するために消費する総コストの最小値を求めてください。
制約
- 1 \leq H \leq 10^9
- 2 \leq W \leq 10^9
- 1 \leq N \leq 3 \times 10^5
- 1 \leq A_k \leq H
- 1 \leq B_k \leq W
- (A_1, B_1), \dots, (A_N, B_N) は相異なる
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W N A_1 B_1 \vdots A_N B_N
出力
答えを 1 行に出力せよ。
入力例 1
6 8 7 1 4 2 2 2 7 3 1 6 3 6 4 6 6
出力例 1
18
以下のように移動することで、総コスト 18 で目標を達成できます。
- マス (1,1) から出発する。
- マス (1,1) から下に 1 マス、右に 1 マス移動し、マス (2,2) を訪れる(コスト 1)。
- マス (2,2) から左に 1 マス、下に 1 マス移動し、マス (3,1) を訪れる(コスト 1)。
- マス (3,1) から下に 3 マス、右に 2 マス移動し、マス (6,3) を訪れる(コスト 2)。
- マス (6,3) から右に 1 マス移動し、マス (6,4) を訪れる(コスト 1)。
- マス (6,4) から右に 2 マス移動し、マス (6,6) を訪れる(コスト 2)。
- マス (6,6) から右に 2 マス、上に 4 マス、左に 1 マス移動し、マス (2,7) を訪れる(コスト 3)。
- マス (2,7) から右に 1 マス、上に 1 マス、左に 4 マス移動し、マス (1,4) を訪れる(コスト 5)。
- マス (1,4) から左に 3 マス移動し、マス (1,1) に到着する(コスト 3)。
入力例 2
1000000000 1000000000 2 888888888 600000000 1000000000 700000000
出力例 2
1999999998
Score : 500 points
Problem Statement
Note: This problem has almost the same setting as Problem G. The differences in the problem statements are shown in red bold.
There is an underground apartment represented by a grid with H rows and W columns. The cell at the i-th row from the top and the j-th column from the left is denoted as cell (i,j).
The entrance to the underground apartment is at cell (1,1).
The underground apartment has N doors. The k-th door is located at cell (A_k, B_k).
Inside the underground apartment, a salesman can perform the following two types of moves any number of times, in any order:
- He can walk to a horizontally adjacent cell (left or right) from his current cell. The cost of this move is 1.
- If his current cell is in column 1 or column W, he can move to a vertically adjacent cell (up or down) by elevator. The cost of this move is \mathbf{0}.
His goal is to start from cell (1,1), repeat moves, visit all N cells with doors at least once, and then arrive at cell (1,1).
Find the minimum total cost needed to achieve his goal.
Constraints
- 1 \leq H \leq 10^9
- 2 \leq W \leq 10^9
- 1 \leq N \leq 3 \times 10^5
- 1 \leq A_k \leq H
- 1 \leq B_k \leq W
- (A_1, B_1), \dots, (A_N, B_N) are distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W N A_1 B_1 \vdots A_N B_N
Output
Output the answer on a single line.
Sample Input 1
6 8 7 1 4 2 2 2 7 3 1 6 3 6 4 6 6
Sample Output 1
18
By moving as follows, the goal can be achieved with a total cost of 18:
- Start from cell (1,1).
- Move 1 cell down and 1 cell right from cell (1,1) to visit cell (2,2) (cost 1).
- Move 1 cell left and 1 cell down from cell (2,2) to visit cell (3,1) (cost 1).
- Move 3 cells down and 2 cells right from cell (3,1) to visit cell (6,3) (cost 2).
- Move 1 cell right from cell (6,3) to visit cell (6,4) (cost 1).
- Move 2 cells right from cell (6,4) to visit cell (6,6) (cost 2).
- Move 2 cells right, 4 cells up, and 1 cell left from cell (6,6) to visit cell (2,7) (cost 3).
- Move 1 cell right, 1 cell up, and 4 cells left from cell (2,7) to visit cell (1,4) (cost 5).
- Move 3 cells left from cell (1,4) to arrive at cell (1,1) (cost 3).
Sample Input 2
1000000000 1000000000 2 888888888 600000000 1000000000 700000000
Sample Output 2
1999999998
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 行 N 列のグリッドがあります。上から i 行目、左から j 列目のマスをマス (i,j) と表します。マス (i,j) にははじめ A_{i,j} 個の石があります。
Alice と Bob がこのグリッドを使って以下のようなゲームを行います。
- Alice から始めて両者は交互に手番をプレイする。
- 各手番では、プレイヤーはマスを 1 つ選び、そこから 1 個以上 K 個以下の石をまとめて上または左に隣接するマスへ移動させる。具体的には、次の手順を順に行う。
- 石が 1 個以上あるマス (i,j) を選ぶ。ただし、マス (1,1) は選べない。
- マス (i,j) にある石の個数を c とし、1 \le x \le \min(c,K) を満たす整数 x を選ぶ。
- 移動先として、存在するならばマス (i-1,j) またはマス (i,j-1) のいずれか一方を選び、マス (i,j) から石を x 個取り出して全てそのマスに移動させる。
先に手番をプレイできなくなったプレイヤーの負けです。
両者が最善を尽くしたとき、どちらのプレイヤーが勝つかを求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T
- 2\le N\le 100
- 1\le K\le 10^9
- 0\le A_{i,j}\le 10^9
- 全てのテストケースにおける N^2 の総和は 3\times 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N K
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
各テストケースについて、両者が最善を尽くしたとき Alice が勝つならば Alice を、Bob が勝つならば Bob を出力せよ。
入力例 1
3 2 2 0 1 3 2 3 1 1 1 1 1 1 1 1 1 1 4 4 3 8 5 3 4 2 0 6 0 9 10 4 1 9 7 10
出力例 1
Alice Bob Alice
1 番目のテストケースについて考えます。
例えばゲームは以下のように進行します(両者が最適に手番をプレイしているとは限らないことに注意してください):
- Alice の手番:マス (2,2) を選び、石を 2 個取り出す。取り出した石をマス (1,2) に移動させる。
- Bob の手番:マス (1,2) を選び、石を 2 個取り出す。取り出した石をマス (1,1) に移動させる。
- Alice の手番:マス (2,1) を選び、石を 2 個取り出す。取り出した石をマス (1,1) に移動させる。
- Bob の手番:マス (2,1) を選び、石を 1 個取り出す。取り出した石をマス (1,1) に移動させる。
- Alice の手番:マス (1,2) を選び、石を 1 個取り出す。取り出した石をマス (1,1) に移動させる。
- Bob の手番:Bob は手番をプレイできないので負けである。
Alice は最善を尽くすことで必ず Bob に勝つことができます。したがって、1 行目には Alice を出力してください。
Score : 500 points
Problem Statement
There is 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 cell (i,j). Cell (i,j) initially contains A_{i,j} stones.
Alice and Bob play the following game using this grid.
- Starting with Alice, the two players alternate taking turns.
- On each turn, the player chooses a cell and moves at least 1 and at most K stones from it together to an adjacent cell above or to the left. Specifically, the following steps are performed in order:
- Choose a cell (i,j) that contains at least 1 stone. Cell (1,1) cannot be chosen.
- Let c be the number of stones in cell (i,j), and choose an integer x satisfying 1 \le x \le \min(c,K).
- Choose as the destination either cell (i-1,j) (if it exists) or cell (i,j-1) (if it exists), then remove x stones from cell (i,j) and move all of them to that cell.
The player who cannot take a turn first loses.
Determine which player wins when both play optimally.
You are given T test cases; solve each of them.
Constraints
- 1\le T
- 2\le N\le 100
- 1\le K\le 10^9
- 0\le A_{i,j}\le 10^9
- The sum of N^2 over all test cases is at most 3\times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N K
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
Output
Output the answers for the test cases in order, separated by newlines.
For each test case, output Alice if Alice wins when both play optimally, and Bob if Bob wins.
Sample Input 1
3 2 2 0 1 3 2 3 1 1 1 1 1 1 1 1 1 1 4 4 3 8 5 3 4 2 0 6 0 9 10 4 1 9 7 10
Sample Output 1
Alice Bob Alice
Consider the first test case.
For example, the game may proceed as follows (note that the players are not necessarily playing optimally):
- Alice's turn: Choose cell (2,2) and remove 2 stones. Move the removed stones to cell (1,2).
- Bob's turn: Choose cell (1,2) and remove 2 stones. Move the removed stones to cell (1,1).
- Alice's turn: Choose cell (2,1) and remove 2 stones. Move the removed stones to cell (1,1).
- Bob's turn: Choose cell (2,1) and remove 1 stone. Move the removed stone to cell (1,1).
- Alice's turn: Choose cell (1,2) and remove 1 stone. Move the removed stone to cell (1,1).
- Bob's turn: Bob cannot take a turn and loses.
By playing optimally, Alice can always beat Bob. Thus, output Alice on the first line.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 600 点
問題文
2H 行 2W 列のグリッド状のショートケーキがあります。上から i 行目、左から j 列目のマスをマス (i,j) と表します。マス (i,j) には S_{i,j}= o ならばイチゴが 1 つ乗っており、 S_{i,j}= x ならば何もありません。ここで、イチゴが乗ったマスはちょうど 2HW マスであることが保証されます。
このショートケーキの各マスを以下の条件を全て満たすように領域 A, B に分けてください。
- 全てのマスは領域 A, B のちょうど一方に含まれる。
- 領域 A, B はどちらも連結である。つまり、任意の同じ領域内の 2 マスは上下左右に辺で接している同じ領域内のマスに移動する操作を繰り返すことで互いに移動可能である。
- 領域 A, B はどちらもマスを 2HW マスずつ含む。
- 領域 A, B はどちらもイチゴを HW 個ずつ含む。
ただし、そのような分け方が必ず存在することが証明できます。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T
- 1\le H\le W
- H\times W\le 10^6
- S_{i,j} は
oまたはx - S_{i,j}=
oを満たす (i,j) がちょうど 2HW 個存在する - 全てのテストケースにおける H\times W の総和は 10^6 以下
- T,H,W は整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
H W
S_{1,1} S_{1,2} \ldots S_{1,2W}
S_{2,1} S_{2,2} \ldots S_{2,2W}
\vdots
S_{2H,1} S_{2H,2} \ldots S_{2H,2W}
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
各テストケースについて、答えを以下の形式で出力せよ。
X_{1,1} X_{1,2} \ldots X_{1,2W}
X_{2,1} X_{2,2} \ldots X_{2,2W}
\vdots
X_{2H,1} X_{2H,2} \ldots X_{2H,2W}
ここで、X_{i,j} はマス (i,j) が領域 A に含まれる場合は A、領域 B に含まれる場合は B とする。
条件を満たす分け方が複数存在する場合、どれを出力しても正答となる。
入力例 1
3 1 1 ox xo 1 2 oxxx ooxo 2 3 oooxxx oooxxx xoxxxx xooooo
出力例 1
AA BB AAAB ABBB AAABBB AAAAAB ABABAB ABBBBB
1 番目のテストケースについて考えます。
出力例の領域 A, B はどちらも連結で 2 つのマスを含んでいます。また、領域 A はマス (1,1) のイチゴを、領域 B はマス (2,2) のイチゴを含んでいます。
他にも、例えば以下のような出力でも正答となります。
AB AB
Score : 600 points
Problem Statement
There is a shortcake in the shape of a 2H \times 2W grid. The cell at the i-th row from the top and the j-th column from the left is denoted as cell (i,j). Cell (i,j) has one strawberry on it if S_{i,j}= o, and nothing if S_{i,j}= x. It is guaranteed that exactly 2HW cells have strawberries.
Divide each cell of this shortcake into regions A and B so that all of the following conditions are satisfied:
- Every cell belongs to exactly one of regions A and B.
- Both regions A and B are connected. That is, for any two cells in the same region, one can move between them by repeatedly moving to an adjacent cell (sharing an edge) in the same region.
- Both regions A and B contain exactly 2HW cells each.
- Both regions A and B contain exactly HW strawberries each.
It can be proved that such a partition always exists.
You are given T test cases; solve each of them.
Constraints
- 1\le T
- 1\le H\le W
- H\times W\le 10^6
- S_{i,j} is
oorx. - There are exactly 2HW pairs (i,j) with S_{i,j}=
o. - The sum of H\times W over all test cases is at most 10^6.
- T, H, W are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
H W
S_{1,1} S_{1,2} \ldots S_{1,2W}
S_{2,1} S_{2,2} \ldots S_{2,2W}
\vdots
S_{2H,1} S_{2H,2} \ldots S_{2H,2W}
Output
Output your solutions for the test cases in order, separated by newlines.
For each test case, output your solution in the following format:
X_{1,1} X_{1,2} \ldots X_{1,2W}
X_{2,1} X_{2,2} \ldots X_{2,2W}
\vdots
X_{2H,1} X_{2H,2} \ldots X_{2H,2W}
Here, X_{i,j} is A if cell (i,j) belongs to region A, and B if it belongs to region B.
If multiple valid partitions exist, any of them will be accepted.
Sample Input 1
3 1 1 ox xo 1 2 oxxx ooxo 2 3 oooxxx oooxxx xoxxxx xooooo
Sample Output 1
AA BB AAAB ABBB AAABBB AAAAAB ABABAB ABBBBB
Consider the first test case.
In the sample output, both regions A and B are connected and contain two cells each. Also, region A contains the strawberry at cell (1,1), and region B contains the strawberry at cell (2,2).
Additionally, for example, the following output is accepted:
AB AB
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 600 点
問題文
長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
A に対して操作を 0 回以上行うことができます。1 回の操作では、以下の手順を順に行います:
- 以下の条件を全て満たす整数の組 (l,r) を選ぶ。
- 1\le l\le r\le N
- A_l,A_{l+1},\ldots,A_r の偶奇が全て同じである
- k=l,l+1,\ldots,r に対し、A_k を \displaystyle\left\lfloor\frac{A_k}2 \right\rfloor で置き換える。
A の要素を全て 0 にするために必要な操作回数の最小値を求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T
- 1\le N\le 30
- 0\le A_i< 2^{60}
- 全てのテストケースにおける N の総和は 30 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N A_1 A_2 \ldots A_N
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
入力例 1
4 2 9 12 3 3 1 2 1 0 7 6 28 24 18 12 22 1
出力例 1
5 3 0 10
1 番目のテストケースについて考えます。
以下のように操作することで 5 回で A の要素を全て 0 にすることができます:
- (l,r)=(1,1) を選ぶ。A=(4,12) となる。
- (l,r)=(1,2) を選ぶ。A=(2,6) となる。
- (l,r)=(1,2) を選ぶ。A=(1,3) となる。
- (l,r)=(1,2) を選ぶ。A=(0,1) となる。
- (l,r)=(2,2) を選ぶ。A=(0,0) となる。
5 回未満の操作で A の要素を全て 0 にすることはできないので、5 を出力してください。
Score : 600 points
Problem Statement
You are given a sequence of non-negative integers A=(A_1,A_2,\ldots,A_N) of length N.
You can perform the following operation on A zero or more times. In one operation, perform the following steps in order:
- Choose a pair of integers (l,r) satisfying all of the following conditions:
- 1\le l\le r\le N
- A_l,A_{l+1},\ldots,A_r all have the same parity (that is, they are all even or all odd).
- For each k=l,l+1,\ldots,r, replace A_k with \displaystyle\left\lfloor\frac{A_k}2 \right\rfloor.
Find the minimum number of operations needed to make all elements of A equal to 0.
You are given T test cases; solve each of them.
Constraints
- 1\le T
- 1\le N\le 30
- 0\le A_i< 2^{60}
- The sum of N over all test cases is at most 30.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N A_1 A_2 \ldots A_N
Output
Output the answers for the test cases in order, separated by newlines.
Sample Input 1
4 2 9 12 3 3 1 2 1 0 7 6 28 24 18 12 22 1
Sample Output 1
5 3 0 10
Consider the first test case.
By performing operations as follows, all elements of A can be made 0 in five operations:
- Choose (l,r)=(1,1). A becomes (4,12).
- Choose (l,r)=(1,2). A becomes (2,6).
- Choose (l,r)=(1,2). A becomes (1,3).
- Choose (l,r)=(1,2). A becomes (0,1).
- Choose (l,r)=(2,2). A becomes (0,0).
It is impossible to make all elements of A equal to 0 in fewer than five operations, so output 5.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
注:この問題は C 問題とほとんど同じ設定です。問題文中の相違点を赤い太字で示します。
H 行 W 列のグリッドで表される地下アパートがあります。上から i 行目・左から j 列目のマスをマス (i,j) と表記します。
地下アパートの出入り口はマス (1,1) にあります。
地下アパートにはドアが N 個あります。k 個目のドアはマス (A_k, B_k) にあります。
地下アパート内で、セールスマンは以下の 2 種類の移動を好きな順番で何回でも行えます。
- 現在いるマスから左か右に隣接するマスに徒歩で移動できる。この移動はコストを 1 消費する。
- 現在いるマスが 1 列目または W 列目の場合、そこから上か下に隣接するマスに階段で移動できる。この移動はコストを \mathbf{1} 消費する。
セールスマンの目標は、マス (1,1) から出発して移動を繰り返し、ドアのある N 個のマスすべてを 1 回以上訪問してからマス (1,1) に到着することです。
セールスマンが目標を達成するために消費する総コストの最小値を求めてください。
制約
- 1 \leq H \leq 10^9
- 2 \leq W \leq 10^9
- 1 \leq N \leq 3 \times 10^5
- 1 \leq A_k \leq H
- 1 \leq B_k \leq W
- (A_1, B_1), \dots, (A_N, B_N) は相異なる
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W N A_1 B_1 \vdots A_N B_N
出力
答えを 1 行に出力せよ。
入力例 1
6 8 7 1 4 2 2 2 7 3 1 6 3 6 4 6 6
出力例 1
28
以下のように移動することで、総コスト 28 で目標を達成できます。
- マス (1,1) から出発する。
- マス (1,1) から下に 1 マス、右に 1 マス移動し、マス (2,2) を訪れる(コスト 2)。
- マス (2,2) から左に 1 マス、下に 1 マス移動し、マス (3,1) を訪れる(コスト 2)。
- マス (3,1) から下に 3 マス、右に 2 マス移動し、マス (6,3) を訪れる(コスト 5)。
- マス (6,3) から右に 1 マス移動し、マス (6,4) を訪れる(コスト 1)。
- マス (6,4) から右に 2 マス移動し、マス (6,6) を訪れる(コスト 2)。
- マス (6,6) から右に 2 マス、上に 4 マス、左に 1 マス移動し、マス (2,7) を訪れる(コスト 7)。
- マス (2,7) から右に 1 マス、上に 1 マス、左に 4 マス移動し、マス (1,4) を訪れる(コスト 6)。
- マス (1,4) から左に 3 マス移動し、マス (1,1) に到着する(コスト 3)。
入力例 2
1000000000 1000000000 2 888888888 600000000 1000000000 700000000
出力例 2
3999999996
Score : 700 points
Problem Statement
Note: This problem has almost the same setting as Problem C. The differences in the problem statements are shown in red bold.
There is an underground apartment represented by a grid with H rows and W columns. The cell at the i-th row from the top and the j-th column from the left is denoted as cell (i,j).
The entrance to the underground apartment is at cell (1,1).
The underground apartment has N doors. The k-th door is located at cell (A_k, B_k).
Inside the underground apartment, a salesman can perform the following two types of moves any number of times, in any order:
- He can walk to a horizontally adjacent cell (left or right) from his current cell. The cost of this move is 1.
- If his current cell is in column 1 or column W, he can move to a vertically adjacent cell (up or down) by stairs. The cost of this move is \mathbf{1}.
His goal is to start from cell (1,1), repeat moves, visit all N cells with doors at least once, and then arrive at cell (1,1).
Find the minimum total cost needed to achieve his goal.
Constraints
- 1 \leq H \leq 10^9
- 2 \leq W \leq 10^9
- 1 \leq N \leq 3 \times 10^5
- 1 \leq A_k \leq H
- 1 \leq B_k \leq W
- (A_1, B_1), \dots, (A_N, B_N) are distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W N A_1 B_1 \vdots A_N B_N
Output
Output the answer on a single line.
Sample Input 1
6 8 7 1 4 2 2 2 7 3 1 6 3 6 4 6 6
Sample Output 1
28
By moving as follows, the goal can be achieved with a total cost of 28:
- Start from cell (1,1).
- Move 1 cell down and 1 cell right from cell (1,1) to visit cell (2,2) (cost 2).
- Move 1 cell left and 1 cell down from cell (2,2) to visit cell (3,1) (cost 2).
- Move 3 cells down and 2 cells right from cell (3,1) to visit cell (6,3) (cost 5).
- Move 1 cell right from cell (6,3) to visit cell (6,4) (cost 1).
- Move 2 cells right from cell (6,4) to visit cell (6,6) (cost 2).
- Move 2 cells right, 4 cells up, and 1 cell left from cell (6,6) to visit cell (2,7) (cost 7).
- Move 1 cell right, 1 cell up, and 4 cells left from cell (2,7) to visit cell (1,4) (cost 6).
- Move 3 cells left from cell (1,4) to arrive at cell (1,1) (cost 3).
Sample Input 2
1000000000 1000000000 2 888888888 600000000 1000000000 700000000
Sample Output 2
3999999996