結果

問題 No.1924 3 color painting on a line
ユーザー akakimidoriakakimidori
提出日時 2022-05-02 22:21:25
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 4,369 bytes
コンパイル時間 15,691 ms
コンパイル使用メモリ 379,804 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-02 02:30:44
合計ジャッジ時間 17,279 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 1 ms
5,376 KB
testcase_40 AC 1 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `n`
   --> src/main.rs:95:9
    |
95  |         n: usize,
    |         ^
    |
help: `n` is captured in macro and introduced a unused variable
   --> src/main.rs:154:13
    |
94  | /     input! {
95  | |         n: usize,
96  | |         s: chars,
97  | |     }
    | |_____- in this macro invocation
...
154 |           let $var = read_value!($iter, $t);
    |               ^^^^
    = note: `#[warn(unused_variables)]` on by default
    = note: this warning originates in the macro `input_inner` which comes from the expansion of the macro `input` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: function `rand_memory` is never used
 --> src/main.rs:1:4
  |
1 | fn rand_memory() -> usize {
  |    ^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: function `rand` is never used
 --> src/main.rs:5:4
  |
5 | fn rand() -> usize {
  |    ^^^^

warning: function `shuffle` is never used
  --> src/main.rs:18:4
   |
18 | fn shuffle<T>(a: &mut [T]) {
   |    ^^^^^^^

warning: function `stress` is never used
  --> src/main.rs:25:4
   |
25 | fn stress() {
   |    ^^^^^^

warning: function `naive` is never used
  --> src/main.rs:38:4
   |
38 | fn naive(s: Vec<char>) -> usize {
   |    ^^^^^

ソースコード

diff #

fn rand_memory() -> usize {
    Box::into_raw(Box::new("I hope this is a random number")) as usize
}

fn rand() -> usize {
    static mut X: usize = 0;
    unsafe {
        if X == 0 {
            X = rand_memory();
        }
        X ^= X << 13;
        X ^= X >> 17;
        X ^= X << 5;
        X
    }
}

fn shuffle<T>(a: &mut [T]) {
    for i in 1..a.len() {
        let p = rand() % (i + 1);
        a.swap(i, p);
    }
}

fn stress() {
    for _ in 0..1000 {
        let n = rand() % 30 + 1;
        let s = (0..n).map(|_| ((rand() % 3) as u8 + b'A') as char).collect::<Vec<_>>();
        let x = naive(s.clone());
        let y = test(s.clone());
        use util::*;
        if x != y {
            println!("Hack! {}: {} {}", s.iter().map(|s| *s as u8 - b'A').join(""), x, y);
        }
    }
}

fn naive(s: Vec<char>) -> usize {
    let mut z = s.clone();
    z.sort();
    z.dedup();
    let s = s.iter().map(|c| z.binary_search(c).unwrap()).collect::<Vec<_>>();
    let n = s.len();
    let inf = n + 1;
    let mut dp = vec![vec![[inf; 3]; n]; n];
    for (i, s) in s.iter().enumerate() {
        dp[i][i] = [1; 3];
        dp[i][i][*s] = 0;
    }
    for l in (0..n).rev() {
        for r in (l + 1)..n {
            let mut val = [inf; 3];
            for m in l..r {
                for i in 0..3 {
                    val[i] = val[i].min(dp[l][m][i] + dp[m + 1][r][i]);
                }
            }
            val[s[l]] = val[s[l]].min(dp[l + 1][r][s[l]]);
            val[s[r]] = val[s[r]].min(dp[l][r - 1][s[r]]);
            for i in 0..3 {
                for j in 0..3 {
                    val[j] = val[j].min(val[i] + 1);
                }
            }
            dp[l][r] = val;
        }
    }
    *dp[0][n - 1].iter().min().unwrap() + 1
}

fn test(s: Vec<char>) -> usize {
    let mut s = s;
    s.dedup();
    let mut ans = 0;
    let mut stack = vec![s[0]];
    for &s in s[1..].iter() {
        let k = stack.len();
        if k >= 2 && stack[k - 2] == s {
            ans += 1;
            stack.pop();
        } else {
            stack.push(s);
        }
    }
    ans += if stack.len() <= 3 {
        stack.len()
    } else {
        stack.len() - (stack.len() - 1) / 3
    };
    ans
}

fn run() {
    input! {
        n: usize,
        s: chars,
    }
    let ans = test(s);
    println!("{}", ans);
}

fn main() {
    run();
}

mod util {
    pub trait Join {
        fn join(self, sep: &str) -> String;
    }

    impl<T, I> Join for I
    where
        I: Iterator<Item = T>,
        T: std::fmt::Display,
    {
        fn join(self, sep: &str) -> String {
            let mut s = String::new();
            use std::fmt::*;
            for (i, v) in self.enumerate() {
                if i > 0 {
                    write!(&mut s, "{}", sep).ok();
                }
                write!(&mut s, "{}", v).ok();
            }
            s
        }
    }
}
// ---------- 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 ----------
0