結果

問題 No.1183 コイン遊び
ユーザー nimon
提出日時 2020-08-22 13:05:54
言語 Nim
(2.2.0)
結果
AC  
実行時間 206 ms / 2,000 ms
コード長 11,178 bytes
コンパイル時間 3,997 ms
コンパイル使用メモリ 83,584 KB
実行使用メモリ 97,536 KB
最終ジャッジ日時 2024-10-15 07:11:21
合計ジャッジ時間 10,188 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 67) Warning: imported and not used: 'sugar' [UnusedImport]
/home/judge/data/code/Main.nim(14, 6) Warning: imported and not used: 'terminal' [UnusedImport]

ソースコード

diff #
プレゼンテーションモードにする

when NimMajor <= 0 and NimMinor <= 18: import future else: import sugar
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
when defined release: {.checks: off.}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from typetraits import arity
from sequtils import map, mapIt, newSeqWith, toSeq
from strutils import split, parseInt, parseFloat, parseBool, parseEnum, parseBiggestInt
when NimMajor == 0 and NimMinor >= 14: from strutils import parseBiggestUInt
import macros
from terminal import setForegroundColor, ForegroundColor, resetAttributes
proc warn(message: string) =
when not defined release:
stderr.setForegroundColor(fgYellow)
stderr.write(": ")
stderr.resetAttributes
stderr.writeLine message
when not declared parseBiggestUInt:
proc parseBiggestUInt(s: string): uint64 = uint64(parseInt(s))
when not declared SomeFloat:
type SomeFloat = float | float64 | float32
when not defined nimHasRunnableExamples:
template runnableExamples*(body: untyped) = discard
proc parseT(s: string; T: typedesc): auto =
## `parse`
## `T` `SomeOrdinal | SomeFloat` (subranges ) `s`
runnableExamples:
doAssert parseT("12", int) == 12
doAssert parseT("12", uint) == 12
doAssert parseT("12", int64) == 12
doAssert parseT("12", float32) == 12.0
doAssert parseT("Yes", bool) == true
when T is SomeSignedInt: cast[T](parseBiggestInt(s))
elif T is SomeUnsignedInt: cast[T](parseBiggestUInt(s))
elif T is SomeFloat: cast[T](parseFloat(s))
elif T is enum: parseEnum[T](s)
elif T is bool: parseBool(s)
elif T is char: s[0]
else: s
proc unpackWithParse*(input: openArray[string]; T: typedesc[tuple]): T =
## `T` `tuple` `parse`
runnableExamples:
let t = unpackWithParse(@["1", "1", "1", "1", "1"], 4, tuple[a: int8, b: uint32, c: float64, d: bool])
doAssert int8 is t.a.type and t.a == 1
doAssert uint32 is t.b.type and t.b == 1
doAssert float64 is t.c.type and t.c == 1.0
doAssert bool is t.d.type and t.d == true
doAssert tuple[a: int8, b: uint32, c: float64, d: bool] is t.type
var i = 0
for x in result.fields:
if i > input.high:
warn " " & $T.arity & " "
break
x = parseT(input[i], x.type)
i.inc
result
proc input(T: typedesc[string]): string = stdin.readLine
proc input(T: typedesc[SomeOrdinal | SomeFloat | char]): auto = input(string).parseT(T)
proc input(T: typedesc[seq[string]]): auto = input(string).split
proc input(T: typedesc[seq[char]]): auto = toSeq(input(string).items)
proc input[E: SomeOrdinal | SomeFloat](T: typedesc[seq[E]]): auto = input(seq[string]).mapIt(it.parseT(E.type))
proc input(T: typedesc[tuple]): auto = input(seq[string]).unpackWithParse(T)
proc toTupleType(parTuple: NimNode): NimNode {.compileTime.} =
## `nnkPar` `tuple[]`
## `nnkPar`
runnableExamples:
static:
doAssert((quote do: (a: int, b: float)).toTupleType == (quote do: tuple[a: int, b: float]))
doAssert((quote do: (a, b: int)).toTupleType == (quote do: tuple[a, b: int]))
doAssert((quote do: (int, int)).toTupleType == (quote do: (int, int)))
doAssert((quote do: ()).toTupleType == (quote do: ()))
if parTuple.len == 0 or parTuple.findChild(it.kind == nnkExprColonExpr) == nil: # () or (T, U, ...)
result = parTuple
else:
result = newTree(nnkTupleTy)
var identDefs = newTree(nnkIdentDefs)
for field in parTuple:
if field.kind == nnkIdent: # (a, b, ..., x: T) a, b, ... (x )
identDefs.add(field)
field.copyChildrenTo(identDefs)
if field.kind != nnkIdent: # (..., x: T, y: ...) x: T
identDefs.add(newEmptyNode())
result.add(identDefs)
identDefs = newTree(nnkIdentDefs)
proc seqInputCall(bracketTree: NimNode): NimNode {.compileTime.} =
## `seq[N, seq[M, ..., [seq[T]]...]]` `newSeqWith(N, newSeqWith(M, ..., input(seq[T])...))`
if bracketTree.kind != nnkBracketExpr:
case bracketTree.kind:
of nnkPar: # x: (Field0: ...)
result = newCall("input", bracketTree.toTupleType)
else: # x: T
result = newCall("input", bracketTree)
else:
case bracketTree.len:
of 2: # seq[N, ... seq[T] ...] seq[T]
if bracketTree[^1].kind == nnkBracketExpr:
error("seq[seq[T]] seq[N, seq[T]] ")
result = newCall("input", bracketTree)
of 3: # (seq[N, ...])
result = newCall("newSeqWith", bracketTree[1], seqInputCall(bracketTree[^1]))
else:
error("")
proc appendedProcCallBlocks(procCalls: NimNode; i: int): NimNode =
## `procCalls[i]`
## .. code-block:: Nim
## block:
## var it = procCall(...)
##
##
## .. code-block:: Nim
## block:
## var it = lastProcCall(...)
## it
## `it`
let it = ident("it")
let procCall = procCalls[i]
result = newStmtList(quote do:
var `it` = `procCall`
)
if i == procCalls.len - 1: # it
result.add(it)
else:
result.add(appendedProcCallBlocks(procCalls, i + 1))
result = newBlockStmt(result)
proc inputsImpl(pre, post: NimNode): NimNode {.compileTime.} =
## pre post
# input()
var inputCall: NimNode
case pre.kind:
of nnkPar: # (x, y, ...): ...
result = newTree(nnkVarTuple)
pre.copyChildrenTo(result)
result.add(newEmptyNode())
case post[0].kind:
of nnkPar: # (x, y, ...): (T, T, ...)
inputCall = newCall("input", post[0].toTupleType)
of nnkTupleTy: # (x, y, ...): tuple[Field0: ...]
inputCall = newCall("input", post)
else: # (x, y, ...): T
var parTupleTy = newTree(nnkPar)
for _ in 0..<pre.len: parTupleTy.add(post[0]) # (int, int, int, ...)
inputCall = newCall("input", parTupleTy)
else: # x: ...
result = newTree(nnkIdentDefs).add(pre).add(newEmptyNode())
case post[0].kind:
of nnkPar: # x: (Field0: ...)
inputCall = newCall("input", post[0].toTupleType)
of nnkBracketExpr:
inputCall = seqInputCall(post[0])
else: # x: T
inputCall = newCall("input", post[0])
#
if post.len > 1:
let it = ident("it")
var itStmts = when NimMajor == 0 and NimMinor < 17: newStmtList(quote do: (var `it` = `inputCall`)) #
else: newStmtList(quote do: (var `it` {.used.} = `inputCall`))
itStmts.add(appendedProcCallBlocks(post, 1)) #
result.add(newTree(nnkStmtListExpr, newBlockStmt(itStmts))) #
else:
result.add(inputCall) #
macro inputs*(body: untyped): untyped =
##
## `it` `y`
##
## `u`, `y`
##
## .. code-block:: Nim
## inputs:
## a: int
## b: string
## c: (x: char, y: float)
## d: tuple[x: char, y: float]
## e: (u, v: BiggestInt)
## f: tuple[u, v: int]
## g: seq[int]
## h: seq[a, seq[int]]
## i: seq[a, (s, t: int)]
## (j, k, l): int
## (m, n): (char, float)
## o: seq[a, string]
## (p): int
## q: seq[int]; it.sorted(system.cmp)
## r: seq[float]; it.sorted(cmp).filterIt(it > 0)
## s: seq[char]
## t: seq[2, seq[a, int]]
## u: string; parseInt(it); abs(-it)
## (_, v): (a: char, b: char)
## (w, x): tuple[a, b: char]
## y: int; "hoge"
expectKind(body, nnkStmtList)
result = newTree(nnkStmtList)
var letSection = newTree(nnkLetSection)
for decl in body:
case decl[0].kind:
of nnkIdent: # x: ...
letSection.add(inputsImpl(decl[0], decl[1]))
of nnkPar:
expectMinLen(decl[0], 1)
if decl[0].len == 1: # (x): ... x: ...
letSection.add(inputsImpl(decl[0][0], decl[1]))
else: # (x, y, ...): ...
letSection.add(inputsImpl(decl[0], decl[1]))
else:
expectKind(decl[0], {nnkIdent, nnkPar})
result.add(letSection)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
proc `ceilDiv`*[T](x, y: T): T = x div y + ord(x mod y != 0)
proc `//=`*(x: var SomeInteger; y: SomeInteger) = x = x div y
proc `%=`*(x: var SomeInteger; y: SomeInteger) = x = x mod y
proc `<?=`*[T](x: var T; y: T) = x = min(x, y)
proc `>?=`*[T](x: var T; y: T) = x = max(x, y)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
proc withIndex*[T](s: openArray[T]): seq[tuple[i: int, v: T]] =
(0..s.high).mapIt((it, s[it]))
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
template countIt*[T](a: openArray[T]; pred: untyped): int =
var result = 0
for it {.inject.} in items(a):
if pred: result.inc
result
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
proc reversed*(s: string): string =
result = newString(s.len)
for i, c in s: result[s.len - i - 1] = c
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from sequtils import newSeqWith, allIt
template newSeqWithImpl[T](lens: seq[int]; init: T; currentDimension, lensLen: static[int]): untyped =
when currentDimension == lensLen:
newSeqWith(lens[currentDimension - 1], init)
else:
newSeqWith(lens[currentDimension - 1], newSeqWithImpl(lens, init, currentDimension + 1, lensLen))
template newSeqWith*[T](lens: varargs[int]; init: T): untyped =
assert(lens.allIt(it > 0))
newSeqWithImpl(@lens, init, 1, lens.len)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
when not defined(release):
template stderrEcho*(x: varargs[string, `$`]) =
for v in x:
stderr.write(v)
stderr.writeLine ""
template stderrEcho*[T](x: seq[seq[T]]) =
for v in x: stderrEcho v
template stderrEcho*(x: seq[string]) =
for v in x: stderrEcho v
else:
template stderrEcho*(x: untyped) = discard
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
inputs:
N: int
A: seq[int]
B: seq[int]
var
result = 0
prev = false
for i, _ in A:
if A[i] != B[i]:
if not prev: result.inc
prev = true
else:
prev = false
echo result
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0