結果
問題 |
No.873 バイナリ、ヤバいなり!w
|
ユーザー |
|
提出日時 | 2022-12-07 23:37:11 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 268 ms / 2,000 ms |
コード長 | 1,329 bytes |
コンパイル時間 | 12,450 ms |
コンパイル使用メモリ | 381,160 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-14 02:48:59 |
合計ジャッジ時間 | 15,548 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 36 |
ソースコード
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); }