AcWing 154. JAVA - 短。
原题链接
简单
作者:
acw_weian
,
2020-04-18 20:38:02
,
所有人可见
,
阅读 617
import java.io.*;
class Main{
static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
int[] q = new int[1000010];
String[] s = read.readLine().split(" ");
int n = Integer.valueOf(s[0]);
int k = Integer.valueOf(s[1]);
s = read.readLine().split(" ");
int[] a = new int[n];
for(int i = 0; i < n; i++) a[i] = Integer.valueOf(s[i]);
//输出最小值
int hh = 0, tt = -1;
for(int i = 0; i < n; i++){
if(hh <= tt && i - k + 1 > q[hh]) hh++;
while(hh <= tt && a[q[tt]] >= a[i]) tt--;
q[++tt] = i;
if(i + 1 >= k) System.out.print(a[q[hh]] + " ");
}
//输出最大值
System.out.println();
hh = 0; tt = -1;
for(int i = 0; i < n; i++){
if(hh <= tt && i - k + 1 > q[hh]) hh++;
while(hh <= tt && a[q[tt]] <= a[i]) tt--;
q[++tt] = i;
if(i + 1 >= k) System.out.print(a[q[hh]] + " ");
}
}
}