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: Wrong Answer

Solution code:
def abs(x):
	return x if x >= 0 else -x

def lower_bound(X, y):
	L, R = 0, len(X)
	while L < R:
		M = (L + R) >> 1
		if y > X[M]:
			L = M + 1
		else:
			R = M
	return L
			
	
ADJUST = 10**49
PI = 31415926535897932384626433832795028841971693993751
ax, ay, bx, by = list(map(int, input().split()))
a = (ax, ay)
b = (bx, by)
n = int(input())
p = [list(map(int, input().split())) for i in range(n)]
p.sort()
if a[0] > b[0]:
	a, b = b, a
ans = 100 * ADJUST * (abs(a[0] - b[0]) + abs(a[1] - b[1]))
mx = min(a[0], b[0])
Mx = max(a[0], b[0])
my = min(a[1], b[1])
My = max(a[1], b[1])
if a[0] == b[0]:
	for point in p:
		if point[0] == a[0]:
			if my < point[1] and point[1] < My:
				ans += 10*PI - 20*ADJUST
elif a[1] == b[1]:
	for point in p:
		if point[1] == a[1]:
			if mx < point[0] and point[0] < Mx:
				ans += 10*PI - 20*ADJUST
else:
	lis = []
	for point in p:
		if point[0] < mx or point[0] > Mx or point[1] < my or point[1] > My:
			continue
		y = abs(point[1] - a[1])
		lb = lower_bound(lis, y)
		if lb == len(lis):
			lis.append(y)
		else:
			lis[lb] = y
	size = len(lis)
	ans += size * (5*PI - 20*ADJUST)

print('%d.%049d' % (ans//ADJUST, ans%ADJUST))

Submission status: Wrong Answer

Solution code:
temp = list(map(int, input().split()))
start = temp[0:2]
goal = temp[2:4]
N = int(input())
fountain = [list(map(int, input().split())) for n in range(N)]
import math
circle = 0.2 * math.pi / 4
# for n in range(N):
#     fountain.append(list(map(int, input().split())))

flag = 0
ans = 0
prevVec = 0
index = []
direction = [[1, 0], [-1, 0], [0, 1], [0, -1]]
distance = (goal[0] - start[0])**2 + (goal[1] - start[1])**2
for i in range(4):
    temp = start[:]
    temp = [t + d for t, d in zip(temp, direction[i])]
    newDistance = (goal[0] - temp[0])**2 + (goal[1] - temp[1])**2
    if distance > newDistance:
        index.append(i)
# print ("move", move)
# for i in range(len(move)):
#     index.append(move.index(min(move)))
#     move[move.index(min(move))] = 1000000000
# print ("index", index)
# direction = [direction[i] for i in index]
while (1):
    # right   = (goal[0] - (start[0]+1))  **2 + (goal[1] - start[1])      **2
    # left    = (goal[0] - (start[0]-1))  **2 + (goal[1] - start[1])      **2
    # up      = (goal[0] - (start[0]))    **2 + (goal[1] - (start[1]+1))  **2
    # down    = (goal[0] - (start[0]))    **2 + (goal[1] - (start[1]-1))  **2
    # move = [right, left, up, down]
    #
    move = [1000000000 for i in range(4)]
    for i in index:
        temp = start[:]
        temp = [t + d for t, d in zip(temp, direction[i])]
        move[i] = (goal[0] - temp[0])**2 + (goal[1] - temp[1])**2
    vec = move.index(min(move))
    # print ("vec", vec)
    if flag:
        if vec != prevVec:
            ans -= circle
        flag = 0

    # print ("start", start)
    start = [s + d for s, d in zip(start, direction[vec])]
    # print ("start", start)

    if start in fountain:
        ans -= 0.2
        ans += circle * 2
        flag = 1
    ans += 1
    prevVec = vec
    if start == goal:
        break

print (ans * 100)

Submission status: Wrong Answer

Solution code:
temp = list(map(int, input().split()))
start = temp[0:2]
goal = temp[2:4]
N = int(input())
fountain = [list(map(int, input().split())) for n in range(N)]
import math
circle = 0.2 * math.pi / 4
# for n in range(N):
#     fountain.append(list(map(int, input().split())))

flag = 0
ans = 0
prevVec = 0
vec = 0
temp = [[1, 0], [-1, 0], [0, 1], [0, -1]]
while (1):
    right   = (goal[0] - (start[0]+1))  **2 + (goal[1] - start[1])      **2
    left    = (goal[0] - (start[0]-1))  **2 + (goal[1] - start[1])      **2
    up      = (goal[0] - (start[0]))    **2 + (goal[1] - (start[1]+1))  **2
    down    = (goal[0] - (start[0]))    **2 + (goal[1] - (start[1]-1))  **2
    move = [right, left, up, down]
    vec = move.index(min(move))
    if flag:
        if vec != prevVec:
            ans -= circle
        flag = 0
    start = [s + t for s, t in zip(start, temp[vec])]
    # if vec == 0:
    #     start[0] += 1
    # elif vec == 1:
    #     start[0] += -1
    # elif vec == 2:
    #     start[1] += 1
    # else:
    #     start[1] += -1
    if start in fountain:
        ans -= 0.2
        ans += circle * 2
        flag = 1
    ans += 1
    prevVec = vec
    if start == goal:
        break

print (ans * 100)

Submission status: Wrong Answer

Solution code:
temp = list(map(int, input().split()))
start = temp[0:2]
goal = temp[2:4]
N = int(input())
fountain = []
import math
circle = 0.2 * math.pi
for n in range(N):
    fountain.append(list(map(int, input().split())))

flag = 0
ans = 0
prevVec = 0
vec = 0
while (1):
    right   = (goal[0] - (start[0]+1))  **2 + (goal[1] - start[1])      **2
    left    = (goal[0] - (start[0]-1))  **2 + (goal[1] - start[1])      **2
    up      = (goal[0] - (start[0]))    **2 + (goal[1] - (start[1]+1))  **2
    down    = (goal[0] - (start[0]))    **2 + (goal[1] - (start[1]-1))  **2
    move = [right, left, up, down]
    vec = move.index(min(move))
    if flag:
        if vec != prevVec:
            ans -= circle / 4
        flag = 0
    if vec == 0:
        start[0] += 1
    elif vec == 1:
        start[0] += -1
    elif vec == 2:
        start[1] += 1
    else:
        start[1] += -1
    if start in fountain:
        ans -= 0.2
        ans += circle / 2
        flag = 1
    ans += 1
    prevVec = vec
    if start == goal:
        break

print (ans * 100)

Submission status: Wrong Answer

Solution code:
temp = list(map(int, input().split()))
start = temp[0:2]
goal = temp[2:4]
N = int(input())
fountain = []
import math
circle = 20 * math.pi
for n in range(N):
    fountain.append(list(map(int, input().split())))

flag = 0
ans = 0
prevVec = 0
vec = 0
while (1):
    right = (goal[0] - (start[0]+1))**2 + (goal[1] - start[1])**2
    left = (goal[0] - (start[0]-1))**2 + (goal[1] - start[1])**2
    up = (goal[0] - (start[0]))**2 + (goal[1] - (start[1]+1))**2
    down = (goal[0] - (start[0]))**2 + (goal[1] - (start[1]-1))**2
    move = [right, left, up, down]
    vec = move.index(min(move))
    if flag:
        if vec != prevVec:
            ans -= circle / 4
        flag = 0
    if vec == 0:
        start[0] += 1
    elif vec == 1:
        start[0] += -1
    elif vec == 2:
        start[1] += 1
    else:
        start[1] += -1
    if start in fountain:
        ans -= 20
        ans += circle / 2
        flag = 1
    ans += 100
    prevVec = vec
    if start == goal:
        break

print (ans)

Submission status: Wrong Answer

Solution code:
temp = list(map(int, input().split()))
start = temp[0:2]
goal = temp[2:4]
N = int(input())
fountain = []
import math
circle = 20 * math.pi
for n in range(N):
    fountain.append(list(map(int, input().split())))

flag = 0
ans = 0
prevVec = 0
vec = 0
while (1):
    right = (goal[0] - (start[0]+1))**2 + (goal[1] - start[1])**2
    left = (goal[0] - (start[0]-1))**2 + (goal[1] - start[1])**2
    up = (goal[0] - (start[0]))**2 + (goal[1] - (start[1]+1))**2
    down = (goal[0] - (start[0]))**2 + (goal[1] - (start[1]-1))**2
    move = [right, left, up, down]
    vec = move.index(min(move))
    if start in fountain:
        ans -= 10
        if vec == prevVec:
            ans += circle / 4
    if vec == 0:
        start[0] += 1
    elif vec == 1:
        start[0] += -1
    elif vec == 2:
        start[1] += 1
    else:
        start[1] += -1
    if start in fountain:
        ans -= 10
        ans += circle / 4

    ans += 100
    prevVec = vec
    if start == goal:
        break

print (ans)

Submission status: Wrong Answer

Solution code:
import operator
from bisect import bisect_left
from math import pi
import decimal


def lis(A):
    L = [A[0]]
    for a in A[1:]:
        if a > L[-1]:
            L.append(a)
        else:
            L[bisect_left(L, a)] = a
    return len(L)


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
    base = decimal.Decimal(100) * (x2 - x1 + y2 - y1)

    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((x, y))

    if not fountains:
        return str(base)

    dpi = decimal.Decimal(pi)

    fountains.sort()
    fountains_y = list(map(operator.itemgetter(1), fountains))
    mc = lis(fountains_y)
    dist = base + (dpi * 5 - 20) * mc
    if mc == min(x2 - x1, y2 - y1) + 1:
        dist += dpi * 10 - 20
    return str(dist)


print(solve())

Submission status: Wrong Answer

Solution code:
import operator
from bisect import bisect_left
from math import pi
import decimal


def lis(A):
    L = [A[0]]
    for a in A[1:]:
        if a > L[-1]:
            L.append(a)
        else:
            L[bisect_left(L, a)] = a
    return len(L)


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
    base = decimal.Decimal((x2 - x1 + y2 - y1) * 100)

    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((x, y))

    if not fountains:
        return str(base)

    dpi = decimal.Decimal(pi)

    if x1 == x2 or y1 == y2:
        return str(base + dpi * 10 - 20)

    fountains.sort()
    fountains_y = list(map(operator.itemgetter(1), fountains))
    mc = lis(fountains_y)
    base = decimal.Decimal((x2 - x1 + y2 - y1) * 100 - mc * 20)

    return str(base + dpi * 5 * mc)


print(solve())

Submission status: Wrong Answer

Solution code:
import operator
from bisect import bisect_left
from math import pi
import decimal


def lis(A):
    L = [A[0]]
    for a in A[1:]:
        if a > L[-1]:
            L.append(a)
        else:
            L[bisect_left(L, a)] = a
    return len(L)


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((x, y))

    if not fountains:
        return (x2 - x1 + y2 - y1) * 100

    fountains.sort()
    dpi = decimal.Decimal(pi)

    if x1 == x2:
        base = decimal.Decimal((y2 - y1) * 100)
        return base + dpi * 10 - 20
    if y1 == y2:
        base = decimal.Decimal((x2 - x1) * 100)
        return base + dpi * 10 - 20

    fountains_y = list(map(operator.itemgetter(1), fountains))
    mc = lis(fountains_y)
    base = decimal.Decimal((x2 - x1 + y2 - y1) * 100 - mc * 20)

    return str(base + dpi * 5 * mc)


print(solve())

Submission status: Wrong Answer

Solution code:
import math
import bisect

def get_lis(S):
    dp = [float('inf')] * len(S)
    for i, (x, y) in enumerate(S):
        a = S[i][1]
        j = bisect.bisect_right(dp, a)
        dp[j] = a
    return bisect.bisect_left(dp, float('inf'))

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
s_num = len(S)

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

round_count = get_lis(S)
dist = (abs(x_t - x_s) + abs(y_t - y_s)) * 100 + (math.pi * 20 / 4 - 20) * round_count

if abs(x_t - x_s) + 1 == s_num or abs(y_t - y_s) + 1 == s_num:
    dist += math.pi * 5

print(dist)

Submission status: Wrong Answer

Solution code:
import math
import bisect

def get_lis(S):
    dp = [float('inf')] * len(S)
    for i, (x, y) in enumerate(S):
        a = S[i][1]
        j = bisect.bisect_right(dp, a)
        dp[j] = a
    return bisect.bisect_left(dp, float('inf'))

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
s_num = len(S)

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

round_count = get_lis(S)
dist = (abs(x_t - x_s) + abs(y_t - y_s)) * 100 + (math.pi * 20 / 4 - 20) * round_count

if abs(x_t - x_s) + 1 == s_num or abs(y_t - y_s) + 1 == s_num:
    dist += math.pi * 10 - 20

print(dist)

Submission status: Wrong Answer

Solution code:
#最大増加部分列（Longest Increasing Subsequence）
def LIS(L):
  """数列L→最大増加部分列の長さ
  計算量はNlogN"""
  from bisect import bisect
  best=[]
  print(L)
  for i in L:
    pos=bisect(best,i)
    print(i,best,pos)
    if len(best)<=pos:
      best.append(i)
    else:
      best[pos]=i
    print(i,best,pos)
  return len(best)

import math 
x1,y1,x2,y2=map(int,input().split())
N=int(input())
W=abs(x2-x1)
H=abs(y2-y1)
xsgn=2*(x2>x1)-1
ysgn=2*(y2>y1)-1
XY=[]
for i in range(N):
  x,y=map(int,input().split())
  if (x-x1)*(x-x2)<=0 and (y-y1)*(y-y2)<=0:
    XY+=[[xsgn*(x-x1),ysgn*(y-y1)]]
N=len(XY)
short=20-5*math.pi
long=10*math.pi-20
straight=100*(W+H)

XY.sort()
Y = [y for x,y in XY]

fountain=LIS(Y)

if fountain<min(W,H)+1:
  print(straight-short*fountain)
else:
  print(straight-short*(fountain-1)+long)

Submission status: Wrong Answer

Solution code:
#最大増加部分列（Longest Increasing Subsequence）
def LIS(N,a):
  """数列の長さN, 数列a→最大増加部分列の長さ
  計算量はN^2"""
  res=0
  dp=[1]*N#最後がa[i] となるような最長の増加部分列の長さ
  for i in range(N):
    for j in range(i):
      if XY[j][0]<XY[i][0] and XY[j][1]<XY[i][1]:
        dp[i]=max(dp[i],dp[j]+1)
    res=max(res,dp[i])
  return res

import math 
x1,y1,x2,y2=map(int,input().split())
N=int(input())
W=abs(x2-x1)
H=abs(y2-y1)
xsgn=2*(x2>x1)-1
ysgn=2*(y2>y1)-1
XY=[]
for i in range(N):
  x,y=map(int,input().split())
  if (x-x1)*(x-x2)<=0 and (y-y1)*(y-y2)<=0:
    XY+=[[xsgn*(x-x1),ysgn*(y-y1)]]
N=len(XY)
short=20-5*math.pi
long=10*math.pi-20
straight=100*(W+H)
from bisect import bisect
 
# find LIS
XY.sort()
XY = [y for x,y in XY]

dp = [H+1]*len(XY)
 
for i,f in enumerate(XY):
  dp[bisect(dp,f)] = f
fountain = bisect(dp,H)
print(fountain)
if fountain<min(W,H)+1:
  print(straight-short*fountain)
else:
  print(straight-short*(fountain-1)+long)

Submission status: Wrong Answer

Solution code:
#最大増加部分列（Longest Increasing Subsequence）
def LIS(N,a):
  """数列の長さN, 数列a→最大増加部分列の長さ
  計算量はN^2"""
  res=0
  dp=[1]*N#最後がa[i] となるような最長の増加部分列の長さ
  for i in range(N):
    for j in range(i):
      if XY[j][0]<XY[i][0] and XY[j][1]<XY[i][1]:
        dp[i]=max(dp[i],dp[j]+1)
    res=max(res,dp[i])
  return res

import math 
x1,y1,x2,y2 = map(int,input().split())
 
W,H = abs(x1-x2),abs(y1-y2)
 
 
 
flip_x = x1 > x2
flip_y = y1 > y2
 
xo = min(x1,x2)
yo = min(y1,y2)
 
N = int(input())
 
XY = []
 
for i in range(N):
  x,y = map(int,input().split())
  x -= xo
  y -= yo
  if 0 <= x <= W and 0 <= y <= H:
    if flip_x:
      x = W-x
    if flip_y:
      y = H-y
    XY.append((x,y))
 
N = len(XY)
short=20-5*math.pi
long=10*math.pi-20
straight=100*(W+H)
fountain=LIS(N,XY)
if fountain<min(W,H)+1:
  print(straight-short*fountain)
else:
  print(straight-short*(fountain-1)+long)

Submission status: Wrong Answer

Solution code:
#最大増加部分列（Longest Increasing Subsequence）
def LIS(N,a):
  """数列の長さN, 数列a→最大増加部分列の長さ
  計算量はN^2"""
  res=0
  dp=[1]*N#最後がa[i] となるような最長の増加部分列の長さ
  for i in range(N):
    for j in range(i):
      if XY[j][0]<XY[i][0] and XY[j][1]<XY[i][1]:
        dp[i]=max(dp[i],dp[j]+1)
    res=max(res,dp[i])
  return res

import math 
x1,y1,x2,y2=map(int,input().split())
N=int(input())
W=abs(x2-x1)
H=abs(y2-y1)
xsgn=2*(x2>x1)-1
ysgn=2*(y2>y1)-1
XY=[]
for i in range(N):
  x,y=map(int,input().split())
  if (x-x1)*(x-x2)<=0 and (y-y1)*(y-y2)<=0:
    XY+=[[xsgn*(x-x1),ysgn*(y-y1)]]
N=len(XY)
pi=math.pi
short=20-5*pi
long=10*pi-20
fountain=0
straight=100*(abs(x1-x2)+abs(y1-y2))
fountain=LIS(N,XY)
if fountain<min(W,H)+1:
  print(straight-short*fountain)
else:
  print(straight-short*(fountain-1)+long)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from math import pi
 
# 그냥 가면 100(|x1 - x2| + |y1 - y2|)
# junction 통과할 때마다 답이 좋아짐. 이 개수를 늘려야?
C = 100
J90 = -20 + pi * 2*10/4
J180 = -20 + pi * 2*10/2
 
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 = J180 * len(P)
elif len(P) == 0:
    ans = 0
elif len(P) > 0:
    P.sort()
 
    def lis(a):
        if len(a) == 0: return -int(2e9)
        import bisect
        cur = [a[0]]
        for y in a[1:]:
            if cur[-1] < y:
                cur.append(y)
            else:
                cur[bisect.bisect_left(cur, y)] = y
        return len(cur)
 
    ans = J90 * lis([y for x, y in P if x1 < x < x2 and y1 < y < y2])
    if P[0][0] == x1 and y1 < P[0][1] < y2:
        ans = min(ans, J180 + J90 * lis([y for x, y in P if x > x1 and P[0][1] < y]))
    if x1 < P[0][0] < x2 and P[0][1] == y1:
        ans =  min(ans, J180 + J90 * lis([y for x, y in P if y > y1 and P[0][0] < x]))
    if P[-1][0] == x2 and y1 < P[-1][1] < y2:
        ans =  min(ans, J180 + J90 * lis([y for x, y in P if x < x2 and P[-1][1] > y]))
    if x1 < P[-1][0] < x2 and P[-1][1] == y2:
        ans =  min(ans, J180 + J90 * lis([y for x, y in P if y < y2 and P[-1][0] > x]))
 
print("%.20f" % (C * (x2 - x1 + y2 - y1) + ans))

Submission status: Wrong Answer

Solution code:
from sys import stdin
from math import pi
 
# 그냥 가면 100(|x1 - x2| + |y1 - y2|)
# junction 통과할 때마다 답이 좋아짐. 이 개수를 늘려야?
C = 100
J90 = -20 + pi * 2*10/4
J180 = -20 + pi * 2*10/2
 
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 = (-20 + 10*pi) * len(P)
elif len(P) == 0:
    ans = 0
elif len(P) > 0:
    P.sort()
 
    def lis(a):
        if len(a) == 0: return -int(1e18)
        import bisect
        cur = [a[0]]
        for y in a[1:]:
            if cur[-1] < y:
                cur.append(y)
            else:
                cur[bisect.bisect_left(cur, y)] = y
        return len(cur)
 
    ans = J90 * lis([y for x, y in P])
    if P[0][0] == x1 and y1 < P[0][1] < y2:
        ans = min(ans, J180 + J90 * lis([y for x, y in P if x > x1 and P[0][1] < y]))
    if x1 < P[0][0] < x2 and P[0][1] == y1:
        ans =  min(ans, J180 + J90 * lis([y for x, y in P if y > y1 and P[0][0] < x]))
    if P[-1][0] == x2 and y1 < P[-1][1] < y2:
        ans =  min(ans, J180 + J90 * lis([y for x, y in P if x < x2 and P[-1][1] > y]))
    if x1 < P[-1][0] < x2 and P[-1][1] == y2:
        ans =  min(ans, J180 + J90 * lis([y for x, y in P if y < y2 and P[-1][0] > x]))
 
print("%.20f" % (C * (x2 - x1 + y2 - y1) + ans))

Submission status: Wrong Answer

Solution code:
import sys
import math 
x1,y1,x2,y2=map(int,input().split())
xs=1
if x1>x2: xs=-1
ys=1
if y1>y2: ys=-1
N=int(input())
X=[]
Y=[]
for i in range(N):
  x,y=map(int,input().split())
  if xs*x1<=xs*x and xs*x<=xs*x2 and ys*y1<=ys*y and ys*y<=ys*y2 :
    X+=[[i,x]]
    Y+=[[i,y]]
print(X,Y)
N=len(X)
pi=math.pi
short=20-5*pi
long=10*pi-20
fountain=0
straight=100*(abs(x1-x2)+abs(y1-y2))

if x1==x2:
  for i in range(N):
    if X[i][1]==x1: fountain+=1
  print(straight+long*fountain)
  sys.exit()
if y1==y2:
  for i in range(N):
    if Y[i][1]==y1: fountain+=1
  print(straight+long*fountain)
  sys.exit()
print(straight-short*fountain)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from math import pi

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

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 = (-20 + 10*pi) * len(P)
elif len(P) == 0:
    ans = 0
elif len(P) > 0:
    P.sort()

    def lis(a):
        if len(a) == 0: return 0
        import bisect
        cur = [a[0]]
        for y in a[1:]:
            if cur[-1] < y:
                cur.append(y)
            else:
                cur[bisect.bisect_left(cur, y)] = y
        return len(cur)

    ans = J90 * lis([y for x, y in P])
    if P[0][0] == x1 and y1 < P[0][1] < y2:
        ans = min(ans, J180 + J90 * lis([y for x, y in P if x > x1 and P[0][1] < y]))
    if x1 < P[0][0] < x2 and P[0][1] == y1:
        ans =  min(ans, J180 + J90 * lis([y for x, y in P if y > y1 and P[0][0] < x]))
    if P[-1][0] == x2 and y1 < P[-1][1] < y2:
        ans =  min(ans, J180 + J90 * lis([y for x, y in P if x < x2 and P[-1][1] > y]))
    if x1 < P[-1][0] < x2 and P[-1][1] == y2:
        ans =  min(ans, J180 + J90 * lis([y for x, y in P if y < y2 and P[-1][0] > x]))

print("%.20f" % (C * (x2 - x1 + y2 - y1) + ans))

Submission status: Wrong Answer

Solution code:
# C
import numpy as np

x1, y1, x2, y2 = map(int, input().split())
N = int(input())

xL = min(x1, x2)
xR = max(x1, x2)
yL = min(y1, y2)
yR = max(y1, y2)

xv = np.zeros(N, dtype='int')
yv = np.zeros(N, dtype='int')

ind = 0
for _ in range(N):
    x, y = map(int, input().split())
    if xL <= x and x <= xR and yL <= y and y <= yR:
        xv[ind] = x
        yv[ind] = y
        ind += 1

xv = xv[:ind]
yv = yv[:ind]

N = ind



if x1 < x2 and y1 > y2:
    direction = 'upper'
elif x1 > x2 and y1 < y2:
    direction = 'upper'
else:
    direction = 'down'

    
# sort
xv_s = np.argsort(xv)
yv_ = yv[xv_s]

if direction == 'upper':
    yv_s = np.argsort(-yv_)
else:
    yv_s = np.argsort(yv_)

# bit
K2 = 0
N2 = 1

while N2 < N:
    N2 *= 2
    K2 += 1

# segtree
SGT = []
for k in range(K2+1):
    SGT.append([0] * (N2 // (2**k)))

# update
def update(i, a):
    global SGT
    j = i
    SGT[0][j] = a
    for k in range(1, K2+1):
        j = j // 2
        SGT[k][j] = max(SGT[k-1][2*j], SGT[k-1][2*j+1])
        
# query [a, b)
def query_max_(a, b, k=K2, l=0, r=N2):
    if r <= a or b <= l:
        return 0
    elif a <= l and r <= b:
        return SGT[k][l // (2**k)]
    else:
        vl = query_max(a, b, k-1, l, (l+r)//2)
        vr = query_max(a, b, k-1, (l+r)//2, r)
        return max(vl, vr)
# query [0, b)
def query_max_0(b):
    res = 0
    j = b-1
    k = 0
    res = max(res, SGT[k][j])
    while (j-1) // 2 >= 0:
        k += 1
        j = (j-1) // 2
        res = max(res, SGT[k][j])
    return res
    
# calculate
for i in range(N):
    l = query_max_0(yv_s[i])
    update(i, l+1)
    
count_ = query_max_0(N)

diff = 5*np.pi - 20
diff2 = 10*np.pi - 20
if count_ == min((xR-xL), (yR-yL)) + 1:
    print(100*((xR-xL) + (yR-yL)) + (count_-1)*diff + diff2)
else:
    print(100*((xR-xL) + (yR-yL)) + count_*diff)

Submission status: Wrong Answer

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['y2'] 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):
    if not pool:
        return 0
    return max( map( lambda x: x[2], pool))
def travel( goal, fcount):
    def special_case(goal, fcount):
        return fcount == 1 and( goal['x1'] == goal['x2'] or goal['y1'] ==
        goal['y2'])
    r = 10
    res = 100 *( goal['x2'] - goal['x1'] + goal['y2'] - goal['y1'] )
    if special_case(goal, fcount):
        res += fcount * (math.pi* 10 - 20)
    else:
        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 '%.12f' % travel( goal, count_max(pool))

Submission status: Wrong Answer

Solution code:
import math
sx,sy,gx,gy = list(map(int,input().split()))
n = int(input())
h = []
for i in range(n):
    a,b= list(map(int,input().split()))
    if sx > a > gx or sx < a <gx and sy>b>gy or sy < b <gy:
        h.append([a,b])
rx = 0
ry = 0
X = sx - 1
Y = sy - 1
if gx - sx <0:
    rx = 1
    X = sx + 1
if gy - sy < 0:
    ry = 1
    Y = sy + 1
_Y = Y

dp = [[0 for i in range(abs(sy-gy)+2)]for j in range(abs(sx-gx)+2)]
for i in range(0,abs(sx-gx)+2):
    if rx == 1:
        x = X - i
    else:
        x = X + i
    Y = _Y
    for j in range(0,abs(sy-gy)+2):
        if ry == 1:
            y = Y - j
        else:
            y = Y + j
        if(i == 0 or j== 9):
            continue
        for k in range(len(h)):
            if h[k][0] == x and h[k][1] == y:
                dp[i][j] += 1
                break
        dp[i][j] = max(dp[i-1][j]+dp[i][j], dp[i][j-1]+dp[i][j])

a = max(dp[len(dp)-1])



ans = (abs(sx-gx) + abs(sy-gy) )* 100
ans -= a*20
ans += a*(20 * math.pi/4)
if(sx==gx or sy == gy):
    ans += 20*math.pi/4
print(ans)