Open1

計算問題ダラダラ

kchokcho

https://atcoder.jp/contests/abc326/editorial/7537

ある区間の最大値を求める問題。配列などの線形データ構造を left, right の2重ポインタを用いて、区間をスライディングさせていく。

  • left_idx = 0, right_idx = 0 で初期化して条件を満たすまで right を動かす
  • 条件を満たさなくなったらその時点の最大値を記録
  • left を1つ動かし上記を繰り返す

#[allow(unused_imports)]
use proconio::marker::Usize1;
use proconio::{marker::Chars, *};
use std::{
    collections::{BTreeMap, HashMap, HashSet},
    rc::{self, Rc},
};

fn main() {
    input! {
        n: usize,
        m: usize,
        mut a: [usize; n],
    }
    a.sort();

    let mut ret = 0;
    let mut r = 0;
    for l in 0..n {
        while r < n && a[r] < a[l] + m {
            r += 1;
        }
        ret = ret.max(r - l);
    }
    println!("{}", ret);
}