結果

問題 No.489 株に挑戦
ユーザー hatoohatoo
提出日時 2017-10-18 01:08:57
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 58 ms / 1,000 ms
コード長 4,325 bytes
コンパイル時間 25,339 ms
コンパイル使用メモリ 400,904 KB
実行使用メモリ 18,328 KB
最終ジャッジ日時 2024-07-19 23:57:42
合計ジャッジ時間 16,007 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
18,012 KB
testcase_01 AC 29 ms
17,904 KB
testcase_02 AC 5 ms
17,476 KB
testcase_03 AC 7 ms
17,476 KB
testcase_04 AC 5 ms
17,460 KB
testcase_05 AC 5 ms
17,520 KB
testcase_06 AC 5 ms
17,524 KB
testcase_07 AC 6 ms
17,444 KB
testcase_08 AC 6 ms
17,344 KB
testcase_09 AC 6 ms
17,424 KB
testcase_10 AC 6 ms
17,396 KB
testcase_11 AC 6 ms
17,452 KB
testcase_12 AC 6 ms
17,288 KB
testcase_13 AC 6 ms
17,440 KB
testcase_14 AC 6 ms
17,488 KB
testcase_15 AC 7 ms
17,472 KB
testcase_16 AC 51 ms
18,196 KB
testcase_17 AC 10 ms
17,580 KB
testcase_18 AC 29 ms
17,872 KB
testcase_19 AC 24 ms
17,776 KB
testcase_20 AC 50 ms
18,148 KB
testcase_21 AC 16 ms
17,612 KB
testcase_22 AC 8 ms
17,528 KB
testcase_23 AC 29 ms
17,852 KB
testcase_24 AC 58 ms
18,220 KB
testcase_25 AC 5 ms
17,412 KB
testcase_26 AC 52 ms
18,208 KB
testcase_27 AC 53 ms
18,328 KB
testcase_28 AC 6 ms
17,492 KB
testcase_29 AC 5 ms
17,444 KB
testcase_30 AC 51 ms
18,220 KB
testcase_31 AC 58 ms
18,232 KB
testcase_32 AC 32 ms
17,796 KB
testcase_33 AC 54 ms
18,072 KB
testcase_34 AC 49 ms
18,164 KB
testcase_35 AC 22 ms
17,704 KB
testcase_36 AC 11 ms
17,584 KB
testcase_37 AC 31 ms
17,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::{max, min, Ordering, Reverse};
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet};

mod util {
    use std::io::stdin;
    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 get<T: FromStr>() -> T
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().parse().unwrap()
    }

    #[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 get2<T: FromStr, U: FromStr>() -> (T, U)
    where
        <T as FromStr>::Err: Debug,
        <U as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        let mut iter = line.split_whitespace();
        (
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap(),
        )
    }

    #[allow(dead_code)]
    pub fn get3<S: FromStr, T: FromStr, U: FromStr>() -> (S, T, U)
    where
        <S as FromStr>::Err: Debug,
        <T as FromStr>::Err: Debug,
        <U as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        let mut iter = line.split_whitespace();
        (
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap(),
        )
    }
}


#[allow(unused_macros)]
macro_rules! debug {
    ($x: expr) => {
        println!("{}: {:?}", stringify!($x), $x)
    }
}

#[derive(Eq, PartialEq, Clone)]
struct Rev<T>(pub T);

impl<T: PartialOrd> PartialOrd for Rev<T> {
    fn partial_cmp(&self, other: &Rev<T>) -> Option<Ordering> {
        other.0.partial_cmp(&self.0)
    }
}

impl<T: Ord> Ord for Rev<T> {
    fn cmp(&self, other: &Rev<T>) -> Ordering {
        other.0.cmp(&self.0)
    }
}

struct SEG {
    n: usize,
    buf: Vec<(u64, Rev<usize>)>,
}

impl SEG {
    fn new(n: usize) -> SEG {
        let n = (1..).find(|&d| 2usize.pow(d) > n).unwrap();
        SEG {
            n: 2usize.pow(n),
            buf: vec![(0, Rev(0)); 1000000],
        }
    }

    fn update(&mut self, k: usize, a: (u64, Rev<usize>)) {
        let mut k = k + self.n - 1;
        self.buf[k] = a;

        while k > 0 {
            k = (k - 1) / 2;
            self.buf[k] = max(self.buf[k * 2 + 1].clone(), self.buf[k * 2 + 2].clone());
        }
    }

    fn query(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> (u64, Rev<usize>) {
        if r <= a || b <= l {
            return (0, Rev(0));
        }

        if a <= l && r <= b {
            self.buf[k].clone()
        } else {
            let vl = self.query(a, b, k * 2 + 1, l, (l + r) / 2);
            let vr = self.query(a, b, k * 2 + 2, (l + r) / 2, r);
            max(vl, vr)
        }
    }

    fn q(&self, a: usize, b: usize) -> (u64, Rev<usize>) {
        self.query(a, b, 0, 0, self.n)
    }
}

/* Can't
impl<T: Clone> Clone for Reverse<T> {
    fn clone(&self) -> Reverse<T> {
        Reverse(self.0.clone())
    }
}
*/

fn main() {
    let (n, d, k): (usize, usize, u64) = util::get3();
    let xs: Vec<u64> = (0..n).map(|_| util::get()).collect();

    let mut seg = SEG::new(n);

    for (i, &x) in xs.iter().enumerate() {
        seg.update(i, (x, Rev(i)));
    }

    let ans = (0..n - 1)
        .filter_map(|i| {
            let r = min(i + 1 + d, n);
            let (maximum, rev) = seg.q(i + 1, r);
            if maximum > xs[i] {
                Some((maximum - xs[i], Rev((i, rev.0))))
            } else {
                None
            }
        })
        .max();

    if let Some((profit, Rev((buy, sell)))) = ans {
        println!("{}", profit * k);
        println!("{} {}", buy, sell);
    } else {
        println!("0");
    }
}
0