Submission #658795

# Submission time Handle Problem Language Result Execution time Memory
658795 2022-11-15T00:21:33 Z coderInTraining Palembang Bridges (APIO15_bridge) Java 11
0 / 100
99 ms 10344 KB
import java.util.*;
import java.io.*;

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

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

            long previousAdding = 0;

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

            for (int i = 0; i < citizenNumber; i++) {

                String line = scan.next();

                char startZone = line.charAt(0);
                int index = 0;

                for (int j = 1; j < line.length(); j++) {

                    if (line.charAt(j) == 'A' || line.charAt(j) == 'B') {
                        index = j;
                        break;
                    }
                }

                int startLocation = Integer.valueOf(line.substring(1, index));
                char endZone = line.charAt(index);
                int endLocation = Integer.valueOf(line.substring(index + 1));

                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) {

                long answer = 0;

                if (locations.size() != 0) {
                    Collections.sort(locations);

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

                    answer += (locations.size() / 2);
                }

                answer += previousAdding;

                System.out.println(answer);
            }
            else {
                System.out.println(100);
            }
        }
        catch (Exception e) {
            return;
        }
    }
}
# Verdict Execution time Memory Grader output
1 Incorrect 91 ms 10204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 92 ms 10344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 92 ms 10096 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 99 ms 9972 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 97 ms 10092 KB Output isn't correct
2 Halted 0 ms 0 KB -