結果

問題 No.1288 yuki collection
ユーザー fukafukatanifukafukatani
提出日時 2021-01-03 22:34:44
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 112 ms / 5,000 ms
コード長 4,683 bytes
コンパイル時間 12,186 ms
コンパイル使用メモリ 379,884 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-13 16:19:50
合計ジャッジ時間 15,900 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
5,248 KB
testcase_04 AC 1 ms
5,248 KB
testcase_05 AC 1 ms
5,248 KB
testcase_06 AC 1 ms
5,248 KB
testcase_07 AC 1 ms
5,248 KB
testcase_08 AC 1 ms
5,248 KB
testcase_09 AC 1 ms
5,248 KB
testcase_10 AC 1 ms
5,248 KB
testcase_11 AC 1 ms
5,248 KB
testcase_12 AC 1 ms
5,248 KB
testcase_13 AC 101 ms
5,248 KB
testcase_14 AC 103 ms
5,248 KB
testcase_15 AC 82 ms
5,248 KB
testcase_16 AC 82 ms
5,248 KB
testcase_17 AC 103 ms
5,248 KB
testcase_18 AC 110 ms
5,248 KB
testcase_19 AC 110 ms
5,248 KB
testcase_20 AC 108 ms
5,248 KB
testcase_21 AC 99 ms
5,248 KB
testcase_22 AC 104 ms
5,248 KB
testcase_23 AC 101 ms
5,248 KB
testcase_24 AC 112 ms
5,248 KB
testcase_25 AC 107 ms
5,248 KB
testcase_26 AC 103 ms
5,248 KB
testcase_27 AC 47 ms
5,248 KB
testcase_28 AC 56 ms
5,248 KB
testcase_29 AC 44 ms
5,248 KB
testcase_30 AC 3 ms
5,248 KB
testcase_31 AC 5 ms
5,248 KB
testcase_32 AC 6 ms
5,248 KB
testcase_33 AC 87 ms
5,248 KB
testcase_34 AC 85 ms
5,248 KB
testcase_35 AC 78 ms
5,248 KB
testcase_36 AC 87 ms
5,248 KB
testcase_37 AC 87 ms
5,248 KB
testcase_38 AC 29 ms
5,248 KB
testcase_39 AC 30 ms
5,248 KB
testcase_40 AC 2 ms
5,248 KB
testcase_41 AC 1 ms
5,248 KB
testcase_42 AC 1 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![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>().chars().collect::<Vec<_>>();
    let v = read_vec::<i64>();

    let mut mcf = MinCostFlow::new(n + 1);
    let inf = 1e9 as i64;

    let s0 = "yuki".chars().collect::<Vec<_>>();
    let mut idx = [n + 1; 5];
    idx[4] = n;
    for i in (0..n).rev() {
        for k in 0..4 {
            if s0[k] != s[i] {
                continue;
            }
            if idx[k] <= n {
                mcf.add_edge(i, idx[k], n as i32 / 4, 0);
            }
            if idx[k + 1] <= n {
                mcf.add_edge(i, idx[k + 1], 1, inf - v[i]);
            }
            idx[k] = i;
        }
    }
    if idx[0] == n + 1 {
        println!("0");
        return;
    }
    mcf.add_edge(idx[0], n, n as i32 / 4, inf * 4);
    let ans = -mcf.flow(idx[0], n, n as i32 / 4).1 + inf * 4 * (n as i64 / 4);
    println!("{}", ans);
}

pub trait BinarySearch<T> {
    fn upper_bound(&self, x: &T) -> usize;
}

impl<T: Ord> BinarySearch<T> for [T] {
    fn upper_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();

        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less | Ordering::Equal => {
                    low = mid + 1;
                }
                Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }
}

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()
}

pub struct MinCostFlow {
    g: Vec<Vec<Edge>>,
}
#[derive(Clone, Copy)]
struct Edge {
    v: usize,
    rev: usize,
    cap: i32,
    cost: i64,
}
impl MinCostFlow {
    pub fn new(n: usize) -> Self {
        Self {
            g: vec![Vec::new(); n],
        }
    }
    pub fn add_edge(&mut self, u: usize, v: usize, cap: i32, cost: i64) {
        let rev_u = self.g[v].len();
        let rev_v = self.g[u].len();
        self.g[u].push(Edge {
            v,
            rev: rev_u,
            cap,
            cost,
        });
        self.g[v].push(Edge {
            v: u,
            rev: rev_v,
            cap: 0,
            cost: -cost,
        })
    }
    pub fn flow(&mut self, s: usize, t: usize, limit: i32) -> (i32, i64) {
        use std::cmp::Reverse;
        let n = self.g.len();
        let mut flow = 0;
        let mut cost = 0;
        let mut p = vec![0; n];
        let mut dist = vec![0; n];
        let mut que = std::collections::BinaryHeap::new();
        let mut prev = vec![(0, 0); n];
        while flow < limit {
            dist.clear();
            dist.resize(n, i64::max_value());
            dist[s] = 0;
            que.push((Reverse(0), s));
            prev[t].0 = !0;
            while let Some((Reverse(d), u)) = que.pop() {
                if dist[u] < d {
                    continue;
                }
                for (i, e) in self.g[u].iter().enumerate() {
                    let dd = d + e.cost - p[e.v] + p[u];
                    if e.cap > 0 && dd < dist[e.v] {
                        dist[e.v] = dd;
                        que.push((Reverse(dd), e.v));
                        prev[e.v] = (u, i);
                    }
                }
            }
            if prev[t].0 == !0 {
                break;
            }
            for u in 0..n {
                if dist[u] != i64::max_value() {
                    p[u] += dist[u];
                }
            }
            let mut v = t;
            let mut add = limit - flow;
            while v != s {
                let (u, i) = prev[v];
                add = add.min(self.g[u][i].cap);
                v = u;
            }
            flow += add;
            let mut v = t;
            while v != s {
                let (u, i) = prev[v];
                let e = &mut self.g[u][i];
                cost += e.cost * add as i64;
                e.cap -= add;
                let rev = e.rev;
                self.g[v][rev].cap += add;
                v = u;
            }
        }
        (flow, cost)
    }
}
0