A - Range Replace

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

長さ N の数列 A=(A_1,\ldots,A_N) が与えられます.あなたは以下の操作をちょうど一回行います.

  • 1\leq L \leq R \leq N なる整数組 (L,R) を選ぶ.A_L,A_{L+1},\ldots,A_R を全て A_L で置き換える.

操作後の A として考えられる数列は何通りですか.

制約

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

入力

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

N 
A_1 \ldots A_N

出力

答えを出力せよ.


入力例 1

4
1 1 2 3

出力例 1

4

操作後に考えられる数列は以下の 4 個です.

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

例えば,(1,1,1,3)L=2,R=3 として操作を行うことで得られます.


入力例 2

10
2 5 6 5 2 1 7 9 7 2

出力例 2

41

Score : 400 points

Problem Statement

You are given a sequence A=(A_1,\ldots,A_N) of length N. You perform the following operation exactly once.

  • Choose a pair of integers (L,R) such that 1\leq L \leq R \leq N. Replace each of A_L,A_{L+1},\ldots,A_R with A_L.

How many different sequences are possible as A after the operation?

Constraints

  • All input values are integers.
  • 1 \leq N \leq 10^6
  • 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

Output the answer.


Sample Input 1

4
1 1 2 3

Sample Output 1

4

The possible sequences after the operation are the following four sequences:

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

For example, (1,1,1,3) can be obtained by performing the operation with L=2,R=3.


Sample Input 2

10
2 5 6 5 2 1 7 9 7 2

Sample Output 2

41
B - Slime Swap

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

N 匹のスライムが一列に並んでいます.左から i 番目のスライムの大きさは P_i で,色は C_i です.ここで,スライムの大きさは相異なります.

スライムの列が以下の条件を満たすとき良い列と呼びます.

  • 隣接する異なる色のスライム二匹の位置を入れ替える操作を繰り返して,スライムを大きさの昇順に並べることができる.

あなたはスライムの列を良い列にするため,以下の操作を 0 回以上好きな回数行うことができます.

  • スライムを一匹選び,そのスライムの色を 1 以上 N 以下の好きな整数に変える.この操作には,この操作直前のスライムの色を x としたとき,x のコストがかかる.

スライムの列を良い列にするために必要なコストの総和の最小値を求めてください.

制約

  • 入力される数値は全て整数
  • 1 \leq N \leq 2\times 10^5
  • 1\leq P_i \leq N
  • 1\leq C_i \leq N
  • P(1,\ldots,N) の順列

入力

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

N
P_1 \ldots P_N
C_1 \ldots C_N

出力

答えを出力せよ.


入力例 1

4
3 1 2 4
1 2 1 3

出力例 1

1

3 番目のスライムの色を 2 に変えます.すると,1 番目のスライムと 2 番目のスライムを入れ替え,2 番目のスライムと 3 番目のスライムを入れ替えることで,スライムを大きさの昇順に並べることができるようになり,良い列となります.

必要なコストは 3 番目のスライムの操作直前の色が 1 なので, 1 です.


入力例 2

10
3 7 10 1 9 5 4 8 6 2
6 4 8 4 6 8 8 4 8 8

出力例 2

28

Score : 500 points

Problem Statement

N slimes are arranged in a line. The size of the i-th slime from the left is P_i, and its color is C_i. Here, the sizes of the slimes are distinct.

A sequence of slimes is called a good sequence when it satisfies the following condition:

  • By repeatedly performing the operation of swapping the positions of two adjacent slimes of different colors, it is possible to arrange the slimes in ascending order of size.

To make the sequence of slimes a good sequence, you can perform the following operation any number of times (possibly zero):

  • Choose a slime, and change the color of the slime to any integer between 1 and N, inclusive. This operation costs x, where x is the color of the slime immediately before this operation.

Find the minimum total cost required to make the sequence of slimes a good sequence.

Constraints

  • All input values are integers.
  • 1 \leq N \leq 2\times 10^5
  • 1\leq P_i \leq N
  • 1\leq C_i \leq N
  • P is a permutation of (1,\ldots,N).

Input

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

N
P_1 \ldots P_N
C_1 \ldots C_N

Output

Output the answer.


Sample Input 1

4
3 1 2 4
1 2 1 3

Sample Output 1

1

Change the color of the 3-rd slime to 2. Then, by swapping the 1-st and 2-nd slimes, and swapping the 2-nd and 3-rd slimes, it is possible to arrange the slimes in ascending order of size, making it a good sequence.

The required cost is 1, since the color of the 3-rd slime immediately before the operation was 1.


Sample Input 2

10
3 7 10 1 9 5 4 8 6 2
6 4 8 4 6 8 8 4 8 8

Sample Output 2

28
C - Tree Sequence

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 600

問題文

1 以上 N 以下の整数からなる長さ N の数列 B=(B_1,\ldots,B_N) が良い数列であるとは,以下の条件を満たすことをいいます.

  • 任意の区間 [l,r]\ (1\leq l \leq r \leq N) に対して,以下の条件が成り立つような整数 x\ (l\leq x\leq r) が存在する:
    • 頂点に 1 から N の番号が付いた N 頂点 0 辺のグラフを用意する.全ての l \leq i \leq r について,i\neq x ならば頂点 i と頂点 B_i の間に辺を張る.このとき 頂点 l, l+1,\ldots,r が木をなしている.つまり,頂点 l, l+1,\ldots,r が連結になっている.

各要素が 1 以上 N 以下の整数,または -1 であるような数列 A=(A_1,\ldots,A_N) が与えられます.A のうち -1 であるような要素を 1 以上 N 以下の整数に置き換える方法であって,A が良い数列になるようなものの個数を 998244353 で割ったあまりを求めてください.

制約

  • 入力される数値は全て整数
  • 1\leq N\leq 2\times 10^5
  • 1\leq A_i\leq N または A_i=-1

入力

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

N 
A_1 \cdots A_N

出力

A のうち -1 であるような要素を 1 以上 N 以下の整数に置き換える方法であって,A が良い数列になるようなものの個数を 998244353 で割ったあまりを出力せよ.


入力例 1

3
-1 -1 -1

出力例 1

7

条件を満たす置き換え方は以下の 7 個です.

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

例えば A=(1,1,2) の場合に区間 [1,2] が条件を満たすことは,x=1 とすると頂点 2 と頂点 A_2=1 が結ばれ頂点 1,2 が木をなすことから確認できます.

同様に区間 [1,1],[2,2],[3,3],[2,3],[1,3] も条件を満たすため,(1,1,2) は良い数列です.


入力例 2

3
-1 3 -1

出力例 2

3

条件を満たす置き換え方は以下の 3 個です.

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

入力例 3

6
2 3 -1 -1 -1 -1

出力例 3

21

Score : 600 points

Problem Statement

A sequence B=(B_1,\ldots,B_N) of length N consisting of integers between 1 and N (inclusive) is called a good sequence if and only if it satisfies the following condition:

  • For every interval [l,r]\ (1\leq l \leq r \leq N), there exists an integer x\ (l\leq x\leq r) such that the following condition holds:
    • Prepare a graph with N vertices numbered from 1 to N and zero edges. For each l \leq i \leq r, if i\neq x, add an edge between vertices i and B_i. Then, vertices l, l+1,\ldots,r form a tree. That is, vertices l, l+1,\ldots,r are connected.

You are given a sequence A=(A_1,\ldots,A_N) where each element is either an integer between 1 and N, or -1. Find the number, modulo 998244353, of ways to replace the elements that are -1 in A with integers between 1 and N such that A becomes a good sequence.

Constraints

  • All input values are integers.
  • 1\leq N\leq 2\times 10^5
  • 1\leq A_i\leq N or A_i=-1

Input

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

N 
A_1 \cdots A_N

Output

Output the number, modulo 998244353, of ways to replace the elements that are -1 in A with integers between 1 and N (inclusive) such that A becomes a good sequence.


Sample Input 1

3
-1 -1 -1

Sample Output 1

7

The replacements that satisfy the condition are the following seven:

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

For example, in the case of A=(1,1,2), that the interval [1,2] satisfies the condition can be confirmed by setting x=1, where vertices 2 and A_2=1 are connected and vertices 1,2 form a tree.

Similarly, intervals [1,1],[2,2],[3,3],[2,3],[1,3] also satisfy the condition, so (1,1,2) is a good sequence.


Sample Input 2

3
-1 3 -1

Sample Output 2

3

The replacements that satisfy the condition are the following three:

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

Sample Input 3

6
2 3 -1 -1 -1 -1

Sample Output 3

21
D - LIS ∩ LDS

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 700

問題文

(1,\ldots,N) の順列 P=(P_1,\ldots,P_N) の要素のうち,以下の条件を満たすものを良い要素と呼びます.

  • その要素は P の最長増加部分列にも最長減少部分列にも含まれうる.

整数 N,K が与えられます. 良い要素がちょうど K 個であるような (1,\ldots,N) の順列 P が存在するか判定し,存在するならば一つ求めてください.

T 個のテストケースについて答えてください.

制約

  • 入力される数値は全て整数
  • 1 \leq T \leq 2\times 10^5
  • 1 \leq N \leq 2\times 10^5
  • 0\leq K \leq N
  • 全てのテストケースにわたる N の総和は 2\times 10^5 を超えない.

入力

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

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる.

N K

出力

T 行出力せよ.

i 行目には,i 番目のテストケースについて,条件を満たす順列が存在しない場合,-1 を出力せよ. 存在する場合,その内一つを以下の形式で出力せよ.

P_1 \ldots P_N

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


入力例 1

3
1 0
7 2
2 1

出力例 1

-1
7 4 6 2 3 1 5
-1

2 番目のテストケースでは,P_4=2P_5=3 が良い要素です.

実際,P の最長増加部分列として, (2,3,5) が考えられます.また,P の最長減少部分列として, (7,6,2,1)(7, 6, 3, 1) が考えられます.

よって,この出力例において 23 は良い要素です.その他の要素は良い要素ではないので,この出力は条件を満たします.

1, 3 番目のテストケースでは,条件を満たす順列は存在しません.よって,-1 を出力します.

Score : 700 points

Problem Statement

Among the elements of a permutation P=(P_1,\ldots,P_N) of (1,\ldots,N), those that satisfy the following condition are called good elements:

  • The element can be included in both a longest increasing subsequence and a longest decreasing subsequence of P.

You are given integers N and K. Determine whether there exists a permutation P of (1,\ldots,N) such that there are exactly K good elements, and if it exists, find one.

Answer for T test cases.

Constraints

  • All input values are integers
  • 1 \leq T \leq 2\times 10^5
  • 1 \leq N \leq 2\times 10^5
  • 0\leq K \leq N
  • The sum of N over all test cases does not exceed 2\times 10^5.

Input

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

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N K

Output

Output T lines.

For the i-th line, if there does not exist a permutation satisfying the condition for the i-th test case, output -1. If it exists, output one of them in the following format:

P_1 \ldots P_N

If there are multiple solutions satisfying the condition, any of them will be accepted.


Sample Input 1

3
1 0
7 2
2 1

Sample Output 1

-1
7 4 6 2 3 1 5
-1

In the 2-nd test case, P_4=2 and P_5=3 are good elements.

Indeed, (2,3,5) is a longest increasing subsequence of P. Also, (7,6,2,1) and (7, 6, 3, 1) are longest decreasing subsequences of P.

Therefore, in this sample output, 2 and 3 are good elements. The other elements are not good elements, so this output satisfies the condition.

In the 1-st and 3-rd test cases, there does not exist a permutation satisfying the condition. Thus, output -1.

E - Rectangle Coloring

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 800

問題文

N\times N の盤面があります.ここで N4 以上です.盤面の最も外側かつ角でないマスを,良いマスと呼びます.より厳密には,1\leq i,j \leq N であって,i=1 または i=N または j=1 または j=N であるような (i,j) のうち,(1,1),(1,N),(N,1),(N,N) を除いたマスを良いマスと呼びます.ここで,(r,c)rc 列目のマスを指します.

全ての良いマスには整数が書いてあります.この情報は, 4 個の長さ N-2 の整数列 U=(U_1,\ldots,U_{N-2}),D=(D_1,\ldots,D_{N-2}),L=(L_1,\ldots,L_{N-2}),R=(R_1,\ldots,R_{N-2}) として与えられます.

この整数列の各要素は,以下のように良いマスに書かれた整数と対応しています.

  • U_i(1,i+1)
  • D_i(N,i+1)
  • L_i(i+1,1)
  • R_i(i+1,N)

また,盤面の全てのマスは初め白色で塗られています.あなたは以下の操作を何回でも行えます.

  • これまでの操作で一度も選ばれていない良いマスを二つ選ぶ.選んだ二つの良いマスを (r_1,c_1)(r_2,c_2) として,\min(r_1,r_2)\leq i \leq \max(r_1,r_2)かつ \min(c_1,c_2)\leq j \leq \max(c_1,c_2)を満たす全ての (i,j) に対して,(i,j) を黒く塗る. 操作にかかるコストは選んだ二つの良いマスに書かれている整数の和である.

全てのマスを黒くするために必要なコストの総和の最小値を求めてください.

T 個のテストケースについて答えてください.

制約

  • 入力される数値は全て整数
  • 1 \leq T \leq 12500
  • 4 \leq N \leq 5\times 10^4
  • 0\leq U_i,D_i,L_i,R_i \leq 10^9
  • 全てのテストケースにわたる N の総和は 5\times 10^4 を超えない

入力

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

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる.

N
U_1 \ldots U_{N-2}
D_1 \ldots D_{N-2}
L_1 \ldots L_{N-2}
R_1 \ldots R_{N-2}

出力

T 行出力せよ.

i 行目には,i 番目のテストケースの答えを出力せよ.


入力例 1

3
4
1 2
5 6
7 8
3 4
6
6 2 10 2
7 1 3 0
2 4 4 2
2 10 6 4
8
5 0 6 10 1 8
5 5 9 9 4 7
7 3 4 9 7 6
10 9 0 4 10 5

出力例 1

36
15
21

1 番目のテストケースでは,

  • (1,2)(3,4) を選んで操作
  • (2,4)(4,2) を選んで操作
  • (4,3)(2,1) を選んで操作
  • (3,1)(1,3) を選んで操作

とすると,全てのマスを黒くすることができます.コストの総和は,(1+4)+(3+5)+(6+7)+(8+2)=36 です.

2 番目,3 番目のテストケースについて,入力される盤面を図示すると次のようになります.

Score : 800 points

Problem Statement

There is an N\times N board, where N is at least 4. The cells on the outermost edge of the board that are not corners are called good cells. More formally, among (i,j) such that 1\leq i,j \leq N and i=1 or i=N or j=1 or j=N, the cells excluding (1,1),(1,N),(N,1),(N,N) are called good cells. Here, (r,c) refers to the cell in row r and column c.

All good cells have integers written on them. This information is given as four integer sequences of length N-2: U=(U_1,\ldots,U_{N-2}),D=(D_1,\ldots,D_{N-2}),L=(L_1,\ldots,L_{N-2}),R=(R_1,\ldots,R_{N-2}).

Each element of these integer sequences corresponds to the integer written on a good cell as follows:

  • U_i: (1,i+1)
  • D_i: (N,i+1)
  • L_i: (i+1,1)
  • R_i: (i+1,N)

Also, all cells on the board are initially colored white. You can perform the following operation any number of times:

  • Choose two good cells that have never been chosen in any previous operation. Let the two chosen good cells be (r_1,c_1) and (r_2,c_2). For all (i,j) satisfying \min(r_1,r_2)\leq i \leq \max(r_1,r_2) and \min(c_1,c_2)\leq j \leq \max(c_1,c_2), color (i,j) black. The cost of the operation is the sum of the integers written on the two chosen good cells.

Find the minimum total cost required to color all cells black.

Answer for T test cases.

Constraints

  • All input values are integers.
  • 1 \leq T \leq 12500
  • 4 \leq N \leq 5\times 10^4
  • 0\leq U_i,D_i,L_i,R_i \leq 10^9
  • The sum of N over all test cases does not exceed 5\times 10^4.

Input

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

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N
U_1 \ldots U_{N-2}
D_1 \ldots D_{N-2}
L_1 \ldots L_{N-2}
R_1 \ldots R_{N-2}

Output

Output T lines.

For the i-th line, output the answer for the i-th test case.


Sample Input 1

3
4
1 2
5 6
7 8
3 4
6
6 2 10 2
7 1 3 0
2 4 4 2
2 10 6 4
8
5 0 6 10 1 8
5 5 9 9 4 7
7 3 4 9 7 6
10 9 0 4 10 5

Sample Output 1

36
15
21

In the 1-st test case, you can color all cells black as follows:

  • Perform the operation choosing (1,2) and (3,4).
  • Perform the operation choosing (2,4) and (4,2).
  • Perform the operation choosing (4,3) and (2,1).
  • Perform the operation choosing (3,1) and (1,3).

The total cost is (1+4)+(3+5)+(6+7)+(8+2)=36.

For the 2-nd and 3-rd test cases, the input boards are illustrated below: