結果

問題 No.9 モンスターのレベル上げ
ユーザー むらためむらため
提出日時 2019-01-28 07:30:04
言語 Nim
(2.0.2)
結果
AC  
実行時間 94 ms / 5,000 ms
コード長 2,424 bytes
コンパイル時間 3,349 ms
コンパイル使用メモリ 70,724 KB
実行使用メモリ 7,920 KB
最終ジャッジ日時 2023-09-14 03:08:39
合計ジャッジ時間 5,349 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,500 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 94 ms
7,864 KB
testcase_03 AC 93 ms
7,856 KB
testcase_04 AC 94 ms
7,868 KB
testcase_05 AC 67 ms
7,916 KB
testcase_06 AC 24 ms
7,204 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 27 ms
7,232 KB
testcase_09 AC 93 ms
7,752 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 93 ms
7,744 KB
testcase_12 AC 83 ms
7,780 KB
testcase_13 AC 10 ms
7,920 KB
testcase_14 AC 93 ms
7,840 KB
testcase_15 AC 94 ms
7,856 KB
testcase_16 AC 5 ms
4,380 KB
testcase_17 AC 94 ms
7,720 KB
testcase_18 AC 93 ms
7,744 KB
testcase_19 AC 4 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 27) Warning: imported and not used: 'random' [UnusedImport]

ソースコード

diff #

import sequtils,algorithm,random,times
# GC_disableMarkAndSweep()
template times*(n:int,body) = (for _ in 0..<n: body)
template `min=`*(x,y) = x = min(x,y)
proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
  while true:
    let k = getchar_unlocked()
    if k < '0': break
    result = 10 * result + k.ord - '0'.ord

type
  Heap*[T] = object
    nodes: seq[T]
    compare: proc(x,y:T):int
proc newHeap*[T](compare:proc(x,y:T):int): Heap[T] = Heap[T](nodes:newSeq[T](),compare:compare)
proc compareNode[T](h:Heap[T],i,j:int): int {.inline.}= h.compare(h.nodes[i],h.nodes[j])
proc size*[T](h:Heap[T]):int = h.nodes.len()
proc items*[T](h:Heap[T]):seq[T] = h.nodes
proc top*[T](h:Heap[T]): T = h.nodes[0]
proc push*[T](h:var Heap[T],node:T):void =
  h.nodes.add(node) #末尾に追加
  var i = h.nodes.len() - 1
  while i > 0: # 末尾から木を整形
    let parent = (i - 1) div 2
    if h.compare(h.nodes[parent],node) <= 0: break
    h.nodes[i] = h.nodes[parent]
    i = parent
  h.nodes[i] = node
proc shiftdown*[T](h:var Heap[T]): void =
  let size = h.nodes.len()
  var i = 0
  while true :
    let L = i * 2 + 1
    let R = i * 2 + 2
    if L >= size : break
    let child = if R < size and h.compareNode(R,L) <= 0 : R else: L
    if h.compareNode(i,child) <= 0: break
    swap(h.nodes[i],h.nodes[child])
    i = child

proc pop*[T](h:var Heap[T]):T =
  result = h.nodes[0] # rootと末尾を入れ替えて木を整形
  h.nodes[0] = h.nodes[^1]
  h.nodes.setLen(h.nodes.len() - 1)
  h.shiftdown()
proc replaceTop*[T](h:var Heap[T],node:T):void =
  h.nodes[0] = node
  h.shiftdown()

let n = scan()
# 一番レベルの低い 一番戦ってないものを戦わせる
type monster = tuple[lv:int,cnt:int]
var myInitialHeap = newHeap(
  proc (a,b:monster):int =
    if a.lv == b.lv : a.cnt - b.cnt
    else: a.lv - b.lv
  )
let A = newSeqWith(n,scan()).sorted(cmp)
let B = newSeqWith(n,scan() shr 1) # enemy
for i in 0..<n : myInitialHeap.push((A[i] ,0))
let startedTime = cpuTime()
var i = 0
var ans = 1e12.int
while true:
  var myHeap = myInitialHeap
  var mx = 0
  for j in 0..<n:
    let b = B[(j + i) mod n]
    var me = myHeap.top()
    me.lv += b
    me.cnt += 1
    myHeap.replaceTop(me)
    if me.cnt > mx :
      mx = me.cnt
      if ans <= mx: break
  ans .min= mx
  i += 1
  if i >= n : break
  if (cpuTime() - startedTime) * 1000 > 90: break
echo ans
0