Arbitrary Shuffling You are given a permutation P...
Creato il: 23 aprile 2025
Creato il: 23 aprile 2025
Arbitrary Shuffling
You are given a permutation
P
P of the integers
[
1
,
N
]
[1,N].
You can do either of the following operations as many times as you want:
X
P
X
=X and shuffle the subarray
P
[
X
,
N
]
P[X,N].
Here the subarray
P
[
L
,
R
]
P[L,R] represents the array
[
P
L
,
P
L
+
1
,
…
,
P
R
]
[P
L
,P
L+1
,…,P
R
]. To shuffle an array
A
A means to replace it with another array
B
B such that
B
B is a permutation of
A
A.
[
4
,
3
,
1
,
2
]
P=[4,3,1,2].
i
P
i
=i for each
1
≤
i
≤
N
1≤i≤N after some (possibly zero) operations. Conversely, a permutation
P
P is called un-sortable if it is not sortable.
You are given a permutation
P
P of the integers
[
1
,
N
]
[1,N].
You want to obtain an un-sortable permutation
P
P. For that, you are allowed to do the following action:
Choose
X
,
Y
X,Y (
1
≤
X
<
Y
≤
N
1≤X<Y≤N) and swap
P
X
P
X
and
P
Y
P
Y
. Pay a cost of
1
1.
Find the minimum cost to obtain an un-sortable permutation
P
P. It can be shown that it is always possible under the given constraints.
Input Format
The first line of input will contain a single integer
T
T, denoting the number of test cases.
Each test case consists of multiple lines of input.
The first line contains
N
N - the number of elements.
The second line contains
N
N integers,
P
1
,
P
2
,
…
P
N
P
1
,P
2
,…P
N
Constraints
1
≤
T
≤
1
0
4
1≤T≤10
4
2
≤
N
≤
2
⋅
1
0
5
2≤N≤2⋅10
5
1
≤
P
i
≤
N
1≤P
i
≤N
P
i
≠
P
j
P
i
=P
j
for
i
≠
j
i=j
The sum of
N
N over all test cases does not exceed
2
⋅
1
0
5
2⋅10
5
Sample 1:
Input
Output
3
2
2 1
3
1 2 3
4
1 4 3 2
0
1
1
Explanation:
Test Case 1 : The given permutation is already un-sortable.
3
X=1,Y=3 and swap
P
1
P
1
and
P
3
P
3
to get
[
3
,
2
,
1
]
[3,2,1] which is un-sortable.
Test Case 3 : The given permutation is sortable. Here is a possible way:
1
P
1
=1 so it is valid, and shuffle the subarray
P
[
1
,
4
]
P[1,4] to get
[
1
,
2
,
3
,
4
]
[1,2,3,4].
We can swap
P
1
P
1
and
P
3
P
3
to get
[
3
,
4
,
1
,
2
]
[3,4,1,2] which is un-sortable.
accepted
Accepted
45
total-Submissions
Submissions
251
accuracy
Accuracy
17.93
Did you like the problem statement?
2 users found this helpful
More Info
Time limit1 secs
Memory limit1.5 GB
Source Limit50000 Bytes
import java.util.;
import java.lang.;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
}
}
write a java solution
Solution Explanation
Let
fixed
= the set of indices i
such that P[i] = i
.pos1
= position of the value 1
in P
.posN
= position of the value N
in P
.L = min(fixed)
and R = max(fixed)
(well-defined only when fixed
is not empty).No fixed points (fixed
is empty).
We can never perform a single shuffle operation, so the identity permutation can
never be reached.
All fixed points lie strictly between posN
and pos1
.
Formally
pos1 > R and posN < L .
In every allowed operation the chosen fixed index splits the array into a
prefix and a suffix.
Elements can only be rearranged inside the chosen sub-array, they can never
cross that fixed index.
If all fixed indices are sandwiched between the value N
(left side) and the
value 1
(right side) then 1
can never move left of every fixed index and
N
can never move right of every fixed index, so the identity permutation is
unreachable.
Hence a permutation is un-sortable iff
textfixed is empty OR (pos1 > R && posN < L).
If the permutation is already un-sortable (condition above is true)
cost = 0.
Otherwise it is sortable, and a single swap always suffices:
Therefore the answer for each test case is simply
text0 if (fixed is empty) OR (pos1 > R && posN < L) 1 otherwise
The whole test is O(N) per case and uses O(1) extra memory.
javaimport java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { // CodeChef: class name can be Main public static void main(String[] args) throws Exception { FastScanner fs = new FastScanner(); StringBuilder out = new StringBuilder(); int T = fs.nextInt(); while (T-- > 0) { int N = fs.nextInt(); int pos1 = -1, posN = -1; int minFixed = Integer.MAX_VALUE; int maxFixed = -1; for (int i = 1; i <= N; ++i) { int v = fs.nextInt(); if (v == 1) pos1 = i; if (v == N) posN = i; if (v == i) { minFixed = Math.min(minFixed, i); maxFixed = Math.max(maxFixed, i); } } boolean noFixed = maxFixed == -1; boolean sandwich = !noFixed && pos1 > maxFixed && posN < minFixed; out.append((noFixed || sandwich) ? 0 : 1).append('\n'); } System.out.print(out.toString()); } /* ---------- fast input ---------- */ private static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; int nextInt() throws Exception { while (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine()); return Integer.parseInt(st.nextToken()); } } }
Time-complexity: O( Σ N )
over all test cases
Memory: O(1)
aside from the input array parsing buffer
Wrong Answer: Failed on a hidden test case
Submission ID:
1154954927
Sub-Task Task # Result
(time)
1 0 Correct
(0.03)
1 1 Wrong Answer
(0.08)
1 2 Skipped Testfile
1 3 Skipped Testfile
1 4 Skipped Testfile
1 5 Skipped Testfile
1 6 Skipped Testfile
1 7 Skipped Testfile
1 8 Skipped Testfile
1 9 Skipped Testfile
1 10 Skipped Testfile
Subtask Score: 0% Result - Wrong Answer
Total Score = 0%
why hideen case not pass can you fix these issue