結果

問題 No.837 Noelちゃんと星々2
ユーザー Yukino DX.Yukino DX.
提出日時 2023-11-20 18:18:36
言語 Rust
(1.77.0)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 3,483 bytes
コンパイル時間 1,185 ms
コンパイル使用メモリ 183,772 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-11-20 18:18:40
合計ジャッジ時間 3,357 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
6,676 KB
testcase_01 AC 16 ms
6,676 KB
testcase_02 AC 16 ms
6,676 KB
testcase_03 AC 16 ms
6,676 KB
testcase_04 AC 16 ms
6,676 KB
testcase_05 AC 16 ms
6,676 KB
testcase_06 AC 16 ms
6,676 KB
testcase_07 AC 16 ms
6,676 KB
testcase_08 AC 16 ms
6,676 KB
testcase_09 AC 1 ms
6,676 KB
testcase_10 AC 1 ms
6,676 KB
testcase_11 AC 1 ms
6,676 KB
testcase_12 AC 0 ms
6,676 KB
testcase_13 AC 1 ms
6,676 KB
testcase_14 AC 1 ms
6,676 KB
testcase_15 AC 1 ms
6,676 KB
testcase_16 AC 0 ms
6,676 KB
testcase_17 AC 1 ms
6,676 KB
testcase_18 AC 0 ms
6,676 KB
testcase_19 AC 1 ms
6,676 KB
testcase_20 AC 1 ms
6,676 KB
testcase_21 AC 1 ms
6,676 KB
testcase_22 AC 1 ms
6,676 KB
testcase_23 AC 1 ms
6,676 KB
testcase_24 AC 1 ms
6,676 KB
testcase_25 AC 1 ms
6,676 KB
testcase_26 AC 1 ms
6,676 KB
testcase_27 AC 1 ms
6,676 KB
testcase_28 AC 1 ms
6,676 KB
testcase_29 AC 1 ms
6,676 KB
testcase_30 AC 1 ms
6,676 KB
testcase_31 AC 0 ms
6,676 KB
testcase_32 AC 1 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
// use itertools::Itertools;
// use proconio::input;
// use proconio::marker::*;
use std::cmp::Reverse;
use std::collections::*;

fn main() {
    input! {
        n:usize,
        y:[i64;n],
    }
    if y.iter().all(|yi| *yi == y[0]) {
        println!("1");
        return;
    }
    let mut y = y;
    y.sort();
    let tyuo = |y: &[i64]| {
        if y.len() % 2 == 0 {
            (y[y.len() / 2] + y[y.len() / 2 - 1]) / 2
        } else {
            y[y.len() / 2]
        }
    };
    let acc_y = y.cum();
    const INF: i64 = std::i64::MAX;
    let mut ans = INF;
    for i in 1..n {
        let tyuo0 = tyuo(&y[..i]);
        let tyuo1 = tyuo(&y[i..]);

        if tyuo0 == tyuo1 {
            continue;
        }

        let cost0 = if i % 2 == 0 {
            -acc_y[i / 2] + acc_y[i] - acc_y[i / 2]
        } else {
            -acc_y[i / 2] + acc_y[i] - acc_y[i / 2 + 1]
        };

        let cost1 = if (n - i) % 2 == 0 {
            -(acc_y[i + (n - i) / 2] - acc_y[i]) + acc_y[n] - acc_y[i + (n - i) / 2]
        } else {
            -(acc_y[i + (n - i) / 2] - acc_y[i]) + acc_y[n] - acc_y[i + (n - i) / 2 + 1]
        };

        ans = ans.min(cost0 + cost1);
    }

    println!("{}", ans);
}

use cumulative::*;
mod cumulative {
    use std::ops::Add;

    pub trait Cumulative<T: Default + Copy + Add> {
        fn cum(&self) -> Vec<T>;
        fn cum_rev(&self) -> Vec<T>;
    }

    impl<T> Cumulative<T> for [T]
    where
        T: Default + Copy + Add<Output = T>,
    {
        //cum[i]:=sum[0,i)
        fn cum(&self) -> Vec<T> {
            let n = self.len();
            let mut res = vec![T::default(); n + 1];
            for i in 0..n {
                res[i + 1] = res[i] + self[i];
            }

            res
        }

        //cum_rev[i]:=sum[i,n)
        fn cum_rev(&self) -> Vec<T> {
            let n = self.len();
            let mut res = vec![T::default(); n + 1];
            for i in (0..n).rev() {
                res[i] = res[i + 1] + self[i];
            }

            res
        }
    }
}
mod input {
    #[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, usize1) => {
            read_value!($iter, usize) - 1
        };

        ($iter:expr, $t:ty) => {
            $iter.next().unwrap().parse::<$t>().expect("Parse error")
        };
    }
}
0