結果
問題 |
No.1248 Kth Sum
|
ユーザー |
![]() |
提出日時 | 2020-10-22 18:18:36 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,218 bytes |
コンパイル時間 | 93 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 49,128 KB |
最終ジャッジ日時 | 2024-07-21 09:25:55 |
合計ジャッジ時間 | 4,947 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 11 TLE * 1 -- * 24 |
ソースコード
import heapq N, K = [ int(a) for a in input().split(' ') ] alist = [ int(a) for a in input().split(' ') ] def is_valid(num_subl, inds): inds = sorted(inds) prev = -1 for i in range(num_subl): start = max(prev+1, K*(i+1)-1) end = min(start+(num_subl-i-1)*(K-1)+ (N-num_subl*K if i > 0 else 0), N-(num_subl-i-1)-1) if inds[i] not in range(start,end+1): return False prev = inds[i] return True agenda = [] enumed = list(enumerate(alist)) sortedl = sorted(enumed, key=lambda p:p[1]) for num_subl in range(1,N//K+1): if num_subl > 1: if min(agenda) < sum([ p[1] for p in sortedl[:num_subl] ]): break queue = [] heapq.heappush(queue,(sum([ p[1] for p in sortedl[:num_subl] ]), tuple(sortedl))) while queue: sumc, checking = heapq.heappop(queue) if is_valid(num_subl, [ p[0] for p in checking[:num_subl] ]): agenda.append(sumc) break if len(checking)>num_subl: for i in range(num_subl): new_checking = checking[:i]+checking[i+1:] heapq.heappush(queue, (sum([ p[1] for p in new_checking[:num_subl] ]), new_checking)) print(min(agenda))