結果

問題 No.325 マンハッタン距離2
ユーザー rpy3cpprpy3cpp
提出日時 2015-12-19 11:38:18
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 2,083 bytes
コンパイル時間 439 ms
コンパイル使用メモリ 11,220 KB
実行使用メモリ 8,568 KB
最終ジャッジ日時 2023-10-14 14:17:00
合計ジャッジ時間 1,550 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
8,360 KB
testcase_01 AC 13 ms
8,420 KB
testcase_02 AC 12 ms
8,528 KB
testcase_03 AC 13 ms
8,352 KB
testcase_04 AC 14 ms
8,476 KB
testcase_05 AC 13 ms
8,484 KB
testcase_06 AC 14 ms
8,356 KB
testcase_07 AC 13 ms
8,464 KB
testcase_08 AC 13 ms
8,544 KB
testcase_09 AC 15 ms
8,352 KB
testcase_10 AC 14 ms
8,244 KB
testcase_11 AC 13 ms
8,348 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 14 ms
8,568 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 16 ms
8,012 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 15 ms
8,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


def solve(x1, y1, x2, y2, d):
    x1, y1, x2, y2 = relocate(x1, y1, x2, y2)
    if x1 < x2 or x1 < 0 or y1 < 0 or y1 < y2:
        raise RuntimeError('relocate failed. x1:{}, y1:{}, x2:{}, y2:{}'.format(x1, y1, x2, y2))
    if x2 > 0:
        if y2 > 0:
            return f(x1, y1) - f(x2 - 1, y1) - f(x1, y2 - 1) + f(x2 - 1, y2 - 1)
        elif y2 == 0:
            return f(x1, y1) - f(x2 - 1, y1)
        else:
            return f(x1, y1) - f(x2 - 1, y1) + f(x1, -y2) - f(x2 - 1, -y2) - min(x1, d) + min(x2 - 1, d)
    elif x2 == 0:
        if y2 > 0:
            return f(x1, y1) - f(x1, y2 - 1)
        elif y2 == 0:
            return f(x1, y1)
        else:
            return f(x1, y1) + f(x1, -y2) - min(x1, d) - 1
    else:
        if y2 > 0:
            xx1 = max(y1, y2)
            yy1 = max(x1, x2)
            xx2 = min(y1, y2)
            yy2 = min(x1, x2)
            return f(xx1, yy1) - f(xx2 - 1, yy1) + f(xx1, -yy2) - f(xx2 - 1, -yy2) - min(xx1, d) + min(xx2 - 1, d)
        elif y2 == 0:
            return f(x1, y1) + f(-x2, y1) - min(y1, d) - 1
        else:
            total = f(x1, y1) + f(-x2, -y2) + f(x1, -y2) + f(-x2, y1)
            vertical_double_counts = min(y1, d) - max(y2 - 1, - d - 1)
            horizontal_double_counts = min(x1, d) - max(x2 - 1, - d - 1)
            return total - vertical_double_counts - horizontal_double_counts + 1
            

def relocate(x1, y1, x2, y2):
    if x2 > 0:
        if y2 > 0:
            return x2, y2, x1, y1
        else:
            return x2, -y1, x1, -y2        
    else:
        if y2 > 0:
            return -x1, y2, -x2, y1
        else:
            return -x1, -y1, -x2, -y2

def f(x, y):
    if x < 0 or y < 0:
        raise RuntimeError('x and y must be non-negative. x:{}, y:{}'.format(x, y))
    if x + y <= d:
        return (x + 1) * (y + 1)
    ydif = max(0, d - y)
    xdif = max(0, d - x)
    return area(d + 1) - area(ydif) - area(xdif)
        
def area(size):
    return size * (size + 1) // 2    

print(solve(x1, y1, x2, y2, d))
0