結果

問題 No.1017 Reiwa Sequence
ユーザー fukafukatani
提出日時 2020-04-04 10:49:18
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 482 ms / 2,000 ms
コード長 2,325 bytes
コンパイル時間 11,397 ms
コンパイル使用メモリ 402,084 KB
実行使用メモリ 213,552 KB
最終ジャッジ日時 2024-07-03 07:05:14
合計ジャッジ時間 55,208 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn get_idx(x: i64) -> usize {
    (x + 11 * 150000) as usize
}

fn main() {
    let n = read::<usize>();
    let a = read_vec::<i64>();
    let a = a.into_iter().take(22).collect::<Vec<_>>();

    let mut dp = vec![None; 2 * 11 * 150000 + 1];

    for i in 0..a.len() {
        let mut added = vec![];
        for prev in -11 * 150000..11 * 150000 + 1 {
            if dp[get_idx(prev)] == None {
                continue;
            }
            let cand = prev + a[i];
            if cand < 11 * 150000 && dp[get_idx(cand)] == None {
                added.push((get_idx(cand), Some(prev), 1));
            }
            let cand = prev - a[i];
            if cand > (-11 * 150000) && dp[get_idx(cand)] == None {
                added.push((get_idx(cand), Some(prev), -1));
            }
        }
        for (idx, prev, sign) in added {
            dp[idx] = Some((prev, i, sign));
        }
        if dp[get_idx(a[i])] == None {
            dp[get_idx(a[i])] = Some((None, i, 1));
        }
        if dp[get_idx(-a[i])] == None {
            dp[get_idx(-a[i])] = Some((None, i, -1));
        }
    }

    if dp[get_idx(0)] == None {
        println!("No");
    } else {
        println!("Yes");
        let mut answers = vec![0i64; n];
        let mut cur = 0;
        loop {
            let (prev, i, sign) = dp[get_idx(cur)].unwrap();
            answers[i] = a[i] * sign;
            if let Some(p) = prev {
                cur = p;
            } else {
                break;
            }
        }
        for i in 0..n - 1 {
            print!("{} ", answers[i]);
        }
        println!("{}", answers[n - 1]);
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
0