結果

問題 No.615 集合に分けよう
ユーザー buey_t
提出日時 2023-03-31 09:55:37
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,181 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 82,384 KB
実行使用メモリ 148,508 KB
最終ジャッジ日時 2024-09-22 15:22:37
合計ジャッジ時間 11,628 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 13 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    from math import sqrt,sin,cos,tan,ceil,radians,floor,gcd,exp,log,log10,log2,factorial,fsum
    from heapq import heapify, heappop, heappush
    from bisect import bisect_left, bisect_right
    from copy import deepcopy
    import copy
    import random
    from collections import deque,Counter,defaultdict
    from itertools import permutations,combinations
    from decimal import Decimal,ROUND_HALF_UP
    #tmp = Decimal(mid).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
    from functools import lru_cache, reduce
    #@lru_cache(maxsize=None)
    from operator import add,sub,mul,xor,and_,or_,itemgetter
    INF = 10**18
    mod1 = 10**9+7
    mod2 = 998244353
    
    #DecimalならPython
    #再帰ならPython!!!!!!!!!!!!!!!!!!!!!!!!!!
    
    
    '''
    
    
    
    '''
    
    N,K = map(int, input().split())
    A = list(map(int, input().split()))
    
    A.sort()
    h = INF
    l = -1
    
    while h-l > 1:
        
        m = (h+l)//2
        
        mx = -INF
        mi = INF
        cnt = 0
        for i in range(N):
            mx = max(A[i],mx)
            mi = min(A[i],mi)
            
            if mx-mi > m:
                
                cnt += 1
                mx = A[i]
                mi = A[i]
        cnt += 1
        
        if cnt > K:
            l = m
        else:
            h = m
    
    ans = INF
    for k in range(l,l+200):
        if k < 0:
            continue
        
        mx = -INF
        mi = INF
        cnt = 0
        tmp = 0
        ls = []
        for i in range(N):
            mx = max(A[i],mx)
            mi = min(A[i],mi)
            
            if mx-mi > k:
                cnt += 1
                tmp += max(ls)-min(ls)
                mx = A[i]
                mi = A[i]
                ls = []
            ls.append(A[i])
        
        tmp += max(ls)-min(ls)
        cnt += 1
        
        if cnt == K:
            ans = min(ans,tmp)
    
    print(ans)
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
if __name__ == '__main__':
    main()
0