結果

問題 No.1360 [Zelkova 4th Tune] 協和音
ユーザー 👑 obakyanobakyan
提出日時 2021-01-22 21:30:46
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 1,579 bytes
コンパイル時間 143 ms
コンパイル使用メモリ 5,300 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-27 17:36:04
合計ジャッジ時間 5,907 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 7 ms
4,376 KB
testcase_24 AC 10 ms
4,376 KB
testcase_25 AC 33 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 32 ms
4,380 KB
testcase_29 AC 5 ms
4,380 KB
testcase_30 AC 7 ms
4,376 KB
testcase_31 AC 7 ms
4,380 KB
testcase_32 AC 4 ms
4,380 KB
testcase_33 AC 32 ms
4,376 KB
testcase_34 AC 34 ms
4,376 KB
testcase_35 AC 33 ms
4,376 KB
testcase_36 AC 35 ms
4,376 KB
testcase_37 AC 32 ms
4,376 KB
testcase_38 AC 34 ms
4,376 KB
testcase_39 AC 33 ms
4,380 KB
testcase_40 AC 32 ms
4,380 KB
testcase_41 AC 31 ms
4,384 KB
testcase_42 AC 32 ms
4,376 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 31 ms
4,376 KB
testcase_45 AC 1 ms
4,380 KB
testcase_46 AC 32 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local bxor = bit.bxor

local function grayCode(x)
  return bxor(x, brs(x, 1))
end

-- add_func(idx), rm_func(idx), work_func()
local function grayWalk(size, add_func, rm_func, work_func)
  local prv = 0
  local total = bls(1, size) - 1
  local bpos = {}
  for i = 1, size do
    bpos[bls(1, i - 1)] = i
  end
  -- if need first work then use below
  -- work_func()
  for i = 1, total do
    local v = grayCode(i)
    if prv < v then
      prv, v = v, v - prv
      add_func(bpos[v])
    else
      prv, v = v, prv - v
      rm_func(bpos[v])
    end
    work_func()
  end
end

local n = io.read("*n")
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
local b = {}
for i = 1, n do
  b[i] = {}
  for j = 1, n do
    b[i][j] = io.read("*n")
  end
end
local maxboxsize = 0
local maxbox = {}
local maxscore = -1
local score = 0
local box = {}
local function add_func(idx)
  score = score + a[idx]
  for i = 1, #box do
    score = score + b[box[i]][idx]
  end
  table.insert(box, idx)
end
local function rm_func(idx)
  score = score - a[idx]
  for i = #box, 1, -1 do
    score = score - b[box[i]][idx]
    if box[i] == idx then table.remove(box, i) end
  end
end
local function work_func()
  if maxscore < score then
    maxscore = score
    maxboxsize = #box
    for i = 1, maxboxsize do
      maxbox[i] = box[i]
    end
  end
end
grayWalk(n, add_func, rm_func, work_func)
print(maxscore)
for i = maxboxsize + 1, #maxbox do
  table.remove(maxbox)
end
table.sort(maxbox)
print(table.concat(maxbox, " "))
0