Problem:


Score : 900 points

Problem Statement

In the city of Nevermore, there are 10^8 streets and 10^8 avenues, both numbered from 0 to 10^8-1.
All streets run straight from west to east, and all avenues run straight from south to north.
The distance between neighboring streets and between neighboring avenues is exactly 100 meters.

Every street intersects every avenue. Every intersection can be described by pair (x, y), where x is avenue ID and y is street ID.

There are N fountains in the city, situated at intersections (X_i, Y_i).
Unlike normal intersections, there's a circle with radius 10 meters centered at the intersection, and there are no road parts inside this circle.

The picture below shows an example of how a part of the city with roads and fountains may look like.

City governors don't like encountering more than one fountain while moving along the same road.
Therefore, every street contains at most one fountain on it, as well as every avenue.

Citizens can move along streets, avenues and fountain perimeters.
What is the shortest distance one needs to cover in order to get from intersection (x_1, y_1) to intersection (x_2, y_2)?

Constraints

0 \leq x_1, y_1, x_2, y_2 < 10^8

1 \leq N \leq 200,000

0 \leq X_i, Y_i < 10^8

X_i \neq X_j for i \neq j

Y_i \neq Y_j for i \neq j

Intersections (x_1, y_1) and (x_2, y_2) are different and don't contain fountains.

All input values are integers.

Input

Input is given from Standard Input in the following format:

x_1 y_1 x_2 y_2
N
X_1 Y_1
X_2 Y_2
:
X_N Y_N

Output

Print the shortest possible distance one needs to cover in order to get from intersection (x_1, y_1) to intersection (x_2, y_2), in meters.
Your answer will be considered correct if its absolute or relative error doesn't exceed 10^{-11}.

Sample Input 1

1 1 6 5
3
3 2
5 3
2 4

Sample Output 1

891.415926535897938

One possible shortest path is shown on the picture below. The path starts at the blue point, finishes at the purple point and follows along the red line.

Sample Input 2

3 5 6 4
3
3 2
5 3
2 4

Sample Output 2

400.000000000000000

Sample Input 3

4 2 2 2
3
3 2
5 3
2 4

Sample Output 3

211.415926535897938



Sample input:
1 1 6 5
3
3 2
5 3
2 4


Sample output:
891.415926535897938

Submission status: Runtime Error

Solution code:
import math

def step(cur_y, cur_idx, S, dp, end_y):
    if cur_idx >= len(S):
        return
    for idx, (x, y) in enumerate(S[cur_idx:], cur_idx):
        if cur_y <= y and dp[y] < dp[cur_y] + 1:
            dp[y] = dp[cur_y] + 1
            step(y, idx + 1, S, dp, end_y)

x_1, y_1, x_2, y_2 = map(int, input().split())
N = int(input())

if x_1 <= x_2:
    x_s = x_1
    y_s = y_1
    x_t = x_2
    y_t = y_2
else:
    x_s = x_2
    y_s = y_2
    x_t = x_1
    y_t = y_1

x_min = min(x_1, x_2)
x_max = max(x_1, x_2)
y_min = min(y_1, y_2)
y_max = max(y_1, y_2)

S = []
for i in range(N):
    X, Y = map(int, input().split())
    if X < x_min or X > x_max or Y < y_min or Y > y_max:
        continue
    S.append((X, Y))

dist = 0
if x_s == x_t:
    s_found = False
    for x, y in S:
        if x_s == x:
            s_found = True
            break
    if s_found:
        dist = abs(y_t - y_s) * 100 - 20 + math.pi * 20 / 2
    else:
        dist = abs(y_t - y_s) * 100
elif y_s == y_t:
    s_found = False
    for x, y in S:
        if y_s == y:
            s_found = True
            break
    if s_found:
        dist = abs(x_t - x_s) * 100 - 20 + math.pi * 20 / 2
    else:
        dist = abs(x_t - x_s) * 100
else:
    S.sort()
    if y_s > y_t:
        S.reverse()
        start_y = y_t
        end_y = y_s
    else:
        start_y = y_s
        end_y = y_t

    dp = {}
    for y in range(start_y, end_y + 1):
        dp[y] = 0
    step(start_y, 0, S, dp, end_y)
    #print(dp)
    round_count = max(dp.values())
    dist = (abs(x_t - x_s) + abs(y_t - y_s)) * 100 + (math.pi * 20 / 4 - 20) * round_count

print(dist)

Submission status: Runtime Error

Solution code:
from sys import stdin
from math import pi

# 그냥 가면 100(|x1 - x2| + |y1 - y2|)
# junction 통과할 때마다 답이 좋아짐. 이 개수를 늘려야?
C = 100
J = -20 + pi * 2*10/4

x1, y1, x2, y2 = map(int, stdin.readline().split())
N = int(stdin.readline())
x = [0] * N
y = [0] * N
for i in range(N):
    x[i], y[i] = map(int, stdin.readline().split())

for i in range(N):
    if x1 > x2: x[i] = int(1e8) - x[i]
    if y1 > y2: y[i] = int(1e8) - y[i]

if x1 > x2: x1, x2 = int(1e8) - x1, int(1e8) - x2
if y1 > y2: y1, y2 = int(1e8) - y1, int(1e8) - y2

ans = 0
P = [(x[i], y[i]) for i in range(N) if x1 <= x[i] <= x2 and y1 <= y[i] <= y2]
if x1 == x2 or y1 == y2:
    ans = C * (x2 - x1 + y2 - y1) + (20 + J) * len(P)
elif len(P) == 0:
    ans = C * (x2 - x1 + y2 - y1)
elif len(P) > 0:
    P.sort()
    # y LIS
    import bisect
    cur = [P[0][1]]
    for x, y in P[1:]:
        if cur[-1] < y:
            cur.append(y)
        elif y < cur[0]:
            cur = [y]
        else:
            cur = cur[:p] + [y]
    ans = C * (x2 - x1 + y2 - y1) + J * len(cur)
print("%.20f" % ans)

Submission status: Runtime Error

Solution code:
import math

def step(cur_y, cur_idx, S, dp, end_y):
    for idx, (x, y) in enumerate(S[cur_idx:], cur_idx):
        if cur_y <= y and dp[y] < dp[cur_y] + 1:
            dp[y] = dp[cur_y] + 1
            step(y, idx + 1, S, dp, end_y)

x_1, y_1, x_2, y_2 = map(int, input().split())
N = int(input())

if x_1 <= x_2:
    x_s = x_1
    y_s = y_1
    x_t = x_2
    y_t = y_2
else:
    x_s = x_2
    y_s = y_2
    x_t = x_1
    y_t = y_1

x_min = min(x_1, x_2)
x_max = max(x_1, x_2)
y_min = min(y_1, y_2)
y_max = max(y_1, y_2)

S = []
for i in range(N):
    X, Y = map(int, input().split())
    if X < x_min or X > x_max or Y < y_min or Y > y_max:
        continue
    S.append((X, Y))

S.sort()
if y_s > y_t:
    S.reverse()
    start_y = y_t
    end_y = y_s
else:
    start_y = y_s
    end_y = y_t

dist = 0
if x_s == x_t:
    s_found = False
    for x, y in S:
        if x_s == x:
            s_found = True
            break
    if s_found:
        dist = abs(y_t - y_s) * 100 - 20 + math.pi * 20 / 2
    else:
        dist = abs(y_t - y_s) * 100
elif y_s == y_t:
    s_found = False
    for x, y in S:
        if y_s == y:
            s_found = True
            break
    if s_found:
        dist = abs(x_t - x_s) * 100 - 20 + math.pi * 20 / 2
    else:
        dist = abs(x_t - x_s) * 100
else:
    dp = {}
    for y in range(start_y, end_y + 1):
        dp[y] = 0
    step(start_y, 0, S, dp, end_y)
    #print(dp)
    round_count = max(dp.values())
    dist = (abs(x_t - x_s) + abs(y_t - y_s)) * 100 + (math.pi * 20 / 4 - 20) * round_count

print(dist)

Submission status: Runtime Error

Solution code:
import sys
import math
def link( pool, f):
    def subset( a, b):
        return a[0] > b[0] and a[1] > b[0]
    notsubset = []
    max_value = 0
    for n,p in enumerate(pool): ##(X, Y, V)
        if subset(p[:2], f):
            max_value = max(max_value, p[2])
        else:
            notsubset.append(n)
    new_pool = [ pool[i] for i in notsubset]
    new_pool.append(
        f + [max_value + 1]
    )
    return new_pool
def orderit( fountains):
    return sorted( fountains, key=lambda x: max( x[0], x[1]), reverse=True)
def removeRedundant( fountains):
    return filter( lambda f:
        f if f[0] >= goal['x1'] and 
        f[0] <= goal['x2'] and
        f[1] >= goal['y1'] and
        f[1] >= goal['y1'] else None,
        fountains)
def transform( goal, fountains):
    if goal['x1'] > goal['x2']:
        goal['x1'] = -goal['x1']
        goal['x2'] = -goal['x2']
        for f in fountains:
            f[0] = -f[0]
    if goal['y1'] > goal['y2']:
        goal['y1'] = -goal['y1']
        goal['y2'] = -goal['y2']
        for f in fountains:
            f[1] = -f[1]
    return goal, fountains
def count_max( pool):
    print pool
    return max( map( lambda x: x[2], pool))
def travel( goal, fcount):
    r = 10
    res = 100 *( goal['x2'] - goal['x1'] + goal['y2'] - goal['y1'] )
    res += fcount * (  math.pi * 5 - 20 )
    return res                                                                                                                                                                                                                        
        
a,b,c,d = map(int, sys.stdin.readline().strip().split())
goal = {
    'x1': a,
    'y1': b,
    'x2': c,
    'y2': d,
}
N = int(sys.stdin.readline().strip())
fountains = []
for i in xrange(N):
    fountains.append(
        map( int, sys.stdin.readline().strip().split() )
    )
goal, fountains = transform( goal, fountains)
fountains = removeRedundant( fountains)
fountains = orderit( fountains)
pool = []
for f in fountains:
    pool = link(pool, f)
print travel( goal, count_max(pool))

Submission status: Runtime Error

Solution code:
from math import pi


def solve():
    x1, y1, x2, y2 = map(int, input().split())
    if x1 > x2:
        x1, y1, x2, y2 = x2, y2, x1, y1
    is_down = y1 > y2
    if is_down:
        y1, y2 = -y1, -y2

    n = int(input())
    fountains = []
    for _ in range(n):
        x, y = map(int, input().split())
        if is_down:
            y = -y
        if x < x1 or x2 < x or y < y1 or y2 < y:
            continue
        fountains.append((y, x))
    fountains.sort()

    if x1 == x2:
        base = (y2 - y1) * 100
        for y, x in fountains:
            if x == x1:
                if y1 < y < y2:
                    return base + 10 * pi - 20
                else:
                    break
        return base
    if y1 == y2:
        base = (x2 - x1) * 100
        for y, x in fountains:
            if y == y1:
                if x1 < x < x2:
                    return base + 10 * pi - 20
                else:
                    break
        return base

    uncmp_x = sorted({x1, x2}.union(f[1] for f in fountains))
    uncmp_y = sorted({y1, y2}.union(f[0] for f in fountains))
    cmp_x = {x: i for i, x in enumerate(uncmp_x)}
    cmp_y = {y: i for i, y in enumerate(uncmp_y)}
    fountains = [(cmp_y[y], cmp_x[x]) for y, x in fountains]

    dp = [0] * len(uncmp_x)
    for y, x in fountains:
        cdp = dp.copy()
        for cx, cnt in enumerate(dp[:x]):
            cdp[cx] = max(dp[cx], cnt + 1)
        cdp[cx] = max(dp[cx], 1)
        dp = cdp

    mc = max(dp)
    return (x2 - x1 + y2 - y1) * 100 - mc * 20 + mc * 5 * pi


print(solve())

Submission status: Runtime Error

Solution code:
from math import pi


def solve():
    x1, y1, x2, y2 = map(int, input().split())
    if x1 > x2:
        x1, y1, x2, y2 = x2, y2, x1, y1
    is_down = y1 > y2
    if is_down:
        y1, y2 = -y1, -y2

    n = int(input())
    fountains = []
    for _ in range(n):
        x, y = map(int, input().split())
        if is_down:
            y = -y
        if x < x1 or x2 < x or y < y1 or y2 < y:
            continue
        fountains.append((y, x))
    fountains.sort()

    if x1 == x2:
        base = (y2 - y1) * 100
        for y, x in fountains:
            if x == x1:
                if y1 < y < y2:
                    return base + 10 * pi - 20
                else:
                    break
        return base
    if y1 == y2:
        base = (x2 - x1) * 100
        for y, x in fountains:
            if y == y1:
                if x1 < x < x2:
                    return base + 10 * pi - 20
                else:
                    break
        return base

    uncmp_x = sorted({x1, x2, *(f[1] for f in fountains)})
    uncmp_y = sorted({y1, y2, *(f[0] for f in fountains)})
    cmp_x = {x: i for i, x in enumerate(uncmp_x)}
    cmp_y = {y: i for i, y in enumerate(uncmp_y)}
    fountains = [(cmp_y[y], cmp_x[x]) for y, x in fountains]

    dp = [0] * len(uncmp_x)
    for y, x in fountains:
        cdp = dp.copy()
        for cx, cnt in enumerate(dp[:x]):
            cdp[cx] = max(dp[cx], cnt + 1)
        cdp[cx] = max(dp[cx], 1)
        dp = cdp

    mc = max(dp)
    return (x2 - x1 + y2 - y1) * 100 - mc * 20 + mc * 5 * pi


print(solve())

Submission status: Runtime Error

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
gosa = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

class Seg():
    def __init__(self, n, default, func):
        i = 1
        while 2**i <= n:
            i += 1
        self.D = default
        self.H = i
        self.N = 2**i
        self.A = [default] * (self.N*2-1)
        self.F = func

    def find(self, i):
        return self.A[i + self.N - 1]

    def update(self, i, x):
        i += self.N - 1
        self.A[i] = x
        while i > 0:
            i = (i-1) // 2
            self.A[i] = self.merge(self.A[i*2+1], self.A[i*2+2])

    def merge(self, a, b):
        return self.F(a, b)

    def total(self):
        return self.A[0]

    def query(self, a, b):
        def _query(k,l,r):
            if r <= a or b <= l:
                return self.D
            if a <= l and r <= b:
                return self.A[k]
            m = (l+r)//2
            vl = _query(k*2+1,l,m)
            vr = _query(k*2+2,m,r)
            return self.merge(vl,vr)
        return _query(0,0,self.N)


def main():
    x1,y1,x2,y2 = LI()
    n = I()
    a = sorted([LI() for _ in range(n)])
    if x1 > x2:
        x1,y1,x2,y2 = x2,y2,x1,y1
    yy1 = y1
    yy2 = y2
    if yy1 > yy2:
        yy1,yy2 = yy2,yy1

    def f(a,b):
        if a>b:
            return a
        return b

    s = Seg(n+2,0,f)

    yf = y2 < y1
    for x,y in a:
        if x < x1:
            continue
        if x > x2:
            continue
        if y < yy1:
            continue
        if y > yy2:
            continue
        if yf:
            y = n-y+1
        t = s.query(0,y)
        s.update(y,t+1)

    r = (abs(x2-x1) + abs(y2-y1)) * 100
    y = y2
    if yf:
        y = n-y2+1
    k = 20 - 5 * math.pi
    t = s.total()
    r = r-t*k
    if t > min(abs(x2-x1),abs(y1-y2)):
        r += 5 * math.pi

    return r

print(main())

Submission status: Runtime Error

Solution code:
from sys import stdin
from math import pi
from bisect import bisect
from itertools import repeat
def main():
    x1, y1, x2, y2 = map(int, stdin.readline().split())
    sx = 1
    if x1 > x2:
        x1, x2 = -x1, -x2
        sx = -1
    sy = 1
    if y1 > y2:
        y1, y2 = -y1, -y2
        sy = -1
    n = int(stdin.readline())
    dat = map(int, stdin.read().split(), repeat(10, 2 * n))
    a = []
    for i in xrange(n):
        x, y = dat[i*2:i*2+2]
        x *= sx
        y *= sy
        if x1 <= x <= x2 and y1 <= y <= y2:
            a.append((x, y))
    a.sort()
    c = pi * 5 - 10
    if x1 == x2 or y1 == y2:
        if a:
            print "%.12f" % ((x2 - x1 + y2 - y1) * 100 + 2 * c)
        else:
            print "%.12f" % ((x2 - x1 + y2 - y1) * 100)
        return
    b = [-10 ** 9] + [10 ** 9] * 200010
    m = 1
    for x, y in a:
        p = bisect(b, y, lo=0, hi=m)
        b[p] = y
        if p == m:
            m += 1
    c = pi * 5 - 20
    print "%.12f" % ((x2 - x1 + y2 - y1) * 100 + (m - 1) * c)
main()
from sys import stdin
from math import pi
from bisect import bisect
from itertools import repeat
def main():
    x1, y1, x2, y2 = map(int, stdin.readline().split())
    sx = 1
    if x1 > x2:
        x1, x2 = -x1, -x2
        sx = -1
    sy = 1
    if y1 > y2:
        y1, y2 = -y1, -y2
        sy = -1
    n = int(stdin.readline())
    dat = map(int, stdin.read().split(), repeat(10, 2 * n))
    a = []
    for i in xrange(n):
        x, y = dat[i*2:i*2+2]
        x *= sx
        y *= sy
        if x1 <= x <= x2 and y1 <= y <= y2:
            a.append((x, y))
    a.sort()
    c = pi * 5 - 10
    if x1 == x2 or y1 == y2:
        if a:
            print "%.12f" % ((x2 - x1 + y2 - y1) * 100 + 2 * c)
        else:
            print "%.12f" % ((x2 - x1 + y2 - y1) * 100)
        return
    b = [-10 ** 9] + [10 ** 9] * 200010
    m = 1
    for x, y in a:
        p = bisect(b, y, lo=0, hi=m)
        b[p] = y
        if p == m:
            m += 1
    c = pi * 5 - 20
    print "%.12f" % ((x2 - x1 + y2 - y1) * 100 + (m - 1) * c)
main()