Magic Trick II Codeforces
Magic Trick II Codeforces
|
NguyenDucHien | Logout
HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP
PROBLEMS SUBMIT CODE MY SUBMISSIONS STATUS HACKS ROOM STANDINGS CUSTOM INVOCATION
The secret behind Oscar's first magic trick has been revealed! Because he still wants to
impress Lura, he comes up with a new idea: he still wants to sort a permutation
→ Submit?
p1 , p2 , … , pn of [1, 2, … , n].
This time, he chooses an integer k . He wants to sort the permutation in non-decreasing order Language: Python 3.8.10
Almost always, if you send a solution
using the following operation several times: on PyPy, it works much faster
Choose
1. Pick a continuous subarray of length k and remove it from p. file:
Choose File No file chosen
2. Insert the continuous subarray back into p at any position (perhaps, in the very front or the Be careful: there is 50 points penalty for
very back). submission which fails the pretests or
resubmission (except failure on the first test,
denial of judgement or similar verdicts).
To be as impressive as possible, Oscar would like to choose the maximal value of k such that "Passed pretests" submission verdict doesn't
he can sort his permutation. Please help him find the maximal k as well as a sequence of guarantee that the solution is absolutely
correct and it will pass system tests.
operations that will sort the permutation. You don't need to minimize the number of operations,
but you are allowed to use at most 5n2 operations. Submit
We have a proof that, for the maximal k such that you can sort the permutation in any number
of operations, you can also sort it in at most 5n2 operations.
→ Score table
Input Score
3
The first line contains a single integer t (1 ≤ t ≤ 10 ) — the number of test cases.
Problem A 444
3 Problem B 666
The first line of each test case contains a single integer n (5 ≤ n ≤ 10 ) — the length of the
permutation. Problem C1 666
The second line of each test case contains a permutation p1 , p2 , … , pn of [1, 2, … , n]. Problem C2 1110
Problem D 2220
The sum of n over all test cases does not exceed 2 ⋅ 103 .
Problem E 2664
Then, output a single integer m — the number of operations used (0 ≤ m ≤ 5n2 ). Problem H 4440
Successful hack 100
Then, on each of the next m lines, output the operations denoted by two integers i and j (
Unsuccessful hack -50
1 ≤ i, j ≤ n − k + 1), representing an operation where you remove the subarray starting
from index i and replace it back into p at index j . Unsuccessful submission -50
Resubmission -50
Example * If you solve problem on 00:42 from the first attempt
input Copy
3
5
5 1 2 3 4
5
2 3 5 4 1
6
1 2 3 4 5 6
output Copy
https://codeforces.com/contest/1984/problem/G 1/2
6/9/24, 10:17 PM Problem - G - Codeforces
4
1
2 1
3
2
1 3
2 1
6
0
Note
In the first test case, it is enough to move the last four numbers to the front.
In the second test case, it can be shown that we cannot have k = 4 or k = 5. With k = 3,
we can move the first three numbers to the end, and then the middle three to the front to sort
the permutation.
In the third test case, the permutation is already sorted. We can have k = 6 and use no
operations.
Supported by
https://codeforces.com/contest/1984/problem/G 2/2