ZYK_luogu @ 2023-08-05 10:54:51
这是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]]);
两条语句调换位置,结果就不一样了,有哪位大佬能解答一下为什么
by ATZdhjeb @ 2023-08-05 11:04:30
调换之后可能你输出时队列为空啊
by _Wind_Leaves_ShaDow_ @ 2023-08-05 11:09:49
@ZYK_luogu 如果 i 把队列清空了,此时队列头应该是 i 而不是空。
比如说队列原本是 2 3 5
,此时加进来 1,那么 1 会把队列清空,应该把 1 放在队列头
by _Wind_Leaves_ShaDow_ @ 2023-08-05 11:10:15
@zsq20100122 在队列单调递增前提下
by ZYK_luogu @ 2023-08-05 11:12:12
@zsq20100122 懂了懂了,感谢大佬