本蒟蒻想要求助,大佬能不能帮忙看一看
查看原帖
本蒟蒻想要求助,大佬能不能帮忙看一看
999548
cyt123bc楼主2024/4/13 16:20

[p1886]提交记录 本蒟蒻用的是单调队列,目前只得90分。

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6+50;
int a[N], q[N];
int n, k;

int main () {
	cin >> n >> k;
	int h = 0, t = -1;
	
	for (int i = 1; i <= n; i++) {
		scanf("%d",&a[i]);
		while (h <= t && i - k >= q[h]) {
			h++;
		}
		while (h <= t && a[q[t]] >= a[i]) {
			t--;
		}
		q[++t] = i;
		if (i >= k) {
			printf("%d ",a[q[h]]);
		}
	}
	cout << "\n";
	
	for (int i = 1; i <= n; i++) {
		while (h <= t && i - k >= q[h]) {
			h++;
		}
		while (h <= t && a[q[t]] <= a[i]) {
			t--;
		}
		q[++t] = i;
		if (i >= k) {
			printf("%d ",a[q[h]]);
		}
	}
	
	return 0;
}
2024/4/13 16:20
加载中...