import java.util.*;
public class joi2019_ho_t4 {
static final int MAXN = 100010;
static int n;
static int[][] mat = new int[MAXN][3];
static long ans = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
readInput(scanner);
calculateBalance();
System.out.println(ans);
scanner.close();
}
private static void readInput(Scanner scanner) {
n = scanner.nextInt();
for (int i = 1; i <= 2 * n; i++) {
int x = scanner.nextInt();
int y = scanner.nextInt();
processPoint(x, y);
}
}
private static void processPoint(int x, int y) {
if (x < 1) {
if (y >= 2) {
mat[1][2]++;
ans += Math.abs(x - 1) + Math.abs(y - 2);
} else {
mat[1][1]++;
ans += Math.abs(x - 1) + Math.abs(y - 1);
}
} else if (x >= 1 && x <= n) {
if (y >= 2) {
mat[x][2]++;
ans += Math.abs(y - 2);
} else {
mat[x][1]++;
ans += Math.abs(y - 1);
}
} else if (x > n) {
if (y >= 2) {
mat[n][2]++;
ans += Math.abs(x - n) + Math.abs(y - 2);
} else {
mat[n][1]++;
ans += Math.abs(x - n) + Math.abs(y - 1);
}
}
}
private static void calculateBalance() {
long num1 = 0, num2 = 0;
for (int i = 1; i <= n; i++) {
num1 += (mat[i][1] - 1);
num2 += (mat[i][2] - 1);
balanceColumns(num1, num2);
ans += Math.abs(num1) + Math.abs(num2);
}
}
private static void balanceColumns(long num1, long num2) {
while (num1 > 0 && num2 < 0) {
num1--;
num2++;
ans++;
}
while (num1 < 0 && num2 > 0) {
num1++;
num2--;
ans++;
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |