虽然AC了但还是有地方不理解
查看原帖
虽然AC了但还是有地方不理解
742157
ZYK_luogu楼主2023/8/5 10:54

这是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]]);

两条语句调换位置,结果就不一样了,有哪位大佬能解答一下为什么

2023/8/5 10:54
加载中...