-
Notifications
You must be signed in to change notification settings - Fork 2
/
RiegnOfNumber.cpp
57 lines (49 loc) · 1003 Bytes
/
RiegnOfNumber.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
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> longestConsecutive(vector<int> &A) {
int n = A.size();
vector<vector<int>> B(n, vector<int>(3, 0));
for (int i = 0; i < n; i++) {
B[i][0] = A[i];
}
stack<int> s;
int i = n - 1;
while (i >= 0) {
if (s.empty()) {
B[i][2] = n;
s.push(i);
i--;
} else if (A[s.top()] > A[i]) {
B[i][2] = s.top();
s.push(i);
i--;
} else
s.pop();
}
stack<int> s2;
i = 0;
while (i < n) {
if (s2.empty()) {
B[i][1] = 1;
s2.push(i);
i++;
} else if (A[i] < A[s2.top()]) {
B[i][1] = s2.top() + 2;
s2.push(i);
i++;
} else
s2.pop();
}
return B;
}
int main() {
vector<int> v1{34, 35, 27, 42, 5, 28, 39, 20, 28};
vector<vector<int>> v2 = longestConsecutive(v1);
for (int i = 0; i < v2.size(); i++) {
for (int j = 0; j < v2[i].size(); j++) {
cout << v2[i][j] << " ";
}
cout << endl;
}
return 0;
}