結果

問題 No.738 平らな農地
ユーザー koba-e964koba-e964
提出日時 2018-12-13 00:53:34
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 4,743 bytes
コンパイル時間 5,619 ms
コンパイル使用メモリ 194,292 KB
実行使用メモリ 8,420 KB
最終ジャッジ日時 2023-10-25 08:47:14
合計ジャッジ時間 10,515 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 9 ms
4,348 KB
testcase_06 AC 10 ms
4,348 KB
testcase_07 AC 91 ms
4,348 KB
testcase_08 AC 11 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 17 ms
4,348 KB
testcase_12 AC 5 ms
4,348 KB
testcase_13 AC 12 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 TLE -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
testcase_65 -- -
testcase_66 -- -
testcase_67 -- -
testcase_68 -- -
testcase_69 -- -
testcase_70 -- -
testcase_71 -- -
testcase_72 -- -
testcase_73 -- -
testcase_74 -- -
testcase_75 -- -
testcase_76 -- -
testcase_77 -- -
testcase_78 -- -
testcase_79 -- -
testcase_80 -- -
testcase_81 -- -
testcase_82 -- -
testcase_83 -- -
testcase_84 -- -
testcase_85 -- -
testcase_86 -- -
testcase_87 -- -
testcase_88 -- -
testcase_89 -- -
testcase_90 -- -
testcase_91 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($format:expr) => (write!(out,$format).unwrap());
        ($format:expr, $($args:expr),+) => (write!(out,$format,$($args),*).unwrap())
    }
    let n;
    let k;
    let mut a;
    if true {
        input! {
            n_: usize,
            k_: usize,
            a_: [i64; n_],
        }
        n = n_;
        k = k_;
        a = a_;
    } else {
        n = 100000;
        k = 50000;
        a = vec![0; n];
        for i in 0 .. n {
            a[i] = i as i64 + 1;
        }
    }
    let b = 1000usize;
    let b = max(b, 1);
    let sz = (n + b - 1) / b;
    let mut acc = vec![vec![0; b + 1]; sz];
    const INF: i64 = 1 << 32;
    let mut srt = vec![INF; b * sz];
    for i in 0 .. n {
        srt[i] = 2 * a[i] + 1;
    }
    for i in 0 .. sz {
        srt[i * b .. i * b + b].sort();
        for j in 0 .. b {
            acc[i][j + 1] = acc[i][j] + srt[i * b + j] / 2;
        }
    }
    let mut mi: i64 = 1 << 50;
    for i in 0 .. n - k + 1 {
        let lb = (i + b - 1) / b;
        let rb = (i + k) / b;
        // binsect
        let mut pass = 0;
        let mut fail = INF;
        while fail - pass > 1 {
            let mid = (fail + pass) / 2;
            let mut count = 0;
            if lb > rb {
                for j in i .. i + k {
                    if a[j] <= mid { count += 1; }
                }
            } else {
                for j in i .. b * lb {
                    if a[j] <= mid { count += 1; }
                }
                for j in lb .. rb {
                    let idx = srt[j * b .. j * b + b].binary_search(&(2 * mid + 2)).unwrap_err();
                    count += idx;
                }
                for j in b * rb .. i + k {
                    if a[j] <= mid { count += 1; }
                }
            }
            if count <= (k - 1) / 2 { pass = mid; }
            else { fail = mid; }
        }
        let med = fail;
        let mut cost = 0;
        if lb > rb {
            for j in i .. i + k {
                cost += (a[j] - med).abs();
            }
        } else {
            for j in i .. b * lb {
                cost += (a[j] - med).abs();
            }
            for j in lb .. rb {
                let idx = srt[j * b .. j * b + b].binary_search(&(2 * med + 2)).unwrap_err();
                cost += idx as i64 * med - acc[j][idx] + (acc[j][b] - acc[j][idx]) - (b - idx) as i64 * med;
            }
            for j in b * rb .. i + k {
                cost += (a[j] - med).abs();
            }
        }
        // eprintln!("i = {}, med = {}, cost = {}, h = {:?}", i, med, cost, &a[i .. i + k]);
        mi = min(mi, cost);
    }
    puts!("{}\n", mi);
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}
0