結果

問題 No.489 株に挑戦
ユーザー tanzaku
提出日時 2017-02-24 20:31:30
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 20 ms / 1,000 ms
コード長 2,543 bytes
コンパイル時間 14,657 ms
コンパイル使用メモリ 378,788 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-19 22:49:30
合計ジャッジ時間 14,602 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(dead_code)]
fn get_line() -> Vec<String> {
    let stdin = std::io::stdin();
    let mut s = String::new();
    loop {
        stdin.read_line(&mut s).ok();
        let s = s.trim();
        if s.len() != 0 {
            return s.split_whitespace().map(|s| s.into()).collect();
        }
    }
}

#[allow(dead_code)]
fn read_array<T>() -> Vec<T> where
    T: std::str::FromStr + std::marker::Copy,
    <T as std::str::FromStr>::Err: std::fmt::Debug
{
    get_line().iter().map(|s| s.parse().unwrap()).collect()
}

#[allow(dead_code)]
fn read2<T, U>() -> (T, U) where
    T: std::str::FromStr + std::marker::Copy,
    <T as std::str::FromStr>::Err: std::fmt::Debug,
    U: std::str::FromStr + std::marker::Copy,
    <U as std::str::FromStr>::Err: std::fmt::Debug
{
    let ary = get_line();
    (ary[0].parse().unwrap(), ary[1].parse().unwrap())
}

#[allow(dead_code)]
fn read3<T, U, W>() -> (T, U, W) where
    T: std::str::FromStr + std::marker::Copy,
    <T as std::str::FromStr>::Err: std::fmt::Debug,
    U: std::str::FromStr + std::marker::Copy,
    <U as std::str::FromStr>::Err: std::fmt::Debug,
    W: std::str::FromStr + std::marker::Copy,
    <W as std::str::FromStr>::Err: std::fmt::Debug
{
    let ary = get_line();
    (ary[0].parse().unwrap(), ary[1].parse().unwrap(), ary[2].parse().unwrap())
}

#[allow(dead_code)]
fn read1<T>() -> T where
    T: std::str::FromStr + std::marker::Copy,
    <T as std::str::FromStr>::Err: std::fmt::Debug
{
    read_array()[0]
}

fn main() {
    let (n, d, k): (usize, i64, i64) = read3();
    let mut x = vec![0; n];
    for i in 0..n {
        x[i] = read1();
    }
    let mut l = 0;
    let mut r = 0;
    let mut ans = 0i64;

    let mut idx = 0;
    let mut max_value = Vec::new();
    for i in (0..n).rev() {
        max_value.push((x[i], -(i as i64)));
        while max_value.len() > 1 {
            let m = max_value.len();
            if max_value[m - 1] < max_value[m - 2] {
                break;
            }
            max_value[m - 2] = max_value[m - 1];
            max_value.pop();
        }
        idx = std::cmp::min(idx, max_value.len() - 1);
        while -max_value[idx].1 - (i as i64) > d {
            idx += 1
        }
        let (val, day) = max_value[idx];
        let profit = val - x[i];
        // println!("{} {} {} {} {}", i, idx, val, day, profit);
        if ans <= profit {
            ans = profit;
            l = i;
            r = -day;
        }
    }
    println!("{}", ans * k);
    if ans != 0 {
        println!("{} {}", l, r);
    }
}
0