結果
問題 | No.1288 yuki collection |
ユーザー | akakimidori |
提出日時 | 2022-04-13 01:49:03 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 112 ms / 5,000 ms |
コード長 | 6,725 bytes |
コンパイル時間 | 13,187 ms |
コンパイル使用メモリ | 402,388 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-12-21 14:48:25 |
合計ジャッジ時間 | 16,912 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,820 KB |
testcase_01 | AC | 1 ms
6,820 KB |
testcase_02 | AC | 1 ms
6,820 KB |
testcase_03 | AC | 1 ms
6,816 KB |
testcase_04 | AC | 1 ms
6,820 KB |
testcase_05 | AC | 1 ms
6,816 KB |
testcase_06 | AC | 1 ms
6,816 KB |
testcase_07 | AC | 1 ms
6,816 KB |
testcase_08 | AC | 1 ms
6,820 KB |
testcase_09 | AC | 1 ms
6,816 KB |
testcase_10 | AC | 1 ms
6,816 KB |
testcase_11 | AC | 1 ms
6,816 KB |
testcase_12 | AC | 1 ms
6,816 KB |
testcase_13 | AC | 103 ms
6,816 KB |
testcase_14 | AC | 100 ms
6,816 KB |
testcase_15 | AC | 81 ms
6,820 KB |
testcase_16 | AC | 79 ms
6,820 KB |
testcase_17 | AC | 104 ms
6,816 KB |
testcase_18 | AC | 102 ms
6,816 KB |
testcase_19 | AC | 103 ms
6,816 KB |
testcase_20 | AC | 105 ms
6,820 KB |
testcase_21 | AC | 101 ms
6,816 KB |
testcase_22 | AC | 102 ms
6,816 KB |
testcase_23 | AC | 100 ms
6,820 KB |
testcase_24 | AC | 104 ms
6,820 KB |
testcase_25 | AC | 102 ms
6,816 KB |
testcase_26 | AC | 104 ms
6,820 KB |
testcase_27 | AC | 46 ms
6,820 KB |
testcase_28 | AC | 55 ms
6,816 KB |
testcase_29 | AC | 47 ms
6,820 KB |
testcase_30 | AC | 4 ms
6,816 KB |
testcase_31 | AC | 5 ms
6,816 KB |
testcase_32 | AC | 6 ms
6,820 KB |
testcase_33 | AC | 88 ms
6,820 KB |
testcase_34 | AC | 108 ms
6,820 KB |
testcase_35 | AC | 99 ms
6,816 KB |
testcase_36 | AC | 112 ms
6,816 KB |
testcase_37 | AC | 110 ms
6,816 KB |
testcase_38 | AC | 23 ms
6,820 KB |
testcase_39 | AC | 24 ms
6,820 KB |
testcase_40 | AC | 2 ms
6,816 KB |
testcase_41 | AC | 1 ms
6,816 KB |
testcase_42 | AC | 1 ms
6,816 KB |
ソースコード
fn main() { input! { n: usize, s: bytes, v: [i64; n], } let mut graph = Graph::new(n + 2); let src = n; let dst = src + 1; let geta = 10i64.pow(9); let mut last = [None; 5]; last[4] = Some(dst); for (i, c) in s.iter().enumerate().rev() { let k = "yuki".bytes().position(|p| p == *c).unwrap(); last[k].map(|p| graph.add_edge(i, p, 500, 0)); last[k + 1].map(|p| graph.add_edge(i, p, 1, geta - v[i])); last[k] = Some(i); } last[0].map(|p| graph.add_edge(src, p, 500, 0)); let slope = graph.slope(src, dst, 500); let mut ans = 0; for (a, b) in slope { ans = ans.max(4 * a * geta - b); } println!("{}", ans); } use std::ops::*; pub trait MinCostFlowValue: Copy + Add<Output = Self> + Sub<Output = Self> + Mul<Output = Self> + Neg<Output = Self> + Ord { fn zero() -> Self; fn inf() -> Self; } impl MinCostFlowValue for i64 { fn zero() -> Self { 0 } fn inf() -> Self { std::i64::MAX } } #[derive(Clone)] struct Edge<T> { to: u32, inv: u32, cap: T, cost: T, } impl<T> Edge<T> where T: MinCostFlowValue, { fn new(to: usize, inv: usize, cap: T, cost: T) -> Self { Edge { to: to as u32, inv: inv as u32, cap, cost, } } fn to(&self) -> usize { self.to as usize } fn inv(&self) -> usize { self.inv as usize } fn cap(&self) -> T { self.cap } fn cost(&self) -> T { self.cost } fn add(&mut self, cap: T) { self.cap = self.cap + cap; } fn sub(&mut self, cap: T) { self.cap = self.cap - cap; } } pub struct Graph<T> { size: usize, edges: Vec<(usize, usize, T, T)>, } impl<T: MinCostFlowValue> Graph<T> { pub fn new(size: usize) -> Self { Graph { size: size, edges: vec![], } } pub fn add_edge(&mut self, src: usize, dst: usize, cap: T, cost: T) { assert!(src.max(dst) < self.size && src != dst); assert!(T::zero() <= cap && T::zero() <= cost); self.edges.push((src, dst, cap, cost)); } pub fn slope(&mut self, src: usize, dst: usize, cap: T) -> Vec<(T, T)> { assert!(src.max(dst) < self.size && src != dst); assert!(T::zero() <= cap); let mut deg = vec![0; self.size]; for e in self.edges.iter() { deg[e.0] += 1; deg[e.1] += 1; } let mut graph = deg .into_iter() .map(|d| Vec::with_capacity(d)) .collect::<Vec<_>>(); for &(src, dst, cap, cost) in self.edges.iter() { let x = graph[src].len(); let y = graph[dst].len(); graph[src].push(Edge::new(dst, y, cap, cost)); graph[dst].push(Edge::new(src, x, T::zero(), -cost)); } let mut heap = std::collections::BinaryHeap::new(); let mut dist = vec![(T::zero(), T::zero()); self.size]; let mut parent = vec![(0, 0); self.size]; let mut visited = vec![false; self.size]; let mut flow = T::zero(); let mut cost = T::zero(); let mut ans = vec![]; while flow < cap { dist.iter_mut().for_each(|p| p.1 = T::inf()); visited.iter_mut().for_each(|v| *v = false); heap.clear(); dist[src].1 = T::zero(); heap.clear(); heap.push(std::cmp::Reverse((dist[src].1, src))); while let Some(std::cmp::Reverse((_, v))) = heap.pop() { if visited[v] { continue; } visited[v] = true; let (a, b) = dist[v]; for (k, e) in graph[v] .iter() .enumerate() .filter(|(_, e)| e.cap() > T::zero()) { let (u, w) = (e.to(), e.cost()); let cost = w - dist[u].0 + a; if dist[u].1 - b > cost { let d = b + cost; dist[u].1 = d; parent[u] = (v, k); heap.push(std::cmp::Reverse((d, u))); } } } if !visited[dst] { break; } for v in 0..self.size { if !visited[v] { continue; } dist[v].0 = dist[v].0 - dist[dst].1 + dist[v].1; } let mut sub = cap; let mut pos = dst; while pos != src { let (pre, k) = parent[pos]; sub = std::cmp::min(sub, graph[pre][k].cap()); pos = pre; } let mut pos = dst; while pos != src { let (pre, k) = parent[pos]; let inv = graph[pre][k].inv(); graph[pre][k].sub(sub); graph[pos][inv].add(sub); pos = pre; } flow = flow + sub; cost = cost + -dist[src].0 * sub; ans.push((flow, cost)); } ans } } // ---------- 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 ----------