結果
問題 | No.1433 Two color sequence |
ユーザー | fukafukatani |
提出日時 | 2021-03-19 22:08:06 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 176 ms / 2,000 ms |
コード長 | 3,052 bytes |
コンパイル時間 | 13,172 ms |
コンパイル使用メモリ | 377,872 KB |
実行使用メモリ | 11,484 KB |
最終ジャッジ日時 | 2024-11-18 22:39:52 |
合計ジャッジ時間 | 17,430 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 160 ms
11,484 KB |
testcase_04 | AC | 141 ms
11,300 KB |
testcase_05 | AC | 1 ms
5,248 KB |
testcase_06 | AC | 0 ms
5,248 KB |
testcase_07 | AC | 1 ms
5,248 KB |
testcase_08 | AC | 168 ms
10,848 KB |
testcase_09 | AC | 165 ms
10,992 KB |
testcase_10 | AC | 167 ms
10,468 KB |
testcase_11 | AC | 162 ms
10,808 KB |
testcase_12 | AC | 176 ms
10,820 KB |
testcase_13 | AC | 165 ms
10,852 KB |
testcase_14 | AC | 172 ms
10,844 KB |
testcase_15 | AC | 170 ms
10,892 KB |
testcase_16 | AC | 171 ms
10,904 KB |
testcase_17 | AC | 146 ms
10,900 KB |
testcase_18 | AC | 149 ms
10,864 KB |
testcase_19 | AC | 150 ms
10,772 KB |
testcase_20 | AC | 169 ms
10,900 KB |
testcase_21 | AC | 151 ms
10,840 KB |
testcase_22 | AC | 149 ms
11,024 KB |
testcase_23 | AC | 150 ms
10,900 KB |
ソースコード
#![allow(unused_imports)] use std::cmp::*; use std::collections::*; use std::io::Write; use std::ops::Bound::*; #[allow(unused_macros)] macro_rules! debug { ($($e:expr),*) => { #[cfg(debug_assertions)] $({ let (e, mut err) = (stringify!($e), std::io::stderr()); writeln!(err, "{} = {:?}", e, $e).unwrap() })* }; } fn main() { let n = read::<usize>(); let s = read::<String>(); let mut a = read_vec::<i64>(); for (i, ch) in s.chars().enumerate() { if ch == 'B' { a[i] *= -1; } } debug!(a); let mut ans = 0; for _ in 0..2 { let mut from_left = vec![0; n]; from_left[0] = a[0]; for i in 1..n { from_left[i] = from_left[i - 1] + a[i]; } let mut from_right = vec![0; n + 1]; for i in (0..n).rev() { from_right[i] = from_right[i + 1] + a[i]; } let mut seg = SegTree::new(n + 1, std::i64::MAX, min); for i in 0..n + 1 { seg.update(i, from_right[i]); } let mut sum = from_left[n - 1]; for i in 0..n { ans = max(sum - seg.query(i + 1, n + 1), ans); sum -= a[i]; } for i in 0..n { a[i] *= -1; } } println!("{}", ans); } fn read<T: std::str::FromStr>() -> T { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().parse().ok().unwrap() } fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>() .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect() } #[derive(Clone)] struct SegTree<T, F> where F: Fn(T, T) -> T, T: std::clone::Clone + std::marker::Copy, { n: usize, dat: Vec<T>, init: T, functor: F, } impl<T, F> SegTree<T, F> where F: Fn(T, T) -> T, T: std::clone::Clone + std::marker::Copy, { fn new(n: usize, init: T, f: F) -> SegTree<T, F> { let mut m = 1; // For simplicity, we use 2 ** n sized SegTree. while m < n { m *= 2; } SegTree { n: m, dat: vec![init; 2 * m - 1], init: init, functor: f, } } // dat[k] = a; fn update(&mut self, k: usize, a: T) { let mut k = k; k += self.n - 1; self.dat[k] = a; while k > 0 { k = (k - 1) / 2; self.dat[k] = (self.functor)(self.dat[k * 2 + 1], self.dat[k * 2 + 2]); } } // [a, b) fn query(&self, a: usize, b: usize) -> T { self.query_inner(a, b, 0, 0, self.n) } fn query_inner(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> T { if r <= a || b <= l { return self.init; } if a <= l && r <= b { return self.dat[k]; } let vl = self.query_inner(a, b, k * 2 + 1, l, (l + r) / 2); let vr = self.query_inner(a, b, k * 2 + 2, (l + r) / 2, r); (self.functor)(vl, vr) } }