#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <time.h>
#include <queue>
using namespace std;
int main () {
int n;
scanf("%d", &n);
vector<int> pearls(2 * n, 0);
vector<int> next_larger(2 * n, -1);
for (int i = 0; i < n; ++i) {
scanf("%d", &pearls[i]);
pearls[i + n] = pearls[i];
}
set<pair<int, int> > waiting;
for (int i = 0; i < 2 * n; ++i) {
int p = pearls[i];
vector<pair<int, int> > to_delete;
for (set<pair<int, int> >::iterator it = waiting.begin(); it != waiting.end(); it++) {
if (it->first >= p) {
break;
}
next_larger[it->second] = i;
to_delete.push_back(*it);
}
waiting.insert(make_pair(p, i));
for (vector<pair<int, int> >::iterator it = to_delete.begin(); it != to_delete.end(); it++) {
waiting.erase(*it);
}
}
// for (int i = 0; i < 2 * n; ++i) {
// printf("%d\n", next_larger[i]);
// }
int max_cnt = 0;
vector<bool> ok(2 * n, false);
int cnt = 1;
for (int i = 0; i < n; ++i) {
--cnt;
int next = i;
// printf("START FROM %d\n", next);
while (true) {
if (!ok[next]) {
cnt++;
ok[next] = true;
}
else {
break;
}
next = next_larger[next];
if (next == -1) {
break;
}
}
if (max_cnt < cnt) {
max_cnt = cnt;
}
}
printf("%d\n", max_cnt);
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 | #include <cstdio> #include <cstdlib> #include <cstdint> #include <vector> #include <map> #include <algorithm> #include <set> #include <time.h> #include <queue> using namespace std; int main () { int n; scanf("%d", &n); vector<int> pearls(2 * n, 0); vector<int> next_larger(2 * n, -1); for (int i = 0; i < n; ++i) { scanf("%d", &pearls[i]); pearls[i + n] = pearls[i]; } set<pair<int, int> > waiting; for (int i = 0; i < 2 * n; ++i) { int p = pearls[i]; vector<pair<int, int> > to_delete; for (set<pair<int, int> >::iterator it = waiting.begin(); it != waiting.end(); it++) { if (it->first >= p) { break; } next_larger[it->second] = i; to_delete.push_back(*it); } waiting.insert(make_pair(p, i)); for (vector<pair<int, int> >::iterator it = to_delete.begin(); it != to_delete.end(); it++) { waiting.erase(*it); } } // for (int i = 0; i < 2 * n; ++i) { // printf("%d\n", next_larger[i]); // } int max_cnt = 0; vector<bool> ok(2 * n, false); int cnt = 1; for (int i = 0; i < n; ++i) { --cnt; int next = i; // printf("START FROM %d\n", next); while (true) { if (!ok[next]) { cnt++; ok[next] = true; } else { break; } next = next_larger[next]; if (next == -1) { break; } } if (max_cnt < cnt) { max_cnt = cnt; } } printf("%d\n", max_cnt); return 0; } |
English