-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort3.java
77 lines (67 loc) · 1.9 KB
/
sort3.java
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/*
ID: Xu Yan
LANG: JAVA
TASK: sort3
*/
/**
* Thoughts:
* Pitfalls:
*
* Take-away tips:
*
* @author Xu Yan
* @date May 22nd, 2016
*/
public class sort3 {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("sort3.in"));
int N = Integer.parseInt(new StringTokenizer(f.readLine()).nextToken());
int[] sequence = new int[N];
int[] numCount = {0, 0, 0};
for (int i = 0; i < N; i++) {
sequence[i] = Integer.parseInt(new StringTokenizer(f.readLine()).nextToken());
numCount[sequence[i] - 1] += 1;
}
f.close();
int[] a = {0, 0, 0};
for (int i = 0; i < numCount[0]; i++) {
a[sequence[i]-1] += 1;
}
int[] b = {0, 0, 0};
for (int i = numCount[0]; i < numCount[0] + numCount[1]; i++) {
b[sequence[i]-1] += 1;
}
int[] c = {0, 0, 0};
for (int i = numCount[0] + numCount[1]; i < N; i++) {
c[sequence[i]-1] += 1;
}
int ans = 0;
int swapPair = 0;
// 1 <-> 2
swapPair = Math.min(a[1], b[0]);
ans += swapPair;
a[1] -= swapPair;
b[0] -= swapPair;
// 2 <-> 3
swapPair = Math.min(b[2], c[1]);
ans += swapPair;
b[2] -= swapPair;
c[1] -= swapPair;
// 1 <-> 3
swapPair = Math.min(a[2], c[0]);
ans += swapPair;
a[2] -= swapPair;
c[0] -= swapPair;
ans += 2 * (a[1] + a[2]);
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("sort3.out")));
out.println(ans);
out.close();
}
}