https://www.nowcoder.com/practice/562630ca90ac40ce89443c91060574c6?tpId=308&tqId=40490&ru=/exam/oj
排序 + 滑动窗口
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;int main() {int n, k;cin >> n >> k;vector<int> v(n);for (int i = 0; i < n; ++i) {cin >> v[i];}sort(v.begin(), v.end());// 滑动窗口int ret = 0;for (int left=0, right=1; right < n; ++right) {while(v[right] - v[left] > k) {++left;}int len = right - left + 1;if (ret < len) ret = len;}cout << ret << endl;return 0;
}