結果

問題 No.1012 荷物収集
ユーザー 👑 obakyan
提出日時 2020-03-20 22:20:04
言語 Lua
(LuaJit 2.1.1734355927)
結果
AC  
実行時間 527 ms / 2,000 ms
コード長 1,315 bytes
コンパイル時間 205 ms
コンパイル使用メモリ 6,692 KB
実行使用メモリ 43,264 KB
最終ジャッジ日時 2024-12-15 06:35:10
合計ジャッジ時間 13,145 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl = math.floor

local function comp(a, b)
  return a[2] < b
end

local function lower_bound(ary, x)
  local num = #ary
  if num == 0 then return 1 end
  if not comp(ary[1], x) then return 1 end
  if comp(ary[num], x) then return num + 1 end
  local min, max = 1, num
  while 1 < max - min do
    local mid = mfl((min + max) / 2)
    if comp(ary[mid], x) then
      min = mid
    else
      max = mid
    end
  end
  return max
end

local n, q = io.read("*n", "*n")
local xs, ws = {}, {}
for i = 1, n do
  xs[i], ws[i] = io.read("*n", "*n")
end
local qs = {}
for i = 1, q do
  qs[i] = {}
  qs[i][1] = i
  qs[i][2] = io.read("*n")
  qs[i][3] = 0LL -- sum of xi * wi
  qs[i][4] = 0LL -- sum of wi (for multiplied by Xi)
end
qs[q + 1] = {q + 1, 1000000007, 0LL, 0LL}
table.sort(qs, function(a, b) return a[2] < b[2] end)
for i = 1, n do
  local x, w = 0LL + xs[i], 0LL + ws[i]
  local pos = lower_bound(qs, x)
  qs[1][3] = qs[1][3] + x * w
  qs[pos][3] = qs[pos][3] - 2LL * x * w
  qs[1][4] = qs[1][4] - w
  qs[pos][4] = qs[pos][4] + 2LL * w
end
for i = 2, q do
  qs[i][3] = qs[i][3] + qs[i - 1][3]
  qs[i][4] = qs[i][4] + qs[i - 1][4]
end
table.sort(qs, function(a, b) return a[1] < b[1] end)
for i = 1, q do
  local v = qs[i][3] + qs[i][2] * qs[i][4]
  local str = tostring(v):gsub("LL", "")
  print(str)
end
0