-
Notifications
You must be signed in to change notification settings - Fork 247
/
QueueWithMinimum.cpp
80 lines (63 loc) · 1.65 KB
/
QueueWithMinimum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*************************************************************************************
Modification of queue, which allows finding the minimum element in it.
Time complexity: O(1) on operation.
Based on problem 756 from informatics.mccme.ru:
http://informatics.mccme.ru//mod/statements/view.php?chapterid=756#1
*************************************************************************************/
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <utility>
#include <iomanip>
using namespace std;
const int MAXN = 205000;
int n, m;
deque < pair <int, int> > d;
int a[MAXN];
void enqueue(int x) {
int num = 1;
while (!d.empty() && d.back().first > x) {
num += d.back().second;
d.pop_back();
}
d.push_back(make_pair(x, num));
}
void dequeue() {
if (d.front().second == 1) {
d.pop_front();
}
else {
d.front().second--;
}
}
int getMin() {
return d.front().first;
}
int main() {
//assert(freopen("input.txt","r",stdin));
//assert(freopen("output.txt","w",stdout));
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= m; i++) {
enqueue(a[i]);
}
printf("%d\n", getMin());
for (int i = m + 1; i <= n; i++) {
dequeue();
enqueue(a[i]);
printf("%d\n", getMin());
}
return 0;
}