time limit per test3 seconds memory limit per test256 megabytes I'm peakly productive…
- User
time limit per test3 seconds memory limit per test256 megabytes I'm peakly productive and this is deep. You are given two permutations∗ a and b , both of length n .
You can perform the following three-step operation on permutation a :
Choose an index i (1≤i≤n ). Cyclic shift a1,a2,…,ai−1 by 1 to the right. If you had chosen i=1 , then this range doesn't exist, and you cyclic shift nothing. Cyclic shift ai+1,ai+2,…,an by 1 to the right. If you had chosen i=n , then this range doesn't exist, and you cyclic shift nothing. After the operation, a1,a2,…,ai−2,ai−1,ai,ai+1,ai+2,…,an−1,an is transformed into ai−1,a1,…,ai−3,ai−2,ai,an,ai+1,…,an−2,an−1 .
Here are some examples of operations done on the identity permutation [1,2,3,4,5,6,7] of length 7 :
If we choose i=3 , it will become [2,1,3,7,4,5,6] . If we choose i=1 , it will become [1,7,2,3,4,5,6] . If we choose i=7 , it will become [6,1,2,3,4,5,7] . Notably, position i is not shifted. Find a construction using at most 2n operations to make a equal to b or print −1 if it is impossible. The number of operations does not need to be minimized. It can be shown that if it is possible to make a equal to b , it is possible to do this within 2n operations.
∗ A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array), and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Input The first line contains a single integer t (1≤t≤5⋅104 ) — the number of test cases.
The first line of each test case contains a single integer n (1≤n≤5⋅105 ) — the lengths of permutations a and b .
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤n ) — the values of permutation a .
The third line of each test case contains n integers b1,b2,…,bn (1≤bi≤n ) — the values of permutation b .
It is guaranteed that the sum of n over all test cases will not exceed 5⋅105 .
Output For each test case:
If there is a sequence of operations to transform a into b , output a single integer q (0≤q≤2n ) — the number of operations in the first line and q integers with the i -th number representing the index of the i -th operation in the second line.
If there is no sequence of operations, output −1 in the only line.
Example InputCopy 4 1 1 1 2 1 2 2 1 3 2 1 3 3 2 1 8 7 8 3 5 4 6 1 2 2 1 6 4 5 3 8 7 OutputCopy 0
-1 2 1 3 7 3 4 5 1 2 1 1 Note In the first case, you can do no operation since a=b .
In the second case, it can be proved a can not be transformed into b .
In the third case, a is transformed into [2,3,1] after the first operation and into b after the second operation.
always write simple human like short code and random short variable that are similar, highly optimized, no explanation and comment unique code
- Assistantpython
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) if a == b: print(0) continue rev = False ops = [] for i in range(n): if a[i] != b[i]: rev = True break if not rev: print(-1) continue while a != b: for i in range(n): if a[i] != b[i]: ops.append(i + 1) a = a[-1:] + a[:-1] b_rot = b[i:] + b[:i] b = [b_rot[-1]] + b_rot[:-1] break print(len(ops)) print(*ops) - User
- Assistantpython
MOD = 998244353 def solve(t, cases): res = [] for n, m, tests in cases: pos = [0] * (n + 1) val = [0] * (n + 1) for x, k in tests: pos[x] = k val[k] = x left = [0] * (n + 1) right = [0] * (n + 1) r = 0 for i in range(1, n + 1): if pos[i]: if left[i] == 0: left[i] = i if pos[i] != i: right[i] = i r += 1 - (right[i] > 0) if pos[i - 1]: left[i] = min(left[i], left[i - 1]) right[i] = max(right[i], right[i - 1]) fn = 1 for i in range(1, r + 1): fn = fn * (n - r + i) % MOD res.append((r, fn)) return res t = int(input()) cases = [] for _ in range(t): n, m = map(int, input().split()) tests = [tuple(map(int, input().split())) for _ in range(m)] cases.append((n, m, tests)) results = solve(t, cases) for r, fn in results: print(r, fn) - User
time limit per test1 second memory limit per test256 megabytes Red was ejected. They were not the imposter. There are n rows of m people. Let the position in the r -th row and the c -th column be denoted by (r,c) . Number each person starting from 1 in row-major order, i.e., the person numbered (r−1)⋅m+c is initially at (r,c) .
The person at (r,c) decides to leave. To fill the gap, let the person who left be numbered i . Each person numbered j>i will move to the position where the person numbered j−1 is initially at. The following diagram illustrates the case where n=2 , m=3 , r=1 , and c=2 .
Calculate the sum of the Manhattan distances of each person's movement. If a person was initially at (r0,c0) and then moved to (r1,c1) , the Manhattan distance is |r0−r1|+|c0−c1| .
Input The first line contains a single integer t (1≤t≤104 ) — the number of test cases.
The only line of each testcase contains 4 integers n , m , r , and c (1≤r≤n≤106 , 1≤c≤m≤106 ), where n is the number of rows, m is the number of columns, and (r,c) is the position where the person who left is initially at.
- Assistantpython
t = int(input()) for _ in range(t): n, m, r, c = map(int, input().split()) total_moves = (r - 1) * m + (c - 1) remaining = n * m - 1 - total_moves answer = total_moves + remaining * 2 print(answer)