30D - King's Problem - CodeForces Solution


geometry greedy *2600

Please click on ads to support us..

Python Code:

from bisect import *

n,k=map(int,input().split())
x=list(map(int,input().split()))
sp_y=int(input())

sp_x = x[-1]
start_x = x[k-1]
min_path = float('inf')

x.pop()
x.sort()

x=[x[0]]+x+[x[-1]]

def dist2sp(b):
    if b in [len(x)-1,0,-1]:
        return 0
    return ((sp_x-x[b])**2+sp_y**2)**.5

if k == n+1:
    print(x[-1]-x[0]+min(dist2sp(-2),dist2sp(1)))
    exit()

k=bisect(x,start_x)-1
for i in range(1,len(x)-1):
    if i<=k:
        min_path=min(min_path,min(x[-2]-x[k]+dist2sp(i),x[k]-x[i]+dist2sp(-2))+x[-2]-x[i]+dist2sp(i-1)+x[i-1]-x[1])
    if k<=i:
        min_path=min(min_path,min(x[k]-x[1]+dist2sp(i),x[i]-x[k]+dist2sp(1))+x[i]-x[1]+dist2sp(i+1)+x[-2]-x[i+1])

print(min_path)


Comments

Submit
0 Comments
More Questions

1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset
350A - TL
1487A - Arena
1520D - Same Differences
376A - Lever
1305A - Kuroni and the Gifts
1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant
978B - File Name
1426B - Symmetric Matrix
732B - Cormen --- The Best Friend Of a Man
1369A - FashionabLee
1474B - Different Divisors
1632B - Roof Construction
388A - Fox and Box Accumulation
451A - Game With Sticks
768A - Oath of the Night's Watch
156C - Cipher
545D - Queue
459B - Pashmak and Flowers
1538A - Stone Game
1454C - Sequence Transformation