제출 #658740

#제출 시각아이디문제언어결과실행 시간메모리
658740coderInTrainingPalembang Bridges (APIO15_bridge)Java
0 / 100
152 ms12256 KiB
import java.util.*;

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

        int allowedBridges = scan.nextInt();
        int citizenNumber = scan.nextInt();

        long previousAdding = 0;

        ArrayList<Integer> locations = new ArrayList<Integer>();

        for (int i = 0; i < citizenNumber; i++) {
            char startZone = scan.next().charAt(0);
            int startLocation = scan.nextInt();

            char endZone = scan.next().charAt(0);
            int endLocation = scan.nextInt();

            if (startZone == endZone) {
                previousAdding += Math.abs(endLocation - startLocation);
                continue;
            }

            locations.add(startLocation);
            locations.add(endLocation);
        }

        /*
         * If we are only able to build one bridge, we should
         * build it at the median of all start and end locations.
         * After find the median, we can calculate the answer
         * by looping through all values and adding the previously
         * calculated value for locations in the same zone. 
         */
        if (allowedBridges == 1) {
            Collections.sort(locations);

            int size = locations.size();
            int median = locations.get(size / 2 - 1);

            long answer = 0;

            for (int i = 0; i < locations.size(); i++) {
                answer += Math.abs(locations.get(i) - median);
            }

            answer += previousAdding;

            System.out.println(answer);
        }
        else {
            System.out.println(100);
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...