結果

問題 No.2139 K Consecutive Sushi
ユーザー zeronosu77108zeronosu77108
提出日時 2022-12-22 15:58:35
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 5,756 bytes
コンパイル時間 12,900 ms
コンパイル使用メモリ 379,212 KB
実行使用メモリ 14,720 KB
最終ジャッジ日時 2024-11-27 19:55:28
合計ジャッジ時間 15,311 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 53 ms
14,720 KB
testcase_04 AC 51 ms
14,592 KB
testcase_05 AC 52 ms
14,720 KB
testcase_06 AC 53 ms
14,720 KB
testcase_07 AC 51 ms
14,720 KB
testcase_08 AC 42 ms
14,720 KB
testcase_09 AC 46 ms
14,720 KB
testcase_10 AC 48 ms
14,720 KB
testcase_11 AC 47 ms
14,720 KB
testcase_12 AC 38 ms
14,720 KB
testcase_13 AC 1 ms
5,248 KB
testcase_14 AC 1 ms
5,248 KB
testcase_15 AC 1 ms
5,248 KB
testcase_16 AC 1 ms
5,248 KB
testcase_17 AC 1 ms
5,248 KB
testcase_18 AC 1 ms
5,248 KB
testcase_19 AC 1 ms
5,248 KB
testcase_20 AC 1 ms
5,248 KB
testcase_21 AC 1 ms
5,248 KB
testcase_22 AC 1 ms
5,248 KB
testcase_23 AC 5 ms
5,248 KB
testcase_24 AC 18 ms
6,016 KB
testcase_25 AC 37 ms
10,240 KB
testcase_26 AC 6 ms
5,248 KB
testcase_27 AC 14 ms
5,376 KB
testcase_28 AC 15 ms
5,504 KB
testcase_29 AC 7 ms
5,248 KB
testcase_30 AC 19 ms
6,400 KB
testcase_31 AC 47 ms
13,312 KB
testcase_32 AC 8 ms
5,248 KB
testcase_33 AC 50 ms
14,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let mut sc = Scanner::new();
    let n = sc.usize();
    let k = sc.usize();
    let a = sc.vec::<usize>(n);

    def_monoid!(M, usize, std::usize::MAX, |a:&usize,b:&usize| *a.min(b));
    let mut dp = SegmentTree::<M>::new(n + 1);
    dp.set(0, 0);

    for (i, &a) in a.iter().enumerate() {
        dp.set(i+1, dp.fold((i+1).saturating_sub(k)..=i) + a);
    }

    let ans = a.iter().sum::<usize>() - dp.fold(n-k+1..);
    println!("{}", ans);
}

struct Scanner { s : std::collections::VecDeque<String> } #[allow(unused)] impl Scanner { fn new() -> Self { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); Self { s : s.split_whitespace().map(|s| s.to_string()).collect() } } fn reload(&mut self) -> () { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); self.s = s.split_whitespace().map(|s| s.to_string()).collect(); } fn usize(&mut self) -> usize { self.input() } fn usize1(&mut self) -> usize { self.input::<usize>() - 1 } fn isize(&mut self) -> isize { self.input() } fn i32(&mut self) -> i32 { self.input() } fn i64(&mut self) -> i64 { self.input() } fn i128(&mut self) -> i128 { self.input() } fn u8(&mut self) -> u8 { self.input() } fn u32(&mut self) -> u32 { self.input() } fn u64(&mut self) -> u64 { self.input() } fn u128(&mut self) -> u128 { self.input() } fn edge(&mut self) -> (usize, usize) { (self.usize1(), self.usize1()) } fn edges(&mut self, m : usize) -> Vec<(usize, usize)> { let mut e = Vec::with_capacity(m); for _ in 0..m { e.push(self.edge()); } e } fn wedge<T:std::str::FromStr>(&mut self) -> (usize, usize, T) { (self.usize1(), self.usize1(), self.input()) } fn wedges<T:std::str::FromStr>(&mut self, m : usize) -> Vec<(usize, usize, T)> { let mut e = Vec::with_capacity(m); for _ in 0..m { e.push(self.wedge()); } e } fn input<T>(&mut self) -> T where T: std::str::FromStr { if self.s.is_empty() { self.reload(); } if let Some(head) = self.s.pop_front() { head.parse::<T>().ok().unwrap() } else { panic!() } } fn tuple<T, U>(&mut self) -> (T, U) where T: std::str::FromStr, U: std::str::FromStr { (self.input(), self.input()) } fn vec<T>(&mut self, n: usize) -> Vec<T> where T: std::str::FromStr { if self.s.is_empty() { self.reload(); } self.s.drain(..n).map(|s| s.parse::<T>().ok().unwrap() ).collect::<Vec<T>>() } fn nvec<T>(&mut self) -> Vec<T> where T: std::str::FromStr { let n : usize = self.input(); self.vec(n) } fn chars(&mut self) -> Vec<char> { let s : String = self.input(); s.chars().collect() } fn bytes(&mut self) -> Vec<u8> { let s : String = self.input(); s.bytes().collect() } }

#[macro_export]
macro_rules! def_monoid { ($m:ident, $t:ty, $id:expr, $op:expr) => { pub struct $m; impl Monoid for $m { type Type = $t; fn identity() -> Self::Type { $id } fn operator(x:&Self::Type, y:&Self::Type) -> Self::Type {$op(x, y) } }}; }
pub trait Monoid { type Type: Copy + Clone + std::fmt::Debug; fn identity() -> Self::Type; fn operator(a: &Self::Type, b: &Self::Type) -> Self::Type; }
struct SegmentTree<T : Monoid> { n : usize, seg: Vec<T::Type>, }
#[allow(unused)]
impl<T: Monoid> SegmentTree<T> {
    pub fn new(n : usize) -> Self { SegmentTree {n, seg:vec![T::identity(); 2*n + 1]} }
    pub fn from(s : &[T::Type]) -> Self {
        let n = s.len();
        let mut seg = vec![T::identity(); 2*n+1];
        for (i, &si) in s.iter().enumerate() { seg[i+n] = si; }
        for i in (1..n).rev() { seg[i] = T::operator(&seg[2*i], &seg[2*i+1]); }
        SegmentTree {n,seg}
    }

    pub fn set(&mut self, i:usize, x:T::Type) {
        let mut index = i + self.n;
        self.seg[index] = x;
        while index > 0 {
            index /= 2;
            self.seg[index] = T::operator(&self.seg[2*index], &self.seg[2*index+1]);
        }
    }

    fn _fold(&self, mut l:usize, mut r:usize) -> T::Type {
        let mut left = T::identity();
        let mut right = T::identity();
        while l < r {
            if l%2 == 1 { left = T::operator(&self.seg[l], &left); l+=1; }
            if r%2 == 1 { r-=1; right = T::operator(&right, &self.seg[r]); }
            l/=2; r/=2;
        }
        T::operator(&left, &right)
    }

    pub fn fold<R:std::ops::RangeBounds<usize>>(&self, range: R) -> T::Type {
        let l = self.n + match range.start_bound() {
            std::ops::Bound::Unbounded => 0,
            std::ops::Bound::Included(&l) => l,
            _ => unreachable!()
        };
        let r = self.n + match range.end_bound() {
            std::ops::Bound::Unbounded => self.n,
            std::ops::Bound::Included(&x) => x + 1,
            std::ops::Bound::Excluded(&x) => x,
        };
        self._fold(l, r)
    }

    pub fn get(&self, index:usize) -> T::Type { self.seg[index + self.n].clone() }

    fn _first_left<F: Fn(&T::Type)->bool>(&self, mut l:usize, r:usize, f : F) -> Option<usize> {
        if !f(&self.fold(l..r)) { return None; }
        l += self.n;
        loop {
            if f(&self.seg[l]) {
                if l >= self.n { return Some(l - self.n); }
                l *= 2;
            } else {
                if l%2 == 0 { l += 1; }
                else { l = l/2 + 1; }
            }
        }
    }

    pub fn first_left<R: std::ops::RangeBounds<usize>, F: Fn(&T::Type)->bool>(&self, range : R, f : F) -> Option<usize> {
        let l = match range.start_bound() {
            std::ops::Bound::Unbounded => 0,
            std::ops::Bound::Included(&l) => l,
            _ => unreachable!()
        };
        let r = match range.end_bound() {
            std::ops::Bound::Unbounded => self.n,
            std::ops::Bound::Included(&x) => x + 1,
            std::ops::Bound::Excluded(&x) => x,
        };
        self._first_left(l, r, f)
    }
}
0