結果

問題 No.5 数字のブロック
ユーザー acky
提出日時 2024-02-03 23:17:15
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 42 ms / 5,000 ms
コード長 928 bytes
コンパイル時間 339 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 12,160 KB
最終ジャッジ日時 2024-09-28 11:04:54
合計ジャッジ時間 2,336 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

# 大きな箱の幅とブロックの数を入力
box_width = int(input())
num_of_blocks = int(input())

# 各ブロックの幅をリストに入れる
width_of_blocks = list(map(int, input().split()))

# クイックソートの導入
def quick_sort(lst):
  # クイックソート関数の定義
  if len(lst) <= 1:
      return lst

  pivot = lst[0]
  left = []
  center = []
  right = []

  for v in lst:
      if v < pivot:
          left.append(v)
      elif v > pivot:
          right.append(v)
      else:
          center.append(v)
  
  left = quick_sort(left)
  right = quick_sort(right)

  return left + center + right

# ブロックを小さい順に並び替える
sorted_blocks = quick_sort(width_of_blocks)

sum_of_width = 0
count = 0

for i in sorted_blocks:
   sum_of_width += i
   count += 1
   if sum_of_width > box_width:
      print(count - 1)
      break
   if count == num_of_blocks:
      print(count)
0