문제: Codeforces 1638A — Reverse · English · 日本語
배열은 1..n의 순열입니다. 하나의 구간을 선택해 뒤집을 수 있을 때, 사전순으로 가장 작은 순열을 만들어야 합니다.
왼쪽부터 살펴보며 p[i] != i + 1인 첫 번째 위치 i를 찾습니다. 그보다 앞선 위치는 이미 가능한 가장 작은 값이므로 그대로 두어야 합니다. 값 i + 1은 순열에 정확히 한 번 나타나므로 그 위치를 j라고 합시다. [i, j]를 뒤집으면 i + 1이 첫 불일치 위치로 이동하고, 이미 올바른 앞부분은 변하지 않습니다. 따라서 이 결과가 사전순으로 가장 작습니다.
불일치가 없다면 순열은 이미 정렬되어 있으므로 길이 1인 구간을 뒤집으면 그대로입니다. 첫 불일치와 목표 값의 위치를 찾는 데 테스트 케이스마다 시간 O(n)이 걸리며, 입력 순열을 저장하는 보조 공간은 O(n)입니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.BufferedInputStream;
import java.io.IOException;
public class Main {
private static final class FastScanner {
private final BufferedInputStream in = new BufferedInputStream(System.in);
private final byte[] buffer = new byte[1 << 16];
private int ptr;
private int len;
private int read() throws IOException {
if (ptr == len) {
len = in.read(buffer);
ptr = 0;
if (len == -1) return -1;
}
return buffer[ptr++];
}
int nextInt() throws IOException {
int c;
do {
c = read();
} while (c <= ' ' && c != -1);
int value = 0;
while (c > ' ') {
value = value * 10 + c - '0';
c = read();
}
return value;
}
}
public static void main(String[] args) throws IOException {
FastScanner fs = new FastScanner();
int tests = fs.nextInt();
StringBuilder answer = new StringBuilder();
while (tests-- > 0) {
int n = fs.nextInt();
int[] permutation = new int[n];
for (int i = 0; i < n; i++) {
permutation[i] = fs.nextInt();
}
int left = 0;
while (left < n && permutation[left] == left + 1) {
left++;
}
if (left < n) {
int right = left;
while (permutation[right] != left + 1) {
right++;
}
while (left < right) {
int value = permutation[left];
permutation[left++] = permutation[right];
permutation[right--] = value;
}
}
for (int i = 0; i < n; i++) {
if (i > 0) answer.append(' ');
answer.append(permutation[i]);
}
answer.append('\n');
}
System.out.print(answer);
}
}