結果

問題 No.9 モンスターのレベル上げ
ユーザー むらためむらため
提出日時 2017-07-31 23:04:04
言語 Nim
(2.0.2)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,476 bytes
コンパイル時間 740 ms
コンパイル使用メモリ 68,404 KB
最終ジャッジ日時 2024-04-27 02:28:33
合計ジャッジ時間 1,016 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 50) Warning: Use the new 'sugar' module instead; future is deprecated [Deprecated]
/home/judge/data/code/Main.nim(1, 62) Error: cannot open file: queues

ソースコード

diff #

import sequtils,strutils,strscans,algorithm,math,future,sets,queues,tables,lists,random
template get():string = stdin.readLine()
template times(n:int,body:untyped): untyped = (for _ in 0..<n: body)
template `max=`(x,y:typed):void = x = max(x,y)
template `min=`(x,y:typed):void = x = min(x,y)


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 = 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 pop*[T](h:var Heap[T]):T =
  result = h.nodes[0] # rootと末尾を入れ替えて木を整形
  let x = h.nodes[^1]
  h.nodes.setLen(h.nodes.len() - 1)
  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.compare(x,h.nodes[child]) <= 0: break
    h.nodes[i ] = h.nodes[child]
    i = child
  h.nodes[i] = x


let
  N = get().parseInt # ~1500
  A = get().split().map(parseInt) # my lv
  B = get().split().map(parseInt) #enemy lv

# 一番レベルの低い 一番戦ってないものを戦わせる
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 )
for a in A: myInitialHeap.push((a,0))
var res = 1e12.int
for i in 0..<N:
  var myHeap = myInitialHeap
  var mx = 0
  for j in 0..<N: # 1500 * 1500 * log(1500)
    let b = B[(i + j) mod N]
    var me = myHeap.pop()
    me.lv += b div 2  # (b div 2)ぶんレベルアップ
    me.cnt += 1
    myHeap.push(me)
    if me.cnt > mx :
      mx = me.cnt
      if res <= mx: break
  res .min= mx
echo res

#[
type
  # 左から大きい順にソート
  CPriorityQueue {.importcpp: "std::priority_queue", header: "<queue>".} [T] = object
  CPair {.importcpp: "std::pair", header: "<utility>".} [T1,T2] = object
proc cNewPriorityQueue(T: typedesc): CPriorityQueue[T]
  {.importcpp: "std::priority_queue<'*1>()", nodecl.}
proc cMakePair[T1,T2](t1:T1,t2:T2):CPair[T1,T2]
  {.importcpp: "std::make_pair(@)", nodecl.}
proc first[T1,T2](this: CPair[T1,T2]): T1 {.importcpp: "#.first", nodecl.}
proc second[T1,T2](this: CPair[T1,T2]): T2 {.importcpp: "#.second", nodecl.}
proc cpop[T](this: CPriorityQueue[T]) {.importcpp: "#.pop(@)", nodecl.}
proc newPriorityQueue*[T](): CPriorityQueue[T] = cNewPriorityQueue(T)
proc empty*[T](this:CPriorityQueue[T]):bool {.importcpp: "#.empty(@)", nodecl.}
proc size*[T](this:CPriorityQueue[T]):int {.importcpp: "#.size(@)", nodecl.}
proc ctop*[T](this: CPriorityQueue[T]):T {.importcpp: "#.top(@)", nodecl.}
proc top*[T](this: CPriorityQueue[T]):T {.importcpp: "#.top(@)", nodecl.}
proc push*[T](this: CPriorityQueue[T],x:T) {.importcpp: "#.push(@)", nodecl.}
proc pop*[T](this:var CPriorityQueue[T]):T =(result = this.top();this.cpop())
proc newPriorityQueue*[T1,T2](): CPriorityQueue[CPair[T1,T2]] = cNewPriorityQueue(CPair[T1,T2])
proc push*[T1,T2](this:var CPriorityQueue[CPair[T1,T2]],x:(T1,T2)): void =
  this.push(cMakePair(x[0],x[1]))
proc pop*[T1,T2](this:var CPriorityQueue[CPair[T1,T2]]):(T1,T2) =
  let res = this.top();this.cpop();return (res.first,res.second)
proc top*[T1,T2](this:var CPriorityQueue[CPair[T1,T2]]):(T1,T2) =
  let res = this.ctop();return (res.first,res.second)

let
  N = get().parseInt # ~1500
  A = get().split().map(parseInt) # my lv
  B = get().split().map(parseInt) #enemy lv

# 一番レベルの低い 一番戦ってないものを戦わせる
type monster = tuple[lv:int,cnt:int]
var myInitialHeap = newPriorityQueue[int,int]()
for a in A: myInitialHeap.push((-a,0))
var res = 1e10.int
for i in 0..<N:
  var myHeap = myInitialHeap
  var mx = 0
  for j in 0..<N: # 1500 * 1500 * log(1500)
    let b = B[(i + j) mod N]
    var me = myHeap.pop()
    me[0] -= b div 2  # (b div 2)ぶんレベルアップ
    me[1] -= 1
    myHeap.push(me)
    if me[1] * -1 > mx :
      mx = me[1] * -1
      if res <= mx: break
  res .min= mx
echo res
]#
0