結果

問題 No.873 バイナリ、ヤバいなり!w
ユーザー phsplsphspls
提出日時 2022-12-07 23:37:11
言語 Rust
(1.77.0)
結果
AC  
実行時間 252 ms / 2,000 ms
コード長 1,329 bytes
コンパイル時間 722 ms
コンパイル使用メモリ 159,360 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 03:54:55
合計ジャッジ時間 3,585 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

use std::{collections::BinaryHeap, cmp::Reverse};

const INF: usize = 1usize << 60;

fn priority(val: usize, flg: bool) -> usize {
    if val % 2 == 1 {
        val
    } else if !flg {
        INF - val
    } else {
        INF + val
    }
}

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();

    let mut dp = vec![INF; n+1];
    dp[0] = 0;
    for i in 1..=n {
        for j in 1..=i {
            let val = j * j;
            if val > i { break; }
            dp[i] = dp[i].min(dp[i-val] + j);
        }
    }
    let mut result = String::new();
    let mut target = n;
    while target > 0 {
        let mut pque = BinaryHeap::new();
        let flg = if result.is_empty() || result.ends_with('0') { false } else { true };
        for i in 1..=target {
            let val = i * i;
            if val > target { break; }
            if dp[target-val] + i == dp[target] {
                pque.push((Reverse(priority(i, flg)), i));
            }
        }
        let (_, i) = pque.pop().unwrap();
        let val = if i % 2 == 1 { format!("0{}", "10".repeat(i/2)) } else if flg { "10".repeat(i/2).to_string() } else { "01".repeat(i/2).to_string() };
        result.push_str(&val);
        target -= i * i;
    }
    println!("{}", result);
}
0