結果

問題 No.1053 ゲーミング棒
ユーザー 👑 obakyan
提出日時 2022-04-29 01:29:12
言語 Lua
(LuaJit 2.1.1734355927)
結果
WA  
実行時間 -
コード長 886 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 6,816 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-28 08:51:07
合計ジャッジ時間 2,194 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

local n = io.read("*n")
local a = {}
for i = 1, n do
  local ai = io.read("*n")
  a[i] = ai
end
local allsame = a[1] == a[n]
for i = 1, n - 1 do
  allsame = allsame and a[i] == a[i + 1]
end
if allsame then
  print(0) os.exit()
end
if a[1] ~= a[n] then
  local t = {}
  local cur = a[1]
  for i = 2, n do
    local ai = a[i]
    if cur ~= ai then
      if t[cur] then print(-1) os.exit() end
      t[cur] = true
      cur = ai
    end
  end
  print(0)
else
  local left, right = 1, n
  for i = 1, n do
    if a[i] == a[1] then
      left = i
    else
      break
    end
  end
  for i = n, 1, -1 do
    if a[i] == a[1] then
      right = i
    else
      break
    end
  end
  local t = {}
  cur = a[1]
  for i = left + 1, right - 1 do
    local ai = a[i]
    if cur ~= ai then
      if t[cur] then print(-1) os.exit() end
      t[cur] = true
      cur = ai
    end
  end
  print(1)
end
0