結果

問題 No.1021 Children in Classrooms
ユーザー conf8oconf8o
提出日時 2020-04-16 18:27:42
言語 Swift
(5.10.0)
結果
RE  
実行時間 -
コード長 2,331 bytes
コンパイル時間 2,423 ms
コンパイル使用メモリ 193,776 KB
実行使用メモリ 33,372 KB
最終ジャッジ日時 2024-05-07 21:09:48
合計ジャッジ時間 7,133 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
15,744 KB
testcase_01 RE -
testcase_02 AC 11 ms
15,360 KB
testcase_03 AC 12 ms
15,616 KB
testcase_04 AC 12 ms
15,744 KB
testcase_05 AC 12 ms
15,744 KB
testcase_06 AC 11 ms
15,616 KB
testcase_07 AC 12 ms
15,744 KB
testcase_08 AC 13 ms
16,000 KB
testcase_09 AC 269 ms
28,796 KB
testcase_10 AC 257 ms
29,280 KB
testcase_11 AC 251 ms
28,140 KB
testcase_12 AC 251 ms
30,848 KB
testcase_13 AC 252 ms
29,688 KB
testcase_14 AC 256 ms
29,836 KB
testcase_15 AC 190 ms
28,820 KB
testcase_16 AC 217 ms
33,372 KB
testcase_17 AC 203 ms
28,696 KB
testcase_18 AC 276 ms
29,124 KB
testcase_19 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.swift:10: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

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]
    var deque = Deque<Int>(N)
    for i in readInt() {
        deque.enqueue(i)
    }
    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()
}

public struct Deque<T> {
  private var array: ArraySlice<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 = ArraySlice<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!
    }
  }
}

main()
0