結果

問題 No.733 分身並列コーディング
ユーザー lam6er
提出日時 2025-04-16 15:25:20
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,842 bytes
コンパイル時間 543 ms
コンパイル使用メモリ 81,600 KB
実行使用メモリ 63,296 KB
最終ジャッジ日時 2025-04-16 15:26:11
合計ジャッジ時間 3,710 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 3
other WA * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    T = int(input[idx]); idx +=1
    N = int(input[idx]); idx +=1
    t = []
    for _ in range(N):
        ti = int(input[idx]); idx +=1
        t.append(ti)
    
    full_mask = (1 << N) - 1
    INF = float('inf')
    dp = [INF] * (1 << N)
    dp[0] = 0
    
    for mask in range(1 << N):
        if dp[mask] == INF:
            continue
        # 残っている最初のビットを探す
        first_bit = mask & -mask
        if first_bit == 0:
            continue
        i = (first_bit).bit_length() - 1
        # iを含む他のビットを取得
        other_bits = []
        for j in range(N):
            if j != i and (mask & (1 << j)):
                other_bits.append(j)
        # other_bitsをt[j]の大きい順にソート
        other_bits.sort(key=lambda x: -t[x])
        # 初期状態はiのみを含む
        sum_s = t[i]
        s = 1 << i
        if sum_s <= T:
            new_mask = mask ^ s
            if dp[new_mask] + 1 < dp[mask]:
                dp[mask] = dp[new_mask] + 1
        # 再帰的に他のビットを追加
        def backtrack(pos, current_sum, current_s):
            if pos >= len(other_bits):
                return
            # 現在のビット
            j = other_bits[pos]
            # 追加する場合
            new_sum = current_sum + t[j]
            new_s = current_s | (1 << j)
            if new_sum <= T:
                new_mask = mask ^ new_s
                if dp[new_mask] + 1 < dp[mask]:
                    dp[mask] = dp[new_mask] + 1
                backtrack(pos + 1, new_sum, new_s)
            # 追加しない場合
            backtrack(pos + 1, current_sum, current_s)
        backtrack(0, sum_s, s)
    
    print(dp[full_mask])

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