QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#264186#7743. Grand Finaleucup-team635RE 0ms2092kbRust4.3kb2023-11-25 13:15:102023-11-25 13:15:11

Judging History

你现在查看的是最新测评结果

  • [2023-11-25 13:15:11]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:2092kb
  • [2023-11-25 13:15:10]
  • 提交

answer

use std::io::Write;

fn run() {
    input! {
        t: usize,
        ask: [(usize, usize, bytes, bytes); t],
    }
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    for (n, m, s, t) in ask {
        let up = (m + 1) / 2;
        let inf = std::i16::MAX / 2;
        let mut dp = vec![vec![0; up + 1]; m + 2];
        for (i, t) in t.iter().enumerate().rev() {
            for j in 0..(up + 1) {
                let mut val = inf;
                if *t == b'W' {
                    val.chmin(dp[i + 1][j] + 1);
                    if j > 0 {
                        val.chmin(dp[i + 2][j - 1]);
                    }
                } else if *t == b'Q' {
                    val.chmin(1i16.max(dp[i + 1][j] - 1));
                    if j > 0 {
                        val.chmin(0i16.max(dp[i + 2][j - 1] - 1));
                    }
                } else {
                    val.chmin(dp[i + 1][up.min(j + 1)] + 1);
                    if j > 0 {
                        val.chmin(dp[i + 2][j]);
                    }
                }
                dp[i][j] = val;
            }
        }
        let ok = dp;
        let mut ans = inf;
        let mut q = s.iter().filter(|s| **s == b'Q').count();
        let mut b = s.iter().filter(|s| **s == b'B').count();
        let mut dp = vec![vec![false; up + 1]; m + 2];
        dp[0][0] = true;
        for i in 0..(m + 1) {
            let src = std::mem::take(&mut dp[i]);
            for (j, _) in src.iter().enumerate().filter(|p| *p.1) {
                let rem_b = b - j;
                let rem_q = q - i.saturating_sub(2 * j);
                if ok[i][up.min(rem_b)] <= (rem_q as i16) {
                    ans.chmin((n + j) as i16);
                }
                if rem_b > 0 {
                    dp[i + 2][j + 1] = true;
                }
                if rem_q > 0 {
                    dp[i + 1][j] = true;
                }
            }
            if let Some(t) = t.get(i) {
                if *t == b'Q' {
                    q += 1;
                }
                if *t == b'B' {
                    b += 1;
                }
            }
        }
        if ans == inf {
            writeln!(out, "IMPOSSIBLE").ok();
        } else {
            writeln!(out, "{}", ans).ok();
        }
    }
}

fn main() {
    run();
}

// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export]
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_export]
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_export]
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 ----------
// ---------- begin chmin, chmax ----------
pub 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 ----------

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 2092kb

input:

2
2 6
BG
BQWBWW
4 6
GQBW
WWWWQB

output:

3
IMPOSSIBLE

result:

ok 2 lines

Test #2:

score: -100
Runtime Error

input:

2
3 8
QBG
BBBWBWWW
3 8
QBG
BBBWBWWW

output:


result: