简单题目

/**//*
ID: sdjllyh1
PROG: sort3
LANG: JAVA
complete date: 2008/12/2
complexity: O(n)
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjls
*/
import java.io.*;
import java.util.*;
public class sort3

{
private static int n;
private static int[] medalists;
private static int[] medalCount = new int[3];
private static int[][] appearance = new int[3][3];
private static int exchanges = 0;
public static void main(String[] args) throws IOException
{
init();
run();
output();
System.exit(0);
}
private static void run()
{
for (int i = 0; i < n; i++)
{
medalCount[medalists[i]]++;
}
for (int i = 0; i < medalCount[0]; i++)
{
appearance[0][medalists[i]]++;
}
for (int i = 0; i < medalCount[1]; i++)
{
appearance[1][medalists[medalCount[0] + i]]++;
}
for (int i = 0; i < medalCount[2]; i++)
{
appearance[2][medalists[medalCount[0] + medalCount[1] + i]]++;
}
int min = Math.min(appearance[0][1], appearance[1][0]);
exchanges += min;
appearance[0][1] -= min;
appearance[1][0] -= min;
min = Math.min(appearance[0][2], appearance[2][0]);
exchanges += min;
appearance[0][2] -= min;
appearance[2][0] -= min;
min = Math.min(appearance[2][1], appearance[1][2]);
exchanges += min;
appearance[2][1] -= min;
appearance[1][2] -= min;
int maxApp = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3;j++ )
{
if (i!=j)
{
maxApp = Math.max(maxApp, appearance[i][j]);
}
}
exchanges += maxApp * 2;
}
private static void init() throws IOException
{
BufferedReader f = new BufferedReader(new FileReader("sort3.in"));
n = Integer.parseInt(f.readLine());
medalists = new int[n];
for (int i = 0; i < n; i++)
{
medalists[i] = Integer.parseInt(f.readLine()) - 1;
}
f.close();
}
private static void output() throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("sort3.out")));
out.println(exchanges);
out.close();
}
}
浙公网安备 33010602011771号