結果
問題 | No.33 アメーバがたくさん |
ユーザー |
|
提出日時 | 2020-02-08 22:16:27 |
言語 | Crystal (1.14.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 2,272 bytes |
コンパイル時間 | 11,839 ms |
コンパイル使用メモリ | 296,676 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-30 19:59:24 |
合計ジャッジ時間 | 12,598 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 11 |
ソースコード
struct Range(B, E)#Perform binary search#Based on Marc-André Lafortune's Ruby backports implementation, ported by @cielavenirdef bsearch#return to_enum(:bsearch) unless block_given?from = self.beginto = self.endunless from.is_a?(Int) && to.is_a?(Int)# Float support is currently droppedraise "can't do binary search for #{from.class}"endmidpoint = 0 # placeholder#convert = ->{ (pointerof(midpoint) as Pointer(Int64)).value }convert = ->{ midpoint }to -= 1 if excludes_end?satisfied = nilwhile from <= tomidpoint = (from + to)//2result = yield(cur = convert.call)case resultwhen Intreturn cur if result == 0result = result < 0when Floatreturn cur if result == 0result = result < 0when truesatisfied = curwhen nil, false# nothing to doelseraise "wrong argument type #{result.class} (must be numeric, true, false or nil)"endif resultto = midpoint - 1elsefrom = midpoint + 1endendsatisfiedendendclass Array(T)def bsearchidx=(0...self.size).bsearch{|i|yield self[i]}idx ? self[idx] : nilenddef bsearch_index(0...self.size).bsearch{|i|yield self[i]}endenddef checkio(data)result=0se=[] of Tuple(Int64,Int64)data.each{|e|l,r=eright_idx=(0...se.size).bsearch{|i|x={l,0}<=>se[i]x<=0}||se.size #l <= se[right][0]if right_idx>0left_idx=right_idx-1if l<=se[left_idx][1]+1 # overlap with leftl=se[left_idx][0]r=[r,se[left_idx][1]].maxresult-=se[left_idx][1]-se[left_idx][0]+1se.delete_at(left_idx)right_idx-=1endendwhile right_idx<se.size && se[right_idx][0]<=r # overlap with rightr=[r,se[right_idx][1]].maxresult-=se[right_idx][1]-se[right_idx][0]+1se.delete_at(right_idx)endresult+=r-l+1se.insert(right_idx,{l,r})}return resultenddic={} of Int64 => Array(Tuple(Int64,Int64))n,d,t=gets.not_nil!.split.map(&.to_i64)gets.not_nil!.split.map(&.to_i64).each{|e|mod=e%ddic[mod]||=[] of Tuple(Int64,Int64)dic[mod]<<{(e-mod)//d-t,(e-mod)//d+t}}r=0_i64dic.each{|k,v|r+=checkio(v)}puts r