結果

問題 No.850 企業コンテスト2位
ユーザー nimonnimon
提出日時 2019-08-31 00:53:00
言語 Nim
(2.0.2)
結果
RE  
実行時間 -
コード長 1,222 bytes
コンパイル時間 3,398 ms
コンパイル使用メモリ 70,968 KB
実行使用メモリ 24,420 KB
平均クエリ数 194.57
最終ジャッジ日時 2023-09-23 18:12:21
合計ジャッジ時間 9,283 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
23,556 KB
testcase_01 AC 26 ms
23,652 KB
testcase_02 AC 25 ms
23,844 KB
testcase_03 AC 26 ms
23,640 KB
testcase_04 AC 26 ms
23,568 KB
testcase_05 AC 26 ms
23,748 KB
testcase_06 AC 26 ms
23,964 KB
testcase_07 AC 30 ms
23,448 KB
testcase_08 AC 32 ms
23,340 KB
testcase_09 AC 32 ms
24,264 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 26 ms
23,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from strutils import parseInt
from sequtils import mapIt, newSeqWith
from algorithm import binarySearch

proc ask(x, y: int16): int16 =
  echo "? ", x, " ", y
  result = stdin.readLine.parseInt.int16

proc ans(x: int16) =
  echo "! ", x


let N = stdin.readLine.parseInt

var histories = newSeqWith(9, newSeq[int16]())
histories[0] = (1..N).mapIt(it.int16)

for h in 0..<8:
  if histories[h].len <= 1: break
  for i in countup(1, histories[h].high, 2):
    histories[h + 1].add(ask(histories[h][i - 1], histories[h][i]))
  if histories[h].len mod 2 == 1:
    histories[h + 1].add(histories[h][^1])

var
  results = newSeq[int16]()
  max: int16
for h in countdown(8, 0):
  if histories[h].len > 1:
    let i = histories[h].binarySearch(max)
    if i mod 2 == 0:
      if i + 1 < histories[h].len: results.add(histories[h][i + 1])
    else:
      if i - 1 >= 0: results.add(histories[h][i - 1])
  elif histories[h].len == 1:
    max = histories[h][0]
    continue
  else:
    continue

var next = newSeq[int16]()
while results.len > 1:
  next = @[]
  for i in countup(1, results.high, 2):
    next.add(ask(results[i - 1], results[i]))
  if results.len mod 2 == 1:
    next.add(results[^1])
  results = next

ans(results[0])
0