Skip to content

Instantly share code, notes, and snippets.

@dolpen
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save dolpen/8232783be47bffb6cfc4 to your computer and use it in GitHub Desktop.

Select an option

Save dolpen/8232783be47bffb6cfc4 to your computer and use it in GitHub Desktop.
# https://paiza.jp/poh/kirishima
# 入力処理
@vendor_employees = Array.new
@vendor_cost = Array.new
m = gets.to_i
n = gets.to_i
while line = gets
l = line.chomp.split(" ")
@vendor_employees << l[0].to_i
@vendor_cost <<l[1].to_i
end
# fill by Fixnum::MAX
dp = Array.new(m + 1)
dp.fill(4611686018427387903)
dp[0] = 0
for i in 0..(n-1)
for k in (m-1).downto(0)
next if dp[k] == 4611686018427387903
j = [m, k + @vendor_employees[i]].min
dp[j] = [dp[j], dp[k] + @vendor_cost[i]].min;
end
end
print dp[m], "\n"
object Main extends App {
import java.util._
val sc = new Scanner(System.in)
def ni = sc.nextInt
val m, n = ni
val vendor = Array.fill(n)((ni, ni)) // tuple2(employees,cost)
val dp = Array.fill(m + 1)(Int.MaxValue)
dp(0) = 0
for (i <- 0 until n; k <- m - 1 to 0 by -1 if dp(k) < Int.MaxValue) {
val j = math.min(m, k + vendor(i)._1)
dp(j) = math.min(dp(j), dp(k) + vendor(i)._2)
}
println(dp(m))
}

絶対に巻き込まれたりしない

なにがあったのか

https://paiza.jp/poh/kirishima/

なにをやったのか

あなたとJava

詳細

  • chokudai氏の「ダイナミック・プログラミン」しか頭に浮かばなかった
  • 一次元DPでよいです
import java.util.*;

public class Main {
        static void solve() throws Exception {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int[] q = new int[n];
        int[] r = new int[n];
        for (int i = 0; i < n; i++) {
            q[i] = sc.nextInt();
            r[i] = sc.nextInt();
        }
        // dp[k] : k人集めたときに一番安い組み合わせ
        int[] dp = new int[m + 1]; 
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        // 各業者、各セル
        for (int i = 0; i < n; i++) {
            for (int k = m - 1; k >= 0; k--) {
                if (dp[k] == Integer.MAX_VALUE) continue;
                // その業者を使って dp[j] が安く出来るなら更新
                int j = Math.min(m, k + q[i]);
                dp[j] = Math.min(dp[j], dp[k] + r[i]);
            }
        }
        // 全業者に対して試行後、最終版の dp[m] が答えになる
        System.out.println(dp[m]);
    }

    public static void main(String[] args) throws Exception {
        solve();
    }
}

結果 : https://paiza.jp/poh/kirishima/result/6728ece1f8ddf7ec3d862e3059bfb67f

さよならScanner

BufferedReader最高だと思ったが、uwi氏が作っていたbyte[]から直接読む奴を作ってみようと思った。

    static InputStream is;
    private static byte[] _ib = new byte[1024];
    static int _p = 0, _l = 0;

    // read one byte
    private static int _rb() throws Exception {
        if (_l < 0) return -1;
        if (_p >= _l) {
            _p = 0;
            _l = is.read(_ib);
            if (_l <= 0) return -1;
        }
        return _ib[_p++];
    }

    // positive integer
    private static int _psi() throws Exception {
        int num = 0, b;
        while ((b = _rb()) > 0 && !(b >= '0' && b <= '9')) ;
        do {
            num = num * 10 + (b - '0');
        } while ((b = _rb()) > 0 && b >= '0' && b <= '9');
        return num;
    }

結果:http://paiza.jp/poh/kirishima/result/4863aa05872a6dae7354ef0bf6836f4d
全ケースで最速+-10msに収まる。Case6で最速塗り替えることが出来たけどまあ誤差っぽいので妥当な解法っぽい。

そして最速へ

    static InputStream is;
    private static byte[] _ib = new byte[1024];
    static int _p = 0, _l = 0;

    private static void readAll() throws Exception {
        _p = 0;
        _l = is.read(_ib);
    }

    // positive integer
    private static int _psi() throws Exception {
        int num = 0, b;
        while ((b = _ib[_p++]) > 0 && !(b >= '0' && b <= '9')) ;
        do {
            num = num * 10 + (b - '0');
        } while ((b = _ib[_p++]) > 0 && b >= '0' && b <= '9');
        return num;
    }

こんな感じで readAll() を初回に1度だけ呼ぶことで、深い関数呼び出しを回避して、
スタック生成/除去のタイムラグを減らす。

結果 : http://paiza.jp/poh/kirishima/result/8e66e51376db1407bb1fbf10c2c32dfe
最大ケースで最速タイ。

そんなことよりその消火器で火村を殴ってこの世から退場させろ

後ろの席で同僚が言語最速出してた

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment