結果

問題 No.1597 Matrix Sort
ユーザー ygd.ygd.
提出日時 2021-08-01 10:53:20
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,003 bytes
コンパイル時間 1,419 ms
コンパイル使用メモリ 87,004 KB
実行使用メモリ 141,468 KB
最終ジャッジ日時 2023-10-14 18:42:21
合計ジャッジ時間 8,099 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
71,484 KB
testcase_01 AC 106 ms
76,316 KB
testcase_02 AC 107 ms
76,168 KB
testcase_03 AC 360 ms
136,916 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
import bisect
import sys 
input = sys.stdin.buffer.readline

def main():
    N,K,P = map(int,input().split()); INF = pow(10,20)
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    dica = defaultdict(int)
    dicb = defaultdict(int)
    for a,b in zip(A,B):
        dica[a%P] += 1
        dicb[b%P] += 1
    #print(dica,dicb)
    #この累積和のメモリが重い
    #sb = [0]*(P+1)
    #for i in range(P):
    #    sb[i+1] = sb[i] + dicb[i]

    #累積和の時値のリストとその位置に対応する累積和をもって二分探索
    LB = list(dicb.keys()); LB.sort()
    sb = [0]
    for i,b in enumerate(LB):
        sb.append(sb[-1] + dicb[b])
    sb += [INF]
    #print(sb)
    #sb[i] - sb[j]: LBのインデックスi以下LBのインデックスj以上 
    
    #和がx以下となるのがk個以上あるか

    def solve(x):
        ret = 0
        for i in dica:
            if i <= x:
                #temp = sb[x-i+1] - sb[0]
                idx1 = bisect.bisect_right(LB,x-i)
                temp = sb[idx1]
                #print(i,"1",temp)
                idx2l = bisect.bisect_left(LB,P-i) #P-iを含まない
                idx2r = bisect.bisect_right(LB,P-1)
                temp += sb[idx2r] - sb[idx2l]
                #print(i,"2",temp)
            else:
                idx2l = bisect.bisect_left(LB,P-i)
                idx2r = bisect.bisect_right(LB,P+x-i)
                temp = sb[idx2r] - sb[idx2l]
                #print(i,"3",temp)
            ret += temp*dica[i]
        #print(x,ret)
        return ret

    #test = 0
    #print(solve(test))
    #exit()

    if solve(0) >= K:
        print(0);exit()

    ok = P
    ng = 0 #これは事前にチェック

    while abs(ok-ng) > 1:
        mid = (ok+ng)//2
        #print(mid)
        if solve(mid) >= K:
            ok = mid
        else:
            ng = mid
    #print(solve(2))
    print(ok)

if __name__ == '__main__':
    main()
0