結果

問題 No.1296 OR or NOR
ユーザー akakimidoriakakimidori
提出日時 2020-11-20 23:14:24
言語 Rust
(1.77.0)
結果
AC  
実行時間 627 ms / 3,000 ms
コード長 3,714 bytes
コンパイル時間 2,584 ms
コンパイル使用メモリ 156,552 KB
実行使用メモリ 13,276 KB
最終ジャッジ日時 2023-09-30 19:59:50
合計ジャッジ時間 18,022 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 347 ms
9,184 KB
testcase_03 AC 342 ms
9,172 KB
testcase_04 AC 442 ms
13,264 KB
testcase_05 AC 427 ms
13,276 KB
testcase_06 AC 423 ms
13,268 KB
testcase_07 AC 407 ms
13,172 KB
testcase_08 AC 407 ms
13,252 KB
testcase_09 AC 374 ms
13,268 KB
testcase_10 AC 365 ms
13,264 KB
testcase_11 AC 345 ms
9,180 KB
testcase_12 AC 333 ms
13,248 KB
testcase_13 AC 388 ms
13,256 KB
testcase_14 AC 522 ms
13,200 KB
testcase_15 AC 242 ms
13,260 KB
testcase_16 AC 215 ms
13,252 KB
testcase_17 AC 626 ms
13,264 KB
testcase_18 AC 627 ms
13,208 KB
testcase_19 AC 611 ms
13,260 KB
testcase_20 AC 330 ms
7,572 KB
testcase_21 AC 338 ms
7,580 KB
testcase_22 AC 175 ms
7,588 KB
testcase_23 AC 177 ms
7,584 KB
testcase_24 AC 340 ms
7,528 KB
testcase_25 AC 320 ms
7,596 KB
testcase_26 AC 334 ms
7,536 KB
testcase_27 AC 331 ms
7,600 KB
testcase_28 AC 311 ms
7,504 KB
testcase_29 AC 331 ms
7,496 KB
testcase_30 AC 279 ms
13,268 KB
testcase_31 AC 280 ms
13,260 KB
testcase_32 AC 279 ms
13,208 KB
testcase_33 AC 163 ms
13,240 KB
testcase_34 AC 168 ms
13,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// ---------- begin chmin, chmax ----------
trait ChangeMinMax {
    fn chmin(&mut self, x: Self) -> bool;
    fn chmax(&mut self, x: Self) -> bool;
}

impl<T: PartialOrd> ChangeMinMax for T {
    fn chmin(&mut self, x: Self) -> bool {
        *self > x && {
            *self = x;
            true
        }
    }
    fn chmax(&mut self, x: Self) -> bool {
        *self < x && {
            *self = x;
            true
        }
    }
}
// ---------- end chmin, chmax ----------
// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
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_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_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, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// ---------- end input macro ----------

use std::io::Write;

// 操作1のあと追加で反転するかを選べる、コスト1
//
// b=0, a=1 だと最後のaの後の反転は奇数回、おろす必要がある
// b=1, a=0 だと何処かで反転して立てる必要あり, 全体で奇数回
// b=0, a=0 だと全体で反転は偶数回
// b=1, a=1 だと... 最後のa=1 の後の反転は偶数回
// これを満たすような01列の和の最小値
// そもそも解があるかの判定はどうするのか
// 偶奇の不一致を消しておく
//

fn run() {
    input! {
        n: usize,
        a: [usize; n],
        q: usize,
        b: [usize; q],
    }
    let w = 60;
    let mut last = vec![None; w];
    for (i, a) in a.iter().enumerate() {
        for (j, k) in last.iter_mut().enumerate() {
            if *a >> j & 1 == 1 {
                *k = Some(i);
            }
        }
    }
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    for b in b {
        let mut cond = vec![];
        for (i, k) in last.iter().enumerate() {
            match (b >> i & 1, *k) {
                (0, None) => cond.push((0, 0)),
                (0, Some(k)) => cond.push((k, 1)),
                (1, None) => cond.push((0, 1)),
                (1, Some(k)) => cond.push((k, 0)),
                _ => unreachable!(),
            };
        }
        cond.sort();
        cond.dedup();
        if cond.len() > 1 && cond.windows(2).any(|c| c[0].0 == c[1].0) {
            writeln!(out, "-1").ok();
            continue;
        }
        let mut ans = 0;
        let mut state = 0;
        for &(_, b) in cond.iter().rev() {
            if state != b {
                ans += 1;
                state = b;
            }
        }
        writeln!(out, "{}", ans).ok();
    }
}

fn main() {
    run();
}
0