結果

問題 No.409 ダイエット
ユーザー hatoohatoo
提出日時 2018-01-26 09:46:21
言語 Rust
(1.77.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 4,744 bytes
コンパイル時間 3,380 ms
コンパイル使用メモリ 148,940 KB
実行使用メモリ 6,548 KB
最終ジャッジ日時 2023-09-08 17:38:33
合計ジャッジ時間 7,938 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 1 ms
4,380 KB
testcase_46 AC 1 ms
4,376 KB
testcase_47 AC 1 ms
4,380 KB
testcase_48 AC 1 ms
4,384 KB
testcase_49 AC 1 ms
4,376 KB
testcase_50 AC 1 ms
4,376 KB
testcase_51 AC 1 ms
4,380 KB
testcase_52 AC 1 ms
4,380 KB
testcase_53 AC 1 ms
4,376 KB
testcase_54 AC 1 ms
4,376 KB
testcase_55 AC 10 ms
4,376 KB
testcase_56 AC 16 ms
6,464 KB
testcase_57 AC 18 ms
6,548 KB
testcase_58 AC 8 ms
4,380 KB
testcase_59 AC 12 ms
4,824 KB
testcase_60 AC 8 ms
4,380 KB
testcase_61 AC 16 ms
5,960 KB
testcase_62 AC 15 ms
6,344 KB
testcase_63 AC 17 ms
5,936 KB
testcase_64 AC 9 ms
4,376 KB
testcase_65 AC 19 ms
6,084 KB
testcase_66 AC 18 ms
6,500 KB
testcase_67 AC 14 ms
5,436 KB
testcase_68 AC 13 ms
4,848 KB
testcase_69 AC 17 ms
5,940 KB
testcase_70 AC 19 ms
5,564 KB
testcase_71 AC 11 ms
4,376 KB
testcase_72 AC 22 ms
6,512 KB
testcase_73 AC 23 ms
6,048 KB
testcase_74 AC 13 ms
4,484 KB
testcase_75 AC 22 ms
6,016 KB
testcase_76 AC 15 ms
4,912 KB
testcase_77 AC 12 ms
4,376 KB
testcase_78 AC 11 ms
4,380 KB
testcase_79 AC 10 ms
4,380 KB
testcase_80 AC 21 ms
6,120 KB
testcase_81 AC 22 ms
5,956 KB
testcase_82 AC 16 ms
4,992 KB
testcase_83 AC 17 ms
5,140 KB
testcase_84 AC 15 ms
4,544 KB
testcase_85 AC 3 ms
4,380 KB
testcase_86 AC 15 ms
4,512 KB
testcase_87 AC 19 ms
5,492 KB
testcase_88 AC 9 ms
4,376 KB
testcase_89 AC 18 ms
5,024 KB
testcase_90 AC 8 ms
4,376 KB
testcase_91 AC 19 ms
5,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *  _           _                 __                            _   _ _   _                                 _                    _                  _
 * | |         | |               / /                           | | (_) | (_)                               | |                  (_)                | |
 * | |__   __ _| |_ ___   ___   / /__ ___  _ __ ___  _ __   ___| |_ _| |_ ___   _____ ______ _ __ _   _ ___| |_ ______ ___ _ __  _ _ __  _ __   ___| |_ ___
 * | '_ \ / _` | __/ _ \ / _ \ / / __/ _ \| '_ ` _ \| '_ \ / _ \ __| | __| \ \ / / _ \______| '__| | | / __| __|______/ __| '_ \| | '_ \| '_ \ / _ \ __/ __|
 * | | | | (_| | || (_) | (_) / / (_| (_) | | | | | | |_) |  __/ |_| | |_| |\ V /  __/      | |  | |_| \__ \ |_       \__ \ | | | | |_) | |_) |  __/ |_\__ \
 * |_| |_|\__,_|\__\___/ \___/_/ \___\___/|_| |_| |_| .__/ \___|\__|_|\__|_| \_/ \___|      |_|   \__,_|___/\__|      |___/_| |_|_| .__/| .__/ \___|\__|___/
 *                                                  | |                                                                           | |   | |
 *                                                  |_|                                                                           |_|   |_|
 *
 * https://github.com/hatoo/competitive-rust-snippets
 */
#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};
mod util {
    use std::io::{stdin, stdout, BufWriter, StdoutLock};
    use std::str::FromStr;
    use std::fmt::Debug;
    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }
    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
    #[allow(dead_code)]
    pub fn with_bufwriter<F: FnOnce(BufWriter<StdoutLock>) -> ()>(f: F) {
        let out = stdout();
        let writer = BufWriter::new(out.lock());
        f(writer)
    }
}
#[allow(unused_macros)]
macro_rules ! get { ( $ t : ty ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . trim ( ) . parse ::<$ t > ( ) . unwrap ( ) } } ; ( $ ( $ t : ty ) ,* ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; let mut iter = line . split_whitespace ( ) ; ( $ ( iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) , ) * ) } } ; ( $ t : ty ; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ ( $ t : ty ) ,*; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ ( $ t ) ,* ) ) . collect ::< Vec < _ >> ( ) } ; ( $ t : ty ;; ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . split_whitespace ( ) . map ( | t | t . parse ::<$ t > ( ) . unwrap ( ) ) . collect ::< Vec < _ >> ( ) } } ; }
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { println ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }

type ConvexHullType = i64;
#[allow(dead_code)]
fn convex_hull_check(
    (a1, b1): (ConvexHullType, ConvexHullType),
    (a2, b2): (ConvexHullType, ConvexHullType),
    (a3, b3): (ConvexHullType, ConvexHullType),
) -> bool {
    (a2 as f64 - a1 as f64) * (b3 as f64 - b2 as f64)
        >= (b2 as f64 - b1 as f64) * (a3 as f64 - a2 as f64)
}

fn f(x: i64, (a, b): (i64, i64)) -> i64 {
    x * a + b
}

fn main() {
    let (n, a, b, w) = get!(usize, i64, i64, i64);
    let ds = get!(i64;;);

    let mut ls = VecDeque::new();
    ls.push_back((0, 0));

    let mut dp = vec![0; n + 1];

    for (&d, i) in ds.iter().zip(1..) {
        while ls.len() >= 2 && f(i, ls[0]) >= f(i, ls[1]) {
            ls.pop_front();
        }

        dp[i as usize] = d - a * i + a + b * i * (i - 1) / 2 + f(i, ls[0]);
        let l = (-b * i, dp[i as usize] + b * i * (i + 1) / 2 + a * i);

        while ls.len() >= 2 && convex_hull_check(ls[ls.len() - 2], ls[ls.len() - 1], l) {
            ls.pop_back();
        }
        ls.push_back(l);
    }

    println!(
        "{}",
        dp.iter()
            .zip(0i64..)
            .map(|(&x, i)| w + x - (n as i64 - i) * a + b * (n as i64 - i) * (n as i64 - i + 1) / 2)
            .min()
            .unwrap()
    );
}
0