結果

問題 No.1021 Children in Classrooms
ユーザー conf8o
提出日時 2020-04-16 16:46:55
言語 Swift
(6.0.3)
結果
AC  
実行時間 228 ms / 2,000 ms
コード長 2,338 bytes
コンパイル時間 1,834 ms
コンパイル使用メモリ 189,184 KB
実行使用メモリ 36,120 KB
最終ジャッジ日時 2024-11-30 17:28:09
合計ジャッジ時間 5,385 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.swift:86:9: warning: initialization of immutable value 'M' was never used; consider replacing with assignment to '_' or removing it
    let M = line[1]
    ~~~~^
    _

ソースコード

diff #

import Foundation

public struct Deque<T> {
  private var array: [T?]
  private var head: Int
  private var capacity: Int
  private let originalCapacity: Int

  public init(_ capacity: Int = 10) {
    self.capacity = max(capacity, 1)
    originalCapacity = self.capacity
    array = [T?](repeating: nil, count: capacity)
    head = capacity
  }

  public var isEmpty: Bool {
    return count == 0
  }

  public var count: Int {
    return array.count - head
  }

  public mutating func enqueue(_ element: T) {
    array.append(element)
  }

  public mutating func enqueueFront(_ element: T) {
    if head == 0 {
      capacity *= 2
      let emptySpace = [T?](repeating: nil, count: capacity)
      array.insert(contentsOf: emptySpace, at: 0)
      head = capacity
    }

    head -= 1
    array[head] = element
  }

  public mutating func dequeue() -> T? {
    guard head < array.count, let element = array[head] else { return nil }

    array[head] = nil
    head += 1

    if capacity >= originalCapacity && head >= capacity*2 {
      let amountToRemove = capacity + capacity/2
      array.removeFirst(amountToRemove)
      head -= amountToRemove
      capacity /= 2
    }
    return element
  }

  public mutating func dequeueBack() -> T? {
    if isEmpty {
      return nil
    } else {
      return array.removeLast()
    }
  }

  public func peekFront() -> T? {
    if isEmpty {
      return nil
    } else {
      return array[head]
    }
  }

  public func peekBack() -> T? {
    if isEmpty {
      return nil
    } else {
      return array.last!
    }
  }
}
func readInt() -> [Int] {
    return readLine()!.split(separator: " ").map { c in Int(c)! }
}

func main() {
    let line = readInt()
    let N = line[0]
    let M = line[1]
    let students = readInt()
    var deque = Deque<Int>(N)
    for s in students {
        deque.enqueue(s)
    }
    let S = readLine()!

    for s in S {
        if s == "L" {
            let a = deque.dequeue()!
            let b = deque.dequeue()!
            deque.enqueueFront(a+b)
            deque.enqueue(0)
        } else {
            let a = deque.dequeueBack()!
            let b = deque.dequeueBack()!
            deque.enqueue(a+b)
            deque.enqueueFront(0)
        }
    }

    for _ in 1...N {
        print(deque.dequeue()!, terminator: " ")
    }
    print()
}

main()
0