結果
問題 | No.545 ママの大事な二人の子供 |
ユーザー |
👑 |
提出日時 | 2020-04-19 16:29:54 |
言語 | Lua (LuaJit 2.1.1734355927) |
結果 |
AC
|
実行時間 | 77 ms / 2,000 ms |
コード長 | 1,532 bytes |
コンパイル時間 | 221 ms |
コンパイル使用メモリ | 6,692 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-10-05 16:57:47 |
合計ジャッジ時間 | 2,049 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 32 |
ソースコード
local mmi, mma = math.min, math.max local mab = math.abs local bls, brs = bit.lshift, bit.rshift local mfl = math.floor local function comp(a, b) return a < 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 = io.read("*n") local t = {} local tgt = 0 for i = 1, n do local a, b = io.read("*n", "*n") tgt = tgt + a t[i] = a + b end if n <= 16 then local ret = tgt local tot = 2^n for it = 0, tot - 1 do local z = 0 for i = 1, n do if it % 2 == 1 then z = z + t[i] end it = brs(it, 1) end ret = mmi(ret, mab(tgt - z)) end print(ret) else local first = {} for it = 0, 65535 do local z = 0 for i = 1, 16 do if it % 2 == 1 then z = z + t[i] end it = brs(it, 1) end table.insert(first, z) end local ret = tgt table.sort(first) local remtot = 2^(n - 16) for it = 0, remtot - 1 do local z = 0 for i = 17, n do if it % 2 == 1 then z = z + t[i] end it = brs(it, 1) end local pos = lower_bound(first, tgt - z) if pos <= #first then ret = mmi(ret, mab(tgt - first[pos] - z)) end if 1 < pos then ret = mmi(ret, mab(tgt - first[pos - 1] - z)) end end print(ret) end