AcWing 154. java 速度读写 滑动窗口
原题链接
简单
作者:
henhen敲
,
2020-06-09 14:35:43
,
所有人可见
,
阅读 609
import java.util.*;
import java.io.*;
class Main{
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int nextInt()throws Exception{
in.nextToken();
return (int)in.nval;
}
public static void main(String[] args) throws Exception{
int n = nextInt(); int k = nextInt();
Deque<Integer> qu1 = new ArrayDeque();
Deque<Integer> qu2 = new ArrayDeque();
int[] nums = new int[n];
int[] ans1 = new int[n-k+1];
int[] ans2 = new int[n-k+1];
for(int i=0; i<n; i++){
int cur = nextInt();
nums[i] = cur;
if(!qu1.isEmpty()&&i-qu1.getFirst()>=k) qu1.pollFirst();
if(!qu2.isEmpty()&&i-qu2.getFirst()>=k) qu2.pollFirst();
while(!qu1.isEmpty()&&cur>=nums[qu1.peekLast()]) qu1.pollLast();
while(!qu2.isEmpty()&&cur<=nums[qu2.peekLast()]) qu2.pollLast();
qu1.offer(i);
qu2.offer(i);
if(i>=k-1){
ans1[i-k+1] = nums[qu1.peekFirst()];
ans2[i-k+1] = nums[qu2.peekFirst()];
}
}
for(int i=0; i<n-k+1; i++) out.print(ans2[i]+" ");
out.println("");
for(int i=0; i<n-k+1; i++) out.print(ans1[i]+" ");
out.close();
}
}