結果
問題 | No.2927 Reverse Polish Equation |
ユーザー | kemuniku |
提出日時 | 2024-10-12 17:14:37 |
言語 | Nim (2.0.2) |
結果 |
AC
|
実行時間 | 344 ms / 2,000 ms |
コード長 | 10,078 bytes |
コンパイル時間 | 3,844 ms |
コンパイル使用メモリ | 75,264 KB |
実行使用メモリ | 11,648 KB |
最終ジャッジ日時 | 2024-10-16 00:29:44 |
合計ジャッジ時間 | 9,912 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 1 ms
5,248 KB |
testcase_04 | AC | 1 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 1 ms
5,248 KB |
testcase_07 | AC | 3 ms
5,248 KB |
testcase_08 | AC | 4 ms
5,248 KB |
testcase_09 | AC | 3 ms
5,248 KB |
testcase_10 | AC | 1 ms
5,248 KB |
testcase_11 | AC | 4 ms
5,248 KB |
testcase_12 | AC | 102 ms
5,248 KB |
testcase_13 | AC | 171 ms
6,400 KB |
testcase_14 | AC | 117 ms
5,376 KB |
testcase_15 | AC | 164 ms
6,144 KB |
testcase_16 | AC | 56 ms
5,248 KB |
testcase_17 | AC | 217 ms
7,296 KB |
testcase_18 | AC | 261 ms
8,320 KB |
testcase_19 | AC | 18 ms
5,248 KB |
testcase_20 | AC | 214 ms
7,296 KB |
testcase_21 | AC | 344 ms
9,600 KB |
testcase_22 | AC | 33 ms
5,248 KB |
testcase_23 | AC | 1 ms
5,248 KB |
testcase_24 | AC | 2 ms
5,248 KB |
testcase_25 | AC | 1 ms
5,248 KB |
testcase_26 | AC | 1 ms
5,248 KB |
testcase_27 | AC | 291 ms
8,832 KB |
testcase_28 | AC | 240 ms
7,808 KB |
testcase_29 | AC | 33 ms
5,248 KB |
testcase_30 | AC | 284 ms
8,576 KB |
testcase_31 | AC | 21 ms
5,248 KB |
testcase_32 | AC | 64 ms
5,248 KB |
testcase_33 | AC | 120 ms
5,248 KB |
testcase_34 | AC | 147 ms
6,016 KB |
testcase_35 | AC | 2 ms
5,248 KB |
testcase_36 | AC | 282 ms
8,448 KB |
testcase_37 | AC | 98 ms
5,248 KB |
testcase_38 | AC | 329 ms
9,344 KB |
testcase_39 | AC | 132 ms
5,632 KB |
testcase_40 | AC | 276 ms
8,320 KB |
testcase_41 | AC | 226 ms
7,296 KB |
testcase_42 | AC | 132 ms
11,648 KB |
testcase_43 | AC | 141 ms
11,520 KB |
testcase_44 | AC | 161 ms
11,520 KB |
testcase_45 | AC | 129 ms
11,008 KB |
ソースコード
import macros;macro ImportExpand(s:untyped):untyped = parseStmt($s[2]) ImportExpand "cplib/tmpl/sheep.nim" <=== "when not declared CPLIB_TMPL_SHEEP:\n const CPLIB_TMPL_SHEEP* = 1\n {.warning[UnusedImport]: off.}\n {.hint[XDeclaredButNotUsed]: off.}\n import algorithm\n import sequtils\n import tables\n import macros\n import math\n import sets\n import strutils\n import strformat\n import sugar\n import heapqueue\n import streams\n import deques\n import bitops\n import std/lenientops\n import options\n #入力系\n proc scanf(formatstr: cstring){.header: \"<stdio.h>\", varargs.}\n proc getchar(): char {.importc: \"getchar_unlocked\", header: \"<stdio.h>\", discardable.}\n proc ii(): int {.inline.} = scanf(\"%lld\\n\", addr result)\n proc lii(N: int): seq[int] {.inline.} = newSeqWith(N, ii())\n proc si(): string {.inline.} =\n result = \"\"\n var c: char\n while true:\n c = getchar()\n if c == ' ' or c == '\\n':\n break\n result &= c\n #chmin,chmax\n template `max=`(x, y) = x = max(x, y)\n template `min=`(x, y) = x = min(x, y)\n #bit演算\n proc `%`*(x: int, y: int): int =\n result = x mod y\n if y > 0 and result < 0: result += y\n if y < 0 and result > 0: result += y\n proc `//`*(x: int, y: int): int{.inline.} =\n result = x div y\n if y > 0 and result * y > x: result -= 1\n if y < 0 and result * y < x: result -= 1\n proc `%=`(x: var int, y: int): void = x = x%y\n proc `//=`(x: var int, y: int): void = x = x//y\n proc `**`(x: int, y: int): int = x^y\n proc `**=`(x: var int, y: int): void = x = x^y\n proc `^`(x: int, y: int): int = x xor y\n proc `|`(x: int, y: int): int = x or y\n proc `&`(x: int, y: int): int = x and y\n proc `>>`(x: int, y: int): int = x shr y\n proc `<<`(x: int, y: int): int = x shl y\n proc `~`(x: int): int = not x\n proc `^=`(x: var int, y: int): void = x = x ^ y\n proc `&=`(x: var int, y: int): void = x = x & y\n proc `|=`(x: var int, y: int): void = x = x | y\n proc `>>=`(x: var int, y: int): void = x = x >> y\n proc `<<=`(x: var int, y: int): void = x = x << y\n proc `[]`(x: int, n: int): bool = (x and (1 shl n)) != 0\n #便利な変換\n proc `!`(x: char, a = '0'): int = int(x)-int(a)\n #定数\n #[ include cplib/utils/constants ]#\n when not declared CPLIB_UTILS_CONSTANTS:\n const CPLIB_UTILS_CONSTANTS* = 1\n const INF32*: int32 = 100100111.int32\n const INF64*: int = int(3300300300300300491)\n const INF = INF64\n #converter\n\n #range\n iterator range(start: int, ends: int, step: int): int =\n var i = start\n if step < 0:\n while i > ends:\n yield i\n i += step\n elif step > 0:\n while i < ends:\n yield i\n i += step\n iterator range(ends: int): int = (for i in 0..<ends: yield i)\n iterator range(start: int, ends: int): int = (for i in\n start..<ends: yield i)\n\n #joinが非stringでめちゃくちゃ遅いやつのパッチ\n proc join*[T: not string](a: openArray[T], sep: string = \"\"): string = a.mapit($it).join(sep)\n" ImportExpand "cplib/collections/segtree.nim" <=== "when not declared CPLIB_COLLECTIONS_SEGTREE:\n const CPLIB_COLLECTIONS_SEGTREE* = 1\n import algorithm\n import strutils\n import sequtils\n type SegmentTree*[T] = ref object\n default: T\n merge: proc(x: T, y: T): T\n arr*: seq[T]\n lastnode: int\n length: int\n proc initSegmentTree*[T](v: seq[T], merge: proc(x: T, y: T): T, default: T): SegmentTree[T] =\n ## セグメントツリーを生成します。\n ## vに元となるリスト、mergeに二つの区間をマージする関数、デフォルトに単位元を与えてください。\n var lastnode = 1\n while lastnode < len(v):\n lastnode*=2\n var arr = newSeq[T](2*lastnode)\n arr.fill(default)\n var self = SegmentTree[T](default: default, merge: merge, arr: arr, lastnode: lastnode, length: len(v))\n #1-indexedで作成する\n for i in 0..<len(v):\n self.arr[self.lastnode+i] = v[i]\n for i in countdown(lastnode-1, 1):\n self.arr[i] = self.merge(self.arr[2*i], self.arr[2*i+1])\n return self\n proc initSegmentTree*[T](n: int, merge: proc(x: T, y: T): T, default: T): SegmentTree[T] =\n ## セグメントツリーを生成します。\n ## nにサイズ、mergeに二つの区間をマージする関数、デフォルトに単位元を与えてください。(全て単位元で構築されます)\n initSegmentTree(newSeqWith(n, default), merge, default)\n\n proc update*[T](self: SegmentTree[T], x: Natural, val: T) =\n ## xの要素をvalに変更します。\n assert x < self.length\n var x = x\n x += self.lastnode\n self.arr[x] = val\n while x > 1:\n x = x shr 1\n self.arr[x] = self.merge(self.arr[2*x], self.arr[2*x+1])\n proc get*[T](self: SegmentTree[T], q_left: Natural, q_right: Natural): T =\n ## 半解区間[q_left,q_right)についての演算結果を返します。\n assert q_left <= q_right and 0 <= q_left and q_right <= self.length\n var q_left = q_left\n var q_right = q_right\n q_left += self.lastnode\n q_right += self.lastnode\n var (lres, rres) = (self.default, self.default)\n while q_left < q_right:\n if (q_left and 1) > 0:\n lres = self.merge(lres, self.arr[q_left])\n q_left += 1\n if (q_right and 1) > 0:\n q_right -= 1\n rres = self.merge(self.arr[q_right], rres)\n q_left = q_left shr 1\n q_right = q_right shr 1\n return self.merge(lres, rres)\n proc get*[T](self: SegmentTree[T], segment: HSlice[int, int]): T =\n assert segment.a <= segment.b + 1 and 0 <= segment.a and segment.b+1 <= self.length\n return self.get(segment.a, segment.b+1)\n proc `[]`*[T](self: SegmentTree[T], segment: HSlice[int, int]): T = self.get(segment)\n proc `[]`*[T](self: SegmentTree[T], index: Natural): T =\n assert index < self.length\n return self.arr[index+self.lastnode]\n proc `[]=`*[T](self: SegmentTree[T], index: Natural, val: T) =\n assert index < self.length\n self.update(index, val)\n proc get_all*[T](self: SegmentTree[T]): T =\n ## [0,len(self))区間の演算結果をO(1)で返す\n return self.arr[1]\n proc len*[T](self: SegmentTree[T]): int =\n return self.length\n proc `$`*[T](self: SegmentTree[T]): string =\n var s = self.arr.len div 2\n return self.arr[s..<s+self.len].join(\" \")\n template newSegWith*(V, merge, default: untyped): untyped =\n initSegmentTree[typeof(default)](V, proc (l{.inject.}, r{.inject.}: typeof(default)): typeof(default) = merge, default)\n proc max_right*[T](self: SegmentTree[T], l: int, f: proc(l: T): bool): int =\n assert 0 <= l and l <= self.len\n assert f(self.default)\n if l == self.len: return self.len\n var l = l\n l += self.lastnode\n var sm = self.default\n while true:\n while l mod 2 == 0: l = (l shr 1)\n if not f(self.merge(sm, self.arr[l])):\n while l < self.lastnode:\n l *= 2\n if f(self.merge(sm, self.arr[l])):\n sm = self.merge(sm, self.arr[l])\n l += 1\n return l - self.lastnode\n sm = self.merge(sm, self.arr[l])\n l += 1\n if (l and -l) == l: break\n return self.len\n proc min_left*[T](self: SegmentTree[T], r: int, f: proc(l: T): bool): int =\n assert 0 <= r and r <= self.len\n assert f(self.default)\n if r == 0: return 0\n var r = r\n r += self.lastnode\n var sm = self.default\n while true:\n r -= 1\n while (r > 1 and r mod 2 != 0): r = (r shr 1)\n if not f(self.merge(self.arr[r], sm)):\n while r < self.lastnode:\n r = 2 * r + 1\n if f(self.merge(self.arr[r], sm)):\n sm = self.merge(self.arr[r], sm)\n r -= 1\n return r + 1 - self.lastnode\n if (r and -r) == r: break\n return 0\n" ImportExpand "cplib/utils/binary_search.nim" <=== "when not declared CPLIB_UTILS_BINARY_SEARCH:\n const CPLIB_UTILS_BINARY_SEARCH* = 1\n proc meguru_bisect*(ok, ng: int, is_ok: proc(x: int): bool): int =\n var\n ok = ok\n ng = ng\n while abs(ok - ng) > 1:\n var mid = (ok + ng) div 2\n if is_ok(mid): ok = mid\n else: ng = mid\n return ok\n\n proc meguru_bisect*(ok, ng: SomeFloat, is_ok: proc(x: SomeFloat): bool, eps: SomeFloat = 1e-10): SomeFloat =\n var\n ok = ok\n ng = ng\n while abs(ok - ng) > eps and abs(ok - ng) / max(ok, ng) > eps:\n var mid = (ok + ng) / 2\n if is_ok(mid): ok = mid\n else: ng = mid\n return ok\n" var Q,Y = ii() var S = newSeqWith(Q,si()) proc solve(x:int):int= var A : seq[int] for i in range(Q): if S[i] == "min": A.add(min(A.pop(),A.pop())) elif S[i] == "max": A.add(max(A.pop(),A.pop())) elif S[i] == "+": A.add((A.pop()+A.pop())) elif S[i] == "X": A.add(x) else: A.add(S[i].parseInt) return A[0] proc isok(arg:int):bool= return solve(arg) >= Y var tmp = meguru_bisect(10**14,-1,isok) if solve(tmp) == Y: echo tmp else: echo "-1"