結果

問題 No.1296 OR or NOR
ユーザー akakimidoriakakimidori
提出日時 2020-11-20 23:14:24
言語 Rust
(1.59.0)
結果
AC  
実行時間 561 ms / 3,000 ms
コード長 3,714 bytes
コンパイル時間 1,235 ms
使用メモリ 13,236 KB
最終ジャッジ日時 2023-02-23 19:31:18
合計ジャッジ時間 14,930 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 1 ms
7,572 KB
testcase_01 AC 1 ms
7,588 KB
testcase_02 AC 266 ms
9,068 KB
testcase_03 AC 267 ms
9,088 KB
testcase_04 AC 343 ms
13,108 KB
testcase_05 AC 338 ms
13,136 KB
testcase_06 AC 361 ms
13,152 KB
testcase_07 AC 348 ms
13,120 KB
testcase_08 AC 347 ms
13,176 KB
testcase_09 AC 306 ms
13,096 KB
testcase_10 AC 299 ms
13,084 KB
testcase_11 AC 273 ms
8,936 KB
testcase_12 AC 261 ms
13,136 KB
testcase_13 AC 297 ms
13,148 KB
testcase_14 AC 424 ms
13,132 KB
testcase_15 AC 236 ms
13,236 KB
testcase_16 AC 203 ms
13,132 KB
testcase_17 AC 561 ms
13,100 KB
testcase_18 AC 548 ms
13,128 KB
testcase_19 AC 539 ms
13,172 KB
testcase_20 AC 262 ms
8,220 KB
testcase_21 AC 268 ms
8,312 KB
testcase_22 AC 167 ms
7,728 KB
testcase_23 AC 168 ms
7,576 KB
testcase_24 AC 259 ms
7,592 KB
testcase_25 AC 248 ms
7,592 KB
testcase_26 AC 262 ms
7,568 KB
testcase_27 AC 262 ms
7,592 KB
testcase_28 AC 245 ms
7,564 KB
testcase_29 AC 262 ms
8,248 KB
testcase_30 AC 279 ms
13,160 KB
testcase_31 AC 279 ms
13,032 KB
testcase_32 AC 280 ms
13,112 KB
testcase_33 AC 174 ms
13,036 KB
testcase_34 AC 175 ms
13,232 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