結果

問題 No.165 四角で囲え!
ユーザー rpy3cpprpy3cpp
提出日時 2015-05-30 01:35:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 983 ms / 5,000 ms
コード長 2,105 bytes
コンパイル時間 608 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 78,720 KB
最終ジャッジ日時 2024-12-26 03:33:07
合計ジャッジ時間 10,494 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 466 ms
77,952 KB
testcase_01 AC 52 ms
52,608 KB
testcase_02 AC 53 ms
52,096 KB
testcase_03 AC 50 ms
52,608 KB
testcase_04 AC 52 ms
52,608 KB
testcase_05 AC 936 ms
77,824 KB
testcase_06 AC 196 ms
76,032 KB
testcase_07 AC 49 ms
52,480 KB
testcase_08 AC 50 ms
52,608 KB
testcase_09 AC 54 ms
52,608 KB
testcase_10 AC 51 ms
52,608 KB
testcase_11 AC 287 ms
77,440 KB
testcase_12 AC 192 ms
76,928 KB
testcase_13 AC 273 ms
76,416 KB
testcase_14 AC 183 ms
76,672 KB
testcase_15 AC 188 ms
76,672 KB
testcase_16 AC 765 ms
78,592 KB
testcase_17 AC 752 ms
78,592 KB
testcase_18 AC 983 ms
78,208 KB
testcase_19 AC 776 ms
78,720 KB
testcase_20 AC 739 ms
78,464 KB
testcase_21 AC 744 ms
78,720 KB
testcase_22 AC 751 ms
78,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def read_data():
    N, B = map(int, input().split())
    Xs = []
    Ys = []
    Ps = []
    for i in range(N):
        x, y, p = map(int, input().split())
        Xs.append(x)
        Ys.append(y)
        Ps.append(p)
    return N, B, Xs, Ys, Ps

def solve(N, B, Xs, Ys, Ps):
    if len(Xs) == 0:
        return 0
    Xs = compress(Xs)
    Ys = compress(Ys)
    if max(Xs) > max(Ys):
        Xs, Ys = Ys, Xs
    cum, cumP = preprocess(Xs, Ys, Ps)
    return find_max(cum, cumP, B)

def compress(As):
    Avals = list(set(As))
    Avals.sort()
    val2idx = {a:i for i, a in enumerate(Avals)}
    return [val2idx[a] for a in As]

def preprocess(Xs, Ys, Ps):
    max_x = max(Xs)
    max_y = max(Ys)
    mat = [[0] * (max_y + 2) for _ in range(max_x + 2)]
    for x, y, p in zip(Xs, Ys, Ps):
        mat[x + 1][y + 1] = p
    cum = [[0] * (max_y + 2) for _ in range(max_x + 2)]
    cumP = [[0] * (max_y + 2) for _ in range(max_x + 2)]
    for x in range(1, max_x + 2):
        cumx = cum[x]
        cumx_prev = cum[x - 1]
        cumPx = cumP[x]
        cumPx_prev = cumP[x - 1]
        matx = mat[x]
        tmp = 0
        tmpP = 0
        for y in range(1, max_y + 2):
            if matx[y]:
                tmp += 1
                tmpP += matx[y]
            cumx[y] = cumx_prev[y] + tmp
            cumPx[y] = cumPx_prev[y] + tmpP
    return cum, cumP

def find_max(cum, cumP, B):
    nrow = len(cum)
    ncol = len(cum[0])
    max_n = 0
    for t in range(nrow - 1):
        cum_t = cum[t]
        cumP_t = cumP[t]
        for b in range(t + 1, nrow):
            n = find_max_n(cum_t, cumP_t, cum[b], cumP[b], B, ncol)
            if n > max_n:
                max_n = n
    return max_n

def find_max_n(t, Pt, b, Pb, B, ncol):
    max_n = 0
    left = 0
    for right in range(1, ncol):
        p = Pb[right] - Pt[right]
        while p - Pb[left] + Pt[left] > B:
            left += 1
        n = b[right] - t[right] - b[left] + t[left]
        if n > max_n:
            max_n = n
    return max_n

if __name__ == '__main__':
    N, B, Xs, Ys, Ps = read_data()
    print(solve(N, B, Xs, Ys, Ps))
0