[Timus] 2023 Donald is a postman

Problem statement is here: http://acm.timus.ru/problem.aspx?space=1&num=2023

This is a classic simulation problem. Do as the problem says and you will get AC. One thing to note is you have to store the name on the mailboxes in a data structure which has fast retrieval. I chose map but you could sove it using arrays too. Also, note where Donald starts and the first letter may not be where Donald starts from.

public void solve(int testNumber, InputReader in, OutputWriter out) {
        Map<Character, Integer> postbox = new HashMap<>();
        postbox.put('A', 0);
        postbox.put('P', 0);
        postbox.put('O', 0);
        postbox.put('R', 0);
        postbox.put('B', 1);
        postbox.put('M', 1);
        postbox.put('S', 1);
        postbox.put('D', 2);
        postbox.put('G', 2);
        postbox.put('J', 2);
        postbox.put('K', 2);
        postbox.put('T', 2);
        postbox.put('W', 2);

        int n = in.nextInt();

        int steps = 0;

        String prev = "";
        for (int i = 0; i < n; i++) {
            String current = in.nextString();
            if (prev == "") {
                steps += postbox.get(current.charAt(0));
            } else {
                steps += Math.abs(postbox.get(prev.charAt(0)) -
                                  postbox.get(current.charAt(0)));
            }
            prev = current;
        }

        out.println(steps);
    }

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.