**E1. Stop Gaming (Easy Version)** **Time limit p...
Creado el: 20 de abril de 2025
Respondido usando O4 Mini High por Chat01
Creado el: 20 de abril de 2025
Respondido usando O4 Mini High por Chat01
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:
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:
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:
text4 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:
text3 5 3 6
Note
In the first test case, the following sequence of 3 operations is suitable:
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:
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
textA[1…N] ← a_1_1, a_1_2, …, a_1m, a_2_1,…, a_nm B[1…N] ← 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:
pos[A[i]] == 0
(A[i] doesn’t occur in B),D[i] < D[i−1]
(the required displacement would go backwards),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:
cppblock = (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 ⇒ 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; }
A
can remain “in place” after doing the minimal number of cascading shifts?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]
.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.m
), otherwise you’d be “stretching” inside one row without having inserted an extra slot there.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).
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).