Submission #1148097

#TimeUsernameProblemLanguageResultExecution timeMemory
1148097rebe__1Coin Collecting (JOI19_ho_t4)Java
0 / 100
84 ms13172 KiB
import java.util.*;
import static java.lang.Math.*;

public class joi2019_ho_t4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int totalCoins = 2 * n;
        
        long moveCost = 0;
        long[] xs = new long[totalCoins];
        int countY1 = 0;
        
        for (int i = 0; i < totalCoins; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            
            long cost = 0;
            if (x < 1) cost += (1 - x);
            if (x > n) cost += (x - n);
            if (y < 1) cost += (1 - y);
            if (y > 2) cost += (y - 2);
            moveCost += cost;
            
            int movedX = x;
            if (movedX < 1) movedX = 1;
            if (movedX > n) movedX = n;
            int movedY = y;
            if (movedY < 1) movedY = 1;
            if (movedY > 2) movedY = 2;
            
            xs[i] = movedX;
            if (movedY == 1) countY1++;
        }
        
        Arrays.sort(xs);
        long horizontalCost = 0;
        for (int i = 0; i < totalCoins; i++) {
            long target = (i / 2) + 1;
            horizontalCost += abs(xs[i] - target);
        }
        
        long verticalCost = abs(countY1 - n);
        
        long answer = moveCost + horizontalCost + verticalCost;
        System.out.println(answer);
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...