結果

問題 No.120 傾向と対策:門松列(その1)
ユーザー むらためむらため
提出日時 2019-10-01 06:21:59
言語 Nim
(2.0.2)
結果
AC  
実行時間 26 ms / 5,000 ms
コード長 3,580 bytes
コンパイル時間 3,068 ms
コンパイル使用メモリ 62,416 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-03 05:34:36
合計ジャッジ時間 3,155 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
6,820 KB
testcase_01 AC 25 ms
6,820 KB
testcase_02 AC 13 ms
6,820 KB
testcase_03 AC 26 ms
6,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils,algorithm
template times*(n:int,body) = (for _ in 0..<n: body)
template `max=`*(x,y) = x = max(x,y)
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

proc putchar_unlocked(c:char){. importc:"putchar_unlocked",header: "<stdio.h>" .}
proc printInt(a:int32) =
  if a == 0:
    putchar_unlocked('0')
    return
  template div10(a:int32) : int32 = cast[int32]((0x1999999A * cast[int64](a)) shr 32)
  template mod10(a:int32) : int32 = a - (a.div10 * 10)
  var n = a
  var rev = a
  var cnt = 0
  while rev.mod10 == 0:
    cnt += 1
    rev = rev.div10
  rev = 0
  while n != 0:
    rev = rev * 10 + n.mod10
    n = n.div10
  while rev != 0:
    putchar_unlocked((rev.mod10 + '0'.ord).chr)
    rev = rev.div10
  while cnt != 0:
    putchar_unlocked('0')
    cnt -= 1
proc printInt(a:int,last:char) =
  a.int32.printInt()
  putchar_unlocked(last)


import algorithm
type BinaryHeap*[T] = ref object
  data*: seq[T]
  cmp*:proc(x,y:T):int
proc revcmp[T](x,y:T):int = cmp[T](y,x)
proc `[]`[T](heap: BinaryHeap[T], i: Natural): T = heap.data[i]
proc siftdown[T](heap: var BinaryHeap[T], startpos, p: int) =
  var pos = p
  var newitem = heap[pos]
  while pos > startpos:
    let parentpos = (pos - 1) shr 1
    let parent = heap[parentpos]
    if 0 <= heap.cmp(newitem, parent): break
    heap.data[pos] = parent
    pos = parentpos
  heap.data[pos] = newitem
proc siftup[T](heap: var BinaryHeap[T], p: int) =
  let endpos = len(heap)
  var pos = p
  let startpos = pos
  let newitem = heap[pos]
  var childpos = 2*pos + 1
  while childpos < endpos:
    let rightpos = childpos + 1
    if rightpos < endpos and 0 <= heap.cmp(heap[childpos], heap[rightpos]):
      childpos = rightpos
    heap.data[pos] = heap[childpos]
    pos = childpos
    childpos = 2*pos + 1
  heap.data[pos] = newitem
  siftdown(heap, startpos, pos)

proc len*[T](heap: BinaryHeap[T]): int = heap.data.len
proc push*[T](heap: var BinaryHeap[T], item: T) =
  heap.data.add(item)
  siftdown(heap, 0, len(heap)-1)
proc pop*[T](heap: var BinaryHeap[T]): T =
  let lastelt = heap.data.pop()
  if heap.len > 0:
    result = heap[0]
    heap.data[0] = lastelt
    siftup(heap, 0)
  else:
    result = lastelt
proc top*[T](heap: var BinaryHeap[T]): T = heap.data[0]
proc poppush*[T](heap: var BinaryHeap[T], item: T): T =
  result = heap[0]
  heap.data[0] = item
  siftup(heap, 0)
proc pushpop*[T](heap: var BinaryHeap[T], item: T): T =
  if heap.len > 0 and 0 > heap.cmp(heap[0], item):
    swap(item, heap[0])
    siftup(heap, 0)
  return item
proc clear*[T](heap: var BinaryHeap[T]) = heap.data.setLen(0)
proc toSeq*[T](heap: BinaryHeap[T]): seq[T] = heap.data.sorted(cmp)
proc `$`*[T](heap: BinaryHeap[T]): string = $heap.toSeq()
iterator items*[T](heap:BinaryHeap[T]) : T =
  for v in heap.toSeq(): yield v
proc newBinaryHeap*[T](cmp:proc(x,y:T):int) : BinaryHeap[T]=
  new(result)
  result.data = @[]
  result.cmp = cmp

proc solve() : int =
  let n = scan()
  let L = newSeqWith(n,scan()).sorted(cmp)
  var R = @[1]
  for i in 1..<n:
    if L[i-1] == L[i] : R[^1] += 1
    else: R .add 1
  if R.len < 3: return 0
  var q = newBinaryHeap[int](revcmp)
  for r in R: q.push(r)
  while true:
    let a = q.pop() - 1
    let b = q.pop() - 1
    let c = q.pop() - 1
    if a < 0 or b < 0 or c < 0 : return
    result += 1
    q.push(a)
    q.push(b)
    q.push(c)

scan().times: printInt(solve().int32,10.chr)
0