70 lines
1.5 KiB
C++
70 lines
1.5 KiB
C++
#include <algorithm>
|
|
#include <climits>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <iostream>
|
|
#include <vector>
|
|
using namespace std;
|
|
|
|
// ----- FILE IO -----
|
|
#define STDIN(filename) freopen(filename, "r", stdin)
|
|
#define STDOUT(filename) freopen(filename, "w", stdout)
|
|
#define NL '\n'
|
|
|
|
// ----- FOR LOOPS -----
|
|
#define F(i,L,R) for (int i = L; i < R; i++)
|
|
#define FE(i,L,R) for (int i = L; i <= R; i++)
|
|
#define FD(i,L,R) for (int i = L; i > R; i--)
|
|
#define FDE(i,L,R) for (int i = L; i >= R; i--)
|
|
|
|
// ----- WEZ -----
|
|
#define takeI(n) int (n); cin >> n
|
|
#define takeII(n,m) int (n),(m); cin >> n >> m
|
|
#define takeIII(n,m,k) int (n),(m),(k); cin >>(n) >> (m) >> (k)
|
|
#define whileZ takeI(T); while(T--)
|
|
//
|
|
// ----- MAX MIN -----
|
|
#define REMAX(a,b) (a)=max((a),(b))
|
|
#define REMIN(a,b) (a)=min((a),(b))
|
|
|
|
// ----- DEBUG -----
|
|
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
|
|
#define printA(a,L,R) FE(i,L,R) cerr << a[i] << (i==R?'\n':' ')
|
|
#define printV(a) printA(a,0,a.size()-1)
|
|
|
|
// ----- STL DS
|
|
#define ALL(c) (c).begin(),(c).end()
|
|
|
|
int main() {
|
|
STDIN("diamond.in");
|
|
STDOUT("diamond.out");
|
|
|
|
takeII(n,k);
|
|
vector<int> s(n+1), b1(n), b2(n);
|
|
s[n] = INT_MAX;
|
|
F(i,0,n) cin >> s[i];
|
|
sort(ALL(s));
|
|
|
|
int l = 0, big = 0;
|
|
F(r,0,n) {
|
|
while (s[r] - s[l] >= k+1) l++;
|
|
REMAX(big, r-l+1);
|
|
b1[r] = big;
|
|
}
|
|
|
|
int r = n-1;
|
|
big = 0;
|
|
FD(l,n-1,-1) {
|
|
while (s[r] - s[l] >= k+1) r--;
|
|
REMAX(big, r-l+1);
|
|
b2[l] = big;
|
|
}
|
|
|
|
int sol = 0;
|
|
F(i,1,n) {
|
|
REMAX(sol, b1[i-1] + b2[i]);
|
|
}
|
|
|
|
cout << sol;
|
|
}
|