結果

問題 No.1008 Bench Craftsman
ユーザー simansiman
提出日時 2023-06-20 18:04:45
言語 Ruby
(3.3.0)
結果
TLE  
実行時間 -
コード長 1,464 bytes
コンパイル時間 446 ms
コンパイル使用メモリ 11,168 KB
実行使用メモリ 130,020 KB
最終ジャッジ日時 2023-09-10 13:54:07
合計ジャッジ時間 42,365 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
15,116 KB
testcase_01 AC 82 ms
15,196 KB
testcase_02 AC 81 ms
15,040 KB
testcase_03 AC 82 ms
15,120 KB
testcase_04 AC 82 ms
15,220 KB
testcase_05 TLE -
testcase_06 AC 1,084 ms
103,740 KB
testcase_07 AC 81 ms
15,000 KB
testcase_08 AC 99 ms
18,236 KB
testcase_09 AC 1,347 ms
44,664 KB
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 AC 457 ms
62,840 KB
testcase_16 TLE -
testcase_17 AC 458 ms
62,844 KB
testcase_18 TLE -
testcase_19 TLE -
testcase_20 AC 995 ms
73,832 KB
testcase_21 AC 998 ms
65,596 KB
testcase_22 AC 1,391 ms
60,748 KB
testcase_23 AC 1,787 ms
91,320 KB
testcase_24 AC 1,825 ms
82,356 KB
testcase_25 AC 1,019 ms
88,152 KB
testcase_26 AC 809 ms
71,164 KB
testcase_27 AC 1,120 ms
49,768 KB
testcase_28 AC 1,433 ms
89,976 KB
testcase_29 TLE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:83: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #

GC.disable

N, M = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)
W = Array.new(N) { [] }
Q = []
max_w = 0

M.times do
  x, w = gets.split.map(&:to_i)
  x -= 1

  max_w = w if max_w < w
  Q << [x, w]
  W[x] << w
end

def f(c)
  cnt = 0
  weight = 0
  sum = Array.new(N, 0)
  counter = Array.new(N + 1, 0)
  remain = Array.new(N + 1, 0)

  0.upto(N - 1) do |i|
    cnt -= counter[i]
    weight -= remain[i]

    weight -= c * cnt
    sum[i] += weight

    W[i].each do |w|
      dist = (c == 0) ? N : w / c
      next if dist == 0

      weight += w
      n_idx = (i + 1) + dist
      n_idx = N if n_idx > N
      counter[n_idx] += 1
      remain[n_idx] += w % c if c > 0
      cnt += 1
    end
  end

  cnt = 0
  weight = 0
  counter = Array.new(N + 1, 0)
  remain = Array.new(N + 1, 0)

  (N - 1).downto(0) do |i|
    cnt -= counter[i]
    weight -= remain[i]

    weight -= c * cnt
    sum[i] += weight

    W[i].each do |w|
      dist = (c == 0) ? -1 : w / c
      next if dist == 0

      n_idx = (i - 1) - dist
      weight += w
      cnt += 1

      if n_idx >= 0
        counter[n_idx] += 1
        remain[n_idx] += w % c if c > 0
      end
    end
  end

  Q.each do |x, w|
    sum[x] += w
  end

  (0..N - 1).all? { |i| sum[i] < A[i] }
end

if f(0)
  puts 0
  exit
elsif !f(max_w)
  puts -1
  exit
else
  ok = max_w
  ng = 0

  while (ok - ng).abs >= 2
    c = (ok + ng) / 2

    if f(c)
      ok = c
    else
      ng = c
    end
  end

  puts ok
end
0