結果

問題 No.288 貯金箱の仕事
ユーザー koba-e964koba-e964
提出日時 2017-01-10 23:19:16
言語 Rust
(1.77.0)
結果
AC  
実行時間 331 ms / 2,000 ms
コード長 2,338 bytes
コンパイル時間 4,246 ms
コンパイル使用メモリ 135,968 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-22 22:26:26
合計ジャッジ時間 9,509 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
4,380 KB
testcase_01 AC 4 ms
4,376 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 35 ms
4,376 KB
testcase_08 AC 12 ms
4,376 KB
testcase_09 AC 28 ms
4,380 KB
testcase_10 AC 31 ms
4,384 KB
testcase_11 AC 15 ms
4,376 KB
testcase_12 AC 28 ms
4,380 KB
testcase_13 AC 23 ms
4,380 KB
testcase_14 AC 27 ms
4,376 KB
testcase_15 AC 33 ms
4,376 KB
testcase_16 AC 29 ms
4,376 KB
testcase_17 AC 22 ms
4,376 KB
testcase_18 AC 16 ms
4,380 KB
testcase_19 AC 19 ms
4,380 KB
testcase_20 AC 13 ms
4,376 KB
testcase_21 AC 15 ms
4,380 KB
testcase_22 AC 27 ms
4,380 KB
testcase_23 AC 331 ms
4,376 KB
testcase_24 AC 168 ms
4,380 KB
testcase_25 AC 4 ms
4,380 KB
testcase_26 AC 4 ms
4,376 KB
testcase_27 AC 4 ms
4,380 KB
testcase_28 AC 4 ms
4,376 KB
testcase_29 AC 4 ms
4,376 KB
testcase_30 AC 86 ms
4,376 KB
testcase_31 AC 87 ms
4,376 KB
testcase_32 AC 170 ms
4,380 KB
testcase_33 AC 169 ms
4,380 KB
testcase_34 AC 251 ms
4,380 KB
testcase_35 AC 250 ms
4,376 KB
testcase_36 AC 328 ms
4,376 KB
testcase_37 AC 95 ms
4,376 KB
testcase_38 AC 228 ms
4,380 KB
testcase_39 AC 112 ms
4,380 KB
testcase_40 AC 115 ms
4,376 KB
testcase_41 AC 318 ms
4,376 KB
testcase_42 AC 278 ms
4,376 KB
testcase_43 AC 196 ms
4,380 KB
testcase_44 AC 137 ms
4,376 KB
testcase_45 AC 257 ms
4,376 KB
testcase_46 AC 276 ms
4,376 KB
testcase_47 AC 185 ms
4,380 KB
testcase_48 AC 202 ms
4,380 KB
testcase_49 AC 122 ms
4,376 KB
testcase_50 AC 160 ms
4,380 KB
testcase_51 AC 116 ms
4,376 KB
testcase_52 AC 164 ms
4,376 KB
testcase_53 AC 214 ms
4,376 KB
testcase_54 AC 148 ms
4,376 KB
testcase_55 AC 4 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::*;
#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok();
    return ret;
}
fn get_word() -> String {
    let mut stdin = std::io::stdin();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.is_err() || res.ok().unwrap() == 0 || u8b[0] <= ' ' as u8 {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = std::string::String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}
fn parse<T: std::str::FromStr>(s: &str) -> T { s.parse::<T>().ok().unwrap() }

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { parse(&get_word()) }


fn solve(a: &[i64], v: i64) -> Option<i64> {
    if v < 0 {
        return None;
    }
    let n = a.len();
    const W: usize = 250000;
    const INF: i32 = 0x3fff_ffff;
    let mut dp: Vec<Vec<i32>> = vec![vec![INF; W]; 2];
    dp[0][0] = 0;
    for i in 0 .. n {
        let t = i % 2;
        for j in 0 .. W {
            dp[1 - t][j] = INF;
        }
        let cur = a[i] as usize;
        for j in 0 .. W {
            let mut mi = dp[t][j];
            if j >= cur {
                mi = min(mi, dp[1 - t][j - cur] + 1);
            }
            dp[1 - t][j] = mi;
        }
    }
    if v < W as i64 {
        let tmp = dp[n % 2][v as usize];
        if tmp >= INF {
            return None;
        }
        return Some(tmp as i64);
    }
    let biggest = a[n - 1] as i64;
    let diff = (v + 1 - W as i64 + biggest - 1) / biggest * biggest;
    assert!(v - diff < W as i64);
    let tmp = dp[n % 2][(v - diff) as usize];
    if tmp >= INF {
        return None;
    }
    return Some(tmp as i64 + diff / biggest);
}


fn main() {
    let n: usize = get();
    let m: i64 = get();
    let a: Vec<i64> = (0 .. n).map(|_| get()).collect();
    let k: Vec<i64> = (0 .. n).map(|_| get()).collect();
    let mut diff: i64 = 0;
    for i in 0 .. n {
        diff += a[i] * k[i];
    }
    diff -= m;
    let result = solve(&a, diff);
    println!("{}", result.unwrap_or(-1));
}
0