結果

問題 No.733 分身並列コーディング
ユーザー gew1fw
提出日時 2025-06-12 16:13:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 65 ms / 1,500 ms
コード長 1,846 bytes
コンパイル時間 264 ms
コンパイル使用メモリ 82,724 KB
実行使用メモリ 76,092 KB
最終ジャッジ日時 2025-06-12 16:13:43
合計ジャッジ時間 3,982 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

def can_assign(tasks, T, k):
    tasks.sort(reverse=True)
    n = len(tasks)
    groups = []

    def backtrack(index):
        if index == n:
            return True
        current = tasks[index]
        # Try adding to existing groups
        for i in range(len(groups)):
            # Skip groups with the same capacity as previous to avoid duplicates
            if i > 0 and groups[i] == groups[i-1]:
                continue
            if groups[i] + current <= T:
                # Save the state before modification
                prev_groups = groups.copy()
                groups[i] += current
                # Sort to maintain descending order and avoid duplicates
                groups.sort(reverse=True)
                if backtrack(index + 1):
                    return True
                # Restore the previous state
                groups[:] = prev_groups
        # Try creating a new group if possible
        if len(groups) < k:
            prev_groups = groups.copy()
            groups.append(current)
            groups.sort(reverse=True)
            if backtrack(index + 1):
                return True
            # Restore the previous state
            groups[:] = prev_groups
        return False

    return backtrack(0)

def main():
    import sys
    input = sys.stdin.read().split()
    ptr = 0
    T = int(input[ptr])
    ptr += 1
    N = int(input[ptr])
    ptr += 1
    tasks = []
    for _ in range(N):
        tasks.append(int(input[ptr]))
        ptr += 1
    # Check if any task exceeds T
    for t in tasks:
        if t > T:
            print(-1)
            return
    # Iterate possible k from 1 to N
    for k in range(1, N+1):
        if can_assign(tasks.copy(), T, k):
            print(k)
            return
    print(N)  # All tasks need separate groups

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