java太容易超时了,读入,输出都加Buffer才能过
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Deque;
import java.util.LinkedList;
//import java.util.Scanner;
/**
* 单调队列
*
* @author dongwa
*
*/
public class Main {
public static void main(String[] arge) throws IOException {
// Scanner scanner = new Scanner(System.in);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
String string[] = reader.readLine().split(" ");
int n = Integer.parseInt(string[0]);
int m = Integer.parseInt(string[1]);
String string2[] = reader.readLine().split(" ");
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(string2[i]);
}
Deque<Integer> queue = new LinkedList<Integer>();
//双端队列存的是数组的下标,方便判断窗口是否滑出去了
for (int i = 0; i < n; i++) {
//判断窗口是否划出去了,保证队列中最多只有m个数
while (!queue.isEmpty() && i - queue.peekFirst() + 1 > m)
queue.pollFirst();
while (!queue.isEmpty() && arr[i] <= arr[queue.peekLast()])
queue.pollLast();
queue.add(i);
if(i>=m-1)writer.write(arr[queue.peek()] + " ");
}
queue.clear();
writer.write("\n");
//System.out.println();
for (int i = 0; i < n; i++) {
//判断窗口是否划出去了,保证队列中最多只有m个数
while (!queue.isEmpty() && i - queue.peekFirst() + 1 > m)
queue.pollFirst();
while (!queue.isEmpty() && arr[i] >= arr[queue.peekLast()])
queue.pollLast();
queue.add(i);
if(i>=m-1) writer.write(arr[queue.peek()] + " ");
}
writer.flush();
reader.close();
writer.close();
// scanner.close();
}
}