Submission #48986747


Source Code Expand

Copy
import heapq
from collections import deque
from collections import defaultdict
import math
#
N,K = map(int, input().split())
Sx,Sy = map(int, input().split())
kid_pos = list()
for i in range(N):
X,Y = map(int, input().split())
kid_pos.append((X,Y))
#
direct = list() #
via = list() #
dist_X = abs(Sx - kid_pos[0][0])
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
import heapq
from collections import deque
from collections import defaultdict
import math

# 1.入力値読み込み
N,K = map(int, input().split())
Sx,Sy = map(int, input().split())

kid_pos = list()

for i in range(N):
    X,Y = map(int, input().split())
    kid_pos.append((X,Y))


# 2.家間直通とサンタ家経由時の差分の値をそれぞれ求める
direct = list() # 直通の場合
via = list()    # サンタの家を経由する場合の差異

dist_X = abs(Sx - kid_pos[0][0])
dist_Y = abs(Sy - kid_pos[0][1])
dist_direct = math.sqrt(dist_X**2 + dist_Y**2)
direct.append(dist_direct)
via.append(0)

for i in range(1,N):
    dist_X = abs(kid_pos[i][0] - kid_pos[i-1][0])
    dist_Y = abs(kid_pos[i][1] - kid_pos[i-1][1])
    dist_direct = math.sqrt(dist_X**2 + dist_Y**2)
    direct.append(dist_direct)
    
    dist_Xa = abs(kid_pos[i][0] - Sx)
    dist_Ya = abs(kid_pos[i][1] - Sy)
    dist_Xb = abs(Sx - kid_pos[i-1][0])
    dist_Yb = abs(Sy - kid_pos[i-1][1])
    dist_via = math.sqrt(dist_Xa**2 + dist_Ya**2) + math.sqrt(dist_Xb**2 + dist_Yb**2) - direct[-1]     # 差異なので直通の場合の値を引く 
    via.append(dist_via)


# 3.dpする
# dp[i] = i番目の家に行くときにサンタの家を経由した場合の、それまでの余分経路長さ最小値 とする。
# dp[i] = dist_via[i] + min(dp[i-K],...,dp[i-1])
# … i番目の家に行くときの余分なコスト + i-1番目の家までに配れるプレゼントを確保できる範囲での最小値
# 右項をスライド最小値を用いることで、dp[i]を求める
dp_deq = deque([])                  # dp[i-K]~dp[i-1]が入るdeque
heap_que = list()
heapq.heapify(heap_que)             # dp_deqにある可能性がある値が入るheap
valid_value_cnt = defaultdict(int)  # dp_deqの値とその個数を管理するdict

for i in range(N):
    # dp_deqの最小値を取り出す
    v = 0

    while len(heap_que) != 0:
        v = heap_que[0]
        if valid_value_cnt[v] == 0:
            heapq.heappop(heap_que)
        else:
            break

    dp_i = via[i] + v

    if len(dp_deq) > K-1:
        v = dp_deq.popleft()
        valid_value_cnt[v] -= 1
    
    heapq.heappush(heap_que,dp_i) 
    dp_deq.append(dp_i)
    valid_value_cnt[dp_i] += 1


# 4.最終的な答えを求める
# dp_deqから最小値を取り出す
v = 0
while len(heap_que) != 0:
    v = heap_que[0]
    if valid_value_cnt[v] == 0:
        heapq.heappop(heap_que)
    else:
        break

# 最後の家からサンタの家まで戻る分の値を求める
dist_X = abs(kid_pos[-1][0] - Sx)
dist_Y = abs(kid_pos[-1][1] - Sy)
dist_return = math.sqrt(dist_X**2 + dist_Y**2)

ans = sum(direct) + v + dist_return
print(ans)

Submission Info

Submission Time
Task F - Christmas Present 2
User tonomotohide
Language Python (CPython 3.11.4)
Score 550
Code Size 2833 Byte
Status AC
Exec Time 824 ms
Memory 76388 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 550 / 550
Status
AC × 3
AC × 36
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 02_line_00.txt, 02_line_01.txt, 02_line_02.txt, 02_line_03.txt, 02_line_04.txt, 02_line_05.txt, 02_line_06.txt, 02_line_07.txt, 02_line_08.txt, 02_line_09.txt, 03_minmax_00.txt, 03_minmax_01.txt, 03_minmax_02.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 12 ms 9608 KB
00_sample_01.txt AC 13 ms 9576 KB
00_sample_02.txt AC 13 ms 9564 KB
01_random_00.txt AC 796 ms 72728 KB
01_random_01.txt AC 738 ms 76388 KB
01_random_02.txt AC 787 ms 72856 KB
01_random_03.txt AC 777 ms 72700 KB
01_random_04.txt AC 810 ms 73812 KB
01_random_05.txt AC 773 ms 74012 KB
01_random_06.txt AC 806 ms 72780 KB
01_random_07.txt AC 757 ms 72728 KB
01_random_08.txt AC 824 ms 72784 KB
01_random_09.txt AC 778 ms 74396 KB
01_random_10.txt AC 756 ms 74232 KB
01_random_11.txt AC 757 ms 72848 KB
01_random_12.txt AC 766 ms 74904 KB
01_random_13.txt AC 765 ms 74088 KB
01_random_14.txt AC 722 ms 74976 KB
01_random_15.txt AC 785 ms 72828 KB
01_random_16.txt AC 767 ms 72824 KB
01_random_17.txt AC 729 ms 76284 KB
01_random_18.txt AC 767 ms 74208 KB
01_random_19.txt AC 767 ms 74116 KB
02_line_00.txt AC 730 ms 66952 KB
02_line_01.txt AC 759 ms 65540 KB
02_line_02.txt AC 710 ms 62864 KB
02_line_03.txt AC 733 ms 65560 KB
02_line_04.txt AC 722 ms 64768 KB
02_line_05.txt AC 711 ms 73884 KB
02_line_06.txt AC 722 ms 64904 KB
02_line_07.txt AC 732 ms 73920 KB
02_line_08.txt AC 718 ms 65064 KB
02_line_09.txt AC 735 ms 74540 KB
03_minmax_00.txt AC 12 ms 9580 KB
03_minmax_01.txt AC 770 ms 72700 KB
03_minmax_02.txt AC 785 ms 62176 KB


2025-04-15 (Tue)
10:09:26 +00:00