https://www.acmicpc.net/problem/11003
덱(Deque)을 이용하여 풀 수 있다.
덱에 수를 넣을 때, 자신의 크기와 자신의 위치를 집어넣고, 덱에 있는 수들이 오름차순을 유지할 수 있도록 수를 넣어준다.
덱의 front 값은 항상 최솟값을 유지하게 된다.
#include <bits/stdc++.h>
#define MEM 100001
#define sanic ios_base::sync_with_stdio(0)
using namespace std;
typedef unsigned long long ull;
int n,m;
deque<pair<int, int>> d;
int main()
{
sanic; cin.tie(0);
cin >> n >> m;
for(int i=0; i<n; i++){
int q;
cin >> q;
if(!d.empty() && d.front().second<=i-m) d.pop_front();
while(!d.empty() && d.back().first>q) d.pop_back();
d.push_back({q,i});
cout << d.front().first << ' ';
}
}
코딩공부 같이하고 싶은 사람은 백준의 "그룹입니다" 그룹에 신청해주세요.
https://www.acmicpc.net/group/6712
_DynamicCube