結果

問題 No.332 数列をプレゼントに
ユーザー koba-e964koba-e964
提出日時 2017-01-11 00:51:40
言語 Rust
(1.77.0)
結果
AC  
実行時間 962 ms / 2,000 ms
コード長 3,174 bytes
コンパイル時間 2,180 ms
コンパイル使用メモリ 152,940 KB
実行使用メモリ 199,560 KB
最終ジャッジ日時 2023-08-25 22:15:44
合計ジャッジ時間 21,853 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 170 ms
57,844 KB
testcase_06 AC 247 ms
83,284 KB
testcase_07 AC 439 ms
144,904 KB
testcase_08 AC 254 ms
85,908 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 10 ms
5,096 KB
testcase_11 AC 152 ms
51,872 KB
testcase_12 AC 476 ms
158,560 KB
testcase_13 AC 286 ms
96,120 KB
testcase_14 AC 80 ms
27,872 KB
testcase_15 AC 422 ms
141,544 KB
testcase_16 AC 321 ms
107,628 KB
testcase_17 AC 446 ms
148,068 KB
testcase_18 AC 962 ms
148,756 KB
testcase_19 AC 324 ms
103,740 KB
testcase_20 AC 416 ms
138,340 KB
testcase_21 AC 486 ms
161,980 KB
testcase_22 AC 386 ms
128,644 KB
testcase_23 AC 467 ms
154,908 KB
testcase_24 AC 469 ms
154,860 KB
testcase_25 AC 465 ms
154,856 KB
testcase_26 AC 466 ms
154,856 KB
testcase_27 AC 468 ms
154,888 KB
testcase_28 AC 466 ms
154,892 KB
testcase_29 AC 465 ms
154,900 KB
testcase_30 AC 468 ms
154,904 KB
testcase_31 AC 464 ms
154,860 KB
testcase_32 AC 469 ms
154,836 KB
testcase_33 AC 428 ms
142,028 KB
testcase_34 AC 424 ms
137,080 KB
testcase_35 AC 473 ms
136,644 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 489 ms
161,928 KB
testcase_38 AC 486 ms
161,980 KB
testcase_39 AC 487 ms
161,948 KB
testcase_40 AC 597 ms
199,560 KB
testcase_41 AC 594 ms
199,536 KB
testcase_42 AC 596 ms
199,552 KB
testcase_43 AC 595 ms
199,552 KB
testcase_44 AC 591 ms
199,484 KB
testcase_45 AC 584 ms
195,336 KB
testcase_46 AC 112 ms
38,836 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 main() {
    let n: usize = get();
    let x: i64 = get();
    let a: Vec<i64> = (0 .. n).map(|_| get()).collect();
    const THRESHOLD: i64 = 20000;
    let giants: Vec<(i64, usize)> =
        (0 .. n).map(|i| (a[i], i)).filter(|&(x, _)| x >= THRESHOLD).collect();
    let babies: Vec<(usize, usize)> =
        (0 .. n).map(|i| (a[i], i)).filter(|&(x, _)| x < THRESHOLD)
        .map(|(x, y)| (x as usize, y)).collect();
    let m = giants.len();
    let p = babies.len();
    // process giants
    assert!(m <= 23); // 20000^24 > 10^10000
    let mut tbl = vec![-1_i64; 1 << m];
    for bits in 0 .. (1 << m) {
        let mut tmp = 0;
        for i in 0 .. m {
            if (bits & (1 << i)) != 0 {
                tmp += giants[i].0;
            }
        }
        tbl[bits] = tmp;
    }
    // process babies
    let maxw = p * THRESHOLD as usize + 1;
    let mut dp = vec![vec![false; maxw]; p + 1];
    dp[0][0] = true;
    for i in 0 .. p {
        for j in 0 .. maxw {
            let mut res = dp[i][j];
            if j >= babies[i].0 {
                res |= dp[i][j - babies[i].0];
            }
            dp[i + 1][j] = res;
        }
    }
    let mut giants_ans = None;
    let mut babies_ans = 0;
    for bits in 0 .. (1 << m) {
        let diff = x - tbl[bits];
        if 0 <= diff && diff < maxw as i64 && dp[p][diff as usize] {
            giants_ans = Some(bits);
            babies_ans = diff as usize;
            break;
        }
    }
    match giants_ans {
        None => println!("No"),
        Some(gg) => {
            let mut ans = vec![false; n];
            for i in 0 .. m {
                if (gg & (1 << i)) != 0 {
                    ans[giants[i].1] = true;
                }
            }
            // Subset recovery
            let mut rem = babies_ans;
            for i in (0 .. p).rev() {
                let cur = babies[i].0;
                if rem >= cur && dp[i][rem - cur] {
                    rem -= cur;
                    ans[babies[i].1] = true;
                }
            }
            assert_eq!(rem, 0);
            for i in 0 .. n {
                print!("{}", if ans[i] { "o" } else { "x" });
            }
            println!();
        }
    }
}
0