結果
問題 | No.2741 Balanced Choice |
ユーザー | naut3 |
提出日時 | 2024-04-25 02:27:59 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 41 ms / 2,000 ms |
コード長 | 5,833 bytes |
コンパイル時間 | 13,736 ms |
コンパイル使用メモリ | 379,344 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-07 12:00:50 |
合計ジャッジ時間 | 15,050 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
5,248 KB |
testcase_01 | AC | 40 ms
5,248 KB |
testcase_02 | AC | 41 ms
5,248 KB |
testcase_03 | AC | 41 ms
5,248 KB |
testcase_04 | AC | 40 ms
5,248 KB |
testcase_05 | AC | 1 ms
5,248 KB |
testcase_06 | AC | 1 ms
5,248 KB |
testcase_07 | AC | 21 ms
5,248 KB |
testcase_08 | AC | 34 ms
5,248 KB |
testcase_09 | AC | 18 ms
5,248 KB |
testcase_10 | AC | 24 ms
5,248 KB |
testcase_11 | AC | 34 ms
5,248 KB |
ソースコード
#![allow(non_snake_case, unused_imports, unused_must_use)] use std::io::{self, prelude::*}; fn main() { let (stdin, stdout) = (io::stdin(), io::stdout()); let mut scan = Scanner::new(stdin.lock()); let mut out = io::BufWriter::new(stdout.lock()); macro_rules! input { ($T: ty) => { scan.token::<$T>() }; ($T: ty, $N: expr) => { (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>() }; } let N = input!(usize); let W = input!(usize); let D = input!(usize); let mut dp0 = vec![isize::MIN; W + 1]; let mut dp1 = vec![isize::MIN; W + 1]; dp0[0] = 0; dp1[0] = 0; for _ in 0..N { let t = input!(u8); let w = input!(usize); let v = input!(isize); if t == 0 { for i in (0..=W).rev() { if i + w <= W && dp0[i] != isize::MIN { dp0[i + w] = std::cmp::max(dp0[i + w], dp0[i] + v); } } } else { for i in (0..=W).rev() { if i + w <= W && dp1[i] != isize::MIN { dp1[i + w] = std::cmp::max(dp1[i + w], dp1[i] + v); } } } } let dp1 = SegmentTree::from(&dp1, |&x, &y| std::cmp::max(x, y), isize::MIN); let mut ans = 0; for i in 0..=W { if dp0[i] == isize::MIN { continue; } // 0 の重量は i // 1 の重量 j は i + j <= W and |i - j| <= D を満たす // i >= j let left = std::cmp::max(i as isize - D as isize, 0) as usize; let right = std::cmp::min(W - i, i); ans = std::cmp::max(ans, dp0[i] + dp1.prod(left..=right)); // j > i let left = i + 1; let right = std::cmp::min(W - i, D + i); ans = std::cmp::max(ans, dp0[i] + dp1.prod(left..=right)); } writeln!(out, "{}", ans); } pub struct SegmentTree<M> { size: usize, tree: Vec<M>, op: fn(&M, &M) -> M, id: M, } impl<M: Copy + PartialEq> SegmentTree<M> { /// self = [id; size], self.op = op, self.id = id pub fn new(size: usize, op: fn(&M, &M) -> M, id: M) -> Self { return Self { size: size, tree: vec![id; 2 * size], op: op, id: id, }; } /// self = arr, self.op = op, self.id = id pub fn from(arr: &[M], op: fn(&M, &M) -> M, id: M) -> Self { let size = arr.len(); let mut tree = vec![id; 2 * size]; for i in 0..size { tree[i + size] = arr[i]; assert!(op(&id, &arr[i]) == arr[i]); } for i in (1..size).rev() { tree[i] = op(&tree[i << 1], &tree[i << 1 | 1]); } return Self { size: size, tree: tree, op: op, id: id, }; } /// self[pos] <- value pub fn insert(&mut self, mut pos: usize, value: M) -> () { pos += self.size; self.tree[pos] = value; while pos > 1 { pos >>= 1; self.tree[pos] = (self.op)(&self.tree[pos << 1], &self.tree[pos << 1 | 1]); } } /// return self[pos] pub fn get_point(&self, pos: usize) -> M { return self[pos]; } /// return Π_{i ∈ [left, right)} self[i] pub fn get(&self, left: usize, right: usize) -> M { let (mut l, mut r) = (left + self.size, right + self.size); let (mut vl, mut vr) = (self.id, self.id); while l < r { if l & 1 == 1 { vl = (self.op)(&vl, &self.tree[l]); l += 1; } if r & 1 == 1 { r -= 1; vr = (self.op)(&self.tree[r], &vr); } l >>= 1; r >>= 1; } return (self.op)(&vl, &vr); } /// return Π_{i ∈ range} self[i] pub fn prod<R: std::ops::RangeBounds<usize>>(&self, range: R) -> M { let left = match range.start_bound() { std::ops::Bound::Included(&l) => l, std::ops::Bound::Excluded(&l) => l + 1, std::ops::Bound::Unbounded => 0, }; let right = match range.end_bound() { std::ops::Bound::Included(&r) => r + 1, std::ops::Bound::Excluded(&r) => r, std::ops::Bound::Unbounded => self.size, }; return self.get(left, right); } } impl<M> std::ops::Index<usize> for SegmentTree<M> { type Output = M; fn index(&self, index: usize) -> &Self::Output { &self.tree[index + self.size] } } impl<M: std::fmt::Display> std::fmt::Display for SegmentTree<M> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, "{}", self.tree[self.size..] .iter() .map(|x| x.to_string()) .collect::<Vec<_>>() .join(" ") ) } } struct Scanner<R> { reader: R, buf_str: Vec<u8>, buf_iter: std::str::SplitWhitespace<'static>, } impl<R: BufRead> Scanner<R> { fn new(reader: R) -> Self { Self { reader, buf_str: vec![], buf_iter: "".split_whitespace(), } } fn token<T: std::str::FromStr>(&mut self) -> T { loop { if let Some(token) = self.buf_iter.next() { return token.parse().ok().expect("Failed parse"); } self.buf_str.clear(); self.reader .read_until(b'\n', &mut self.buf_str) .expect("Failed read"); self.buf_iter = unsafe { let slice = std::str::from_utf8_unchecked(&self.buf_str); std::mem::transmute(slice.split_whitespace()) } } } }