結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー 👑 obakyanobakyan
提出日時 2021-07-30 23:53:52
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,081 bytes
コンパイル時間 89 ms
コンパイル使用メモリ 5,156 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 09:00:08
合計ジャッジ時間 1,686 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,352 KB
testcase_13 AC 1 ms
4,352 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 21 ms
4,348 KB
testcase_17 AC 19 ms
4,352 KB
testcase_18 AC 26 ms
4,352 KB
testcase_19 AC 26 ms
4,352 KB
testcase_20 AC 6 ms
4,348 KB
testcase_21 AC 5 ms
4,348 KB
testcase_22 AC 5 ms
4,348 KB
testcase_23 AC 5 ms
4,348 KB
testcase_24 AC 14 ms
4,348 KB
testcase_25 AC 8 ms
4,348 KB
testcase_26 AC 26 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local n, k = io.read():match("(%d+) (%d+)")
n = tonumber(n)
local a = {}
for i = 1, 9 do
  a[i] = io.read("*n")
end
if #k < n then
  for i = 1, 9 do
    io.write(string.rep(tostring(i), a[i]))
  end
  io.write("\n")
  os.exit()
elseif n < #k then
  print(-1)
  os.exit()
end

local b = {}
for i = 1, 9 do b[i] = a[i] end
local lastpos = 0
for i = 1, n - 1 do
  local kv = k:byte(i) - 48
  if 0 < kv and 0 < b[kv] then
    lastpos = i
    b[kv] = b[kv] - 1
  else
    break
  end
end
local f = false
for i = lastpos + 1, 1, -1 do
  local kv = k:byte(i) - 48
  for j = kv + 1, 9 do
    if 0 < b[j] then
      f = i break
    end
  end
  if f then break end
  if 1 < i then
    kv = k:byte(i - 1) - 48
    b[kv] = b[kv] + 1
  end
end
if not f then
  print(-1) os.exit()
end
for i = 1, f - 1 do
  local kv = k:byte(i) - 48
  io.write(kv)
  a[kv] = a[kv] - 1
end
do
  local kv = k:byte(f) - 48
  for i = kv + 1, 9 do
    if 0 < a[i] then
      io.write(i)
      a[i] = a[i] - 1
      break
    end
  end
end

for i = 1, 9 do
  io.write(string.rep(tostring(i), a[i]))
end
io.write("\n")
0