結果

問題 No.609 Noelちゃんと星々
ユーザー cympfhcympfh
提出日時 2017-12-17 16:15:43
言語 Rust
(1.77.0)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 2,153 bytes
コンパイル時間 2,883 ms
コンパイル使用メモリ 144,052 KB
実行使用メモリ 8,500 KB
最終ジャッジ日時 2023-08-22 04:31:37
合計ジャッジ時間 6,062 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
6,904 KB
testcase_01 AC 45 ms
4,472 KB
testcase_02 AC 31 ms
4,376 KB
testcase_03 AC 41 ms
4,376 KB
testcase_04 AC 21 ms
4,376 KB
testcase_05 AC 111 ms
7,848 KB
testcase_06 AC 94 ms
7,240 KB
testcase_07 AC 56 ms
5,124 KB
testcase_08 AC 12 ms
4,380 KB
testcase_09 AC 36 ms
4,376 KB
testcase_10 AC 69 ms
5,744 KB
testcase_11 AC 65 ms
5,464 KB
testcase_12 AC 81 ms
6,340 KB
testcase_13 AC 15 ms
4,380 KB
testcase_14 AC 7 ms
4,376 KB
testcase_15 AC 97 ms
7,264 KB
testcase_16 AC 115 ms
8,140 KB
testcase_17 AC 88 ms
6,916 KB
testcase_18 AC 31 ms
4,376 KB
testcase_19 AC 94 ms
7,184 KB
testcase_20 AC 118 ms
8,428 KB
testcase_21 AC 118 ms
8,396 KB
testcase_22 AC 118 ms
8,500 KB
testcase_23 AC 117 ms
8,460 KB
testcase_24 AC 120 ms
8,492 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: constant `M` is never used
  --> Main.rs:15:7
   |
15 | const M: usize = 1_000_000_007;
   |       ^
   |
   = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

ソースコード

diff #

#![allow(unused_imports)]
use std::io::{ self, Write };
use std::str::FromStr;
use std::cmp::{ min, max };
use std::collections::{ BinaryHeap, VecDeque };

#[allow(unused_macros)]
macro_rules! trace {
    ($var:expr) => {
        let _ = writeln!(&mut std::io::stderr(), ">>> {} = {:?}", stringify!($var), $var);
    };
    ($($args:expr),*) => { trace!(($($args),*)) }
}

const M: usize = 1_000_000_007;

fn sum(ys: &Vec<i64>, r: i64) -> i64 {
    let mut s = 0;
    for &y in ys.iter() { s += abs(y - r); }
    s
}

fn minimum(ys: &Vec<i64>) -> i64 {
    let mut s = ys[0];
    for i in 1..ys.len() { s = min(s, ys[i]); }
    s
}

fn maximum(ys: &Vec<i64>) -> i64 {
    let mut s = ys[0];
    for i in 1..ys.len() { s = max(s, ys[i]); }
    s
}

fn abs(x: i64) -> i64 {
    if x < 0 { -x } else { x }
}

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.cin();
    let mut ys = vec![0; n];
    for i in 0..n { ys[i] = sc.cin(); }

    let mut left = minimum(&ys);
    let mut right = maximum(&ys);

    for _ in 0..1000 {
        let r1 = (left * 2 + right) / 3;
        let r2 = (left + right * 2) / 3;
        let s1 = sum(&ys, r1);
        let s2 = sum(&ys, r2);
        if s1 < s2 {
            right = r2;
        } else {
            left = r1;
        }
    }

    let mut ans = sum(&ys, 0);
    for r in (left-10)..(right+10) {
        let s = sum(&ys, r);
        ans = min(ans, s);
    }
    println!("{}", ans);
}

#[allow(dead_code)]
struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
#[allow(dead_code)]
impl Scanner {
    fn new() -> Scanner { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn reserve(&mut self) {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
    }
    fn cin<T: FromStr>(&mut self) -> T {
        self.reserve();
        match self.buffer.pop_front().unwrap().parse::<T>() {
            Ok(a) => a,
            Err(_) => panic!("parse err")
        }
    }
}
0