結果

問題 No.1117 数列分割
ユーザー akakimidoriakakimidori
提出日時 2020-07-17 23:14:42
言語 Rust
(1.77.0 + proconio)
結果
WA  
実行時間 -
コード長 5,452 bytes
コンパイル時間 11,693 ms
コンパイル使用メモリ 378,820 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-07 11:45:05
合計ジャッジ時間 16,898 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 WA -
testcase_04 AC 24 ms
5,376 KB
testcase_05 AC 0 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 15 ms
5,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 63 ms
5,376 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 14 ms
5,376 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 256 ms
5,376 KB
testcase_22 AC 253 ms
5,376 KB
testcase_23 WA -
testcase_24 AC 279 ms
5,376 KB
testcase_25 WA -
testcase_26 AC 1 ms
5,376 KB
testcase_27 WA -
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

// ---------- begin Foldable Deque ----------
struct FoldableDeque<T, F> {
    front: Vec<(T, T)>,
    back: Vec<(T, T)>,
    op: F,
}

#[allow(dead_code)]
impl<T, F> FoldableDeque<T, F>
where
    T: Clone,
    F: Fn(&T, &T) -> T,
{
    fn new(op: F) -> Self {
        FoldableDeque {
            front: Vec::new(),
            back: Vec::new(),
            op: op,
        }
    }
    fn find(&self) -> Option<T> {
        match (self.front.last(), self.back.last()) {
            (Some(a), Some(b)) => Some((self.op)(&a.1, &b.1)),
            (Some(a), None) => Some(a.1.clone()),
            (None, Some(b)) => Some(b.1.clone()),
            (None, None) => None,
        }
    }
    fn clear(&mut self) {
        self.front.clear();
        self.back.clear();
    }
    fn len(&self) -> usize {
        self.front.len() + self.back.len()
    }
    fn push_back(&mut self, val: T) {
        let sum = self
            .back
            .last()
            .map_or_else(|| val.clone(), |p| (self.op)(&p.1, &val));
        self.back.push((val, sum));
    }
    fn push_front(&mut self, val: T) {
        let sum = self
            .front
            .last()
            .map_or_else(|| val.clone(), |p| (self.op)(&val, &p.1));
        self.front.push((val, sum));
    }
    fn pop_front(&mut self) -> Option<T> {
        if self.len() == 0 {
            return None;
        }
        if self.front.is_empty() {
            let a = self.back.clone();
            let m = (self.back.len() + 1) / 2;
            self.back.clear();
            let (f, b) = a.split_at(m);
            for v in f.iter().rev() {
                self.push_front(v.0.clone());
            }
            for v in b.iter() {
                self.push_back(v.0.clone());
            }
        }
        self.front.pop().map(|p| p.0)
    }
    fn pop_back(&mut self) -> Option<T> {
        if self.len() == 0 {
            return None;
        }
        if self.back.is_empty() {
            let a = self.front.clone();
            let m = self.front.len() / 2;
            self.front.clear();
            let (f, b) = a.split_at(m);
            for v in f.iter().rev() {
                self.push_front(v.0.clone());
            }
            for v in b.iter() {
                self.push_back(v.0.clone());
            }
        }
        self.back.pop().map(|p| p.0)
    }
}
// ---------- end Foldable Deque ----------
//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より
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_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_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")
    };
}

//

fn run() {
    input! {
        n: usize,
        k: usize,
        m: usize,
        a: [f64; n],
    }
    let mut sum = a.clone();
    sum.push(0f64);
    for i in (0..n).rev() {
        sum[i] += sum[i + 1];
    }
    /*
    let inf = 1e14;
    let mut dp = vec![-inf; n + 1];
    dp[n] = 0f64;
    for _ in 0..k {
        let mut next = vec![-inf; n + 1];
        for i in (0..n).rev() {
            let mut val = -inf;
            for j in ((i + 1)..=n).take(m) {
                val = val.max(dp[j] + (sum[i] - sum[j]).abs());
            }
            next[i] = val;
        }
        dp = next;
    }
    println!("{}", dp[0].round() as i64);
    */
    type T = (f64, f64);// f = |x - a| + b
    let eval = |f: &T, x: f64| -> f64 {
        f.1 + (f.0 - x).abs()
    };
    let merge = |a: &T, b: &T| -> T {
        let mut a = *a;
        let mut b = *b;
        if a.0 > b.0 {
            std::mem::swap(&mut a, &mut b);
        }
        if eval(&a, a.0) > eval(&b, a.0) + 0.25 {
            return a;
        }
        if eval(&a, b.0) + 0.25 < eval(&b, b.0) {
            return b;
        }
        let x = (a.0 - a.1 + b.0 + b.1) * 0.5;
        (x, eval(&a, x))
    };
    let mut deq = FoldableDeque::new(merge);
    let inf = 1e14;
    let mut dp = vec![-inf; n + 1];
    dp[n] = 0f64;
    for _it in 0..k {
        deq.clear();
        deq.push_front((sum[n], dp[n]));
        for i in (0..n).rev() {
            let v = eval(&deq.find().unwrap(), sum[i]);
            deq.push_front((sum[i], dp[i]));
            dp[i] = v;
            if deq.len() > m {
                deq.pop_back();
            }
        }
    }
    let ans = dp[0].round() as i64;
    println!("{}", ans);
}

fn main() {
    run();
}
0