結果

問題 No.2743 Twisted Lattice
ユーザー tcltktcltk
提出日時 2024-04-28 15:26:29
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,323 bytes
コンパイル時間 407 ms
コンパイル使用メモリ 82,472 KB
実行使用メモリ 185,600 KB
最終ジャッジ日時 2024-04-28 15:26:37
合計ジャッジ時間 7,361 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 658 ms
155,616 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
# from typing import *

import sys
import io
import math
import collections
import decimal
import itertools
import bisect
import heapq
import random


def input():
    return sys.stdin.readline()[:-1]


# sys.setrecursionlimit(1000000)

# _INPUT = """10 10 6
# 1 2
# 2 3
# 3 3
# 10 7
# 8 2
# 3 5

# """
# sys.stdin = io.StringIO(_INPUT)

INF = 10**18

YES = 'Yes'
NO = 'No'

    
def solve(H, W, N, X, Y):
    # 座標圧縮
    DX = {a: i for i, a in enumerate(sorted(set(X)))}

    Ans = [INF] * N

    Line = collections.defaultdict(list)
    for i in range(N):
        Line[Y[i]].append((DX[X[i]], i))
    for l in Line.values():
        l.sort()
    
    for i in range(N):
        x, y = X[i], Y[i]
        k = bisect.bisect_left(Line[y], (DX[x], i))
        if 0 <= k-1:
            dx1, i1 = Line[y][k-1]
            Ans[i] = min(Ans[i], x-X[i1])
        if k+1 < len(Line[y]):
            dx2, i2 = Line[y][k+1]
            Ans[i] = min(Ans[i], X[i2]-x)

        for y1 in [y-1, y+1]:
            if y1 in Line:
                k = bisect.bisect_left(Line[y1], (DX[x], 0))
                if 0 <= k-1:
                    dx1, i1 = Line[y1][k-1]
                    Ans[i] = min(Ans[i], x) #x1 + (x-x1)
                if k < len(Line[y1]):
                    dx2, i2 = Line[y1][k]
                    Ans[i] = min(Ans[i], X[i2]) # x + (x2-x)

    i0 = -1
    for y in sorted(Y):
        if i0 == -1:
            i0 = Line[y][0][1]
            continue
        for dx, i in Line[y]:
            x0, y0 = X[i0], Y[i0]
            if y0 == y:
                continue
            cost = (X[i]-1) + (x0-1) + (y-y0)
            Ans[i] = min(Ans[i], cost)
            if x-y < x0-y0:
                i0 = i

    i0 = -1
    for y in sorted(Y, reverse=True):
        if i0 == -1:
            i0 = Line[y][0][1]
            continue
        for dx, i in Line[y]:
            x0, y0 = X[i0], Y[i0]
            if y0 == y:
                continue
            cost = (X[i]-1) + (x0-1) + (y0-y)
            Ans[i] = min(Ans[i], cost)
            if x+y < x0+y0:
                i0 = i

    return Ans







H, W, N = map(int, input().split())
X, Y = [], []
for _ in range(N):
    x, y = map(int, input().split())
    X.append(x)
    Y.append(y)
ans = solve(H, W, N, X, Y)
for v in ans:
    print(v)
0