滑动窗口代码

//滑动窗口 
#include
#include
using namespace std;
using ll = long long;
const int N = 2e6+9;
ll a[N], mx[N], mi[N];

void solve()
{
	int n, k; cin >> n >> k;
	for (int i = 1; i <= n; ++i)cin >> a[i];
	multiset mst;
	for (int i = 1; i <= n; ++i)
	{
		mst.insert(a[i]);
		if (i >= k) mi[i] = *mst.begin(), mx[i] = *mst.rbegin(), mst.erase(mst.find(a[i - k + 1]));
	}
	for (int i = k; i <= n; ++i)cout << mx[i] << " \n"[i == n];
	for (int i = k; i <= n; ++i)cout << mi[i] << " \n"[i == n];
}

int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	solve();
	return 0;
}

你可能感兴趣的:(c++,算法)