#include <bits/stdc++.h>
using namespace std;
int main(int argc, char* argv[]) {
bool debug = (argc >= 2 && std::string(argv[1]) == "--debug");
int n;
cin >> n;
vector<int> tab(2LL * n);
vector<int> candidates;
int maxVal = -1;
int maxIdx = -1;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
tab[i] = x;
tab[n + i] = x;
if (x >= maxVal) {
maxVal = x;
maxIdx = n + i;
}
}
int result = 1;
int current = maxIdx-1;
candidates.push_back(maxVal);
while(current >= 0){
if (debug) {
cout << "candidates.back(): " << candidates.back() << " tab[current]: " << tab[current] << "\n";
}
while(candidates.back() < tab[current]){
candidates.pop_back();
}
if(candidates.back() > tab[current]){
candidates.push_back(tab[current]);
}
if((int)candidates.size() > result){
result = (int)candidates.size();
}
current--;
}
cout << result<< "\n";
return 0;
}
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 | #include <bits/stdc++.h> using namespace std; int main(int argc, char* argv[]) { bool debug = (argc >= 2 && std::string(argv[1]) == "--debug"); int n; cin >> n; vector<int> tab(2LL * n); vector<int> candidates; int maxVal = -1; int maxIdx = -1; for (int i = 0; i < n; i++) { int x; cin >> x; tab[i] = x; tab[n + i] = x; if (x >= maxVal) { maxVal = x; maxIdx = n + i; } } int result = 1; int current = maxIdx-1; candidates.push_back(maxVal); while(current >= 0){ if (debug) { cout << "candidates.back(): " << candidates.back() << " tab[current]: " << tab[current] << "\n"; } while(candidates.back() < tab[current]){ candidates.pop_back(); } if(candidates.back() > tab[current]){ candidates.push_back(tab[current]); } if((int)candidates.size() > result){ result = (int)candidates.size(); } current--; } cout << result<< "\n"; return 0; } |
English