Problem p03004 (python)

Fixed submission:
#!/usr/bin/env python
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip

import random
import collections
import math
import itertools
import bisect

def real_main():
    n = read_int()
    x, y, d = [], [], []
    for _ in range(n):
        s = read_array()
        x.append(int(s[0]))
        y.append(int(s[1]))
        d.append(s[2])
        # if d == 'R':
        #     dx.append(1)
        #     dy.append(0)
        # elif d == 'L':
        #     dx.append(-1)
        #     dy.append(0)
        # elif d == 'U':
        #     dx.append(0)
        #     dy.append(1)
        # else:
        #     dx.append(0)
        #     dy.append(-1)

    print(solve(x, y, d))

def solve(x, y, d):
    # POS, NEG, CONST
    y_max = [float('-inf')]*3
    x_max = [float('-inf')]*3
    x_min = [float('inf')]*3
    y_min = [float('inf')]*3
    for x, y, d in zip(x, y, d):
        if d == 'R' or d == 'L':
            y_max[2] = max(y_max[2], y)
            y_min[2] = min(y_min[2], y)
        if d == 'U' or d == 'D':
            x_max[2] = max(x_max[2], x)
            x_min[2] = min(x_min[2], x)

        if d == 'U':
            y_max[0] = max(y_max[0], y)
            y_min[1] = min(y_min[1], y)
        elif d == 'D':
            y_min[0] = min(y_min[0], y)
            y_max[1] = max(y_max[1], y)
        elif d == 'R':
            x_max[0] = max(x_max[0], x)
            x_min[1] = min(x_min[1], x)
        else:
            x_min[0] = min(x_min[0], x)
            x_max[1] = max(x_max[1], x)

    def score_at(t):
        ymax = max(y_max[0] + t, y_max[1] - t, y_max[2])
        xmax = max(x_max[0] + t, x_max[1] - t, x_max[2])

        ymin = min(y_min[0] - t, y_min[1] + t, y_min[2])
        xmin = min(x_min[0] - t, x_min[1] + t, x_min[2])

        return (ymax-ymin) * (xmax - xmin)

    lo, hi = 0, 10**8
    while hi - lo > 0.000001:
        mid_left = lo * 2 / 3 + hi / 3
        mid_right = lo / 3 + hi * 2 / 3
        if score_at(mid_left) >= score_at(mid_right):
            lo = mid_left
        else:
            hi = mid_right

    return score_at(round(hi, 3))



def main():
    if False and 'PYCHARM_HOSTED' in os.environ:
        main_pycharm()
    else:
        real_main()


from copy import deepcopy
def main_pycharm():
    solution = solve

    test_inputs = None
    test_outputs = None
    judge = None
    slow_solution = None
    if solution is not None:
        if test_outputs is not None:
            test_with_answers(solution, test_inputs, test_outputs)
        if judge is not None:
            test_with_judge(solution, test_inputs, judge)
        if slow_solution is not None:
            test_with_slow_solution(solution, test_inputs, slow_solution)


def test_with_answers(solution, inputs_answers):
    total, wrong = 0, 0
    for args, test_ans in inputs_answers:
        ans = solution(*args.copy())
        if ans != test_ans:
            print('WRONG! ans=%s, test_ans=%s, args=%s' % (ans, test_ans, args))
            wrong += 1
        else:
            print('GOOD')
        total += 1
    print('ALL %d TESTS PASSED' % total if not wrong else '%d out of %d tests are WRONG' % (wrong, total))


def test_with_judge(solution, inputs_gen, judge):
    total, wrong = 0, 0
    for args in inputs_gen:
        ans = solution(*deepcopy(args))
        if not judge(deepcopy(ans), *deepcopy(args)):
            print('WRONG! ans=%s, args=%s' % (ans, args))
            wrong += 1
        total += 1
    print('ALL %d TESTS PASSED' % total if not wrong else '%d out of %d tests are WRONG' % (wrong, total))


def test_with_slow_solution(solution, inputs_gen, solution_slow):
    total, wrong = 0, 0
    for args in inputs_gen:
        ans = solution(*deepcopy(args))
        slow = solution_slow(*deepcopy(args))
        if ans != slow:
            print('WRONG! ans=%s, slow=%s, args=%s' % (ans, slow, args))
            wrong += 1
        total += 1
    print('ALL %d TESTS PASSED' % total if not wrong else '%d out of %d tests are WRONG' % (wrong, total))


# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")

def read():
    return input()

def read_int():
    return int(input())

def read_array(sep=None, maxsplit=-1):
    return input().split(sep, maxsplit)

def read_int_array():
    return [int(a) for a in read_array()]

# endregion

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip

import random
import collections
import math
import itertools
import bisect

def real_main():
    n = read_int()
    x, y, d = [], [], []
    for _ in range(n):
        s = read_array()
        x.append(int(s[0]))
        y.append(int(s[1]))
        d.append(s[2])
        # if d == 'R':
        #     dx.append(1)
        #     dy.append(0)
        # elif d == 'L':
        #     dx.append(-1)
        #     dy.append(0)
        # elif d == 'U':
        #     dx.append(0)
        #     dy.append(1)
        # else:
        #     dx.append(0)
        #     dy.append(-1)

    print(solve(x, y, d))

def solve(x, y, d):
    # POS, NEG, CONST
    y_max = [float('-inf')]*3
    x_max = [float('-inf')]*3
    x_min = [float('inf')]*3
    y_min = [float('inf')]*3
    for x, y, d in zip(x, y, d):
        if d == 'R' or d == 'L':
            y_max[2] = max(y_max[2], y)
            y_min[2] = min(y_min[2], y)
        if d == 'U' or d == 'D':
            x_max[2] = max(x_max[2], x)
            x_min[2] = min(x_min[2], x)

        if d == 'U':
            y_max[0] = max(y_max[0], y)
            y_min[1] = min(y_min[1], y)
        elif d == 'D':
            y_min[0] = min(y_min[0], y)
            y_max[1] = max(y_max[1], y)
        elif d == 'R':
            x_max[0] = max(x_max[0], x)
            x_min[1] = min(x_min[1], x)
        else:
            x_min[0] = min(x_min[0], x)
            x_max[1] = max(x_max[1], x)

    ans = (max(y_max) - min(y_min)) * (max(x_max) - min(x_min))

    def score_at(t):
        ymax = max(y_max[0] + t, y_max[1] - t, y_max[2])
        xmax = max(x_max[0] + t, x_max[1] - t, x_max[2])

        ymin = min(y_min[0] - t, y_min[1] + t, y_min[2])
        xmin = min(x_min[0] - t, x_min[1] + t, x_min[2])

        return (ymax-ymin) * (xmax - xmin)

    lo, hi = 0, 10**8
    eps = 10**(-9)
    while high - low > 0.000001:
        mid = (lo+hi) / 2
        mid_score = score_at(mid)
        ans = min(ans, mid_score)
        if mid_score > score_at(mid + eps):
            lo = mid
        else:
            hi = mid

    return ans



def main():
    if False and 'PYCHARM_HOSTED' in os.environ:
        main_pycharm()
    else:
        real_main()


from copy import deepcopy
def main_pycharm():
    solution = solve

    test_inputs = None
    test_outputs = None
    judge = None
    slow_solution = None
    if solution is not None:
        if test_outputs is not None:
            test_with_answers(solution, test_inputs, test_outputs)
        if judge is not None:
            test_with_judge(solution, test_inputs, judge)
        if slow_solution is not None:
            test_with_slow_solution(solution, test_inputs, slow_solution)


def test_with_answers(solution, inputs_answers):
    total, wrong = 0, 0
    for args, test_ans in inputs_answers:
        ans = solution(*args.copy())
        if ans != test_ans:
            print('WRONG! ans=%s, test_ans=%s, args=%s' % (ans, test_ans, args))
            wrong += 1
        else:
            print('GOOD')
        total += 1
    print('ALL %d TESTS PASSED' % total if not wrong else '%d out of %d tests are WRONG' % (wrong, total))


def test_with_judge(solution, inputs_gen, judge):
    total, wrong = 0, 0
    for args in inputs_gen:
        ans = solution(*deepcopy(args))
        if not judge(deepcopy(ans), *deepcopy(args)):
            print('WRONG! ans=%s, args=%s' % (ans, args))
            wrong += 1
        total += 1
    print('ALL %d TESTS PASSED' % total if not wrong else '%d out of %d tests are WRONG' % (wrong, total))


def test_with_slow_solution(solution, inputs_gen, solution_slow):
    total, wrong = 0, 0
    for args in inputs_gen:
        ans = solution(*deepcopy(args))
        slow = solution_slow(*deepcopy(args))
        if ans != slow:
            print('WRONG! ans=%s, slow=%s, args=%s' % (ans, slow, args))
            wrong += 1
        total += 1
    print('ALL %d TESTS PASSED' % total if not wrong else '%d out of %d tests are WRONG' % (wrong, total))


# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")

def read():
    return input()

def read_int():
    return int(input())

def read_array(sep=None, maxsplit=-1):
    return input().split(sep, maxsplit)

def read_int_array():
    return [int(a) for a in read_array()]

# endregion

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip

import random
import collections
import math
import itertools
import bisect

def real_main():
    n = read_int()
    x, y, d = [], [], []
    for _ in range(n):
        s = read_array()
        x.append(int(s[0]))
        y.append(int(s[1]))
        d.append(s[2])
        # if d == 'R':
        #     dx.append(1)
        #     dy.append(0)
        # elif d == 'L':
        #     dx.append(-1)
        #     dy.append(0)
        # elif d == 'U':
        #     dx.append(0)
        #     dy.append(1)
        # else:
        #     dx.append(0)
        #     dy.append(-1)

    print(solve(x, y, d))

def solve(x, y, d):
    # POS, NEG, CONST
    y_max = [-math.inf]*3
    x_max = [-math.inf]*3
    x_min = [math.inf]*3
    y_min = [math.inf]*3
    for x, y, d in zip(x, y, d):
        if d == 'R' or d == 'L':
            y_max[2] = max(y_max[2], y)
            y_min[2] = min(y_min[2], y)
        if d == 'U' or d == 'D':
            x_max[2] = max(x_max[2], x)
            x_min[2] = min(x_min[2], x)

        if d == 'U':
            y_max[0] = max(y_max[0], y)
            y_min[1] = min(y_min[1], y)
        elif d == 'D':
            y_min[0] = min(y_min[0], y)
            y_max[1] = max(y_max[1], y)
        elif d == 'R':
            x_max[0] = max(x_max[0], x)
            x_min[1] = min(x_min[1], x)
        else:
            x_min[0] = min(x_min[0], x)
            x_max[1] = max(x_max[1], x)

    ans = (max(y_max) - min(y_min)) * (max(x_max) - min(x_min))

    def score_at(t):
        ymax = max(y_max[0] + t, y_max[1] - t, y_max[2])
        xmax = max(x_max[0] + t, x_max[1] - t, x_max[2])

        ymin = min(y_min[0] - t, y_min[1] + t, y_min[2])
        xmin = min(x_min[0] - t, x_min[1] + t, x_min[2])

        return (ymax-ymin) * (xmax - xmin)

    lo, hi = 0, ans
    eps = 10**(-10)
    while hi - lo >= eps:

        mid = (lo+hi) / 2
        mid_score = score_at(mid)
        ans = min(ans, mid_score)
        if mid_score > score_at(mid + eps):
            lo = mid
        else:
            hi = mid

    return ans



def main():
    if False and 'PYCHARM_HOSTED' in os.environ:
        main_pycharm()
    else:
        real_main()


from copy import deepcopy
def main_pycharm():
    solution = solve

    test_inputs = None
    test_outputs = None
    judge = None
    slow_solution = None
    if solution is not None:
        if test_outputs is not None:
            test_with_answers(solution, test_inputs, test_outputs)
        if judge is not None:
            test_with_judge(solution, test_inputs, judge)
        if slow_solution is not None:
            test_with_slow_solution(solution, test_inputs, slow_solution)


def test_with_answers(solution, inputs_answers):
    total, wrong = 0, 0
    for args, test_ans in inputs_answers:
        ans = solution(*args.copy())
        if ans != test_ans:
            print('WRONG! ans=%s, test_ans=%s, args=%s' % (ans, test_ans, args))
            wrong += 1
        else:
            print('GOOD')
        total += 1
    print('ALL %d TESTS PASSED' % total if not wrong else '%d out of %d tests are WRONG' % (wrong, total))


def test_with_judge(solution, inputs_gen, judge):
    total, wrong = 0, 0
    for args in inputs_gen:
        ans = solution(*deepcopy(args))
        if not judge(deepcopy(ans), *deepcopy(args)):
            print('WRONG! ans=%s, args=%s' % (ans, args))
            wrong += 1
        total += 1
    print('ALL %d TESTS PASSED' % total if not wrong else '%d out of %d tests are WRONG' % (wrong, total))


def test_with_slow_solution(solution, inputs_gen, solution_slow):
    total, wrong = 0, 0
    for args in inputs_gen:
        ans = solution(*deepcopy(args))
        slow = solution_slow(*deepcopy(args))
        if ans != slow:
            print('WRONG! ans=%s, slow=%s, args=%s' % (ans, slow, args))
            wrong += 1
        total += 1
    print('ALL %d TESTS PASSED' % total if not wrong else '%d out of %d tests are WRONG' % (wrong, total))


# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")

def read():
    return input()

def read_int():
    return int(input())

def read_array(sep=None, maxsplit=-1):
    return input().split(sep, maxsplit)

def read_int_array():
    return [int(a) for a in read_array()]

# endregion

if __name__ == "__main__":
    main()