AcWing 154. java用linkedlist过不去。。。难受
原题链接
简单
作者:
莫得感情的刷题机器
,
2020-09-29 15:15:12
,
所有人可见
,
阅读 416
import java.util.*;
import java.io.*;
public class Main{
public static void main(String [] args) throws Exception{
Scanner sc=new Scanner(System.in);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String [] str=br.readLine().split(" ");
int n=Integer.parseInt(str[0]);
int k=Integer.parseInt(str[1]);
LinkedList<Integer> dq=new LinkedList<>();
int [] arr=new int[n+1];
str=br.readLine().split(" ");
for(int i = 0;i<n;i++) arr[i]=Integer.parseInt(str[i]);
for(int i = 0;i<n;i++){
if(!dq.isEmpty()&&i-k+1>dq.peekFirst()) dq.pollFirst();
while(!dq.isEmpty()&&arr[i]<=arr[dq.peekLast()]) dq.pollLast();
dq.addLast(i);
if(i>=k-1) System.out.print(arr[dq.peekFirst()]+" ");
}
System.out.println();
dq.clear();
for(int i = 0;i<n;i++){
if(!dq.isEmpty()&&i-k+1>dq.peekFirst()) dq.pollFirst();
while(!dq.isEmpty()&&arr[i]>=arr[dq.peekLast()]) dq.pollLast();
dq.addLast(i);
if(i>=k-1) System.out.print(arr[dq.peekFirst()]+" ");
}
}
}