結果

問題 No.180 美しいWhitespace (2)
ユーザー rpy3cpprpy3cpp
提出日時 2015-05-19 19:28:53
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 22 ms / 5,000 ms
コード長 2,192 bytes
コンパイル時間 87 ms
コンパイル使用メモリ 11,144 KB
実行使用メモリ 8,596 KB
最終ジャッジ日時 2023-09-20 10:13:59
合計ジャッジ時間 2,556 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,308 KB
testcase_01 AC 17 ms
8,260 KB
testcase_02 AC 18 ms
8,276 KB
testcase_03 AC 19 ms
8,340 KB
testcase_04 AC 19 ms
8,424 KB
testcase_05 AC 18 ms
8,440 KB
testcase_06 AC 18 ms
8,440 KB
testcase_07 AC 18 ms
8,360 KB
testcase_08 AC 19 ms
8,396 KB
testcase_09 AC 21 ms
8,536 KB
testcase_10 AC 21 ms
8,596 KB
testcase_11 AC 21 ms
8,424 KB
testcase_12 AC 22 ms
8,452 KB
testcase_13 AC 22 ms
8,428 KB
testcase_14 AC 22 ms
8,536 KB
testcase_15 AC 22 ms
8,480 KB
testcase_16 AC 21 ms
8,548 KB
testcase_17 AC 22 ms
8,468 KB
testcase_18 AC 22 ms
8,432 KB
testcase_19 AC 21 ms
8,424 KB
testcase_20 AC 16 ms
8,400 KB
testcase_21 AC 17 ms
8,424 KB
testcase_22 AC 17 ms
8,296 KB
testcase_23 AC 17 ms
8,260 KB
testcase_24 AC 17 ms
8,260 KB
testcase_25 AC 17 ms
8,292 KB
testcase_26 AC 17 ms
8,320 KB
testcase_27 AC 17 ms
8,288 KB
testcase_28 AC 17 ms
8,384 KB
testcase_29 AC 16 ms
8,412 KB
testcase_30 AC 21 ms
8,464 KB
testcase_31 AC 21 ms
8,476 KB
testcase_32 AC 20 ms
8,408 KB
testcase_33 AC 19 ms
8,392 KB
testcase_34 AC 20 ms
8,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def convex_hull(points):
    '''二次元平面上の点のリスト points の凸包を返す。Andrew のアルゴリズムを使用。
    引数:
        points = [(x0, y0), (x1, y1),...] 点のリスト
        同じ点は含まない(全て異なる点である)とする。
    返り値:
        convex_hull = [(xi, yi), (xj, yj),...] 点のリスト。時計回りに並んでいる。
    '''
    points.sort()
    upper_bounds = get_bounds(points)
    points.reverse()
    lower_bounds = get_bounds(points)
    del upper_bounds[-1]
    del lower_bounds[-1]
    upper_bounds.extend(lower_bounds)
    return upper_bounds


def get_bounds(points):
    bounds = [points[0], points[1]]
    for xi, yi in points[2:]:
        while len(bounds) > 1 and not is_convex(bounds, xi, yi):
            del bounds[-1]
        bounds.append((xi, yi))
    return bounds


def is_convex(bounds, x2, y2):
    x1, y1 = bounds[-1]
    x0, y0 = bounds[-2]
    return (x1 - x0) * (y2 - y1) < (y1 - y0) * (x2 - x1)


def read_data():
    N = int(input())
    AB = [tuple(map(int, input().split())) for i in range(N)]
    return N, AB


def solve(N, AB):
    # 重複する点を除く
    AB = list(set(AB))
    if len(AB) == 1:
        return 1

    # 凸包を作り、1周走査して、辺の傾き(の逆数に-1を乗じた値を整数化したもの)の一覧を取得する。
    ps = convex_hull(AB)
    slopes = get_slopes(ps)

    # tab の候補を1つづつ試していく
    min_cost = float('inf')
    best_tab = 1
    tabs = list(slopes)
    tabs.sort()
    for tab in tabs:
        xy = [x + tab * y for x, y in ps]
        cost = max(xy) - min(xy)
        if cost < min_cost:
            min_cost = cost
            best_tab = tab
    return best_tab


def get_slopes(ps):
    slopes = set([1])
    prev_x, prev_y = ps[-1]
    for x, y in ps:
        dx = x - prev_x
        dy = y - prev_y
        prev_x, prev_y = x, y
        if dy == 0:
            continue
        slope = int(- dx / dy)
        if slope > 0:
            slopes.add(slope)
            slopes.add(slope + 1)
    return slopes


if __name__ == '__main__':
    N, AB = read_data()
    print(solve(N, AB))
0