結果

問題 No.489 株に挑戦
ユーザー mio_h
提出日時 2017-02-24 23:22:53
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 55 ms / 1,000 ms
コード長 2,735 bytes
コンパイル時間 15,158 ms
コンパイル使用メモリ 379,260 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2024-07-19 23:05:35
合計ジャッジ時間 16,084 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: use of deprecated method `std::error::Error::description`: use the Display impl or to_string()
  --> src/main.rs:69:62
   |
69 |             Err(why) => panic!("error in read_line: {}", why.description()),
   |                                                              ^^^^^^^^^^^
   |
   = note: `#[warn(deprecated)]` on by default

ソースコード

diff #

use std::io::{self, Stdin};
use std::str::{self, FromStr};
use std::error::Error;
use std::collections::BTreeSet;
fn exec() {
    let mut sc = Scanner::new();
    let n: usize = sc.ne();
    let d: usize = sc.ne();
    let k: i64 = sc.ne();
    let a: Vec<i64> = (0..n).map(|_| sc.ne()).collect();
    let mut s = BTreeSet::new();
    for i in 0..d {
        s.insert((a[i] * -1, i));
    }
    let mut ma = 0;
    let mut id = (0, 0);
    for i in 0..n {
        let nxt = i + d;
        if nxt < n {
            s.insert((a[nxt] * -1, nxt));
        }
        let rem = a[i] * -1;
        s.remove(&(rem, i));
        if let Some(x) = s.iter().next() {
            let tmp = (x.0 * -1) - a[i];
            if ma < tmp {
                ma = tmp;
                id = (i, x.1);
            }
        }
    }
    println!("{}", ma * k);
    if ma != 0 {
        println!("{} {}", id.0, id.1);
    }
}

fn main() {
    const STACK: usize = 16 * 1024 * 1024;
    let _ = std::thread::Builder::new()
        .stack_size(STACK)
        .spawn(|| { exec(); })
        .unwrap()
        .join()
        .unwrap();
}

#[allow(dead_code)]
struct Scanner {
    stdin: Stdin,
    id: usize,
    buf: Vec<u8>,
}

#[allow(dead_code)]
impl Scanner {
    fn new() -> Scanner {
        Scanner {
            stdin: io::stdin(),
            id: 0,
            buf: Vec::new(),
        }
    }
    fn next_line(&mut self) -> Option<String> {
        let mut res = String::new();
        match self.stdin.read_line(&mut res) {
            Ok(0) => None,
            Ok(_) => Some(res),
            Err(why) => panic!("error in read_line: {}", why.description()),
        }
    }
    fn next<T: FromStr>(&mut self) -> Option<T> {
        while self.buf.len() == 0 {
            self.buf = match self.next_line() {
                Some(r) => {
                    self.id = 0;
                    r.trim().as_bytes().to_owned()
                }
                None => return None,
            };
        }
        let l = self.id;
        assert!(self.buf[l] != b' ');
        let n = self.buf.len();
        let mut r = l;
        while r < n && self.buf[r] != b' ' {
            r += 1;
        }
        let res = match str::from_utf8(&self.buf[l..r]).ok().unwrap().parse::<T>() {
            Ok(s) => Some(s),
            Err(_) => {
                panic!("parse error, {:?}",
                       String::from_utf8(self.buf[l..r].to_owned()))
            }
        };
        while r < n && self.buf[r] == b' ' {
            r += 1;
        }
        if r == n {
            self.buf.clear();
        } else {
            self.id = r;
        }
        res
    }
    fn ne<T: FromStr>(&mut self) -> T {
        self.next::<T>().unwrap()
    }
}
0