#include <bits/stdc++.h>
using namespace std;
unsigned int findLongestAscending(const int from, const int to, const vector<int> &mts) {
if (from == to) {
return 1;
}
stack<int> seq;
seq.push(to);
unsigned int longest = 1;
for (int i = to - 1; i >= from; i--) {
if (mts[i] < mts[seq.top()]) {
seq.push(i);
longest = max(longest, (unsigned int)seq.size());
} else if (mts[i] == mts[seq.top()]) {
continue;
} else {
while (mts[i] >= mts[seq.top()]) {
seq.pop();
}
seq.push(i);
}
}
return longest;
}
int main() {
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(false);
int n, val;
cin >> n;
vector<int> metrics(n);
vector<int> indices;
int maxValue = 0;
for (int i = 0; i < n; i++) {
cin >> val;
metrics[i] = val;
if (val > maxValue) {
maxValue = val;
indices.clear();
indices.push_back(i);
} else if (val == maxValue) {
indices.push_back(i);
}
}
int lastMaxIdx = indices.back();
rotate(metrics.begin(), metrics.begin() + lastMaxIdx + 1, metrics.end());
const int shift = metrics.size() - (lastMaxIdx + 1);
for (int &idx : indices) {
idx = (idx + shift) % metrics.size();
}
unsigned int maxLength = 0;
int startIndex = 0;
for (int endInc : indices) {
const unsigned int longest = findLongestAscending(startIndex, endInc, metrics);
maxLength = max(maxLength, longest);
startIndex = endInc + 1;
}
cout << maxLength << 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 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 | #include <bits/stdc++.h> using namespace std; unsigned int findLongestAscending(const int from, const int to, const vector<int> &mts) { if (from == to) { return 1; } stack<int> seq; seq.push(to); unsigned int longest = 1; for (int i = to - 1; i >= from; i--) { if (mts[i] < mts[seq.top()]) { seq.push(i); longest = max(longest, (unsigned int)seq.size()); } else if (mts[i] == mts[seq.top()]) { continue; } else { while (mts[i] >= mts[seq.top()]) { seq.pop(); } seq.push(i); } } return longest; } int main() { cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); int n, val; cin >> n; vector<int> metrics(n); vector<int> indices; int maxValue = 0; for (int i = 0; i < n; i++) { cin >> val; metrics[i] = val; if (val > maxValue) { maxValue = val; indices.clear(); indices.push_back(i); } else if (val == maxValue) { indices.push_back(i); } } int lastMaxIdx = indices.back(); rotate(metrics.begin(), metrics.begin() + lastMaxIdx + 1, metrics.end()); const int shift = metrics.size() - (lastMaxIdx + 1); for (int &idx : indices) { idx = (idx + shift) % metrics.size(); } unsigned int maxLength = 0; int startIndex = 0; for (int endInc : indices) { const unsigned int longest = findLongestAscending(startIndex, endInc, metrics); maxLength = max(maxLength, longest); startIndex = endInc + 1; } cout << maxLength << endl; } |
English