제출 #1148061

#제출 시각아이디문제언어결과실행 시간메모리
1148061pablo_floresCoin Collecting (JOI19_ho_t4)Java
100 / 100
619 ms167872 KiB
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 processCoordinates(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) {
                ans += Math.abs(y - 2);
                mat[x][2]++;
            } else {
                ans += Math.abs(y - 1);
                mat[x][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);
            }
        }
    }

    public static void solve() {
        long num1 = 0, num2 = 0;

        for (int i = 1; i <= n; i++) {
            num1 += (mat[i][1] - 1);
            num2 += (mat[i][2] - 1);

            while (num1 > 0 && num2 < 0) {
                num1--;
                num2++;
                ans++;
            }
            while (num1 < 0 && num2 > 0) {
                num1++;
                num2--;
                ans++;
            }

            ans += Math.abs(num1) + Math.abs(num2);
        }
    }

    public static void readInput(Scanner scanner) {
        n = scanner.nextInt();
        for (int i = 1; i <= 2 * n; i++) {
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            processCoordinates(x, y);
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        readInput(scanner); 

        solve();

        System.out.println(ans);

        scanner.close();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...