#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <deque>
using namespace std;
#define REP(x,n) for(int x=0;x<(n);++x)
#define FOREACH(x, c) for(auto x = (c).begin(); x != (c).end(); ++x)
// #define DEBUG_LEVEL 1
#ifdef DEBUG_LEVEL
#define DEBUG(level, x) {if (level >= DEBUG_LEVEL) {x}}
#else
#define DEBUG(level, x)
#endif
const int MAX_N = 1000001;
int tab[MAX_N];
int score[MAX_N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int maxIndex = 0;
REP(x,n) {
cin >> tab[x];
}
int currentMax = 0;
score[0] = 1;
deque<int> maxes;
maxes.push_back(0);
for (int x=1; x<n; ++x) {
DEBUG(-1, cerr << "[" << x << "] = " << tab[x] << " - " << maxes.size() << " elements" << endl;)
if (tab[x] < tab[x-1]) {
score[x] = 1;
} else {
int maxmax = 0;
while (!maxes.empty() && tab[x] >= tab[maxes.back()]) {
if (tab[x] == tab[maxes.back()]) {
maxmax = max(maxmax, score[maxes.back()]);
} else {
maxmax = max(maxmax, score[maxes.back()] + 1);
}
DEBUG(-2, cerr << " pop " << tab[maxes.back()] << endl;)
maxes.pop_back();
}
score[x] = maxmax;
}
maxes.push_back(x);
DEBUG(-2, cerr << " push " << tab[x] << " with score " << score[x] << endl;)
}
DEBUG(0,
REP(x,n) {
cerr << score[x] << " ";
}
cerr << endl;
)
for (int x=0; x<n; ++x) {
DEBUG(-1, cerr << "[" << x << "] = " << tab[x] << " - " << maxes.size() << " elements" << endl;)
if (tab[x] < tab[(x ? x-1 : n-1)]) {
score[x] = 1;
} else {
int maxmax = 0;
while (!maxes.empty() && tab[x] >= tab[maxes.back()]) {
if (tab[x] == tab[maxes.back()]) {
maxmax = max(maxmax, score[maxes.back()]);
} else {
maxmax = max(maxmax, score[maxes.back()] + 1);
}
DEBUG(-2, cerr << " pop " << tab[maxes.back()] << endl;)
maxes.pop_back();
}
score[x] = maxmax;
}
maxes.push_back(x);
DEBUG(-2, cerr << " push " << tab[x] << " with score " << score[x] << endl;)
}
DEBUG(0,
REP(x,n) {
cerr << score[x] << " ";
}
cerr << endl;
)
cout << score[maxes.front()] << endl;
DEBUG(1,
cerr << "maxes: " << endl;
while (!maxes.empty()) {
cerr << " * [" << maxes.front() << "] = " << tab[maxes.front()] << " with score " << score[maxes.front()] << endl;
maxes.pop_front();
}
)
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | #include <iostream> #include <vector> #include <set> #include <map> #include <deque> using namespace std; #define REP(x,n) for(int x=0;x<(n);++x) #define FOREACH(x, c) for(auto x = (c).begin(); x != (c).end(); ++x) // #define DEBUG_LEVEL 1 #ifdef DEBUG_LEVEL #define DEBUG(level, x) {if (level >= DEBUG_LEVEL) {x}} #else #define DEBUG(level, x) #endif const int MAX_N = 1000001; int tab[MAX_N]; int score[MAX_N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int maxIndex = 0; REP(x,n) { cin >> tab[x]; } int currentMax = 0; score[0] = 1; deque<int> maxes; maxes.push_back(0); for (int x=1; x<n; ++x) { DEBUG(-1, cerr << "[" << x << "] = " << tab[x] << " - " << maxes.size() << " elements" << endl;) if (tab[x] < tab[x-1]) { score[x] = 1; } else { int maxmax = 0; while (!maxes.empty() && tab[x] >= tab[maxes.back()]) { if (tab[x] == tab[maxes.back()]) { maxmax = max(maxmax, score[maxes.back()]); } else { maxmax = max(maxmax, score[maxes.back()] + 1); } DEBUG(-2, cerr << " pop " << tab[maxes.back()] << endl;) maxes.pop_back(); } score[x] = maxmax; } maxes.push_back(x); DEBUG(-2, cerr << " push " << tab[x] << " with score " << score[x] << endl;) } DEBUG(0, REP(x,n) { cerr << score[x] << " "; } cerr << endl; ) for (int x=0; x<n; ++x) { DEBUG(-1, cerr << "[" << x << "] = " << tab[x] << " - " << maxes.size() << " elements" << endl;) if (tab[x] < tab[(x ? x-1 : n-1)]) { score[x] = 1; } else { int maxmax = 0; while (!maxes.empty() && tab[x] >= tab[maxes.back()]) { if (tab[x] == tab[maxes.back()]) { maxmax = max(maxmax, score[maxes.back()]); } else { maxmax = max(maxmax, score[maxes.back()] + 1); } DEBUG(-2, cerr << " pop " << tab[maxes.back()] << endl;) maxes.pop_back(); } score[x] = maxmax; } maxes.push_back(x); DEBUG(-2, cerr << " push " << tab[x] << " with score " << score[x] << endl;) } DEBUG(0, REP(x,n) { cerr << score[x] << " "; } cerr << endl; ) cout << score[maxes.front()] << endl; DEBUG(1, cerr << "maxes: " << endl; while (!maxes.empty()) { cerr << " * [" << maxes.front() << "] = " << tab[maxes.front()] << " with score " << score[maxes.front()] << endl; maxes.pop_front(); } ) return 0; } |
English