結果
問題 |
No.873 バイナリ、ヤバいなり!w
|
ユーザー |
|
提出日時 | 2022-12-13 10:26:59 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 275 ms / 2,000 ms |
コード長 | 1,589 bytes |
コンパイル時間 | 14,054 ms |
コンパイル使用メモリ | 378,856 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-07 06:31:28 |
合計ジャッジ時間 | 17,015 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 36 |
ソースコード
use std::collections::VecDeque; const INF: usize = 1usize << 60; fn priority(a: usize) -> usize { if a % 2 == 1 { a } else { INF - a } } 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.. { let val = j * j; if val > i { break; } dp[i] = dp[i].min(dp[i-val]+j); } } let mut used = vec![]; let mut target = n; while target > 0 { let mut cands = vec![]; for i in 1.. { let val = i * i; if val > target { break; } if dp[target] == dp[target-val]+i { cands.push(i); } } cands.sort_by_key(|&i| priority(i)); let val = cands[0]; used.push(val); target -= val*val; } used.sort_by_key(|&v| priority(v)); let odds = used.iter().filter(|&&v| v%2 == 1).copied().collect::<Vec<_>>(); let mut evens = used.iter().filter(|&&v| v%2 == 0).copied().collect::<VecDeque<_>>(); let mut result = String::new(); for &v in odds.iter() { result.push_str(&format!("0{}", "10".repeat(v/2))); } while !evens.is_empty() { let val = evens.pop_front().unwrap(); result.push_str(&format!("{}", "01".repeat(val/2))); if !evens.is_empty() { result.push_str(&format!("{}", "10".repeat(evens.pop_back().unwrap()/2))); } } println!("{}", result); }