結果

問題 No.592 括弧の対応 (2)
ユーザー むらためむらため
提出日時 2019-01-18 11:51:20
言語 Nim
(2.0.2)
結果
AC  
実行時間 228 ms / 5,000 ms
コード長 1,364 bytes
コンパイル時間 2,642 ms
コンパイル使用メモリ 67,488 KB
実行使用メモリ 6,916 KB
最終ジャッジ日時 2023-09-14 02:10:46
合計ジャッジ時間 4,101 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 228 ms
6,852 KB
testcase_02 AC 223 ms
6,900 KB
testcase_03 AC 222 ms
6,916 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 8) Warning: imported and not used: 'sequtils' [UnusedImport]
/home/judge/data/code/Main.nim(1, 17) Warning: imported and not used: 'strutils' [UnusedImport]
/home/judge/data/code/Main.nim(1, 26) Warning: imported and not used: 'algorithm' [UnusedImport]
/home/judge/data/code/Main.nim(1, 36) Warning: imported and not used: 'math' [UnusedImport]

ソースコード

diff #

import sequtils,strutils,algorithm,math

proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
  while true:
    var k = getchar_unlocked()
    if k < '0' or k > '9': break
    else: result = 10 * result + k.ord - '0'.ord

template useStack() =
  type Stack*[T] = ref object
    data: seq[T]
    size: int
    index: int
  proc newStack*[T](size: int = 64): Stack[T] =
    new(result)
    result.data = newSeq[T](size)
    result.size = size
    result.index = -1
  proc `$`*[T](self: Stack[T]): string = $(self.data[..self.index])
  proc isEmpty*[T](self: Stack[T]): bool = self.index < 0
  proc isValid*[T](self: Stack[T]): bool = self.index >= 0 and self.index < self.size
  proc len*[T](self: Stack[T]): int = (if self.isEmpty(): 0 else: self.index + 1)
  proc top*[T](self: Stack[T]): T = self.data[self.index]
  proc pop*[T](self: var Stack[T]): T {.discardable.} = (result = self.top(); self.index -= 1)
  proc push*[T](self: var Stack[T], elem: T) =
    self.index += 1
    if self.index < self.size: self.data[self.index] = elem
    else:
      self.data.add(elem)
      self.size += 1

useStack()

let n = scan()
var A = newSeq[int](n + 1)
var s = newStack[int](n div 2 + 10)
for i in 1..n:
  if getchar_unlocked() == '(': s.push(i)
  else:
    A[i] = s.pop()
    A[A[i]] = i
# 3 4
for a in A[1..^1]: echo a
0