結果

問題 No.873 バイナリ、ヤバいなり!w
ユーザー Yukino DX.Yukino DX.
提出日時 2023-12-10 15:06:18
言語 Rust
(1.77.0)
結果
AC  
実行時間 248 ms / 2,000 ms
コード長 4,055 bytes
コンパイル時間 1,658 ms
コンパイル使用メモリ 189,952 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-12-10 15:06:24
合計ジャッジ時間 4,997 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
6,676 KB
testcase_01 AC 50 ms
6,676 KB
testcase_02 AC 226 ms
6,676 KB
testcase_03 AC 199 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 1 ms
6,676 KB
testcase_07 AC 1 ms
6,676 KB
testcase_08 AC 45 ms
6,676 KB
testcase_09 AC 1 ms
6,676 KB
testcase_10 AC 1 ms
6,676 KB
testcase_11 AC 1 ms
6,676 KB
testcase_12 AC 12 ms
6,676 KB
testcase_13 AC 1 ms
6,676 KB
testcase_14 AC 1 ms
6,676 KB
testcase_15 AC 1 ms
6,676 KB
testcase_16 AC 1 ms
6,676 KB
testcase_17 AC 1 ms
6,676 KB
testcase_18 AC 1 ms
6,676 KB
testcase_19 AC 1 ms
6,676 KB
testcase_20 AC 1 ms
6,676 KB
testcase_21 AC 0 ms
6,676 KB
testcase_22 AC 1 ms
6,676 KB
testcase_23 AC 0 ms
6,676 KB
testcase_24 AC 1 ms
6,676 KB
testcase_25 AC 1 ms
6,676 KB
testcase_26 AC 1 ms
6,676 KB
testcase_27 AC 1 ms
6,676 KB
testcase_28 AC 0 ms
6,676 KB
testcase_29 AC 1 ms
6,676 KB
testcase_30 AC 1 ms
6,676 KB
testcase_31 AC 1 ms
6,676 KB
testcase_32 AC 6 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 26 ms
6,676 KB
testcase_35 AC 235 ms
6,676 KB
testcase_36 AC 118 ms
6,676 KB
testcase_37 AC 248 ms
6,676 KB
testcase_38 AC 235 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case)]
#![allow(unused_imports)]
//use itertools::Itertools;
// use proconio::input;
// use proconio::marker::*;
use std::cmp::Reverse;
use std::collections::*;

fn main() {
    input! {
        n:usize,
    }
    const INF: usize = std::usize::MAX;

    let sqs = (1..).take_while(|&i| i * i <= n).collect::<Vec<_>>();
    let mut dp = vec![INF; n + 1];
    dp[0] = 0;
    for i in 0..n {
        if dp[i] == INF {
            continue;
        }

        for &sq in sqs.iter() {
            if i + sq * sq > n {
                break;
            }
            dp[i + sq * sq] = dp[i + sq * sq].min(dp[i] + sq);
        }
    }

    let mut lss = vec![];
    let mut q = VecDeque::new();
    q.push_back((vec![], n));
    while let Some((mut ls, yaba)) = q.pop_front() {
        if yaba == 0 {
            lss.push(ls.clone());
        }

        let prv = *ls.last().unwrap_or(&0);
        for &sq in sqs.iter() {
            if yaba < sq * sq {
                break;
            }
            if sq < prv {
                continue;
            }

            if dp[yaba - sq * sq] + sq == dp[yaba] {
                ls.push(sq);
                q.push_back((ls.clone(), yaba - sq * sq));
                ls.pop();
            }
        }
    }

    let mut ans = vec![];
    for mut ls in lss {
        ls.sort();
        let mut odd = ls
            .iter()
            .filter(|l| **l % 2 != 0)
            .copied()
            .collect::<VecDeque<_>>();
        let mut even = ls
            .iter()
            .filter(|l| **l % 2 == 0)
            .copied()
            .collect::<VecDeque<_>>();

        let mut bin = vec![0];
        while !odd.is_empty() || !even.is_empty() {
            let last = *bin.last().unwrap();
            if last == 0 {
                if let Some(l) = odd.pop_front() {
                    bin.append(&mut make(0, l));
                } else {
                    let l = even.pop_back().unwrap();
                    bin.append(&mut make(0, l));
                }
            } else {
                if let Some(l) = even.pop_front() {
                    bin.append(&mut make(1, l));
                } else {
                    let l = odd.pop_back().unwrap();
                    bin.append(&mut make(1, l));
                }
            }
        }

        bin.remove(0);
        ans = if ans.is_empty() { bin } else { ans.min(bin) }
    }

    for ans in ans {
        print!("{}", ans);
    }
    println!();
}

fn make(mut top: usize, l: usize) -> Vec<usize> {
    let mut res = vec![];
    for _ in 0..l {
        res.push(top);
        top = 1 - top;
    }

    res
}

mod input {
    #[macro_export]
    macro_rules! input {
        (source = $s:expr, $($r:tt)*) => {
            let mut iter = $s.split_whitespace();
            input_inner!{iter, $($r)*}
        };
        ($($r:tt)*) => {
            let s = {
                use std::io::Read;
                let mut s = String::new();
                std::io::stdin().read_to_string(&mut s).unwrap();
                s
            };
            let mut iter = s.split_whitespace();
            input_inner!{iter, $($r)*}
        };
    }

    #[macro_export]
    macro_rules! input_inner {
        ($iter:expr) => {};
        ($iter:expr, ) => {};

        ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
            let $var = read_value!($iter, $t);
            input_inner!{$iter $($r)*}
        };
    }

    #[macro_export]
    macro_rules! read_value {
        ($iter:expr, ( $($t:tt),* )) => {
            ( $(read_value!($iter, $t)),* )
        };

        ($iter:expr, [ $t:tt ; $len:expr ]) => {
            (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        };

        ($iter:expr, chars) => {
            read_value!($iter, String).chars().collect::<Vec<char>>()
        };

        ($iter:expr, usize1) => {
            read_value!($iter, usize) - 1
        };

        ($iter:expr, $t:ty) => {
            $iter.next().unwrap().parse::<$t>().expect("Parse error")
        };
    }
}
0