import java.io.*;
import java.util.*;
public class glo {
public static void main(String[] args) throws IOException {
BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer l1 = new StringTokenizer(scan.readLine()); int num = Integer.parseInt(l1.nextToken()); int x = Integer.parseInt(l1.nextToken());
int[] arr = new int[num]; StringTokenizer st = new StringTokenizer(scan.readLine());
for (int i = 0;i<num;i++) arr[i] = Integer.parseInt(st.nextToken());
int[] pLIS = new int[num]; //For each i, store the LIS up to i that includes arr[i]
ArrayList<Integer> dp = new ArrayList<>();
for (int i = 0;i<num;i++) {
int pos = Collections.binarySearch(dp, arr[i]);
if (pos<0) pos = (-1)*pos-1;
if (pos==dp.size()) dp.add(arr[i]);
else dp.set(pos, arr[i]);
pLIS[i] = pos+1;
}
int[] sLIS = new int[num]; //For each i, store the LIS from i->num-1 that has minimum number > arr[i]-x
dp = new ArrayList<>();
for (int i = num-1;i>=0;i--) {
int idx = Collections.binarySearch(dp, arr[i]-x+1, (k,j)->j-k);
if (idx<0) idx = (-1)*idx-2;
sLIS[i] = idx+1;
int pos = Collections.binarySearch(dp, arr[i], (k,j)->j-k);
if (pos<0) pos = (-1)*pos-1;
if (pos==dp.size()) dp.add(arr[i]);
else dp.set(pos, arr[i]);
}
int ans = Math.max(pLIS[num-1], sLIS[0]);
for (int i = 0;i<num-1;i++) {
ans = Math.max(ans, pLIS[i]+sLIS[i]);
}
System.out.println(ans);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |