結果

問題 No.2304 Distinct Elements
ユーザー koba-e964koba-e964
提出日時 2023-06-09 09:42:22
言語 Rust
(1.77.0)
結果
AC  
実行時間 82 ms / 3,000 ms
コード長 3,453 bytes
コンパイル時間 1,791 ms
コンパイル使用メモリ 165,200 KB
実行使用メモリ 4,780 KB
最終ジャッジ日時 2023-08-30 06:13:30
合計ジャッジ時間 8,694 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 49 ms
4,708 KB
testcase_05 AC 49 ms
4,708 KB
testcase_06 AC 48 ms
4,704 KB
testcase_07 AC 48 ms
4,704 KB
testcase_08 AC 49 ms
4,756 KB
testcase_09 AC 81 ms
4,780 KB
testcase_10 AC 81 ms
4,724 KB
testcase_11 AC 82 ms
4,752 KB
testcase_12 AC 82 ms
4,704 KB
testcase_13 AC 82 ms
4,700 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,384 KB
testcase_29 AC 46 ms
4,376 KB
testcase_30 AC 5 ms
4,380 KB
testcase_31 AC 39 ms
4,380 KB
testcase_32 AC 64 ms
4,380 KB
testcase_33 AC 6 ms
4,376 KB
testcase_34 AC 65 ms
4,380 KB
testcase_35 AC 72 ms
4,376 KB
testcase_36 AC 5 ms
4,376 KB
testcase_37 AC 15 ms
4,380 KB
testcase_38 AC 49 ms
4,504 KB
testcase_39 AC 51 ms
4,700 KB
testcase_40 AC 30 ms
4,380 KB
testcase_41 AC 15 ms
4,380 KB
testcase_42 AC 31 ms
4,708 KB
testcase_43 AC 65 ms
4,692 KB
testcase_44 AC 57 ms
4,380 KB
testcase_45 AC 38 ms
4,380 KB
testcase_46 AC 30 ms
4,376 KB
testcase_47 AC 2 ms
4,376 KB
testcase_48 AC 1 ms
4,376 KB
testcase_49 AC 56 ms
4,696 KB
testcase_50 AC 62 ms
4,696 KB
testcase_51 AC 60 ms
4,708 KB
testcase_52 AC 40 ms
4,764 KB
testcase_53 AC 48 ms
4,748 KB
testcase_54 AC 34 ms
4,704 KB
testcase_55 AC 33 ms
4,712 KB
testcase_56 AC 32 ms
4,708 KB
testcase_57 AC 48 ms
4,704 KB
testcase_58 AC 45 ms
4,780 KB
testcase_59 AC 35 ms
4,752 KB
testcase_60 AC 1 ms
4,376 KB
testcase_61 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: associated function `sliding_min` is never used
  --> Main.rs:74:8
   |
74 |     fn sliding_min(&mut self, a: i64, b: i64) {
   |        ^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

ソースコード

diff #

// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); }
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) { if *self < x { *self = x; } }
    fn chmin(&mut self, x: T) { if *self > x { *self = x; } }
}

// https://maspypy.com/slope-trick-1-%e8%a7%a3%e8%aa%ac%e7%b7%a8
#[derive(Clone, Debug, Default)]
struct SlopeTrick {
    l: std::collections::BinaryHeap<i64>,
    r: std::collections::BinaryHeap<std::cmp::Reverse<i64>>,
    ladd: i64,
    radd: i64,
    mi: i64,
}

impl SlopeTrick {
    fn new() -> Self {
        Self::default()
    }
    fn min(&self) -> i64 {
        self.mi
    }
    #[allow(unused)]
    fn add_const(&mut self, a: i64) {
        self.mi += a;
    }
    // self += max(0, x - a)
    fn add_plus(&mut self, a: i64) {
        self.l.push(a - self.ladd);
        let x = self.l.pop().unwrap() + self.ladd;
        self.r.push(std::cmp::Reverse(x - self.radd));
        self.mi += std::cmp::max(0, x - a);
    }
    // self += max(0, a - x)
    fn add_minus(&mut self, a: i64) {
        self.r.push(std::cmp::Reverse(a - self.radd));
        let x = self.r.pop().unwrap().0 + self.radd;
        self.l.push(x - self.ladd);
        self.mi += std::cmp::max(0, a - x);
    }
    // self <- min(self(y) where x-b <= y <= x-a)
    fn sliding_min(&mut self, a: i64, b: i64) {
        self.ladd += a;
        self.radd += b;
    }
    // self <- min(self(y) where y <= x-a)
    fn sliding_min_left(&mut self, a: i64) {
        self.ladd += a;
        self.r.clear();
    }
}

// https://yukicoder.me/problems/no/2304 (4)
// a はソートされていると、また最終的な配列もソートされていると仮定して良い。
// i 番目の要素を x にするときの最小コストを f(x) とすると、i+1 番目の要素を x にするときの最小コストは
// min_{u < x} f(u) + |a[i+1] - x| である。これは slope trick (左からの累積 min と右に 1 シフト) でできる。
// Tags: slope-trick
fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}

fn solve() {
    input! {
        n: usize,
        a: [i64; n],
    }
    let mut a = a;
    a.sort();
    let mut st = SlopeTrick::new();
    for &a in &a {
        st.sliding_min_left(1);
        st.add_plus(a);
        st.add_minus(a);
    }
    println!("{}", st.min());
}
0