This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
import java.io.*;
import java.util.*;
public class glo {
static int n, x;
static int[]t;
static int[] dt;
static int[] lenLIS;
public static int LIS1() {
ArrayList<Integer> dp = new ArrayList<Integer>();
for(int i = 0 ; i < dt.length ; ++i){
int pos = binarySearch(dp, dt[i]);
lenLIS[i] = pos + 1;
if (pos >= dp.size()) {
dp.add(dt[i]);
} else {
dp.set(pos, dt[i]);
}
}
return dp.size();
}
public static int LIS2() {
int res = 0;
ArrayList<Integer> dp = new ArrayList<Integer>();
for(int i = 0 ; i < t.length ; ++i){
int pos = binarySearch(dp, t[i]);
res = Math.max(res, lenLIS[i] + pos);
pos = binarySearch(dp, t[i] - x);
if (pos >= dp.size()) {
dp.add(t[i] - x);
} else {
dp.set(pos, t[i] - x);
}
}
return res;
}
static int binarySearch(ArrayList<Integer> dp, int x){
int l = 0, r = dp.size() - 1, mid;
while(l <= r){
mid = (l + r + 1)/2;
if(dp.get(mid) < x)
l = mid + 1;
else
r = mid - 1;
}
return l;
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
st = new StringTokenizer(in.readLine());
n = Integer.valueOf(st.nextToken());
x = Integer.valueOf(st.nextToken());
t = new int[n];
dt = new int[n];
lenLIS = new int[n];
st = new StringTokenizer(in.readLine());
for(int i = 0 ; i < n ; ++i){
t[i] = Integer.valueOf(st.nextToken());
dt[n - i - 1] = t[i]*-1;
}
LIS1();
int i = 0, j = n - 1;
while(i < j){
int aux = lenLIS[i];
lenLIS[i] = lenLIS[j];
lenLIS[j] = aux;
i++;
j--;
}
System.out.println(LIS2());
in.close();
out.flush();
out.close();
}
}
# | 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... |