이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//package lis;
import java.io.*;
import java.util.*;
class GlobWarm {
public static void main(String[] args) throws IOException {
GlobWarm obj = new GlobWarm();
obj.doStuff();
}
int binsearch(int val) {
int l = 0, r = this.r;
while (l < r) {
int m = (l+r)/2;
if (val < maxlist[m]) {
if (val > maxlist[m+1]) return m;
l = m+1;
} else r=m;
}
return l;
}
int[] maxlist; int r = 0;
int[][] changes;
void create() {
maxlist = new int[nums.length+1];
changes = new int[nums.length][2];
Arrays.fill(maxlist, Integer.MIN_VALUE);
maxlist[0] = Integer.MAX_VALUE;
for (int i = nums.length-1; i >= 0; i--) {
int pos = binsearch(nums[i]);
pos++; r = Math.max(r, pos);
changes[i] = new int[] {pos, maxlist[pos]};
maxlist[pos] = nums[i];
}
}
int binsearch2(int val) {
int l = 0, r = this.r2;
while (l < r) {
int m = (l+r)/2;
if (val > lis[m]) {
if (val < lis[m+1]) return m;
l=m+1;
} else r=m;
}
return l;
}
int find(int val) {
int l = 1, r = maxlist.length-1;
while (l<r) {
int m = (l+r)/2;
if (val >= maxlist[m]) {
if (val < maxlist[m-1]) return m;
r = m;
} else l = m+1;
}
return l;
}
int[] lis; int r2 = 0;
int process() {
int ans = r;
lis = new int[changes.length+1];
Arrays.fill(lis, Integer.MAX_VALUE);
lis[0] = Integer.MIN_VALUE;
for (int i = 0; i < changes.length-1; i++) {
maxlist[changes[i][0]] = changes[i][1];
int pos = binsearch2(nums[i]);
pos++; r2 = Math.max(r2, pos);
lis[pos] = nums[i];
int res = find(nums[i]-max)-1;
ans = Math.max(ans, r2+res);
}
return ans;
}
int[] nums; int max;
private void doStuff() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
max = Integer.parseInt(st.nextToken());
nums = new int[n];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < nums.length; i++) {
nums[i] = Integer.parseInt(st.nextToken());
}
br.close();
create();
System.out.println(process());
}
}
# | 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... |