253C - Text Editor - CodeForces Solution


data structures dfs and similar graphs greedy shortest paths *1600

Please click on ads to support us..

Python Code:

import os
import math
import os
import sys
from bisect import bisect_left, bisect_right, insort
from collections import defaultdict, Counter
from io import BytesIO, IOBase
from types import GeneratorType

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


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')


def isPrime(n):
    if n <= 1:
        return False
    if n <= 3:
        return True
    if n % 2 == 0 or n % 3 == 0:
        return False
    i = 5
    while i * i <= n:
        if n % i == 0 or n % (i + 2) == 0:
            return False
        i = i + 6
    return True


def lcm(a, b): return (a * b) // math.gcd(a, b)


def ciel(a, b):
    x = a // b
    if a % b == 0:
        return x
    return x + 1


def ints_get(): return map(int, input().strip().split())


def list_get(): return list(map(int, sys.stdin.readline().strip().split()))


def chars_get(): return list(map(str, sys.stdin.readline().strip().split()))


def ipn(): return int(input())


def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to

    return wrappedfunc




def main():
        file = open("input.txt", "r")
    ar = file.readline().split()
    n = int(ar[0])
    a = []
    ar = file.readline().split()
    for i in ar:
        a.append(int(i))
    ar = file.readline().split()
    r1, c1, r2, c2 = int(ar[0]), int(ar[1]), int(ar[2]), int(ar[3])
    r1 -= 1
    c1 -= 1
    r2 -= 1
    c2 -= 1
    val = sys.maxsize
    for i in range(0, n):
        x = c1
        if i < r1:
            for j in range(r1 - 1, i - 1, -1):
                x = min(x, a[j])

        else:
            for j in range(r1, i + 1):
                x = min(x, a[j])
        q = abs(r1 - i) + abs(r2 - i)
        if i < r2:
            for j in range(i, r2 + 1):
                x = min(x, a[j])
        else:
            for j in range(i - 1, r2 - 1, -1):
                x = min(x, a[j])
        val = min(val, q + abs(x - c2))

    file.close()
    file1 = open("output.txt", "w")
    L = str(val)
        file1.writelines(L)

    return


if __name__ == "__main__":
    main()


Comments

Submit
0 Comments
More Questions

1149A - Prefix Sum Primes
277A - Learning Languages
769A - Year of University Entrance
1738A - Glory Addicts
1738C - Even Number Addicts
1064B - Equations of Mathematical Magic
384A - Coder
1738B - Prefix Sum Addicts
1352D - Alice Bob and Candies
1316D - Nash Matrix
1548B - Integers Have Friends
348A - Mafia
315B - Sereja and Array
204A - Little Elephant and Interval
385B - Bear and Strings
114C - Grammar Lessons
1427A - Avoiding Zero
583A - Asphalting Roads
1358B - Maria Breaks the Self-isolation
828A - Restaurant Tables
1735A - Working Week
1735D - Meta-set
1735B - Tea with Tangerines
1735C - Phase Shift
1321C - Remove Adjacent
281B - Nearest Fraction
1043A - Elections
1598C - Delete Two Elements
1400C - Binary String Reconstruction
1734D - Slime Escape