Submission #285758

# Submission time Handle Problem Language Result Execution time Memory
285758 2020-08-29T14:44:19 Z cprayer 개구리 (KOI13_frog) Java 11
1.98 / 22
1000 ms 262148 KB
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

public class frog {

    public static void main(String[] args) {
        Problems problems = new Problems();
        problems.solve();
    }
}

class Problems {

    Parser parser = new Parser();

    void solve() {
        int t = 1;
        for (int i = 0; i < t; i++) {

            Problem problem = new Problem();
            problem.solve(i);
        }
    }

    class Problem {

        int N;
        int r;
        int d;
        int[] f;
        List<Point> points = new ArrayList<>();

        public Problem() {
            N = parser.parseInt();
            r = parser.parseInt();

            points.add(new Point(0, 0, 0));
            for (int i = 1; i <= N; i++) {
                int x = parser.parseInt();
                int y = parser.parseInt();
                if(x == 0 && y == 0) {
                    continue;
                }
                points.add(new Point(x, y, i));
            }

            d = parser.parseInt();
            f = new int[N + 1];
            for(int i = 0; i <= N; i++) {
                f[i] = i;
            }
        }

        void solve(int testCase) {
            Map<Integer, List<Point>> sorted = new TreeMap<>();
            for (int i = 0; i < points.size(); i++) {
                Point p = points.get(i);
                sorted.computeIfAbsent(p.y, ArrayList::new).add(p);
            }

            merge(sorted);
            sorted.clear();

            for (int i = 0; i < points.size(); i++) {
                Point p = points.get(i);
                int t = p.x;
                p.x = p.y;
                p.y = t;
                sorted.computeIfAbsent(p.y, ArrayList::new).add(p);
            }

            merge(sorted);

            int ans = 0;

            for(int i = 0; i < points.size(); i++) {
                Point p = points.get(i);
                if(find(p.index) == 0) {
                    ans = Math.max(ans, p.x + p.y + 2 * r);
                }
            }

            System.out.println(ans);
        }

        void merge(Map<Integer, List<Point>> sorted) {

            TreeMap<Integer, Point> all = new TreeMap<>();
            List<Integer> keys = new ArrayList<>(sorted.keySet());

            int leftKey = 0;
            int leftVal = keys.get(0);
            for(int rightKey = 0; rightKey < keys.size(); rightKey++) {
                int rightVal = keys.get(rightKey);
                while (leftVal + r < rightVal) {

                    for (Point deleted : sorted.get(leftVal)) {
                        all.remove(deleted.x, deleted);
                    }
                    leftKey += 1;
                    leftVal = keys.get(leftKey);
                }

                for (Point p : sorted.get(rightVal)) {
                    all.put(p.x, p);
                    Map.Entry<Integer, Point> el = all.floorEntry(p.x - 1);
                    Map.Entry<Integer, Point> er = all.ceilingEntry(p.x + 1);

                    if (el != null && p.x <= el.getValue().x + r + d) {
                        merge(el.getValue().index, p.index);
                    }

                    if (er != null && er.getValue().x <= p.x + r + d) {
                        merge(er.getValue().index, p.index);
                    }
                }
            }
        }

        void merge(int a, int b) {
            a = find(a);
            b = find(b);

            if (a == b) {
                return;
            }

            if (a > b) {
                int t = a;
                a = b;
                b = t;
            }

            f[b] = a;
        }

        int find(int v) {
            return f[v] == v ? v : (f[v] = find(f[v]));
        }

        class Point {
            int x, y, index;

            public Point(int x, int y, int index) {
                this.x = x;
                this.y = y;
                this.index = index;
            }
        }

    }

}

class Parser {
    private final Iterator<String> stringIterator;
    private final Deque<String> inputs;

    Parser() {
        this(System.in);
    }

    Parser(InputStream in) {
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        stringIterator = br.lines().iterator();
        inputs = new ArrayDeque<>();
    }

    void fill() {
        while (inputs.isEmpty()) {
            if (!stringIterator.hasNext()) throw new NoSuchElementException();
            inputs.addAll(Arrays.asList(stringIterator.next().split(" ")));
            while (!inputs.isEmpty() && inputs.getFirst().isEmpty()) {
                inputs.removeFirst();
            }
        }
    }

    Integer parseInt() {
        fill();
        if (!inputs.isEmpty()) {
            return Integer.parseInt(inputs.pollFirst());
        }
        throw new NoSuchElementException();
    }

    Long parseLong() {
        fill();
        if (!inputs.isEmpty()) {
            return Long.parseLong(inputs.pollFirst());
        }
        throw new NoSuchElementException();
    }

    Double parseDouble() {
        fill();
        if (!inputs.isEmpty()) {
            return Double.parseDouble(inputs.pollFirst());
        }
        throw new NoSuchElementException();
    }

    String parseString() {
        fill();
        return inputs.removeFirst();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 205 ms 16104 KB Output is correct
2 Correct 233 ms 16920 KB Output is correct
3 Correct 228 ms 16236 KB Output is correct
4 Correct 237 ms 19040 KB Output is correct
5 Correct 248 ms 17248 KB Output is correct
6 Correct 245 ms 34404 KB Output is correct
7 Correct 265 ms 23004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 474 ms 141004 KB Output is correct
2 Correct 321 ms 41684 KB Output is correct
3 Runtime error 905 ms 262148 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 293 ms 40904 KB Output is correct
2 Runtime error 964 ms 262144 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1091 ms 239716 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1224 ms 262144 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 804 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -