QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#308914#8131. Filesystemucup-team635#WA 0ms2020kbRust3.7kb2024-01-20 13:42:012024-01-20 13:42:02

Judging History

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

  • [2024-01-20 13:42:02]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:2020kb
  • [2024-01-20 13:42:01]
  • 提交

answer

// ---------- 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 ----------
// ---------- begin scannner ----------
#[allow(dead_code)]
mod scanner {
    use std::str::FromStr;
    pub struct Scanner<'a> {
        it: std::str::SplitWhitespace<'a>,
    }
    impl<'a> Scanner<'a> {
        pub fn new(s: &'a String) -> Scanner<'a> {
            Scanner {
                it: s.split_whitespace(),
            }
        }
        pub fn next<T: FromStr>(&mut self) -> T {
            self.it.next().unwrap().parse::<T>().ok().unwrap()
        }
        pub fn next_bytes(&mut self) -> Vec<u8> {
            self.it.next().unwrap().bytes().collect()
        }
        pub fn next_chars(&mut self) -> Vec<char> {
            self.it.next().unwrap().chars().collect()
        }
        pub fn next_vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
            (0..len).map(|_| self.next()).collect()
        }
    }
}
// ---------- end scannner ----------

use std::io::Write;
use std::collections::*;

type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;

fn main() {
    use std::io::Read;
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut sc = scanner::Scanner::new(&s);
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    run(&mut sc, &mut out);
}

fn run<W: Write>(sc: &mut scanner::Scanner, out: &mut std::io::BufWriter<W>) {
    let t: u32 = sc.next();
    for _ in 0..t {
        let n: usize = sc.next();
        let k: usize = sc.next();
        let u = sc.next_vec::<usize>(k);
        let p = sc.next_vec::<usize>(n);
        let ans = solve(n, u, p);
        writeln!(out, "{}", ans).ok();
    }
}

fn solve(n: usize, u: Vec<usize>, p: Vec<usize>) -> i32 {
    let u = u.into_iter().map(|u| u - 1).collect::<Vec<_>>();
    let p = p.into_iter().map(|p| p - 1).collect::<Vec<_>>();
    let mut ip = vec![0; n];
    for (i, p) in p.iter().enumerate() {
        ip[*p] = i;
    }
    let mut del = vec![false; n];
    let mut cdel = vec![false; n];
    for u in u.iter() {
        del[*u] = true;
        cdel[ip[*u]] = true;
    }
    let inf = 10000i32;
    // dp[x][y][a][b]: a..n, p[b..n] を削除した、現在のソートはa, 削除操作を続けてるかb
    let mut dp = vec![vec![inf; n + 1]; n + 1];
    dp[n][n] = 0;
    for i in (0..(n + 1)).rev() {
        let mut x = del.clone();
        for j in (0..(n + 1)).rev() {
            let mut val = dp[i][j];
            if i > 0 && x[i - 1] {
                dp[i - 1][j].chmin(val);
            } else if j > 0 && cdel[j - 1] {
                dp[i][j - 1].chmin(val);
            } else {
                val += 1;
                for k in (0..i).rev() {
                    if x[k] {
                        break;
                    }
                    dp[k][j].chmin(val);
                }
                for k in (0..j).rev() {
                    if cdel[k] {
                        break;
                    }
                    dp[i][k].chmin(val);
                }
            }
            if j > 0 {
                x[p[j - 1]] = true;
            }
        }
        if i > 0 {
            del[i - 1] = true;
            cdel[ip[i - 1]] = true;
        }
    }
    dp[0][0]
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
12 8
2 5 8 3 4 10 12 1
10 5 8 6 4 7 2 3 11 12 1 9
8 4
1 3 5 7
1 4 5 8 7 6 3 2

output:

3
4

result:

ok 2 number(s): "3 4"

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 2020kb

input:

100
10 3
10 5 2
2 4 9 8 7 3 1 5 10 6
10 3
8 1 10
8 4 3 7 1 2 9 5 6 10
10 3
6 5 2
8 7 6 4 2 10 1 3 5 9
10 3
5 8 4
10 4 5 7 8 9 1 2 3 6
10 3
8 4 10
10 6 9 2 8 7 1 4 3 5
10 3
9 8 1
8 5 6 10 2 4 1 7 9 3
10 3
5 4 1
7 5 8 4 3 6 9 10 2 1
10 3
2 4 3
6 7 3 9 1 2 5 8 4 10
10 3
9 5 3
6 10 7 4 9 3 1 8 5 2
10 3
...

output:

2
2
3
3
3
2
2
2
3
2
3
2
2
2
3
2
3
3
2
3
3
2
2
3
3
3
2
2
2
4
2
3
2
2
2
2
3
2
3
2
3
3
2
2
3
2
2
3
2
2
3
2
2
2
3
2
2
2
3
2
2
3
2
1
2
2
2
2
2
2
3
3
3
2
3
2
3
3
2
4
3
2
4
3
2
2
2
3
3
1
2
2
3
2
3
3
2
2
1
2

result:

wrong answer 2nd numbers differ - expected: '3', found: '2'