A - Add and Swap

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 600

問題文

整数 N,K 及び長さ N の数列 A=(A_1,\ldots,A_N) が与えられます.

A に対し以下の操作を 500000 回以下行うことで A を広義単調増加にできるか判定し,可能な場合は実際に操作手順を一つ示してください.

  • 1 以上 N-1 以下の整数 i を選ぶ.A_iA_{i+1}+K で,A_{i+1}A_i で同時に置き換える.

制約

  • 入力される数値は全て整数
  • 2 \leq N \leq 50
  • 1\leq K \leq 50
  • 1\leq A_i\leq 50

入力

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

N K
A_1 \ldots A_N

出力

500000 回以下の操作で A を広義単調増加にできない場合 No を出力せよ.広義単調増加にできる場合,操作回数を M\ (0 \leq M \leq 500000) とし,k 回目 (1\leq k \leq M) の操作で選んだ ii_k として以下の形式で出力せよ.

Yes
M
i_1 \ldots i_M

条件を満たす解が複数存在する場合,どれを出力しても正解とみなされる.


入力例 1

3 2
3 6 4

出力例 1

Yes
1
2

i=2 として操作を行います.A_2A_3+2=6 で,A_3A_2=6 で同時に置き換えるので,操作後の AA=(3,6,6) となります.

これにより A は広義単調増加になっているので,この出力は条件を満たします.


入力例 2

3 3
1 5 8

出力例 2

Yes
2
2 2

操作回数を最小化する必要はありません.

Score : 600 points

Problem Statement

You are given integers N, K, and a sequence A = (A_1, \ldots, A_N) of length N.

Determine whether it is possible to make A non-decreasing by performing the following operation at most 500000 times, and if possible, provide one sequence of operations to do so.

  • Choose an integer i between 1 and N-1, inclusive. Simultaneously replace A_i with A_{i+1} + K, and A_{i+1} with A_i.

Constraints

  • All input numbers are integers.
  • 2 \leq N \leq 50
  • 1 \leq K \leq 50
  • 1 \leq A_i \leq 50

Input

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

N K
A_1 \ldots A_N

Output

If it is impossible to make A non-decreasing within 500000 operations, print No. Otherwise, print a solution in the following format, where M is the number of operations (0 \leq M \leq 500000), and i_k is the i chosen in the k-th operation (1 \leq k \leq M):

Yes
M
i_1 \ldots i_M

If multiple valid solutions exist, any of them will be considered correct.


Sample Input 1

3 2
3 6 4

Sample Output 1

Yes
1
2

Let us perform the operation with i=2. This simultaneously replaces A_2 with A_3 + 2 = 6, and A_3 with A_2 = 6, making A = (3,6,6).

Now A is non-decreasing, so this output satisfies the conditions.


Sample Input 2

3 3
1 5 8

Sample Output 2

Yes
2
2 2

It is not necessary to minimize the number of operations.

B - Sum of CC

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 600

問題文

長さ N の数列 A=(A_1,\ldots,A_N) に対し,f(A) を以下で定義します.

  • 頂点に 1 から N の番号がついた N 頂点 0 辺のグラフを用意する.1\leq i < j\leq N を満たす全ての整数組 (i,j) に対し,A_i\leq A_j ならば頂点 i と頂点 j を双方向に結ぶ辺を張る.最終的に得られるグラフの連結成分数を f(A) と定める.

長さ N の数列 B=(B_1,\ldots,B_N) が与えられます. B の各要素は -1 または 1 以上 M 以下の整数です.

B の要素のうち,-1 を全て 1 以上 M 以下の整数に置き換えて得られる数列 B' は,B に含まれる -1 の個数を q とすると M^q 通りあります.

考えられる B' 全てに対する f(B') の総和を 998244353 で割ったあまりを求めてください.

制約

  • 入力される数値は全て整数
  • 2 \leq N \leq 2000
  • 1\leq M\leq 2000
  • B_i-1 または 1 以上 M 以下の整数

入力

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

N M
B_1 \ldots B_N

出力

答えを出力せよ.


入力例 1

3 3
2 -1 1

出力例 1

6

B' として考えられるのは (2,1,1),(2,2,1),(2,3,1)3 個です.

B'=(2,1,1) のとき,頂点 2 と頂点 3 の間にのみ辺が張られるので,連結成分数は 2 です.よって f(B')=2 です.

同様に,B'=(2,2,1) のとき f(B')=2B'=(2,3,1) のとき f(B')=2 なので答えは 2+2+2=6 となります.


入力例 2

10 8
-1 7 -1 -1 -1 2 -1 1 -1 2

出力例 2

329785

入力例 3

11 12
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

出力例 3

529513150

総和を 998244353 で割ったあまりを求めることに注意してください.

Score : 600 points

Problem Statement

For a sequence A = (A_1, \ldots, A_N) of length N, define f(A) as follows.

  • Prepare a graph with N vertices labeled 1 to N and zero edges. For every integer pair (i, j) satisfying 1 \leq i < j \leq N, if A_i \leq A_j, draw a bidirectional edge connecting vertices i and j. Define f(A) as the number of connected components in the resulting graph.

You are given a sequence B = (B_1, \ldots, B_N) of length N. Each element of B is -1 or an integer between 1 and M, inclusive.

By replacing every occurrence of -1 in B with an integer between 1 and M, one can obtain M^q sequences B', where q is the number of -1 in B.

Find the sum, modulo 998244353, of f(B') over all possible B'.

Constraints

  • All input numbers are integers.
  • 2 \leq N \leq 2000
  • 1 \leq M \leq 2000
  • Each B_i is -1 or an integer between 1 and M, inclusive.

Input

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

N M
B_1 \ldots B_N

Output

Print the answer.


Sample Input 1

3 3
2 -1 1

Sample Output 1

6

There are three possible sequences B': (2,1,1), (2,2,1), and (2,3,1).

When B' = (2,1,1), an edge is drawn only between vertices 2 and 3, so the number of connected components is 2. Thus, f(B') = 2.

Similarly, f(B') = 2 for B' = (2,2,1) and f(B') = 2 for B' = (2,3,1), so the answer is 2 + 2 + 2 = 6.


Sample Input 2

10 8
-1 7 -1 -1 -1 2 -1 1 -1 2

Sample Output 2

329785

Sample Input 3

11 12
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

Sample Output 3

529513150

Remember to find the sum modulo 998244353.

C - 1 Loop Bubble Sort

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 800

問題文

(1,\ldots,N) の順列 P=(P_1,\ldots,P_N) に対して,以下の操作を一度行うことにより得られる順列を P'=(P'_1,\ldots,P'_N) とします.

  • i=1,2,\ldots,N-1 の順に,P_i>P_{i+1} ならば P_iP_{i+1} を入れ替える.

長さ N の数列 Q=(Q_1,\ldots,Q_N) が与えられます. Q_i-1 または 1 以上 N 以下の整数 です.

全ての i について Q_i\neq -1 ならば Q_i=P'_i を満たすような (1,\ldots,N) の順列 P の個数を 998244353 で割ったあまりを求めてください.

制約

  • 入力される数値は全て整数
  • 2 \leq N \leq 5000
  • Q_i-1 または 1 以上 N 以下の整数
  • Q1,2,\ldots,N が含まれる個数はそれぞれ 1 個以下

入力

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

N 
Q_1 \ldots Q_N

出力

答えを出力せよ.


入力例 1

4
-1 -1 2 4

出力例 1

6

例えば P=(3,1,4,2) は条件を満たします.これは以下の操作の挙動から確認できます.

  • i=1 のとき,P_1 > P_2 なので P_1P_2 を入れ替え,P=(1,3,4,2) となる.
  • i=2 のとき,P_2 < P_3 なので 何もしない.
  • i=3 のとき,P_3 > P_4 なので P_3P_4 を入れ替え,P=(1,3,2,4) となる.
  • 以上より P'=(1,3,2,4) となり,P'_3=2,P'_4=4 を満たす.

条件を満たす P は以下の 6 個です.

  • (1,3,4,2)
  • (1,4,3,2)
  • (3,1,4,2)
  • (3,4,1,2)
  • (4,1,3,2)
  • (4,3,1,2)

入力例 2

6
-1 -1 -1 -1 2 -1

出力例 2

120

入力例 3

15
-1 -1 -1 -1 -1 4 -1 -1 -1 -1 7 -1 -1 -1 -1

出力例 3

237554682

個数を 998244353 で割ったあまりを求めることに注意してください.

Score : 800 points

Problem Statement

For a permutation P = (P_1, \ldots, P_N) of (1, \ldots, N), let P' = (P'_1, \ldots, P'_N) be the permutation obtained by performing the following operation once.

  • For i = 1, 2, \ldots, N-1 in this order, if P_i > P_{i+1}, swap P_i and P_{i+1}.

You are given a sequence Q = (Q_1, \ldots, Q_N) of length N. Each Q_i is -1 or an integer between 1 and N, inclusive.

Find the number, modulo 998244353, of permutations P of (1, \ldots, N) such that, for every i, if Q_i \neq -1 then Q_i = P'_i.

Constraints

  • All input numbers are integers.
  • 2 \leq N \leq 5000
  • Each Q_i is -1 or an integer between 1 and N.
  • Each of 1,2,\ldots,N appears at most once in Q.

Input

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

N 
Q_1 \ldots Q_N

Output

Print the answer.


Sample Input 1

4
-1 -1 2 4

Sample Output 1

6

For example, P = (3,1,4,2) satisfies the conditions. This can be confirmed by the following behavior of the operation.

  • For i=1, since P_1 > P_2, swap P_1 and P_2, resulting in P = (1,3,4,2).
  • For i=2, since P_2 < P_3, do nothing.
  • For i=3, since P_3 > P_4, swap P_3 and P_4, resulting in P = (1,3,2,4).
  • Thus, P' = (1,3,2,4), satisfying P'_3=2 and P'_4=4.

There are six permutations P that satisfy the conditions:

  • (1,3,4,2)
  • (1,4,3,2)
  • (3,1,4,2)
  • (3,4,1,2)
  • (4,1,3,2)
  • (4,3,1,2)

Sample Input 2

6
-1 -1 -1 -1 2 -1

Sample Output 2

120

Sample Input 3

15
-1 -1 -1 -1 -1 4 -1 -1 -1 -1 7 -1 -1 -1 -1

Sample Output 3

237554682

Remember to find the count modulo 998244353.

D - Many Easy Optimizations

Time Limit: 5 sec / Memory Limit: 1024 MiB

配点 : 900

問題文

数列 Xコストを,(X の最大値 - X の最小値) で定義します.

長さ N の数列 A=(A_1,\ldots,A_N), B=(B_1,\ldots,B_N) が与えられるので,k=1,2,\ldots,N に対して次の問題を解いてください.

  • i 番目の要素 C_iA_i または B_i であるような数列 C=(C_1,\ldots,C_k) のコストとしてあり得る最小値を求めてください.

制約

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

入力

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

N 
A_1 \ldots A_N
B_1 \ldots B_N

出力

N 行出力せよ.

i\ (1\leq i\leq N) 行目には,k=i の場合の数列 C のコストとしてあり得る最小値を出力せよ.


入力例 1

3
8 11 10
7 6 1

出力例 1

0
1
3

k=1 の場合,C=(8) とすると C のコストは 0 となりこれが最小です.

k=2 の場合,C=(7,6) とすると C のコストは 1 となりこれが最小です.

k=3 の場合,C=(8,11,10) とすると C のコストは 3 となりこれが最小です.


入力例 2

10
43 35 36 58 25 7 61 4 96 3
55 29 88 15 99 49 67 57 92 49

出力例 2

0
8
8
23
28
33
36
36
64
64

Score : 900 points

Problem Statement

Define the cost of a sequence X as (the maximum value of X minus the minimum value of X).

You are given sequences A = (A_1, \ldots, A_N) and B = (B_1, \ldots, B_N) of length N. Solve the following problem for k = 1, 2, \ldots, N.

  • Find the minimum possible cost of the sequence C = (C_1, \ldots, C_k) whose i-th element C_i is A_i or B_i.

Constraints

  • All input numbers are integers.
  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq A_i, B_i \leq 10^9

Input

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

N 
A_1 \ldots A_N
B_1 \ldots B_N

Output

Print N lines.

The i-th line (1 \leq i \leq N) should contain the minimum possible cost of sequence C for k = i.


Sample Input 1

3
8 11 10
7 6 1

Sample Output 1

0
1
3

For k=1, if we choose C = (8), the cost of C is 0, which is the minimum.

For k=2, if we choose C = (7,6), the cost of C is 1, which is the minimum.

For k=3, if we choose C = (8,11,10), the cost of C is 3, which is the minimum.


Sample Input 2

10
43 35 36 58 25 7 61 4 96 3
55 29 88 15 99 49 67 57 92 49

Sample Output 2

0
8
8
23
28
33
36
36
64
64
E - Replace Triplets

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 1000

問題文

長さ N の数列 A=(A_1,\ldots,A_N) が与えられます.ここで N3 以上の整数です.

あなたは以下の操作を 0 回以上任意の回数行うことができます.

  • 1\leq i\leq N かつ A_i=A_{i+1}=A_{i+2} を満たす整数 i を選ぶ.A_i,A_{i+1},A_{i+2} のうちいずれか 2 つをそれぞれ 1 以上 N 以下の整数で置き換える.ここで,A_{N+1}=A_1,A_{N+2}=A_2 とする.

最終的に得られる A のうち,A(1,\ldots,N) の順列であるものの個数を 998244353 で割ったあまりを求めてください.

制約

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

入力

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

N 
A_1 \ldots A_N

出力

答えを出力せよ.


入力例 1

6
1 2 3 3 1 1

出力例 1

360

例えば,最終的に A=(1,2,4,3,5,6) という順列を以下の手順で得ることができます.

  • i=5 として操作を行う.A_53 で,A_66 で置き換える.A=(1,2,3,3,3,6) となる.
  • i=3 として操作を行う.A_34 で,A_55 で置き換える.A=(1,2,4,3,5,6) となる.

最終的に得られる A であって (1,\ldots,6) の順列であるものの個数は 360 個です.


入力例 2

5
3 1 3 4 1

出力例 2

0

最終的に得られる A であって (1,\ldots,5) の順列であるものは存在しません.


入力例 3

10
1 1 1 8 8 8 7 7 7 10

出力例 3

604800

Score : 1000 points

Problem Statement

You are given a sequence A = (A_1, \ldots, A_N) of length N. Here, N is an integer not less than 3.

You can perform the following operation any number of times (zero or more).

  • Choose an integer i satisfying 1 \leq i \leq N and A_i = A_{i+1} = A_{i+2}. Replace two of A_i, A_{i+1}, and A_{i+2} with integers between 1 and N, inclusive. Here, assume A_{N+1} = A_1 and A_{N+2} = A_2.

Find the number, modulo 998244353, of possible resulting sequences A that are permutations of (1, \ldots, N).

Constraints

  • All input numbers are integers.
  • 3 \leq N \leq 5 \times 10^5
  • 1 \leq A_i \leq N

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

6
1 2 3 3 1 1

Sample Output 1

360

For example, we can obtain the permutation A = (1,2,4,3,5,6) through the following steps.

  • Perform the operation with i=5. Replace A_5 with 3 and A_6 with 6. Now, A = (1,2,3,3,3,6).
  • Perform the operation with i=3. Replace A_3 with 4 and A_5 with 5. Now, A = (1,2,4,3,5,6).

There are 360 possible resulting sequences A that are permutations of (1, \ldots, 6).


Sample Input 2

5
3 1 3 4 1

Sample Output 2

0

There are no possible resulting sequences A that are permutations of (1, \ldots, 5).


Sample Input 3

10
1 1 1 8 8 8 7 7 7 10

Sample Output 3

604800