这是AC代码:
#include <cstdio>
using namespace std;
const int N = 1000005;
int n, k, a[N], que[N], head = 1, tail = 0;
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i ++)
scanf("%d", &a[i]);
for (int i = 1; i <= n; i ++) {
while (head <= tail && i - k + 1 > que[head]) head ++;
while (head <= tail && a[que[tail]] >= a[i]) tail --;
que[++ tail] = i;
if (i >= k) printf("%d ", a[que[head]]);
}
printf("\n");
head = 1, tail = 0;
for (int i = 1; i <= n; i ++) {
while (head <= tail && i - k + 1 > que[head]) head ++;
while (head <= tail && a[que[tail]] <= a[i]) tail --;
que[++ tail] = i;
if (i >= k) printf("%d ", a[que[head]]);
}
return 0;
}
可是,要是把第12和13行的
que[++ tail] = i;
if (i >= k) printf("%d ", a[que[head]]);
两条语句调换位置,结果就不一样了,有哪位大佬能解答一下为什么