結果

問題 No.332 数列をプレゼントに
ユーザー koba-e964koba-e964
提出日時 2017-01-11 00:51:40
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 848 ms / 2,000 ms
コード長 3,174 bytes
コンパイル時間 13,293 ms
コンパイル使用メモリ 376,836 KB
実行使用メモリ 199,552 KB
最終ジャッジ日時 2024-06-06 16:48:23
合計ジャッジ時間 27,644 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 133 ms
57,856 KB
testcase_06 AC 195 ms
83,072 KB
testcase_07 AC 352 ms
145,024 KB
testcase_08 AC 202 ms
85,632 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 9 ms
5,376 KB
testcase_11 AC 118 ms
51,840 KB
testcase_12 AC 372 ms
158,464 KB
testcase_13 AC 222 ms
96,256 KB
testcase_14 AC 62 ms
27,904 KB
testcase_15 AC 327 ms
141,568 KB
testcase_16 AC 254 ms
107,648 KB
testcase_17 AC 356 ms
148,096 KB
testcase_18 AC 848 ms
148,608 KB
testcase_19 AC 260 ms
103,808 KB
testcase_20 AC 320 ms
138,240 KB
testcase_21 AC 376 ms
162,176 KB
testcase_22 AC 297 ms
128,512 KB
testcase_23 AC 369 ms
155,008 KB
testcase_24 AC 368 ms
155,008 KB
testcase_25 AC 368 ms
155,008 KB
testcase_26 AC 367 ms
155,008 KB
testcase_27 AC 368 ms
155,008 KB
testcase_28 AC 368 ms
155,008 KB
testcase_29 AC 370 ms
154,880 KB
testcase_30 AC 371 ms
155,008 KB
testcase_31 AC 391 ms
155,008 KB
testcase_32 AC 368 ms
155,008 KB
testcase_33 AC 340 ms
142,080 KB
testcase_34 AC 339 ms
137,088 KB
testcase_35 AC 388 ms
136,704 KB
testcase_36 AC 1 ms
5,376 KB
testcase_37 AC 388 ms
162,048 KB
testcase_38 AC 389 ms
162,048 KB
testcase_39 AC 388 ms
162,048 KB
testcase_40 AC 448 ms
199,424 KB
testcase_41 AC 450 ms
199,424 KB
testcase_42 AC 470 ms
199,424 KB
testcase_43 AC 462 ms
199,424 KB
testcase_44 AC 464 ms
199,552 KB
testcase_45 AC 459 ms
195,328 KB
testcase_46 AC 86 ms
38,784 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