单调队列TLE了,有没有路过的巨佬?
查看原帖
单调队列TLE了,有没有路过的巨佬?
213218
蒟蒻CGZ楼主2020/6/21 23:10

单调队列大量一个模板,第二个点和最后一个点TLE(C++)

#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1e6 + 10;
int head, tail, q[N], a[N], n, m;

int main() {
	ios::sync_with_stdio(false);
	cin >> n >> m;
	for(int i = 1; i <= n; ++ i) 
		cin >> a[i];
	head = 1; tail = 0;
	for(int i = 1; i <= n; ++ i) {
		cout << a[q[head]] << endl;
		while(head <= tail && q[head] + m <= i) ++ head;
		while(head <= tail && a[i] < a[q[tail]]) -- tail;
		q[++ tail] = i;
	}
	return 0;
}
2020/6/21 23:10
加载中...