QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#264126 | #7743. Grand Finale | ucup-team635# | WA | 50ms | 10800kb | Rust | 5.2kb | 2023-11-25 12:56:18 | 2023-11-25 12:56:18 |
Judging History
answer
// 単調性あるから二分探索したい気になるが
// K を固定した時の問題は解けるか?
// 今i枚ある、j枚目まで見た、Qの枚数、Bの枚数
// のboolDPができる
// Kに触れてない間はj枚目まで見た、Qの使用枚数
// で残りは復元できる
// N=K の時に解けるか
// Qは1マス進む
// Bは2マス進む2マス目は捨てる
// といった具合
// ゴールから遷移させてって、何が何枚あればゴールに行ける
// というのを計算する?
// 上限に達してiにいる、Qがj枚ある
// この時にゴールに到達するのに必要なPの枚数は?
// でDP
//
use std::collections::*;
use std::io::Write;
type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;
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![inf; up + 1]; m + 1];
dp[m].fill(0);
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[m.min(i + 2)][j - 1]);
}
} else if *t == b'Q' {
val.chmin(1i16.max(dp[i + 1][j] - 1));
if j > 0 {
val.chmin(1i16.max(dp[m.min(i + 2)][j - 1] - 1));
}
} else {
val.chmin(dp[i + 1][up.min(j + 1)] + 1);
if j > 0 {
val.chmin(dp[m.min(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 + 1];
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 i < m {
if rem_b > 0 {
dp[(i + 2).min(m)][j + 1] = true;
}
if rem_q > 0 {
dp[i + 1][j] = true;
}
}
}
if i < m {
if t[i] == b'Q' {
q += 1;
}
if t[i] == 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 ----------
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 2144kb
input:
2 2 6 BG BQWBWW 4 6 GQBW WWWWQB
output:
3 IMPOSSIBLE
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 2180kb
input:
2 3 8 QBG BBBWBWWW 3 8 QBG BBBWBWWW
output:
3 3
result:
ok 2 lines
Test #3:
score: -100
Wrong Answer
time: 50ms
memory: 10800kb
input:
13 184 887 WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...
output:
184 66 164 161 118 117 28 125 134 89 33 160 73
result:
wrong answer 2nd lines differ - expected: '372', found: '66'