E1. Stop Gaming (Easy Version) time limit per test...
Creado el: 26 de abril de 2025
Creado el: 26 de abril de 2025
E1. Stop Gaming (Easy Version)
time limit per test3 seconds
memory limit per test256 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.
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 ai,j
. It is guaranteed that all ai,j
are pairwise distinct. In one operation, you can do the following:
Choose some integer i
(1≤i≤n
) and an integer x
(1≤x≤2⋅n⋅m
).
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 bi,j
. It is guaranteed that all bi,j
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≤104
) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n
and m
(1≤n,m≤3⋅105
) — the number of arrays and the number of elements in each array.
The i
-th of the following n
lines contains m
integers ai,1,ai,2,…,ai,m
(1≤ai,j≤2⋅n⋅m
) — the elements of the i
-th original array. It is guaranteed that all ai,j
are pairwise distinct.
The i
-th of the following n
lines contains m
integers bi,1,bi,2,…,bi,m
(1≤bi,j≤2⋅n⋅m
) — the elements of the i
-th final array. It is guaranteed that all bi,j
are pairwise distinct.
It is guaranteed that the sum of n⋅m
over all test cases does not exceed 3⋅105
.
Output
For each test case, output a single integer — the minimum number of operations that need to be performed.
Example
Input
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
3
5
3
6
Note
In the first test case, the following sequence of 3
operations is suitable:
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 both arrays look like [1,2]
and [6,3]
respectively after the first operation.
Apply the operation to the second array with x=8
. Then the first array remains unchanged, and both arrays will look like [1,2]
and [8,6]
respectively.
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:
Apply the operation with x=11
to the first array.
Apply the operation with x=12
to the second array.
Apply the operation with x=13
to the third array.
Key observations
notation | meaning |
---|---|
n , m | number of arrays, length of each array |
Aᵢ | current content of the i-th array (will be updated on the fly) |
Bᵢ | desired content of the i-th array |
kᵢ | number of operations we finally execute starting at array i |
What a single operation really does
Starting at array i
we
Aᵢ
,Aᵢ
and push it to the front of Aᵢ₊_1
,After k consecutive operations on the same row,
m − k
items of the array before the first operation slide right and form the suffix;k
items of that old array disappear – they are exactly the elements forwarded to Aᵢ₊_1
.How many operations does one row really need?
Let
L = longest L ≥ 0 s.t. prefix_L( Aᵢ ) == suffix_L( Bᵢ )
(i.e. the longest prefix of the current array that already sits at the right end
of the target array).
Then the minimal number of operations for this row is
kᵢ = m – L (1)
because we have to get rid of the last kᵢ
elements of Aᵢ
and insert the kᵢ
missing prefix elements of Bᵢ
.
We can find L
in O(m) with a standard prefix-function/Z-algorithm:
textS = Aᵢ + [0] + Bᵢ // 0 is a delimiter not appearing in data π = prefix_function(S) L = π.back() // the very last value
π.back()
is precisely the length of the longest prefix of Aᵢ
that appears as a suffix of Bᵢ
.
Updating the next row without simulating every hop
Before we start working on row i+1
, its content must already include the
kᵢ
elements that row i has just forwarded.
Those elements are the last kᵢ
items of Aᵢ
before the operations,
and they maintain their order.
texttail = last kᵢ elements of the old Aᵢ keep = first m−kᵢ elements of the current Aᵢ₊_1 (they will slide right) Aᵢ₊_1 = tail • keep (2)
Because we update one whole row at a time, the total amount of copying over the
entire test case is Σ m = n·m ≤ 3·10^5
, perfectly within the limit.
Why the greedy “row-by-row minimum” is globally optimal
Every operation costs exactly 1, no matter in which row we perform it. If we did more than the minimum on some row, we would throw away an element that already matched its target suffix. That extra operation can at most save one later operation (because it forwards one element). So the overall sum of operations can never decrease by overspending earlier. Hence the greedy choice (1) is optimal.
textans = 0 cur = original arrays A[1…n] // vectors of size m each for i = 1 .. n L = longest prefix of cur[i] matching suffix of B[i] // KMP / Z, O(m) k = m - L ans += k // prepare the (i+1)-st array, if it exists if i < n tail = last k elements of cur[i] // copy, size k keep = first m-k elements of cur[i+1] // copy, size m-k cur[i+1] = tail + keep // (2) // row i is done; replace it by its target (optional) cur[i] = B[i] output ans
cpp#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; // test cases cin >> T; const int SEP = 0; // separator (all aᵢⱼ ≥ 1) while (T--) { int n, m; cin >> n >> m; vector<vector<int>> cur(n, vector<int>(m)); for (auto &row : cur) for (int &x : row) cin >> x; vector<vector<int>> B(n, vector<int>(m)); for (auto &row : B) for (int &x : row) cin >> x; long long ans = 0; auto pref_func = [](const vector<int>& s) { vector<int> pi(s.size()); for (size_t i = 1; i < s.size(); ++i) { int j = pi[i - 1]; while (j > 0 && s[i] != s[j]) j = pi[j - 1]; if (s[i] == s[j]) ++j; pi[i] = j; } return pi; }; for (int i = 0; i < n; ++i) { /* ---- 1. longest overlap L ---- */ vector<int> comb; comb.reserve(2 * m + 1); comb.insert(comb.end(), cur[i].begin(), cur[i].end()); comb.push_back(SEP); comb.insert(comb.end(), B[i].begin(), B[i].end()); int L = pref_func(comb).back(); // overlap length int k = m - L; ans += k; /* ---- 2. forward the tail to the next row ---- */ if (i + 1 < n && k > 0) { const auto tailBeg = cur[i].end() - k; vector<int> next; next.reserve(m); next.insert(next.end(), tailBeg, cur[i].end()); // tail next.insert(next.end(), cur[i + 1].begin(), cur[i + 1].begin() + (m - k)); // keep cur[i + 1].swap(next); } /* ---- 3. row i reaches its final form (not needed later) --- */ cur[i].swap(B[i]); } cout << ans << '\n'; } return 0; }
The code follows exactly the steps explained above and runs comfortably within the 3 s / 256 MB limits for the whole easy version.