結果

問題 No.2157 崖
ユーザー 👑 KazunKazun
提出日時 2022-12-12 04:16:22
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,660 bytes
コンパイル時間 194 ms
コンパイル使用メモリ 82,264 KB
実行使用メモリ 111,104 KB
最終ジャッジ日時 2024-04-23 22:07:43
合計ジャッジ時間 30,690 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
52,992 KB
testcase_01 AC 40 ms
52,864 KB
testcase_02 AC 39 ms
52,864 KB
testcase_03 AC 40 ms
53,248 KB
testcase_04 AC 40 ms
52,736 KB
testcase_05 AC 42 ms
52,864 KB
testcase_06 AC 39 ms
52,352 KB
testcase_07 AC 41 ms
53,120 KB
testcase_08 AC 47 ms
61,056 KB
testcase_09 AC 54 ms
63,488 KB
testcase_10 AC 43 ms
52,864 KB
testcase_11 AC 75 ms
75,520 KB
testcase_12 AC 43 ms
59,136 KB
testcase_13 AC 5,851 ms
99,448 KB
testcase_14 AC 673 ms
108,476 KB
testcase_15 AC 5,213 ms
99,104 KB
testcase_16 AC 5,478 ms
99,056 KB
testcase_17 AC 580 ms
103,624 KB
testcase_18 AC 566 ms
102,728 KB
testcase_19 TLE -
testcase_20 AC 709 ms
111,104 KB
testcase_21 AC 692 ms
111,024 KB
testcase_22 AC 535 ms
101,396 KB
testcase_23 AC 39 ms
52,992 KB
testcase_24 AC 97 ms
76,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def General_Binary_Increase_Search_Integer(L, R, cond, default=None):
    """ 条件式が単調増加であるとき, 整数上で二部探索を行う.

    L: 解の下限
    R: 解の上限
    cond: 条件(1変数関数, 広義単調増加を満たす)
    default: Lで条件を満たさないときの返り値
    """

    if not(cond(R)):
        return default

    if cond(L):
        return L

    R+=1
    while R-L>1:
        C=L+(R-L)//2
        if cond(C):
            R=C
        else:
            L=C
    return R
#==================================================
class Product_List_2D:
    def __init__(self, default, H, W):
        """ H x W の直積リストを生成する. """

        self.H=H
        self.W=W
        self.list=[default]*(H*W)

    def __len__(self):
        return self.H*self.W

    def get(self, i, j):
        if i<0:
            i+=self.H

        if j<0:
            j+=self.W

        return self.list[i*self.W+j]

    def get_all(self):
        return [self.list[i*self.W: (i+1)*self.W] for i in range(self.H)]

    def projection_H(self, i):
        if i<0:
            i+=self.H

        return self.list[i*self.W: (i+1)*self.W]

    def set(self, i, j, value):
        if i<0:
            i+=self.H

        if j<0:
            j+=self.W

        self.list[i*self.W+j]=value

    def set_once(self, i, A):
        if i<0:
            i+=self.H

        index=i*self.W
        for j in range(self.W):
            self.list[index]=A[j]
            index+=1

    def __getitem__(self, index):
        return self.get(index[0], index[1])

    def __setitem__(self, index, value):
        self.set(index[0], index[1], value)

    def __repr__(self):
        return "[Product List 2D] : Height: {}, Width: {}".format(self.H, self.W)

#==================================================
def solve():
    N,M=map(int,input().split())

    D=Product_List_2D(0,N,M)
    for i in range(N):
        Di=list(map(int,input().split()))
        Di.sort()
        D.set_once(i, Di)

    DP=Product_List_2D(0,N,M)
    DP.set_once(0,[1]*M)

    def check(X):
        for i in range(1,N):
            l=0; r=0; k=0
            for j in range(M):
                while r<M and D[i-1,r]<=D[i,j]:
                    k+=DP[i-1,r]
                    r+=1
                while l<r and D[i,j]-D[i-1,l]>X:
                    k-=DP[i-1,l]
                    l+=1
                DP[i,j]=1 if k else 0

        return any(DP.projection_H(-1))

    if not check(float("inf")):
        return -1

    return General_Binary_Increase_Search_Integer(0,10**9,check)

#==================================================
print(solve())
0