結果

問題 No.59 鉄道の旅
ユーザー tshigtshig
提出日時 2016-08-19 10:42:43
言語 Ruby
(3.2.2)
結果
AC  
実行時間 1,162 ms / 5,000 ms
コード長 802 bytes
コンパイル時間 233 ms
コンパイル使用メモリ 11,560 KB
実行使用メモリ 33,568 KB
最終ジャッジ日時 2023-08-26 07:12:52
合計ジャッジ時間 4,657 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
15,052 KB
testcase_01 AC 81 ms
15,248 KB
testcase_02 AC 82 ms
15,052 KB
testcase_03 AC 82 ms
15,336 KB
testcase_04 AC 418 ms
30,096 KB
testcase_05 AC 81 ms
15,296 KB
testcase_06 AC 81 ms
15,124 KB
testcase_07 AC 80 ms
15,280 KB
testcase_08 AC 109 ms
16,624 KB
testcase_09 AC 109 ms
16,544 KB
testcase_10 AC 110 ms
16,620 KB
testcase_11 AC 100 ms
16,388 KB
testcase_12 AC 228 ms
32,352 KB
testcase_13 AC 539 ms
32,924 KB
testcase_14 AC 1,162 ms
33,568 KB
testcase_15 AC 83 ms
15,320 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Calc0059
  def initialize(args)
    args = args.map { |l| l.chomp.split(/\s+/) }
    @n, @k = args.shift.map(&:to_i)
    @ws = args.map(&:first).map(&:to_i)
  end

  def run
    stock = []
    @ws.each do |w|
      if w > 0
        if stock.size < @k || stock[-@k] < w
          add(stock, w)
        end
      else
        remove(stock, -w)
      end
    end
    stock.size
  end

  def add(stock, w)
    i = stock.bsearch_index { |x| x >= w }
    if i
      stock.insert(i, w)
    else
      stock.push(w)
    end
  end

  def remove(stock, w)
    i = stock.bsearch_index { |x| x >= w }
    if i
      if stock[i] == w
        stock.delete_at(i)
      end
    else
      if stock[0] == w
        stock.shift
      end
    end
  end
end

puts Calc0059.new(STDIN.readlines).run if __FILE__ == $0
0