結果

問題 No.576 E869120 and Rings
ユーザー snteasntea
提出日時 2018-05-22 00:42:24
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 788 ms / 1,500 ms
コード長 6,946 bytes
コンパイル時間 13,881 ms
コンパイル使用メモリ 379,304 KB
実行使用メモリ 27,928 KB
最終ジャッジ日時 2024-06-28 15:40:59
合計ジャッジ時間 30,655 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 788 ms
26,008 KB
testcase_01 AC 782 ms
25,620 KB
testcase_02 AC 787 ms
25,872 KB
testcase_03 AC 785 ms
25,876 KB
testcase_04 AC 787 ms
26,008 KB
testcase_05 AC 787 ms
26,008 KB
testcase_06 AC 783 ms
25,748 KB
testcase_07 AC 786 ms
25,748 KB
testcase_08 AC 595 ms
23,964 KB
testcase_09 AC 587 ms
25,792 KB
testcase_10 AC 595 ms
24,084 KB
testcase_11 AC 600 ms
26,004 KB
testcase_12 AC 586 ms
26,260 KB
testcase_13 AC 618 ms
25,888 KB
testcase_14 AC 652 ms
27,928 KB
testcase_15 AC 442 ms
18,068 KB
testcase_16 AC 588 ms
23,960 KB
testcase_17 AC 660 ms
27,928 KB
testcase_18 AC 578 ms
23,884 KB
testcase_19 AC 575 ms
23,956 KB
testcase_20 AC 573 ms
26,004 KB
testcase_21 AC 577 ms
25,876 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `std::fs::File`
   --> src/main.rs:140:5
    |
140 | use std::fs::File;
    |     ^^^^^^^^^^^^^
    |
    = note: `#[warn(unused_imports)]` on by default

warning: unused macro definition: `readlf`
   --> src/main.rs:143:14
    |
143 | macro_rules! readlf {
    |              ^^^^^^
    |
    = note: `#[warn(unused_macros)]` on by default

warning: the item `Write` is imported redundantly
   --> src/main.rs:225:9
    |
141 | use std::io::prelude::*;
    |     ------------------- the item `Write` is already imported here
...
225 |     use std::io::Write;
    |         ^^^^^^^^^^^^^^

warning: unused macro definition: `printf`
   --> src/main.rs:232:18
    |
232 |     macro_rules! printf { ($($arg:tt)*) => (write!(out, $($arg)*).unwrap()); }
    |                  ^^^^^^

warning: unused import: `std::io::prelude`
   --> src/main.rs:141:5
    |
141 | use std::io::prelude::*;
    |     ^^^^^^^^^^^^^^^^

warning: method `unwrap` is never used
   --> src/main.rs:118:8
    |
117 | impl<T: PartialOrd> Ordered<T> {
    | ------------------------------ method in this implementation
118 |     fn unwrap(self) -> T {
    |        ^^^^^^
    |
    = note: `#[warn(dead_code)]` on by default

ソースコード

diff #

#[allow(dead_code)]
fn getline() -> String {
    let mut res = String::new();
    std::io::stdin().read_line(&mut res).ok();
    res
}

#[allow(unused_macros)]
macro_rules! readl {
    ($t: ty) => {
        {
            let s = getline();
            s.trim().parse::<$t>().unwrap()
        }
    };
    ($( $t: ty),+ ) => {
        {
            let s = getline();
            let mut iter = s.trim().split(' ');
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*)
        }
    };
}

#[allow(unused_macros)]
macro_rules! readlvec {
    ($t:ty) => {{
        let s = getline();
        let iter = s.trim().split(' ');
        iter.map(|x| x.parse().unwrap()).collect::<Vec<$t>>()
    }};
}

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

#[allow(dead_code)]
fn show<T>(iter: T) -> String
where
    T: Iterator,
    T::Item: std::fmt::Display,
{
    let mut res = iter.fold(String::new(), |sum, e| sum + &format!("{} ", e));
    res.pop();
    res
}

fn sliding_minimum<T>(a: &Vec<T>, width: usize) -> Vec<T>
where
    T: std::cmp::PartialOrd + Clone
{
    let mut res = Vec::with_capacity(a.len() - width);
    let mut set = std::collections::VecDeque::new();

    for (i, e) in a.iter().enumerate().take(width) {
        while let Some((v, i)) = set.pop_back() {
            if v < e {
                set.push_back((v, i));
                break;
            }
        }
        set.push_back((e, i));
    }
    for (s, e) in a.iter().skip(width).enumerate() {
        res.push(set.front().expect("sliding_minimum error").0.clone());
        while let Some((v, i)) = set.pop_back() {
            if v < e {
                set.push_back((v, i));
                break;
            }
        }
        set.push_back((e, s + width));
        while let Some((v, i)) = set.pop_front() {
            if s + 1 <= i {
                set.push_front((v, i));
                break;
            }
        }
    }
    res.push(set.front().expect("sliding_minimum error").0.clone());

    res
}

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::btree_map::Entry::{Occupied, Vacant};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

#[derive(PartialEq, PartialOrd, Debug, Clone)]
struct Ordered<T: PartialOrd>(T);

impl<T> Eq for Ordered<T>
where
    T: PartialOrd + Clone,
{
}

impl<T> Ord for Ordered<T>
where
    T: PartialOrd + Clone,
{
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.partial_cmp(other).unwrap()
    }
}

impl<T: Copy + PartialOrd> Copy for Ordered<T> {}

impl<T: PartialOrd> Ordered<T> {
    fn unwrap(self) -> T {
        let Ordered(res) = self;
        res
    }
}

#[derive(Eq, PartialEq, Clone, Debug)]
struct Reverse<T>(T);

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

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


use std::fs::File;
use std::io::prelude::*;

macro_rules! readlf {
    ($br: expr, $t: ty) => {
        {
            let mut s = String::new();
            $br.read_line(&mut s);
            s.trim().parse::<$t>().expect("parse error!")
        }
    };
    ($br: expr, $( $t: ty),+ ) => {
        {
            let mut s = String::new();
            $br.read_line(&mut s);
            let mut iter = s.trim().split(' ');
            ($(iter.next().unwrap().parse::<$t>().expect("parse error!"),)*)
        }
    };
}


use std::io::BufRead;

pub struct StdinReader<R: BufRead> {
    pub reader: R,
    pub buf: String,
}

impl<R: BufRead> StdinReader<R> {
    pub fn new(reader: R) -> Self {
        Self {
            reader,
            buf: String::new(),
        }
    }
}

macro_rules! get {
    ($r:expr, $t:ty) => {
        {
            let mut line = &mut $r.buf;
            line.clear();
            $r.reader.read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($r:expr, $($t:ty),*) => {
        {
            let mut line = &mut $r.buf;
            line.clear();
            $r.reader.read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            (
                $(iter.next().unwrap().parse::<$t>().unwrap(),)*
            )
        }
    };
    ($r:expr, $t:ty; $n:expr) => {
        (0..$n).map(|_|
            get!($r, $t)
        ).collect::<Vec<_>>()
    };
    ($r:expr, $($t:ty),*; $n:expr) => {
        (0..$n).map(|_|
            get!($r, $($t),*)
        ).collect::<Vec<_>>()
    };
    ($r:expr, $t:ty ;;) => {
        {
            let mut line = &mut $r.buf;
            line.clear();
            $r.reader.read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|t| t.parse::<$t>().unwrap())
                .collect::<Vec<_>>()
        }
    };
    ($r:expr, $t:ty ;; $n:expr) => {
        (0..$n).map(|_| get!($r, $t ;;)).collect::<Vec<_>>()
    };
}


fn main() {
    use std::io::Write;
    let input = std::io::stdin();
    let mut r = StdinReader::new(input.lock());


    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    macro_rules! printf { ($($arg:tt)*) => (write!(out, $($arg)*).unwrap()); }
    macro_rules! printfln { () => (writeln!(out).unwrap()); ($($arg:tt)*) => (writeln!(out, $($arg)*).unwrap()); }

    let (n, k) = get!(r, usize, usize);
    let a: Vec<i32> = get!(r, String)
        .into_bytes()
        .into_iter()
        .map(|x| if x == b'0' { 0 } else { 1 })
        .collect();

    
    
    
    if n == 1 {
        printfln!("{}", a[0]);
        return;
    }
    let check = |x: f64| {
        let mut a: Vec<_> = vec![Reverse(Ordered(0.))].into_iter()
            .chain(
                a.iter().cloned()
                .chain(a.iter().cloned())
                .map(|e| Reverse(Ordered(e as f64 - x)))
            ).collect();
        debug!(a);
        for i in 0..a.len() - 1 {
            (a[i + 1].0).0 += (a[i].0).0;
        }
        debug!(a);
        let maxi: Vec<_> = sliding_minimum(
            &a,
            n - k,
        ).into_iter().map(|Reverse(Ordered(x))| x).collect();
        debug!(maxi);
        for (Reverse(Ordered(l)), &rmax) in a.into_iter().take(n+1).zip(maxi.iter().cycle().skip(k)) { 
            if l < rmax {
                return false;
            }
        }
        true
    };
    debug!(check(1.));
    let mut l = 0.;
    let mut r = 1.;
    for _ in 0..20 {
        let m = (l + r) / 2.;
        let res = check(m);
        // debug!(res);
        if res {
            r = m;
        } else {
            l = m;
        }
    }

    printfln!("{}", r);
}
0