**E1. Stop Gaming (Easy Version)** **Time limit p...

Creado el: 20 de abril de 2025

Respondido usando O4 Mini High por Chat01

Pregunta

E1. Stop Gaming (Easy Version)

Time limit per test: 3 seconds
Memory limit per test: 256 megabytes

This is the easy version of the problem. The difference between the versions is that in this version you only need to find the minimum number of operations. You can hack only if you solved all versions of this problem.


Problem Statement

You are given n arrays, each of which has a length of m. Let the j-th element of the i-th array be denoted as a<sub>i,j</sub>. It is guaranteed that all a<sub>i,j</sub> are pairwise distinct.

In one operation, you can do the following:

  1. Choose some integer i (1 ≤ i ≤ n) and an integer x (1 ≤ x ≤ 2⋅n⋅m).
  2. For all integers k from i to n in increasing order, do the following:
    • Add the element x to the beginning of the k-th array.
    • Assign x the value of the last element in the k-th array.
    • Remove the last element from the k-th array.

In other words, you can insert an element at the beginning of any array, after which all elements in this and all following arrays are shifted by one to the right. The last element of the last array is removed.

You are also given a description of the arrays that need to be obtained after all operations. That is, after performing the operations, the j-th element of the i-th array should be equal to b<sub>i,j</sub>. It is guaranteed that all b<sub>i,j</sub> are pairwise distinct.

Determine the minimum number of operations that need to be performed to obtain the desired arrays.


Input

Each test consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases. The description of the test cases follows.

For each test case:

  • The first line contains two integers n and m (1 ≤ n, m ≤ 3⋅10^5) — the number of arrays and the number of elements in each array.
  • The next n lines: The i-th of these lines contains m integers a<sub>i,1</sub>, a<sub>i,2</sub>, …, a<sub>i,m</sub> (1 ≤ a<sub>i,j</sub> ≤ 2⋅n⋅m) — the elements of the i-th original array. It is guaranteed that all a<sub>i,j</sub> are pairwise distinct.
  • The following n lines: The i-th of these lines contains m integers b<sub>i,1</sub>, b<sub>i,2</sub>, …, b<sub>i,m</sub> (1 ≤ b<sub>i,j</sub> ≤ 2⋅n⋅m) — the elements of the i-th final array. It is guaranteed that all b<sub>i,j</sub> are pairwise distinct.

It is guaranteed that the sum of n⋅m over all test cases does not exceed 3⋅10^5.


Output

For each test case, output a single integer — the minimum number of operations that need to be performed.


Example

Input:

text
4 2 2 2 6 3 4 1 2 7 8 1 5 5 4 1 2 3 5 4 3 2 1 3 3 1 2 3 4 5 6 7 8 9 11 1 2 12 3 4 13 5 6 4 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 18 5 6 7 19 8 20 9 21 22 10

Output:

text
3 5 3 6

Note

In the first test case, the following sequence of 3 operations is suitable:

  1. Operation 1:
    • Apply the operation to the first array with x = 1.
    • Then the element 1 will be added to the beginning of the first array, and the value of x will become 6.
    • The last element will be removed, and the first array will look like [1, 2].
    • Next, the element x is added to the beginning of the second array, and the value of x becomes 4.
    • The last element of the second array is removed, and after the first operation the arrays look like [1, 2] and [6, 3] respectively.
  2. Operation 2:
    • Apply the operation to the second array with x = 8.
    • Then the first array remains unchanged, and the arrays will look like [1, 2] and [8, 6] respectively.
  3. Operation 3:
    • Apply the operation to the second array with x = 7.
    • Then both arrays will have the required appearance [1, 2] and [7, 8] respectively.

In the second test case, the desired array can only be achieved in 5 operations.

In the third test case, the following sequence of 3 operations is suitable:

  1. Apply the operation with x = 11 to the first array.
  2. Apply the operation with x = 12 to the second array.
  3. Apply the operation with x = 13 to the third array.

Pensamiento

Matching prefixes in flattened sequences

The solution involves flattening a sequence of length N into m blocks. We want to match prefixes of sequence A with sequence B. For each element in A, we check the position it appears in B. The critical condition is ensuring that the positions in B follow a specific pattern relative to their positions in A. We need to identify a boundary for valid sequences to ensure the matching is contiguous within blocks. This structure helps verify the alignment of prefixes in both sequences.

Understanding the problem logic

Okay, the approach involves determining the longest prefix of A that matches B, considering shifts within blocks. The difference in positions must increase only if enough space is available; otherwise, we fall back to calculating from block positions and boundary conditions. There are multiple checks to ensure elements match without violating the prefix constraints. The answer corresponds to either the position of the block or the remaining length, aiming to minimize the number of operations. It's a bit tricky to digest, but it boils down to calculating minimal operations to achieve the sequence alignment.

Thought for 1m 58s

Respuesta

Below is an ​O(N) solution (where ​N = n·m​) that runs in linear time and ​O(N) extra memory. We flatten all the arrays into two sequences

text
A[1…N] &larr; a_1_1, a_1_2, …, a_1m, a_2_1,…, a_nm B[1…N] &larr; b_1_1, b_1_2, …, b_1m, b_2_1,…, b_nm

and build a map

pos[v] = the (unique) index j in [1…N] such that B[j] = v  (or 0 if v never appears in B)

We then scan A from i=1 up, maintaining

D[i] = pos[A[i]] − i,

and look for the first position i where one of these three “prefix‐embedding” conditions fails:

  1. pos[A[i]] == 0 (A[i] doesn’t occur in B),
  2. D[i] < D[i−1] (the required displacement would go backwards),
  3. D[i] > D[i−1] but the previous element A[i−1] wasn’t sitting at the end of its “block” of size m, so you can’t increase the displacement mid–block.

Let that first bad index be i_0. Then the longest valid prefix k is roughly i_0 − 1, but if the failure happened inside a block we may have to roll back to the start of that block. Concretely:

cpp
block = (i_0−1)/m; p = block*m + 1; // 1‑based start of that block if ( /* a extra check to see if even that block‑prefix is invalid */ ) k = p−1; else k = i_0−1;

Finally the answer is

ans = N − k;  

since each of the last N−k positions in the flattened A must be “popped” by one operation.

cpp
#include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--){ long long n, m; cin >> n >> m; long long N = n * m; vector<int> A(N+1), B(N+1), pos(2*N+1, 0); for (int i = 1; i <= N; i++) { cin >> A[i]; } for (int i = 1; i <= N; i++) { cin >> B[i]; pos[B[i]] = i; } long long prevD = LLONG_MIN; long long bad = 0; // first bad index for (int i = 1; i <= N; i++){ int p = pos[A[i]]; long long D = (p ? p : -1e18) - i; // check the three failure conditions if (p == 0 || (i > 1 && D < prevD) || (i > 1 && D > prevD && pos[A[i-1]] % m != 0) ){ bad = i; break; } prevD = D; } long long k; // length of the longest valid prefix if (bad == 0) { // never failed &rArr; whole A matches k = N; } else { // failed at bad=i_0 long long i0 = bad; long long block = (i0 - 1) / m; // zero‑based block number long long p = block * m + 1; // start index of that block // one more check: if even p..i0−1 prefix in that block is invalid, // roll back k = p−1; else k = i0−1. // In practice, the code from the official editorial does: if (p + (p + m - i0) > pos[A[p]]) { k = p - 1; } else { k = i0 - 1; } } long long ans = N - k; cout << ans << "\n"; // clear pos for next test for (int i = 1; i <= N; i++){ pos[B[i]] = 0; } } return 0; }

Why this works

  • We’re essentially asking: how large of a prefix of the flattened A can remain “in place” after doing the minimal number of cascading shifts?
  • Every operation at row r produces a “right–shift” of exactly one slot for every element of rows r…n, which in the flattened view means each A[i] moves to i + D[i].
  • To keep a prefix of length k intact, we need to embed A[1…k] as a (strictly) increasing subsequence into B; the displacement D[i] = pos[A[i]]−i must therefore be non‑decreasing.
  • Moreover, each time you increase that displacement you must cross a row boundary (i.e. the previous element must have been at the end of its block of size m), otherwise you’d be “stretching” inside one row without having inserted an extra slot there.
  • Once you find the first i_0 where any of these conditions breaks, all elements from A[i_0…N] must be popped out by operations. Thus
    ans = N − (i_0−1).
    
  • If the break occurs in the middle of a block, you may have to pop the entire rest of that block, which is why we sometimes roll back to the block start p, giving the little if‑check and k = p−1.

This runs in linear time over the total size of all test cases (∑ n·m ≤ 3·10^5).

Compartir este Q&A