Submission #1148069

#TimeUsernameProblemLanguageResultExecution timeMemory
1148069pablo_floresCoin Collecting (JOI19_ho_t4)Java
100 / 100
718 ms168492 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 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 main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();

        for (int i = 1; i <= 2 * n; i++) {
            int x = scanner.nextInt();
            int y = scanner.nextInt();

            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);
                }
            }
        }

        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...