Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
682 B
2
Indexable
#include <iostream>
#include <algorithm>

using namespace std;

int a[100000];

int main() {
    freopen("DUONGKINH.inp", "r", stdin);
    freopen("DUONGKINH.out", "w", stdout);

    // input, sort
    int n, k; cin >> n >> k;
    for(int i = 0; i < n; i++) cin >> a[i];
    sort(a, a+n);
    cout << a[n-1] - a[0] << endl;

    int d, l = 0, len = 0, ans = 0;
    // 2 con tro cung chieu
    for(int r = 0; r < n; r++){
        d = a[r] - a[l];
        if(d <= k){
            ++len;
            ans = max(ans, len);
        }
        else{
            ++l;
            --len;
        }
    }
    cout << n - ans; // y tuong: tim ans la day con dai nhat co D <= k 

    return 0;
}
Leave a Comment