欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 文化 > 滑动窗口(C++)

滑动窗口(C++)

2025/3/20 0:33:41 来源:https://blog.csdn.net/qxh10/article/details/140878105  浏览:    关键词:滑动窗口(C++)

样例输入

8 3
1 3 -1 -3 5 3 6 7

样例输出

-1 -3 -3 -3 3 3
3 3 5 5 6 7

数据范围

对于 20% 的数据,K≤N≤1000;

对于 50% 的数据,K≤N≤10^5;

对于 100% 的数据,K≤N≤10^6。

#include<bits/stdc++.h>
using namespace std;int a[1000006];void yjc(int n, int w) {deque<int> q;for (int i = 0; i < n; ++i) {while (!q.empty() && a[q.back()] <= a[i]) {q.pop_back();}q.push_back(i);if (i >= w - 1) {cout << a[q.front()] << ' ';if (q.front() == i - w + 1) {q.pop_front();}}}cout << endl;
}void xzh(int n, int w) {deque<int> q;for (int i = 0; i < n; ++i) {while (!q.empty() && a[q.back()] >= a[i]) {q.pop_back();}q.push_back(i);if (i >= w - 1) {cout << a[q.front()] << ' ';if (q.front() == i - w + 1) {q.pop_front();}}}cout << endl;
}int main() {int n, w;cin >> n >> w;for (int i = 0; i < n; ++i) {cin >> a[i];}xzh(n, w);yjc(n, w);return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词