/* Use the slash-star style comments or the system won't see your
identification information */
/*
ID: lincans1
LANG: JAVA
TASK: sort3
*/
import java.io.*;
import java.util.*;
public class sort3 {
private void swap (int[] array, int a, int b) {
int temp = array[a];
array[a] = array[b];
array[b] = temp;
}
private int count (int[] array) {
int[] count = new int[4];
for (int i : array) {
count[i]++;
}
count[2] += count[1];
count[3] += count[2];
int one_three = 0, three_one = 0, sum = 0;
for (int i = 0, n = count[1]; i < n; i++) {
if (array[i] == 3) {
one_three++;
}
}
for (int i = count[1], n = count[2]; i < n; i++) {
if (array[i] != 2) {
sum++;
}
}
for (int i = count[2], n = count[3]; i < n; i++) {
if (array[i] == 1) {
three_one++;
}
}
return Math.max(one_three, three_one) + sum;
}
public sort3() throws IOException {
// Use BufferedReader rather than RandomAccessFile; it's much faster
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[] array = new int[N];
for (int i = 0; i < N; i++) {
array[i] = Integer.parseInt(f.readLine());
}
int ans = count(array);
out.println(ans);
out.close();
f.close();
}
public static void main (String [] args) throws IOException {
new sort3();
}
}
[Java] Sorting A Three-Valued Sequence
猜你喜欢
转载自blog.csdn.net/weixin_41714373/article/details/112685984
今日推荐
周排行