#include <iostream>
#include <stack>
using namespace std;
struct Foo {
int v, i;
};
int n, T[2000001];
stack<Foo> wieksi;
int result[2000001], best = 0;
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> T[i];
T[i+n] = T[i];
}
for (int i = 2*n-1; i >= 0; i--) {
result[i] = 1;
while (!wieksi.empty() && wieksi.top().v <= T[i]) {
wieksi.pop();
}
if (!wieksi.empty()) {
result[i] = result[wieksi.top().i] + 1;
}
if (best < result[i]) {
best = result[i];
}
Foo ja;
ja.i = i;
ja.v = T[i];
wieksi.push(ja);
}
cout << best << endl;
}
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 | #include <iostream> #include <stack> using namespace std; struct Foo { int v, i; }; int n, T[2000001]; stack<Foo> wieksi; int result[2000001], best = 0; int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 0; i < n; i++) { cin >> T[i]; T[i+n] = T[i]; } for (int i = 2*n-1; i >= 0; i--) { result[i] = 1; while (!wieksi.empty() && wieksi.top().v <= T[i]) { wieksi.pop(); } if (!wieksi.empty()) { result[i] = result[wieksi.top().i] + 1; } if (best < result[i]) { best = result[i]; } Foo ja; ja.i = i; ja.v = T[i]; wieksi.push(ja); } cout << best << endl; } |
English