#P17196. [KOI 2026 #2] 序列运算

    ID: 19512 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 显示难度提高 上传者: 标签>Special Judge2026KOI(韩国)

[KOI 2026 #2] 序列运算

Problem Description

You are given a sequence A=[A1,A2,⋯ ,AN]A=[A_1,A_2,\cdots,A_N] of length NN and a sequence B=[B1,B2,⋯ ,BM]B=[B_1,B_2,\cdots,B_M] of length MM.

Sequence AA is a sequence formed by each of the integers 1,2,⋯ ,N1,2,\cdots,N appearing exactly once. Each element of sequence BB is between 11 and NN, and all elements are pairwise distinct.

Let the sequence to be operated on be XX. Initially, sequence XX is the same as sequence AA. You may perform the following two operations on XX any number of times (possibly zero times).

  • Swap operation

    Let X=[X1,X2,⋯ ,XK]X=[X_1,X_2,\cdots,X_K].

    Choose an integer ii such that 1≤i≤K−11 \le i \le K-1 and Xi<Xi+1X_i<X_{i+1}, and swap the values of the adjacent elements XiX_i and Xi+1X_{i+1}.

  • Merge operation

    Let X=[X1,X2,⋯ ,XK]X=[X_1,X_2,\cdots,X_K].

    Choose an integer ii such that 1≤i≤K−11 \le i \le K-1, and merge the adjacent elements XiX_i and Xi+1X_{i+1} into a single element min⁡(Xi,Xi+1)\min(X_i,X_{i+1}).

    In other words, after performing this operation, the length of sequence XX decreases by 11.

Determine whether it is possible to transform sequence XX into sequence BB using the given operations. If it is possible, output any sequence of operations that transforms XX into BB.

Note that you do not need to minimize the number of operations.

Input Format

The first line contains two integers NN and MM separated by spaces.

The second line contains NN integers A1,A2,⋯ ,ANA_1,A_2,\cdots,A_N separated by spaces.

The third line contains MM integers B1,B2,⋯ ,BMB_1,B_2,\cdots,B_M separated by spaces.

Output Format

If it is impossible to transform sequence XX into sequence BB, output NO on the first line.

If it is possible to transform sequence XX into sequence BB, output in the following format.

Output YES on the first line.

Output the number of operations QQ on the second line. QQ must satisfy:

0≤Q≤N20 \le Q \le N^2

In the next QQ lines, output each operation in the order performed. Each operation must use one of the following two formats.

  • 1 i

    Perform the swap operation on the ii-th and (i+1)(i+1)-th elements of sequence XX.

    Let the length of sequence XX before this operation be KK. Then it must satisfy 1≤i≤K−11 \le i \le K-1, and the value of the ii-th element of sequence XX must be less than the value of the (i+1)(i+1)-th element.

  • 2 i

    Perform the merge operation on the ii-th and (i+1)(i+1)-th elements of sequence XX.

    Let the length of sequence XX before this operation be KK. Then it must satisfy 1≤i≤K−11 \le i \le K-1.

All positions are based on sequence XX before performing the corresponding operation, and the first position in the sequence is numbered 11.

After executing all operations in the given output in order, the resulting sequence must be exactly the same as sequence BB.

If multiple valid outputs exist, any one of them is accepted.

If sequence XX can be transformed into sequence BB using the given operations, it can be proven that there always exists an output satisfying all the conditions above.

4 2
1 4 2 3
3 1
YES
6
1 1
1 2
1 3
2 1
1 1
2 2
2 1
1 2
2
NO
4 4
3 2 1 4
3 1 2 4
NO
4 2
1 3 2 4
1 3
NO

Hint

Sample 1 Explanation

Sequence XX changes as follows:

$[1,4,2,3]\to[4,1,2,3]\to[4,2,1,3]\to[4,2,3,1]\to[2,3,1]\to[3,2,1]\to[3,1]$

Therefore, sequence XX can be transformed into sequence BB.

Constraints

  • All given numbers are integers.
  • 1≤M≤N≤3 0001 \le M \le N \le 3\,000
  • Sequence AA is a permutation of 1,2,⋯ ,N1,2,\cdots,N, i.e. {A1,A2,⋯ ,AN}={1,2,⋯ ,N}\{A_1,A_2,\cdots,A_N\}=\{1,2,\cdots,N\}.
  • For each integer ii (1≤i≤M1 \le i \le M), 1≤Bi≤N1 \le B_i \le N.
  • B1,B2,⋯ ,BMB_1,B_2,\cdots,B_M are pairwise distinct.

Subtasks

  1. (77 points) N≤8N \le 8.
  2. (88 points) M=1M=1.
  3. (1212 points) M=NM=N.
  4. (1010 points) For each integer ii (1≤i≤N1 \le i \le N), Ai=iA_i=i.
  5. (1313 points) M=N−1M=N-1.
  6. (1515 points) Sequence BB is a subsequence of sequence AA. In other words, there exist integers p1,p2,⋯ ,pMp_1,p_2,\cdots,p_M satisfying 1≤p1<p2<⋯<pM≤N1 \le p_1<p_2<\cdots<p_M \le N such that for each integer ii (1≤i≤M1 \le i \le M), Bi=ApiB_i=A_{p_i}.
  7. (3030 points) N≤300N \le 300.
  8. (55 points) No additional constraints.

Translated by ChatGPT 5