106 lines
2.5 KiB
Java
106 lines
2.5 KiB
Java
package chelper;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
|
|
import io.InputReader;
|
|
import io.OutputWriter;
|
|
import misc.GCJSolution;
|
|
|
|
|
|
public class TaskC extends GCJSolution {
|
|
|
|
public void solve(int testNumber, InputReader in, OutputWriter out) {
|
|
wrapSolve(testNumber, in, out);
|
|
}
|
|
|
|
@Override
|
|
public void solve(int testNumber) {
|
|
int n = in.nextInt();
|
|
|
|
int[][] a = new int[n][n];
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
for (int j = 0; j < n; j++) {
|
|
a[i][j] = in.nextInt();
|
|
}
|
|
}
|
|
|
|
Map<Integer, Set<Integer>> mxs1 = new HashMap<>();
|
|
Map<Integer, Set<Integer>> mys1 = new HashMap<>();
|
|
|
|
Map<Integer, Set<Integer>> mxs2 = new HashMap<>();
|
|
Map<Integer, Set<Integer>> mys2 = new HashMap<>();
|
|
|
|
Map<Integer, Integer> counts1 = new HashMap<>();
|
|
Map<Integer, Integer> counts2 = new HashMap<>();
|
|
Map<Integer, Integer> total = new HashMap<>();
|
|
|
|
int changes = 0;
|
|
|
|
for (int i = -n; i <= n; i++) {
|
|
mxs1.put(i, new HashSet<>());
|
|
mys1.put(i, new HashSet<>());
|
|
counts1.put(i, 0);
|
|
|
|
mxs2.put(i, new HashSet<>());
|
|
mys2.put(i, new HashSet<>());
|
|
counts2.put(i, 0);
|
|
|
|
total.put(i, 0);
|
|
}
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
for (int j = 0; j < n; j++) {
|
|
|
|
int f = a[i][j];
|
|
|
|
total.put(f, total.get(f) + 1);
|
|
|
|
if (!mxs1.get(f).contains(i) && !mys1.get(f).contains(j)) {
|
|
counts1.put(f, counts1.get(f) + 1);
|
|
mxs1.get(f).add(i);
|
|
mys1.get(f).add(j);
|
|
} else {
|
|
if (!mxs2.get(f).contains(i) && !mys2.get(f).contains(j)) {
|
|
counts2.put(f, counts2.get(f) + 1);
|
|
mxs2.get(f).add(i);
|
|
mys2.get(f).add(j);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
for (int j = 0; j < n; j++) {
|
|
|
|
int f = a[i][j];
|
|
|
|
if (!mxs1.get(f).contains(i) && !mys1.get(f).contains(j)) {
|
|
counts1.put(f, counts1.get(f) + 1);
|
|
mxs1.get(f).add(i);
|
|
mys1.get(f).add(j);
|
|
} else {
|
|
if (!mxs2.get(f).contains(i) && !mys2.get(f).contains(j)) {
|
|
counts2.put(f, counts2.get(f) + 1);
|
|
mxs2.get(f).add(i);
|
|
mys2.get(f).add(j);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for (int f = -n; f <= n; f++) {
|
|
int maxLeft = Math.max(counts1.get(f), counts2.get(f));
|
|
|
|
changes += total.get(f) - maxLeft;
|
|
}
|
|
|
|
out.println(changes);
|
|
}
|
|
}
|