社区讨论

60分求助!!!#2#8#9#10 RE了

P1886【模板】单调队列 / 滑动窗口参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@lo8pz311
此快照首次捕获于
2023/10/27 22:39
2 年前
此快照最后确认于
2023/10/27 22:39
2 年前
查看原帖
CPP
#include<iostream>
#include<deque>
using namespace std;
int n, k, a[100005];
class MyQueue_big {
public:
	deque<int> que;
	void pop(int value) {
		if (!que.empty() && value == que.front()) {
			que.pop_front();
		}
	}
	void push(int value) {
		while (!que.empty() && value > que.back()) {
			que.pop_back();
		}
		que.push_back(value);
	}
	int front(void) {
		return que.front();
	}
};
class MyQueue_small {
public:
	deque<int> que;
	void pop(int value) {
		if (!que.empty() && value == que.front()) {
			que.pop_front();
		}
	}
	void push(int value) {
		while (!que.empty() && value < que.back()) {
			que.pop_back();
		}
		que.push_back(value);
	}
	int front(void) {
		return que.front();
	}
};
int main(void) {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> k;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	MyQueue_small small;
	MyQueue_big big;
	for (int i = 0; i < k; ++i) {
		small.push(a[i]);
		big.push(a[i]);
	}
	cout << small.front() << ' ';
	for (int i = k; i < n; ++i) {
		small.pop(a[i - k]);
		small.push(a[i]);
		cout << small.front() << ' ';
	}
	cout << endl;
	cout << big.front() << ' ';
	for (int i = k; i < n; ++i) {
		big.pop(a[i - k]);
		big.push(a[i]);
		cout << big.front() << ' ';
	}
	cout << endl;
	return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...