-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMINMAXHEAPWITHFASTIO.cpp
More file actions
99 lines (92 loc) · 2.09 KB
/
MINMAXHEAPWITHFASTIO.cpp
File metadata and controls
99 lines (92 loc) · 2.09 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
#define MAX 400005
const int SIZE = 1 << 19;
char buff[SIZE], *p = buff + SIZE;
char read_char() {
if (p == buff + SIZE) {
fread(buff, 1, SIZE, stdin);
p = buff;
}
return *(p++);
}
bool cmp(int a, int b) {
return a>b;
}
int read_int() {
register char c;
while (!isdigit(c = read_char()));
register int r = c - '0';
while (isdigit(c = read_char())) r = 10 * r + c - '0';
return r;
}
int main() {
//freopen("1.txt", "r", stdin);
int N, W;
N = read_int();
W = read_int();
//scanf("%d %d",&N,&W);
int H[N];
int k = 0;
while (k != N) {
H[k] = read_int();
k++;
}
vector<int> min, max;
for (int i = 0; i < W; i++) {
min.push_back(H[i]);
max.push_back(H[i]);
}
make_heap(max.begin(), max.end());
make_heap(min.begin(), min.end(), cmp);
int count = 0;
bool A[N + 1];
for (int i = 0; i <= N; i++)
A[i] = 0;
for (int ia = W; ia < N; ia++) {
int i = max.front();
int j = min.front();
while (A[i]) {
pop_heap(max.begin(), max.end());
max.pop_back();
i = max.front();
}
while (A[j]) {
pop_heap(min.begin(), min.end(), cmp);
min.pop_back();
j = min.front();
}
if (i - j + 1 == W)
count++;
A[H[ia - W]] = 1;
max.push_back(H[ia]);
min.push_back(H[ia]);
push_heap(min.begin(), min.end(), cmp);
push_heap(max.begin(), max.end());
}
int i = max.front();
int j = min.front();
while (A[i]) {
i = max.front();
pop_heap(max.begin(), max.end());
max.pop_back();
}
while (A[j]) {
j = min.front();
pop_heap(min.begin(), min.end(), cmp);
min.pop_back();
}
if (i - j + 1 == W)
count++;
printf("%d\n", count);
}