-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort3.java
More file actions
85 lines (77 loc) · 1.67 KB
/
sort3.java
File metadata and controls
85 lines (77 loc) · 1.67 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
/*
ID: 001207j1
LANG: JAVA
TASK: sort3
*/
import java.io.*;
import java.util.*;
public class sort3 {
public static int[] n = { 0, 0, 0, 0 };
public static int exc = 0;
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("sort3.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("sort3.out")));
int N = Integer.parseInt(f.readLine());
int[] v = new int[N];
for (int i = 0; i < N; i++) {
v[i] = Integer.parseInt(f.readLine());
n[v[i]] += 1;
}
int[] cor = new int[N];
for (int i = 0; i < N; i++)
cor[i] = sec(i, n);
sec_swap(N, n, v, cor);
for (int i = 1; i <= 3; i++) {
int t = -1;
while (t != exc) {
t = exc;
num_swap(i, N, v, cor);
}
}
out.println(exc);
f.close();
out.close();
}
public static void swap(int i, int j, int[] v) {
int t = v[i];
v[i] = v[j];
v[j] = t;
exc++;
}
public static void sec_swap(int N, int[] n, int[] v, int[] cor) {
for (int i = 0; i < N; i++) {
if (v[i] == cor[i])
continue;
for (int j = N - 1; j > i; j--) {
if (v[j] == cor[j])
continue;
if (v[i] == sec(j, n) && v[j] == sec(i, n))
swap(i, j, v);
if (v[i] == cor[i])
break;
}
}
}
public static int sec(int i, int[] n) {
if (i < n[1])
return 1;
else if (i < n[2] + n[1])
return 2;
else
return 3;
}
public static void num_swap(int x, int N, int[] v, int[] cor) {
for (int i = 0; i < N; i++) {
if (v[i] == cor[i])
continue;
for (int j = N - 1; j > i; j--) {
if (v[j] == cor[j])
continue;
if (v[i] != x && v[j] == x)
swap(i, j, v);
if (v[i] == cor[i])
break;
}
}
}
}