結果

問題 No.59 鉄道の旅
ユーザー hoktohokto
提出日時 2020-03-03 09:00:06
言語 Ruby
(3.2.2)
結果
RE  
実行時間 -
コード長 1,483 bytes
コンパイル時間 359 ms
コンパイル使用メモリ 11,604 KB
実行使用メモリ 17,172 KB
最終ジャッジ日時 2023-08-04 00:39:09
合計ジャッジ時間 3,182 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
16,880 KB
testcase_01 AC 96 ms
16,672 KB
testcase_02 AC 95 ms
16,724 KB
testcase_03 AC 96 ms
16,740 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 108 ms
16,728 KB
testcase_12 AC 192 ms
16,716 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def get_i() #空白区切の入力を数値(整数)の配列で返す
  return gets.chomp.split(" ").map(&:to_i)
end
def get_f() #空白区切の入力を数値(実数)の配列で返す
  return gets.chomp.split(" ").map(&:to_f)
end
def get() #空白区切の入力を文字列の配列で返す
  return gets.chomp.split(" ")
end
def get_nsp() #入力されたものを一文字ずつに区切った文字列の配列で返す
  return gets.chomp.split("")
end
def yn_judge(bool,y="Yes",n="No") #boolに真偽を投げることで、trueならy、falseならnの値を出力する
  return bool ? y : n 
end
def array(size,n=1,init=nil) #nに配列の次元数、sizeに配列の大きさ、initに初期値を投げることでその配列を返す
  if n==1
    return Array.new(size){init}
  else
    return Array.new(n).map{Array.new(size){init}}
  end
end

class BIT
  attr_accessor:node,:n
  def initialize(n)
    @n=n
    @node=array(@n+1,1,0)
  end

  def add(i,x)
    while i<=@n
      @node[i]+=x
      i|=i+1
    end
  end

  def sum(i)
    s=0
    while i>0
      s+=@node[i]
      i=(i&(i+1))-1
    end
    return s
  end

  def getSum(x,y)
    return sum(y)-sum(x-1)
  end
end

n,k=get_i
bit=BIT.new(100_000)
weight=array(100_000+1,1,0)
n.times do|i|
  w=gets.to_i
  if w>0
    if bit.getSum(w,100_000)<k
      bit.add(w,1)
      weight[w]+=1
    end
  else
    if weight[w.abs]>0
      weight[w.abs]-=1
      bit.add(w.abs,-1)
    end
  end
end
puts bit.sum(100_000)
0