forked from t3nsor/codebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ansv.cpp
36 lines (35 loc) · 1.02 KB
/
ansv.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
// Linear time all nearest smaller values, standard
// stack-based algorithm. ansv_left stores indices of
// nearest smaller values to the left in res. -1 means no
// smaller value was found. ansv_right likewise looks to the
// right. v.size() means no smaller value was found.
void ansv_left(vector<int> &v, vector<int> &res) {
stack<pair<int, int>> stk;
stk.push(make_pair(INT_MIN, v.size()));
for (int i = v.size() - 1; i >= 0; i--) {
while (stk.top().first > v[i]) {
res[stk.top().second] = i;
stk.pop();
}
stk.push(make_pair(v[i], i));
}
while (stk.top().second < v.size()) {
res[stk.top().second] = -1;
stk.pop();
}
}
void ansv_right(vector<int> &v, vector<int> &res) {
stack<pair<int, int>> stk;
stk.push(make_pair(INT_MIN, -1));
for (int i = 0; i < v.size(); i++) {
while (stk.top().first > v[i]) {
res[stk.top().second] = i;
stk.pop();
}
stk.push(make_pair(v[i], i));
}
while (stk.top().second > -1) {
res[stk.top().second] = v.size();
stk.pop();
}
}