Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
N 個のマスからなるマス目があります。マスには 1 から N の番号が付いています。
始め、マス i(1 \le i \le N) には i \bmod 2 が書かれています。あなたは以下の操作を任意の回数(0 回でもよい)行うことができます。
- 以下の条件を満たすマス l,r(l+1 < r) を選び、マス l+1,l+2,\dots,r-1 に書かれている整数をマス l に書かれている整数に書き換える。
- マス l に書かれている整数とマス r に書かれている整数は等しい。
- マス i(l < i < r) に書かれている整数とマス l に書かれている整数は異なる。
最終的にマス i(1 \le i \le N) に書かれている整数が A_i になるような操作列の個数を 998244353 で割ったあまりを求めてください。
ただし 2 個の操作列が異なるとは、操作列の長さが異なるか、操作列の長さ以下の正整数 t が存在して t 回目の操作で選んだ (l,r) が異なる場合、かつその時に限り言うものとします。
制約
- 1 \le N \le 2 \times 10^5
- 0 \le A_i \le 1
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
6 1 1 1 1 1 0
出力例 1
3
各マス i(1 \le i \le N) に書かれている整数を A_i にするためには、例えば以下のように操作を行えばよいです。(ここで、マス目の状態を数列 X = (X_1, X_2, \dots, X_N) のように表します。)
- はじめ、X = (1, 0, 1, 0, 1, 0) である。
- マス 2, 4 を選ぶ。X = (1, 0, 0, 0, 1, 0) となる。
- マス 1, 5 を選ぶ。X = (1, 1, 1, 1, 1, 0) となる。
上記以外にマス i に書かれている整数が A_i になるような操作列の個数は 2 個あるため、答えは 3 です。
入力例 2
10 1 1 1 1 1 0 1 1 1 0
出力例 2
9
Score : 400 points
Problem Statement
There is a grid consisting of N cells numbered 1 to N.
Initially, cell i (1 \le i \le N) has an integer i \bmod 2 written in it. You can perform the following operation any number of times, possibly zero:
- Choose cells l and r (l+1 < r) that satisfy the following conditions, and replace each of the integers written in cells l+1, l+2, \dots, r-1 with the integer written in cell l.
- The integer written in cell l is equal to the integer written in cell r.
- The integer written in cell i (l < i < r) is different from the integer written in cell l.
Find the number, modulo 998244353, of sequences of operations that result in the integers written in cell i (1 \leq i \leq N) being A_i.
Two sequences of operations are considered different if and only if their lengths are different or there exists a positive integer t not exceeding the length of the sequences such that the (l, r) chosen in the t-th operations differ.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq A_i \leq 1
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
6 1 1 1 1 1 0
Sample Output 1
3
To make the integers written in each cell i equal to A_i, for example, you can perform the following operations. (Here, we represent the state of the grid as a sequence X = (X_1, X_2, \dots, X_N).)
- Initially, X = (1, 0, 1, 0, 1, 0).
- Choose cells 2 and 4. X becomes (1, 0, 0, 0, 1, 0).
- Choose cells 1 and 5. X becomes (1, 1, 1, 1, 1, 0).
Besides the above, there are two other sequences of operations that result in the integers written in cell i being A_i, so the answer is 3.
Sample Input 2
10 1 1 1 1 1 0 1 1 1 0
Sample Output 2
9
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
数直線上に N 個のコマが置かれており、最初、すべてのコマは相異なる座標に置かれています。
最初にコマが置かれている座標は X_1,X_2,\ldots, X_N です。
高橋君は、次の操作を繰り返し (0 回でも良い) 行う事ができます。
1\leq i\leq N-3 をみたす整数 i を 1 つ選び、 座標が小さい方から i 番目のコマのある位置と i+3 番目のコマのある位置の中点を M とする。
座標が小さい方から i+1 番目のコマと i+2 番目のコマを、それぞれ M と対称な位置に動かす。
なお、本問題の制約下において、どのように操作を繰り返し行ってもすべてのコマが相異なる座標に置かれていることが証明できる。
高橋君の目標は N 個のコマが置かれている座標の総和を最小にする事です。
操作を繰り返した後の座標の総和としてあり得る最小の値を求めてください。
制約
- 4 \leq N \leq 2\times 10^5
- 0\leq X_1<X_2<\cdots<X_N\leq 10^{12}
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X_1 X_2 \ldots X_N
出力
操作後の N 個のコマの座標の総和としてあり得る最小の値を出力せよ。
入力例 1
4 1 5 7 10
出力例 1
21
高橋君が i=1 を選ぶと、操作は次のように行われます。
- 座標が小さい方から 1,4 番目のコマの座標が 1,10 である事から、 この操作における M の座標は \frac{1+10}{2}=5.5 となる。
- 左から 2 番目のコマは座標 5 から座標 5.5+(5.5-5)=6へ動かされ、 左から 3 番目のコマは座標 7 から座標 5.5-(7-5.5)=4へ動かされる。
この操作の後で 4 つのコマの置かれている座標の総和は 1+4+6+10=21 であり、この時が最小となります。よって、21 を出力します。
入力例 2
6 0 1 6 10 14 16
出力例 2
41
Score : 500 points
Problem Statement
There are N pieces placed on a number line. Initially, all pieces are placed at distinct coordinates.
The initial coordinates of the pieces are X_1, X_2, \ldots, X_N.
Takahashi can repeat the following operation any number of times, possibly zero.
Choose an integer i such that 1 \leq i \leq N-3, and let M be the midpoint between the positions of the i-th and (i+3)-rd pieces in ascending order of coordinate.
Then, move each of the (i+1)-th and (i+2)-th pieces in ascending order of coordinate to positions symmetric to M.
Under the constraints of this problem, it can be proved that all pieces always occupy distinct coordinates, no matter how one repeatedly performs the operation.
His goal is to minimize the sum of the coordinates of the N pieces.
Find the minimum possible sum of the coordinates of the N pieces after repeating the operations.
Constraints
- 4 \leq N \leq 2 \times 10^5
- 0 \leq X_1 < X_2 < \cdots < X_N \leq 10^{12}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X_1 X_2 \ldots X_N
Output
Print the minimum possible sum of the coordinates of the N pieces after repeating the operations.
Sample Input 1
4 1 5 7 10
Sample Output 1
21
If Takahashi chooses i = 1, the operation is performed as follows:
- The coordinates of the 1st and 4th pieces in ascending order of coordinate are 1 and 10, so the coordinate of M in this operation is (1 + 10)/2 = 5.5.
- The 2nd piece from the left moves from coordinate 5 to 5.5 + (5.5 - 5) = 6.
- The 3rd piece from the left moves from coordinate 7 to 5.5 - (7 - 5.5) = 4.
After this operation, the sum of the coordinates of the four pieces is 1 + 4 + 6 + 10 = 21, which is minimal. Thus, print 21.
Sample Input 2
6 0 1 6 10 14 16
Sample Output 2
41
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 600 点
問題文
N 個の箱があります。 i = 1, 2, \ldots, N について、i 番目の箱には A_i 個の赤いボールと B_i 個の青いボールが入っています。
また、数列 (1, 2, \ldots, N) を並べ替えた 2 つの順列 P = (P_1, P_2, \ldots, P_N) と Q = (Q_1, Q_2, \ldots, Q_N) が与えられます。
高橋君は、下記の操作を好きな回数( 0 回でも良い)だけ繰り返すことができます。
- 1 \leq i \leq N を満たす整数 i を選び、i 番目の箱に入っているすべてのボールを箱から取り出して手に持つ。
- 手に持っているすべての赤いボールを P_i 番目の箱に入れる。
- 手に持っているすべての青いボールを Q_i 番目の箱に入れる。
高橋君の目標は、上記の操作の繰り返しによって、X 番目の箱以外にボールが入っていない状態にすることです。 この目標を達成することが可能かどうかを判定し、可能な場合はそのために行う操作回数の最小値を出力してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 0 \leq A_i, B_i \leq 1
- 1 \leq P_i, Q_i \leq N
- P,Q はそれぞれ (1, 2, \ldots, N) の順列
- 1 \leq X \leq N
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N P_1 P_2 \ldots P_N Q_1 Q_2 \ldots Q_N
出力
高橋君が X 番目の箱以外にボールが入っていない状態にすることが不可能な場合は -1
を、
可能な場合はそのために行う操作回数の最小値を出力せよ。
入力例 1
5 3 0 1 0 1 0 0 0 1 0 1 4 1 2 3 5 3 4 5 2 1
出力例 1
4
各箱に入っている赤いボールと青いボールの個数はそれぞれ A = (0, 1, 0, 1, 0), B = (0, 0, 1, 0, 1) で与えられています。 下記の手順を考えます。
- まず、5 番目の箱に対して操作を行う。その結果、A = (0, 1, 0, 1, 0), B = (1, 0, 1, 0, 0) となる。
- 次に、2 番目の箱に対して操作を行う。その結果、A = (1, 0, 0, 1, 0), B = (1, 0, 1, 0, 0) となる。
- さらに、1 番目の箱に対して操作を行う。その結果、A = (0, 0, 0, 2, 0), B = (0, 0, 2, 0, 0) となる。
- 最後に、4 番目の箱に対して操作を行う。その結果、A = (0, 0, 2, 0, 0), B = (0, 0, 2, 0, 0) となる。
上記の 4 回の操作によって、X = 3 番目の箱以外にボールが入っていない状態にすることができます。 これが考えられる最小の操作回数です。
入力例 2
5 3 0 0 0 0 0 0 0 0 0 0 4 1 2 3 5 3 4 5 2 1
出力例 2
0
どの箱にもボールが入っていません。 よって、はじめから X = 3 番目の箱以外にボールが入っていない状態であるので、必要な操作回数は 0 回です。
入力例 3
2 2 1 1 1 1 1 2 1 2
出力例 3
-1
どのような手順で操作を行っても、X = 2 番目の箱以外にボールが入っていない状態にすることはできません。
入力例 4
10 10 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 1 0 1 4 9 5 8 2 3 6 10 7 7 4 9 10 6 3 1 2 8 5
出力例 4
8
Score : 600 points
Problem Statement
There are N boxes. For i = 1, 2, \ldots, N, the i-th box contains A_i red balls and B_i blue balls.
You are also given two permutations P = (P_1, P_2, \ldots, P_N) and Q = (Q_1, Q_2, \ldots, Q_N) of (1, 2, \ldots, N).
Takahashi can repeat the following operation any number of times, possibly zero:
- Choose an integer 1 \leq i \leq N, and take all the balls from the i-th box into his hand.
- Put all the red balls in his hand into the P_i-th box.
- Put all the blue balls in his hand into the Q_i-th box.
His goal is to make a state where all boxes other than the X-th box contain no balls by repeating the above operations. Determine whether it is possible to achieve his goal, and if possible, print the minimum number of operations needed to achieve it.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 0 \leq A_i, B_i \leq 1
- 1 \leq P_i, Q_i \leq N
- P and Q are permutations of (1, 2, \ldots, N).
- 1 \leq X \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N P_1 P_2 \ldots P_N Q_1 Q_2 \ldots Q_N
Output
If it is impossible for Takahashi to achieve a state where all boxes other than the X-th box contain no balls, print -1
. If it is possible, print the minimum number of operations needed to achieve it.
Sample Input 1
5 3 0 1 0 1 0 0 0 1 0 1 4 1 2 3 5 3 4 5 2 1
Sample Output 1
4
The numbers of red and blue balls in each box are A = (0, 1, 0, 1, 0) and B = (0, 0, 1, 0, 1), respectively. Consider the following steps:
- First, perform the operation on the 5th box. As a result, A = (0, 1, 0, 1, 0), B = (1, 0, 1, 0, 0).
- Next, perform the operation on the 2nd box. As a result, A = (1, 0, 0, 1, 0), B = (1, 0, 1, 0, 0).
- Then, perform the operation on the 1st box. As a result, A = (0, 0, 0, 2, 0), B = (0, 0, 2, 0, 0).
- Finally, perform the operation on the 4th box. As a result, A = (0, 0, 2, 0, 0), B = (0, 0, 2, 0, 0).
These four operations achieve a state where all boxes other than the X-th (3rd) box contain no balls. This is the minimum number of operations possible.
Sample Input 2
5 3 0 0 0 0 0 0 0 0 0 0 4 1 2 3 5 3 4 5 2 1
Sample Output 2
0
There are no balls in any boxes. Thus, the state where all boxes other than the X-th (3rd) box contain no balls is already achieved, so the required number of operations is 0.
Sample Input 3
2 2 1 1 1 1 1 2 1 2
Sample Output 3
-1
There is no way to perform the operation to achieve a state where all boxes other than the X-th (2nd) box contain no balls.
Sample Input 4
10 10 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 1 0 1 4 9 5 8 2 3 6 10 7 7 4 9 10 6 3 1 2 8 5
Sample Output 4
8
Time Limit: 5 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
N 体のスライムが左右一列に並んでいます。
i = 1, 2, \ldots, N について、左から i 番目のスライムの大きさは A_i です。
K = 1, 2, \ldots, N のそれぞれの場合について、下記の問題を解いてください。
初期状態で左から K 番目にいるスライムが高橋君です。 高橋君が下記の行動を好きな回数( 0 回でも良い)だけ行ったあとの、高橋君の大きさとしてあり得る最大値を求めてください。
- 高橋君に隣接するスライムのうち、高橋君より大きさが真に小さいものを選んで吸収する。 その結果、吸収されたスライムは消滅し、高橋君の大きさは吸収したスライムの大きさだけ増加する。
上記の操作の際、スライムが吸収され消滅したことで生じた隙間は直ちに埋められ、 消滅したスライムの両端のスライムどうし(それらが存在すれば)は新たに隣接します(入出力例1の説明も参照してください)。
制約
- 2 \leq N \leq 5 \times 10^5
- 1 \leq A_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
下記の形式にしたがい、K = 1, 2, \ldots, N のそれぞれの場合における問題の答え B_K をそれぞれ空白区切りで出力せよ。
B_1 B_2 \ldots B_N
入力例 1
6 4 13 2 3 2 6
出力例 1
4 30 2 13 2 13
例として、K = 4 の場合の問題を考えます。 残っているスライムの大きさを列の左にいるスライムのものから順に並べ、高橋君の大きさに対応する値を角括弧[ ]で括った列を列の状態と呼ぶことにします。 すなわち、初期状態の列の状態は (4, 13, 2, [3], 2, 6) です。高橋君の下記の行動手順を考えます。
- 高橋君が右隣のスライムを吸収する。その結果、吸収されたスライムは消滅し、高橋君の大きさは 3 + 2 = 5 になる。その後の列の状態は (4, 13, 2, [5], 6) である。
- 高橋君が左隣のスライムを吸収する。その結果、吸収されたスライムは消滅し、高橋君の大きさは 5 + 2 = 7 になる。その後の列の状態は (4, 13, [7], 6) である。
- 高橋君が右隣のスライムを吸収する。その結果、吸収されたスライムは消滅し、高橋君の大きさは 7 + 6 = 13 になる。その後の列の状態は (4, 13, [13]) である。
高橋君に隣接するスライムであって高橋君より大きさが真に小さいものは存在しないので、高橋君はこれ以上行動することができません。高橋君の最終的な大きさは 13 であり、これが考えられる最大値です。
入力例 2
12 22 25 61 10 21 37 2 14 5 8 6 24
出力例 2
22 47 235 10 31 235 2 235 5 235 6 235
Score : 700 points
Problem Statement
There are N slimes lined up in a row from left to right.
For i = 1, 2, \ldots, N, the i-th slime from the left has size A_i.
For each K = 1, 2, \ldots, N, solve the following problem.
Takahashi is the K-th slime from the left in the initial state. Find the maximum size that he can have after performing the following action any number of times, possibly zero:
- Choose a slime adjacent to him that is strictly smaller than him, and absorb it. As a result, the absorbed slime disappears, and Takahashi's size increases by the size of the absorbed slime.
When a slime disappears due to absorption, the gap is immediately closed, and the slimes that were adjacent to the disappearing slime (if they exist) become adjacent (see the explanation in Sample Input 1).
Constraints
- 2 \leq N \leq 5 \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 A_2 \ldots A_N
Output
Print the answers B_K for each K = 1, 2, \ldots, N, separated by spaces, in the following format:
B_1 B_2 \ldots B_N
Sample Input 1
6 4 13 2 3 2 6
Sample Output 1
4 30 2 13 2 13
As an example, consider the problem for K = 4. We will refer to the sizes of the remaining slimes, listed from left to right, with Takahashi's size enclosed in square brackets [ ], as the state of the row. That is, the initial state is (4, 13, 2, [3], 2, 6). Consider the following sequence of actions by Takahashi.
- He absorbs the slime to his right. As a result, the absorbed slime disappears, and his size becomes 3 + 2 = 5. The state becomes (4, 13, 2, [5], 6).
- He absorbs the slime to his left. As a result, the absorbed slime disappears, and his size becomes 5 + 2 = 7. The state becomes (4, 13, [7], 6).
- He absorbs the slime to his right. As a result, the absorbed slime disappears, and his size becomes 7 + 6 = 13. The state becomes (4, 13, [13]).
There are no slimes adjacent to him that are strictly smaller than him, so he cannot perform any more actions. His final size is 13, which is the maximum possible.
Sample Input 2
12 22 25 61 10 21 37 2 14 5 8 6 24
Sample Output 2
22 47 235 10 31 235 2 235 5 235 6 235
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 800 点
問題文
N 頂点の完全グラフ G の辺に正整数の番号を付ける方法のうち、以下の条件を満たすものを「良い完全グラフ」と言うことにします。
- N 個の頂点を全てちょうど 1 回ずつ通るパスのうち、通った辺の番号を通った順に並べた数列が広義単調増加になるものが存在しない。
「良い完全グラフ」が存在するか判定し、存在する場合は「辺に付ける正整数の番号の最大値」が最小になるものを 1 個構築してください。
制約
- 2 \le N \le 20
入力
入力は以下の形式で標準入力から与えられる。
N
出力
「良い完全グラフ」が存在しない場合、No
と出力せよ。
「良い完全グラフ」が存在する場合、以下のように出力せよ。ここで、a_{i,j} は頂点 i と頂点 j を結ぶ無向辺に付けられる番号とする。
Yes a_{1,2} a_{1,3} a_{1,4} \dots a_{1,N} a_{2,3} a_{2,4} \dots a_{2,N} \vdots a_{N-1,N}
条件を満たす解が複数存在する場合、どれを出力しても正解とみなされる。
入力例 1
5
出力例 1
Yes 2 1 4 4 4 3 1 1 3 2
例えば頂点 2,5,1,4,3 の順に通るパスの場合、通った辺の番号を通った順に並べた数列は (1,4,4,1) となるため、広義単調増加ではありません。
他にも通った辺の番号を通った順に並べた数列が広義単調増加となるパスが存在しないため、このグラフは条件を満たします。
また、N=5 の場合「辺に付ける正整数の番号の最大値」を 3 以下にすることはできないため、この出力は正当です。
入力例 2
2
出力例 2
No
Score : 800 points
Problem Statement
Among the ways to label the edges of a complete graph G with N vertices with positive integers, a graph satisfying the following condition is called a "good complete graph".
- There exists no path that visits all N vertices exactly once and whose sequence of edge labels, in the order the edges are traversed, is non-decreasing.
Determine whether a good complete graph exists. If it exists, construct one that minimizes the maximum label assigned to an edge.
Constraints
- 2 \leq N \leq 20
Input
The input is given from Standard Input in the following format:
N
Output
If a good complete graph does not exist, print No
.
If it exists, print one as follows. Let a_{i,j} be the label assigned to the undirected edge connecting vertices i and j.
Yes a_{1,2} a_{1,3} a_{1,4} \dots a_{1,N} a_{2,3} a_{2,4} \dots a_{2,N} \vdots a_{N-1,N}
If multiple solutions exist, you may print any of them.
Sample Input 1
5
Sample Output 1
Yes 2 1 4 4 4 3 1 1 3 2
For example, consider the path that visits vertices in the order 2, 5, 1, 4, 3. The sequence of edge labels traversed is (1, 4, 4, 1), which is not non-decreasing.
Moreover, there is no path whose sequence of edge labels is non-decreasing, so this graph satisfies the condition.
Also, when N=5, it is impossible to assign labels so that the maximum label assigned to an edge is 3 or less, so this output is valid.
Sample Input 2
2
Sample Output 2
No