paiza POH Vol.2 木野さん攻略
解答
#!/usr/bin/env ruby file = $stdin lines = file.readlines file.close H, W = lines[0].split.map{|x| x.to_i} screen = lines[1, H].map{|x| x.strip}.join N = lines[H + 1].to_i widgets = lines[H + 2, N].each_with_index.map{|x, i| x.split.map{|y| y.to_i} + [i]} sorted_widgets = {} for widget_h, widget_w, index in widgets if not sorted_widgets.has_key?(widget_h) sorted_widgets[widget_h] = [] end sorted_widgets[widget_h] += [[widget_h, widget_w, index]] end count_map = Array.new(W * H, 0) results = Array.new(N, 0) (0..H - 1).reverse_each do |y| (0..W - 1).reverse_each do |x| pos = y * W + x sign = count_map[pos] = screen[pos] == "0" ? +1 : -1 if x < W - 1 and count_map[pos + 1] * sign > 0 count_map[pos] += count_map[pos + 1] end end end def get_left_most(x, y, n, count_map) while x < W left_min = (0..n - 1).map{|i| count_map[(y + i) * W + x]}.min if left_min > 0 return x, left_min end x += -left_min end return x, 0 end sorted_widgets.each{|widget_h, values| for y in 0..H - widget_h x = 0 while x < W new_x, width = get_left_most(x, y, widget_h, count_map) if new_x == W break end for widget_h, widget_w, i in values if width >= widget_w results[i] += width - widget_w + 1 end end x = new_x + width end end } puts results
コードの覚え書き
解説を読んでのメモ
- 定石を知らなかった。累積和を使って書き直す必要がある。
paiza POH Vol.1 野田さん攻略
解答
#!/usr/bin/env ruby file = $stdin lines = file.readlines file.close N, D = lines[0].split.map{|x| x.to_i} p = lines[1, N].map{|x| x.to_i} m = lines[N + 1, D].map{|x| x.to_i} p.sort! def calc(th, p) i = 0 j = p.length - 1 max_total = 0 while i < j total = p[i] + p[j] if total == th return total elsif total < th max_total = [max_total, total].max i += 1 elsif total > th j -= 1 end end return max_total end results = [] for th in m p_selected = p.select {|x| x <= th - p.first} results += [calc(th, p_selected)] end puts results
コードの覚え書き
- 必ず 2 つの商品を購入してその合計金額ということだったので、商品をソートして並べて、2 つのマーカーを移動させて探索する。
- キャンペーン数でループ
- マーカーを移動させて探索
- 最高額(最右) p[j] と最低額(最左) p[i] の組み合わせで、すでにキャンペーン金額 (th) を越えているのなら、高額商品 p[j] を下げるしかない。
- キャンペーン金額内の最高額の商品 p[j] が確定したら、低額商品 p[i] の方を目一杯高額なものを選択する。
- もしかしたら高額商品を別のを選択することで、合計金額はより高いものがあるかもしれない。
- しかし、その場合は低額商品 p[j] はより高いものになる。
- なぜならば過去に p[i] + p[j] <= th が成立しているならば、単に高額側の商品を下げた p[i] + p[j - 1] は (ソートしてあるため) p[i] + p[j - 1] < p[i] + p[j] <= th が成立して最大候補ではない。
- マーカーを移動させて探索
- 計算量
- キャンペーン日数 D のループ * 商品点数 N の移動量 = O(DN + 事前ソート)
Google chrome で1年以内の検索をデフォルトにする
{google:baseURL}search?q=%s&tbs=qdr:y&{google:RLZ}{google:originalQueryForSuggestion}{google:assistedQueryStats}{google:searchFieldtrialParameter}{google:bookmarkBarPinned}{google:searchClient}{google:sourceId}{google:instantExtendedEnabledParameter}{google:omniboxStartMarginParameter}ie={inputEncoding}
元のやつに tbs=qdr:y を追加する。
tar で tar: Error exit delayed from previous errors
$ tar ... &> log.txt
などとして、"tar:" で検索すれば分かるが、大概はパーミッションの問題でアクセスできないファイルやフォルダが含まれている。ファイルならば r、フォルダならば x もついているかも確認する。
$ sudo chown -R user:group * $ chmod -R oug+r * $ find . -type d -print0 | xargs -0 chmod +x
Moutain Lion 10.8.3 から VoodooBattery が動かない
- http://forum.thinkpads.com/viewtopic.php?f=32&t=105334
- AppleSmartBattery.kext と VoodooBattery.kext を削除
- 修正された AppleSmartBattery をインストール